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