author | leonid |
Wed, 08 Aug 2007 18:40:47 +0200 | |
changeset 257 | 4f3364846782 |
parent 253 | bf58ce630b88 |
child 258 | 8f7725451453 |
permissions | -rw-r--r-- |
205 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of CanFestival, a library implementing CanOpen Stack. |
|
5 |
# |
|
6 |
#Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD |
|
7 |
# |
|
8 |
#See COPYING file for copyrights details. |
|
9 |
# |
|
10 |
#This library is free software; you can redistribute it and/or |
|
11 |
#modify it under the terms of the GNU Lesser General Public |
|
12 |
#License as published by the Free Software Foundation; either |
|
13 |
#version 2.1 of the License, or (at your option) any later version. |
|
14 |
# |
|
15 |
#This library is distributed in the hope that it will be useful, |
|
16 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
#Lesser General Public License for more details. |
|
19 |
# |
|
20 |
#You should have received a copy of the GNU Lesser General Public |
|
21 |
#License along with this library; if not, write to the Free Software |
|
22 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
||
24 |
from node import * |
|
25 |
import eds_utils |
|
26 |
import os, shutil |
|
27 |
||
28 |
#------------------------------------------------------------------------------- |
|
29 |
# Definition of NodeList Object |
|
30 |
#------------------------------------------------------------------------------- |
|
31 |
||
32 |
""" |
|
33 |
Class recording a node list for a CANOpen network. |
|
34 |
""" |
|
35 |
||
36 |
class NodeList: |
|
37 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
38 |
def __init__(self, manager, netname = ""): |
205 | 39 |
self.Root = "" |
40 |
self.Manager = manager |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
41 |
self.NetworkName = netname |
205 | 42 |
self.SlaveNodes = {} |
43 |
self.EDSNodes = {} |
|
44 |
self.CurrentSelected = None |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
45 |
self.Changed = False |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
46 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
47 |
def HasChanged(self): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
48 |
return self.Changed or not self.Manager.CurrentIsSaved() |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
49 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
50 |
def ForceChanged(self, changed): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
51 |
self.Changed = changed |
205 | 52 |
|
53 |
def GetNetworkName(self): |
|
54 |
return self.NetworkName |
|
55 |
||
56 |
def SetNetworkName(self, name): |
|
57 |
self.NetworkName = name |
|
58 |
||
59 |
def GetManager(self): |
|
60 |
return self.Manager |
|
61 |
||
62 |
def GetRoot(self): |
|
63 |
return self.Root |
|
64 |
||
65 |
def GetSlaveNumber(self): |
|
66 |
return len(self.SlaveNodes) |
|
67 |
||
68 |
def GetSlaveNames(self): |
|
69 |
nodes = self.SlaveNodes.keys() |
|
70 |
nodes.sort() |
|
71 |
return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes] |
|
72 |
||
73 |
def GetSlaveIDs(self): |
|
74 |
nodes = self.SlaveNodes.keys() |
|
75 |
nodes.sort() |
|
76 |
return nodes |
|
77 |
||
78 |
def SetCurrentSelected(self, selected): |
|
79 |
self.CurrentSelected = selected |
|
80 |
||
81 |
def GetCurrentSelected(self): |
|
82 |
return self.CurrentSelected |
|
83 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
84 |
def LoadProject(self, root, netname = None): |
205 | 85 |
self.SlaveNodes = {} |
86 |
self.EDSNodes = {} |
|
87 |
||
88 |
self.Root = root |
|
89 |
if not os.path.exists(self.Root): |
|
90 |
return "\"%s\" folder doesn't exist"%self.Root |
|
91 |
||
92 |
self.EDSFolder = os.path.join(self.Root, "eds") |
|
93 |
if not os.path.exists(self.EDSFolder): |
|
94 |
return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root |
|
95 |
||
96 |
files = os.listdir(self.EDSFolder) |
|
97 |
for file in files: |
|
243 | 98 |
filepath = os.path.join(self.EDSFolder, file) |
99 |
if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".eds": |
|
100 |
result = self.LoadEDS(file) |
|
101 |
if result != None: |
|
102 |
return result |
|
205 | 103 |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
104 |
result = self.LoadMasterNode(netname) |
205 | 105 |
if result != None: |
106 |
return result |
|
107 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
108 |
result = self.LoadSlaveNodes(netname) |
205 | 109 |
if result != None: |
110 |
return result |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
111 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
112 |
self.NetworkName = netname |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
113 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
114 |
def SaveProject(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
115 |
result = self.SaveMasterNode(netname) |
205 | 116 |
if result != None: |
117 |
return result |
|
118 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
119 |
result = self.SaveNodeList(netname) |
205 | 120 |
if result != None: |
121 |
return result |
|
122 |
||
123 |
def ImportEDSFile(self, edspath): |
|
124 |
dir, file = os.path.split(edspath) |
|
125 |
eds = os.path.join(self.EDSFolder, file) |
|
126 |
if os.path.isfile(eds): |
|
127 |
return "EDS file already imported" |
|
128 |
else: |
|
129 |
shutil.copy(edspath, self.EDSFolder) |
|
130 |
return self.LoadEDS(file) |
|
131 |
||
132 |
def LoadEDS(self, eds): |
|
133 |
edspath = os.path.join(self.EDSFolder, eds) |
|
134 |
node = eds_utils.GenerateNode(edspath, self.Manager.ScriptDirectory) |
|
135 |
if isinstance(node, Node): |
|
136 |
self.EDSNodes[eds] = node |
|
137 |
return None |
|
138 |
else: |
|
139 |
return node |
|
140 |
||
141 |
def AddSlaveNode(self, nodeName, nodeID, eds): |
|
142 |
if eds in self.EDSNodes.keys(): |
|
143 |
slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]} |
|
144 |
self.SlaveNodes[nodeID] = slave |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
145 |
self.Changed = True |
205 | 146 |
return None |
147 |
else: |
|
148 |
return "\"%s\" EDS file is not available"%eds |
|
149 |
||
150 |
def RemoveSlaveNode(self, index): |
|
151 |
if index in self.SlaveNodes.keys(): |
|
152 |
self.SlaveNodes.pop(index) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
153 |
OnCloseProjectMenu |
205 | 154 |
return None |
155 |
else: |
|
156 |
return "Node with \"0x%2.2X\" ID doesn't exist" |
|
157 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
158 |
def LoadMasterNode(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
159 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
160 |
masterpath = os.path.join(self.Root, "%s_master.od"%netname) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
161 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
162 |
masterpath = os.path.join(self.Root, "master.od") |
205 | 163 |
if os.path.isfile(masterpath): |
164 |
self.Manager.OpenFileInCurrent(masterpath) |
|
165 |
else: |
|
166 |
self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"]) |
|
167 |
return None |
|
168 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
169 |
def SaveMasterNode(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
170 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
171 |
masterpath = os.path.join(self.Root, "%s_master.od"%netname) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
172 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
173 |
masterpath = os.path.join(self.Root, "master.od") |
205 | 174 |
if self.Manager.SaveCurrentInFile(masterpath): |
175 |
return None |
|
176 |
else: |
|
177 |
return "Fail to save Master Node" |
|
178 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
179 |
def LoadSlaveNodes(self, netname = None): |
205 | 180 |
cpjpath = os.path.join(self.Root, "nodelist.cpj") |
181 |
if os.path.isfile(cpjpath): |
|
182 |
try: |
|
183 |
networks = eds_utils.ParseCPJFile(cpjpath) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
184 |
network = None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
185 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
186 |
for net in networks: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
187 |
if net["Name"] == netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
188 |
network = net |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
189 |
self.NetworkName = netname |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
190 |
elif len(networks) > 0: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
191 |
network = networks[0] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
192 |
self.NetworkName = network["Name"] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
193 |
if network: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
194 |
for nodeid, node in network["Nodes"].items(): |
205 | 195 |
if node["Present"] == 1: |
196 |
result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"]) |
|
197 |
if result != None: |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
198 |
return result |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
199 |
self.Changed = False |
205 | 200 |
except SyntaxError, message: |
201 |
return "Unable to load CPJ file\n%s"%message |
|
202 |
return None |
|
203 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
204 |
def SaveNodeList(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
205 |
try: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
206 |
cpjpath = os.path.join(self.Root, "nodelist.cpj") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
207 |
content = eds_utils.GenerateCPJContent(self) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
208 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
209 |
file = open(cpjpath, "a") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
210 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
211 |
file = open(cpjpath, "w") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
212 |
file.write(content) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
213 |
file.close() |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
214 |
self.Changed = False |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
215 |
return None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
216 |
except: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
217 |
return "Fail to save node list" |
205 | 218 |
|
219 |
def GetSlaveNodeEntry(self, nodeid, index, subindex = None): |
|
220 |
if nodeid in self.SlaveNodes.keys(): |
|
221 |
self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid) |
|
222 |
return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex) |
|
223 |
else: |
|
224 |
return "Node 0x%2.2X doesn't exist"%nodeid |
|
225 |
||
226 |
def GetMasterNodeEntry(self, index, subindex = None): |
|
227 |
return self.Manager.GetCurrentEntry(index, subindex) |
|
228 |
||
229 |
def SetMasterNodeEntry(self, index, subindex = None, value = None): |
|
230 |
self.Manager.SetCurrentEntry(index, subindex, value) |
|
231 |
||
232 |
def GetOrderNumber(self, nodeid): |
|
233 |
nodeindexes = self.SlaveNodes.keys() |
|
234 |
nodeindexes.sort() |
|
235 |
return nodeindexes.index(nodeid) + 1 |
|
236 |
||
237 |
def GetNodeByOrder(self, order): |
|
238 |
if order > 0: |
|
239 |
nodeindexes = self.SlaveNodes.keys() |
|
240 |
nodeindexes.sort() |
|
241 |
print nodeindexes |
|
242 |
if order <= len(nodeindexes): |
|
243 |
return self.SlaveNodes[nodeindexes[order - 1]]["Node"] |
|
244 |
return None |
|
245 |
||
246 |
def IsCurrentEntry(self, index): |
|
247 |
if self.CurrentSelected != None: |
|
248 |
if self.CurrentSelected == 0: |
|
249 |
return self.Manager.IsCurrentEntry(index) |
|
250 |
else: |
|
251 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
252 |
if node: |
|
253 |
return node.IsEntry(index) |
|
254 |
return False |
|
255 |
||
256 |
def GetEntryInfos(self, index): |
|
257 |
if self.CurrentSelected != None: |
|
258 |
if self.CurrentSelected == 0: |
|
259 |
return self.Manager.GetEntryInfos(index) |
|
260 |
else: |
|
261 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
262 |
if node: |
|
263 |
return node.GetEntryInfos(index) |
|
264 |
return None |
|
265 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
266 |
def GetSubentryInfos(self, index, subindex): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
267 |
if self.CurrentSelected != None: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
268 |
if self.CurrentSelected == 0: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
269 |
return self.Manager.GetSubentryInfos(index, subindex) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
270 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
271 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
272 |
if node: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
273 |
return node.GetSubentryInfos(index, subindex) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
274 |
return None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
275 |
|
205 | 276 |
def GetCurrentValidIndexes(self, min, max): |
277 |
if self.CurrentSelected != None: |
|
278 |
if self.CurrentSelected == 0: |
|
279 |
return self.Manager.GetCurrentValidIndexes(min, max) |
|
280 |
else: |
|
281 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
282 |
if node: |
|
283 |
validindexes = [] |
|
284 |
for index in node.GetIndexes(): |
|
285 |
if min <= index <= max: |
|
286 |
validindexes.append((node.GetEntryName(index), index)) |
|
287 |
return validindexes |
|
288 |
else: |
|
289 |
print "Can't find node" |
|
290 |
return [] |
|
291 |
||
292 |
def GetCurrentEntryValues(self, index): |
|
293 |
if self.CurrentSelected != None: |
|
294 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
295 |
if node: |
|
296 |
return self.Manager.GetNodeEntryValues(node, index) |
|
297 |
else: |
|
298 |
print "Can't find node" |
|
299 |
return [], [] |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
300 |
|
205 | 301 |
if __name__ == "__main__": |
302 |
from nodemanager import * |
|
303 |
import os, sys, shutil |
|
304 |
||
305 |
manager = NodeManager(sys.path[0]) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
306 |
|
205 | 307 |
nodelist = NodeList(manager) |
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
308 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
309 |
result = nodelist.LoadProject("/home/laurent/test_nodelist") |
205 | 310 |
if result != None: |
311 |
print result |
|
312 |
else: |
|
313 |
print "MasterNode :" |
|
314 |
manager.CurrentNode.Print() |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
315 |
|
205 | 316 |
for nodeid, node in nodelist.SlaveNodes.items(): |
317 |
print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid) |
|
318 |
node["Node"].Print() |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
319 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
320 |