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