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