author | etisserant |
Wed, 01 Aug 2007 13:48:03 +0200 | |
changeset 246 | d635cfc520ee |
parent 243 | 7fcc129a06ce |
child 253 | bf58ce630b88 |
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 gnosis.xml.pickle import * |
|
25 |
from gnosis.xml.pickle.util import setParanoia |
|
26 |
setParanoia(0) |
|
27 |
||
28 |
from node import * |
|
29 |
import eds_utils |
|
30 |
import os, shutil |
|
31 |
||
32 |
#------------------------------------------------------------------------------- |
|
33 |
# Definition of NodeList Object |
|
34 |
#------------------------------------------------------------------------------- |
|
35 |
||
36 |
""" |
|
37 |
Class recording a node list for a CANOpen network. |
|
38 |
""" |
|
39 |
||
40 |
class NodeList: |
|
41 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
42 |
def __init__(self, manager, netname = ""): |
205 | 43 |
self.Root = "" |
44 |
self.Manager = manager |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
45 |
self.NetworkName = netname |
205 | 46 |
self.SlaveNodes = {} |
47 |
self.EDSNodes = {} |
|
48 |
self.CurrentSelected = None |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
49 |
self.Changed = False |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
50 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
51 |
def HasChanged(self): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
52 |
return self.Changed or not self.Manager.CurrentIsSaved() |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
53 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
54 |
def ForceChanged(self, changed): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
55 |
self.Changed = changed |
205 | 56 |
|
57 |
def GetNetworkName(self): |
|
58 |
return self.NetworkName |
|
59 |
||
60 |
def SetNetworkName(self, name): |
|
61 |
self.NetworkName = name |
|
62 |
||
63 |
def GetManager(self): |
|
64 |
return self.Manager |
|
65 |
||
66 |
def GetRoot(self): |
|
67 |
return self.Root |
|
68 |
||
69 |
def GetSlaveNumber(self): |
|
70 |
return len(self.SlaveNodes) |
|
71 |
||
72 |
def GetSlaveNames(self): |
|
73 |
nodes = self.SlaveNodes.keys() |
|
74 |
nodes.sort() |
|
75 |
return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes] |
|
76 |
||
77 |
def GetSlaveIDs(self): |
|
78 |
nodes = self.SlaveNodes.keys() |
|
79 |
nodes.sort() |
|
80 |
return nodes |
|
81 |
||
82 |
def SetCurrentSelected(self, selected): |
|
83 |
self.CurrentSelected = selected |
|
84 |
||
85 |
def GetCurrentSelected(self): |
|
86 |
return self.CurrentSelected |
|
87 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
88 |
def LoadProject(self, root, netname = None): |
205 | 89 |
self.SlaveNodes = {} |
90 |
self.EDSNodes = {} |
|
91 |
||
92 |
self.Root = root |
|
93 |
if not os.path.exists(self.Root): |
|
94 |
return "\"%s\" folder doesn't exist"%self.Root |
|
95 |
||
96 |
self.EDSFolder = os.path.join(self.Root, "eds") |
|
97 |
if not os.path.exists(self.EDSFolder): |
|
98 |
return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root |
|
99 |
||
100 |
files = os.listdir(self.EDSFolder) |
|
101 |
for file in files: |
|
243 | 102 |
filepath = os.path.join(self.EDSFolder, file) |
103 |
if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".eds": |
|
104 |
result = self.LoadEDS(file) |
|
105 |
if result != None: |
|
106 |
return result |
|
205 | 107 |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
108 |
result = self.LoadMasterNode(netname) |
205 | 109 |
if result != None: |
110 |
return result |
|
111 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
112 |
result = self.LoadSlaveNodes(netname) |
205 | 113 |
if result != None: |
114 |
return result |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
115 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
116 |
self.NetworkName = netname |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
117 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
118 |
def SaveProject(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
119 |
result = self.SaveMasterNode(netname) |
205 | 120 |
if result != None: |
121 |
return result |
|
122 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
123 |
result = self.SaveNodeList(netname) |
205 | 124 |
if result != None: |
125 |
return result |
|
126 |
||
127 |
def ImportEDSFile(self, edspath): |
|
128 |
dir, file = os.path.split(edspath) |
|
129 |
eds = os.path.join(self.EDSFolder, file) |
|
130 |
if os.path.isfile(eds): |
|
131 |
return "EDS file already imported" |
|
132 |
else: |
|
133 |
shutil.copy(edspath, self.EDSFolder) |
|
134 |
return self.LoadEDS(file) |
|
135 |
||
136 |
def LoadEDS(self, eds): |
|
137 |
edspath = os.path.join(self.EDSFolder, eds) |
|
138 |
node = eds_utils.GenerateNode(edspath, self.Manager.ScriptDirectory) |
|
139 |
if isinstance(node, Node): |
|
140 |
self.EDSNodes[eds] = node |
|
141 |
return None |
|
142 |
else: |
|
143 |
return node |
|
144 |
||
145 |
def AddSlaveNode(self, nodeName, nodeID, eds): |
|
146 |
if eds in self.EDSNodes.keys(): |
|
147 |
slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]} |
|
148 |
self.SlaveNodes[nodeID] = slave |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
149 |
self.Changed = True |
205 | 150 |
return None |
151 |
else: |
|
152 |
return "\"%s\" EDS file is not available"%eds |
|
153 |
||
154 |
def RemoveSlaveNode(self, index): |
|
155 |
if index in self.SlaveNodes.keys(): |
|
156 |
self.SlaveNodes.pop(index) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
157 |
OnCloseProjectMenu |
205 | 158 |
return None |
159 |
else: |
|
160 |
return "Node with \"0x%2.2X\" ID doesn't exist" |
|
161 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
162 |
def LoadMasterNode(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
163 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
164 |
masterpath = os.path.join(self.Root, "%s_master.od"%netname) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
165 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
166 |
masterpath = os.path.join(self.Root, "master.od") |
205 | 167 |
if os.path.isfile(masterpath): |
168 |
self.Manager.OpenFileInCurrent(masterpath) |
|
169 |
else: |
|
170 |
self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"]) |
|
171 |
return None |
|
172 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
173 |
def SaveMasterNode(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
174 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
175 |
masterpath = os.path.join(self.Root, "%s_master.od"%netname) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
176 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
177 |
masterpath = os.path.join(self.Root, "master.od") |
205 | 178 |
if self.Manager.SaveCurrentInFile(masterpath): |
179 |
return None |
|
180 |
else: |
|
181 |
return "Fail to save Master Node" |
|
182 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
183 |
def LoadSlaveNodes(self, netname = None): |
205 | 184 |
cpjpath = os.path.join(self.Root, "nodelist.cpj") |
185 |
if os.path.isfile(cpjpath): |
|
186 |
try: |
|
187 |
networks = eds_utils.ParseCPJFile(cpjpath) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
188 |
network = None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
189 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
190 |
for net in networks: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
191 |
if net["Name"] == netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
192 |
network = net |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
193 |
self.NetworkName = netname |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
194 |
elif len(networks) > 0: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
195 |
network = networks[0] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
196 |
self.NetworkName = network["Name"] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
197 |
if network: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
198 |
for nodeid, node in network["Nodes"].items(): |
205 | 199 |
if node["Present"] == 1: |
200 |
result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"]) |
|
201 |
if result != None: |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
202 |
return result |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
203 |
self.Changed = False |
205 | 204 |
except SyntaxError, message: |
205 |
return "Unable to load CPJ file\n%s"%message |
|
206 |
return None |
|
207 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
208 |
def SaveNodeList(self, netname = None): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
209 |
try: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
210 |
cpjpath = os.path.join(self.Root, "nodelist.cpj") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
211 |
content = eds_utils.GenerateCPJContent(self) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
212 |
if netname: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
213 |
file = open(cpjpath, "a") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
214 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
215 |
file = open(cpjpath, "w") |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
216 |
file.write(content) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
217 |
file.close() |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
218 |
self.Changed = False |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
219 |
return None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
220 |
except: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
221 |
return "Fail to save node list" |
205 | 222 |
|
223 |
def GetSlaveNodeEntry(self, nodeid, index, subindex = None): |
|
224 |
if nodeid in self.SlaveNodes.keys(): |
|
225 |
self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid) |
|
226 |
return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex) |
|
227 |
else: |
|
228 |
return "Node 0x%2.2X doesn't exist"%nodeid |
|
229 |
||
230 |
def GetMasterNodeEntry(self, index, subindex = None): |
|
231 |
return self.Manager.GetCurrentEntry(index, subindex) |
|
232 |
||
233 |
def SetMasterNodeEntry(self, index, subindex = None, value = None): |
|
234 |
self.Manager.SetCurrentEntry(index, subindex, value) |
|
235 |
||
236 |
def GetOrderNumber(self, nodeid): |
|
237 |
nodeindexes = self.SlaveNodes.keys() |
|
238 |
nodeindexes.sort() |
|
239 |
return nodeindexes.index(nodeid) + 1 |
|
240 |
||
241 |
def GetNodeByOrder(self, order): |
|
242 |
if order > 0: |
|
243 |
nodeindexes = self.SlaveNodes.keys() |
|
244 |
nodeindexes.sort() |
|
245 |
print nodeindexes |
|
246 |
if order <= len(nodeindexes): |
|
247 |
return self.SlaveNodes[nodeindexes[order - 1]]["Node"] |
|
248 |
return None |
|
249 |
||
250 |
def IsCurrentEntry(self, index): |
|
251 |
if self.CurrentSelected != None: |
|
252 |
if self.CurrentSelected == 0: |
|
253 |
return self.Manager.IsCurrentEntry(index) |
|
254 |
else: |
|
255 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
256 |
if node: |
|
257 |
return node.IsEntry(index) |
|
258 |
return False |
|
259 |
||
260 |
def GetEntryInfos(self, index): |
|
261 |
if self.CurrentSelected != None: |
|
262 |
if self.CurrentSelected == 0: |
|
263 |
return self.Manager.GetEntryInfos(index) |
|
264 |
else: |
|
265 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
266 |
if node: |
|
267 |
return node.GetEntryInfos(index) |
|
268 |
return None |
|
269 |
||
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
270 |
def GetSubentryInfos(self, index, subindex): |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
271 |
if self.CurrentSelected != None: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
272 |
if self.CurrentSelected == 0: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
273 |
return self.Manager.GetSubentryInfos(index, subindex) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
274 |
else: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
275 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
276 |
if node: |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
277 |
return node.GetSubentryInfos(index, subindex) |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
278 |
return None |
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
279 |
|
205 | 280 |
def GetCurrentValidIndexes(self, min, max): |
281 |
if self.CurrentSelected != None: |
|
282 |
if self.CurrentSelected == 0: |
|
283 |
return self.Manager.GetCurrentValidIndexes(min, max) |
|
284 |
else: |
|
285 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
286 |
if node: |
|
287 |
validindexes = [] |
|
288 |
for index in node.GetIndexes(): |
|
289 |
if min <= index <= max: |
|
290 |
validindexes.append((node.GetEntryName(index), index)) |
|
291 |
return validindexes |
|
292 |
else: |
|
293 |
print "Can't find node" |
|
294 |
return [] |
|
295 |
||
296 |
def GetCurrentEntryValues(self, index): |
|
297 |
if self.CurrentSelected != None: |
|
298 |
node = self.SlaveNodes[self.CurrentSelected]["Node"] |
|
299 |
if node: |
|
300 |
return self.Manager.GetNodeEntryValues(node, index) |
|
301 |
else: |
|
302 |
print "Can't find node" |
|
303 |
return [], [] |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
304 |
|
205 | 305 |
if __name__ == "__main__": |
306 |
from nodemanager import * |
|
307 |
import os, sys, shutil |
|
308 |
||
309 |
manager = NodeManager(sys.path[0]) |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
310 |
|
205 | 311 |
nodelist = NodeList(manager) |
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
312 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
313 |
result = nodelist.LoadProject("/home/laurent/test_nodelist") |
205 | 314 |
if result != None: |
315 |
print result |
|
316 |
else: |
|
317 |
print "MasterNode :" |
|
318 |
manager.CurrentNode.Print() |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
319 |
|
205 | 320 |
for nodeid, node in nodelist.SlaveNodes.items(): |
321 |
print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid) |
|
322 |
node["Node"].Print() |
|
242
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
323 |
|
4864f7f01e1d
Changes in networkedit for integration with beremiz
lbessard
parents:
239
diff
changeset
|
324 |