nico@207: nico@207: nico@207: CanFestival: /home/epimerde/documents/tc11/CanFestival-3/objdictgen/nodelist.py Source File nico@207: nico@207: nico@207: nico@207: nico@207:
nico@207:
nico@207:
nico@207:
nico@207:

/home/epimerde/documents/tc11/CanFestival-3/objdictgen/nodelist.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
nico@207: 00002 # -*- coding: utf-8 -*-
nico@207: 00003 
nico@207: 00004 #This file is part of CanFestival, a library implementing CanOpen Stack. 
nico@207: 00005 #
nico@207: 00006 #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
nico@207: 00007 #
nico@207: 00008 #See COPYING file for copyrights details.
nico@207: 00009 #
nico@207: 00010 #This library is free software; you can redistribute it and/or
nico@207: 00011 #modify it under the terms of the GNU Lesser General Public
nico@207: 00012 #License as published by the Free Software Foundation; either
nico@207: 00013 #version 2.1 of the License, or (at your option) any later version.
nico@207: 00014 #
nico@207: 00015 #This library is distributed in the hope that it will be useful,
nico@207: 00016 #but WITHOUT ANY WARRANTY; without even the implied warranty of
nico@207: 00017 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nico@207: 00018 #Lesser General Public License for more details.
nico@207: 00019 #
nico@207: 00020 #You should have received a copy of the GNU Lesser General Public
nico@207: 00021 #License along with this library; if not, write to the Free Software
nico@207: 00022 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
nico@207: 00023 
nico@207: 00024 from gnosis.xml.pickle import *
nico@207: 00025 from gnosis.xml.pickle.util import setParanoia
nico@207: 00026 setParanoia(0)
nico@207: 00027 
nico@207: 00028 from node import *
nico@207: 00029 import eds_utils
nico@207: 00030 import os, shutil
nico@207: 00031 
nico@207: 00032 #-------------------------------------------------------------------------------
nico@207: 00033 #                          Definition of NodeList Object
nico@207: 00034 #-------------------------------------------------------------------------------
nico@207: 00035 
nico@207: 00036 """
nico@207: 00037 Class recording a node list for a CANOpen network.
nico@207: 00038 """
nico@207: 00039 
nico@207: 00040 class NodeList:
nico@207: 00041     
nico@207: 00042     def __init__(self, manager):
nico@207: 00043         self.RootRoot = ""
nico@207: 00044         self.ManagerManager = manager
nico@207: 00045         self.NetworkNameNetworkName = ""
nico@207: 00046         self.SlaveNodesSlaveNodes = {}
nico@207: 00047         self.EDSNodesEDSNodes = {}
nico@207: 00048         self.CurrentSelectedCurrentSelected = None
nico@207: 00049     
nico@207: 00050     def GetNetworkName(self):
nico@207: 00051         return self.NetworkNameNetworkName
nico@207: 00052     
nico@207: 00053     def SetNetworkName(self, name):
nico@207: 00054         self.NetworkNameNetworkName = name
nico@207: 00055     
nico@207: 00056     def GetManager(self):
nico@207: 00057         return self.ManagerManager
nico@207: 00058     
nico@207: 00059     def GetRoot(self):
nico@207: 00060         return self.RootRoot
nico@207: 00061     
nico@207: 00062     def GetSlaveNumber(self):
nico@207: 00063         return len(self.SlaveNodesSlaveNodes)
nico@207: 00064     
nico@207: 00065     def GetSlaveNames(self):
nico@207: 00066         nodes = self.SlaveNodesSlaveNodes.keys()
nico@207: 00067         nodes.sort()
nico@207: 00068         return ["0x%2.2X %s"%(idx, self.SlaveNodesSlaveNodes[idx]["Name"]) for idx in nodes]
nico@207: 00069     
nico@207: 00070     def GetSlaveIDs(self):
nico@207: 00071         nodes = self.SlaveNodesSlaveNodes.keys()
nico@207: 00072         nodes.sort()
nico@207: 00073         return nodes
nico@207: 00074         
nico@207: 00075     def SetCurrentSelected(self, selected):
nico@207: 00076         self.CurrentSelectedCurrentSelected = selected
nico@207: 00077         
nico@207: 00078     def GetCurrentSelected(self):
nico@207: 00079         return self.CurrentSelectedCurrentSelected
nico@207: 00080             
nico@207: 00081     def LoadProject(self, root):
nico@207: 00082         self.SlaveNodesSlaveNodes = {}
nico@207: 00083         self.EDSNodesEDSNodes = {}
nico@207: 00084         
nico@207: 00085         self.RootRoot = root
nico@207: 00086         if not os.path.exists(self.RootRoot):
nico@207: 00087             return "\"%s\" folder doesn't exist"%self.RootRoot
nico@207: 00088         
nico@207: 00089         self.EDSFolderEDSFolder = os.path.join(self.RootRoot, "eds")
nico@207: 00090         if not os.path.exists(self.EDSFolderEDSFolder):
nico@207: 00091             return "\"%s\" folder doesn't contain a \"eds\" folder"%self.RootRoot
nico@207: 00092         
nico@207: 00093         files = os.listdir(self.EDSFolderEDSFolder)
nico@207: 00094         for file in files:
nico@207: 00095             result = self.LoadEDSLoadEDS(file)
nico@207: 00096             if result != None:
nico@207: 00097                 return result
nico@207: 00098                 
nico@207: 00099         result = self.LoadMasterNodeLoadMasterNode()
nico@207: 00100         if result != None:
nico@207: 00101             return result
nico@207: 00102             
nico@207: 00103         result = self.LoadSlaveNodesLoadSlaveNodes()
nico@207: 00104         if result != None:
nico@207: 00105             return result
nico@207: 00106     
nico@207: 00107     def SaveProject(self):
nico@207: 00108         result = self.SaveMasterNodeSaveMasterNode()
nico@207: 00109         if result != None:
nico@207: 00110             return result
nico@207: 00111             
nico@207: 00112         result = self.SaveNodeListSaveNodeList()
nico@207: 00113         if result != None:
nico@207: 00114             return result
nico@207: 00115     
nico@207: 00116     def ImportEDSFile(self, edspath):
nico@207: 00117         dir, file = os.path.split(edspath)
nico@207: 00118         eds = os.path.join(self.EDSFolderEDSFolder, file)
nico@207: 00119         if os.path.isfile(eds):
nico@207: 00120             return "EDS file already imported"
nico@207: 00121         else:
nico@207: 00122             shutil.copy(edspath, self.EDSFolderEDSFolder)
nico@207: 00123             return self.LoadEDSLoadEDS(file)
nico@207: 00124     
nico@207: 00125     def LoadEDS(self, eds):
nico@207: 00126         edspath = os.path.join(self.EDSFolderEDSFolder, eds)
nico@207: 00127         node = eds_utils.GenerateNode(edspath, self.ManagerManager.ScriptDirectory)
nico@207: 00128         if isinstance(node, Node):
nico@207: 00129             self.EDSNodesEDSNodes[eds] = node
nico@207: 00130             return None
nico@207: 00131         else:
nico@207: 00132             return node
nico@207: 00133     
nico@207: 00134     def AddSlaveNode(self, nodeName, nodeID, eds):
nico@207: 00135         if eds in self.EDSNodesEDSNodes.keys():
nico@207: 00136             slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodesEDSNodes[eds]}
nico@207: 00137             self.SlaveNodesSlaveNodes[nodeID] = slave
nico@207: 00138             return None
nico@207: 00139         else:
nico@207: 00140             return "\"%s\" EDS file is not available"%eds
nico@207: 00141     
nico@207: 00142     def RemoveSlaveNode(self, index):
nico@207: 00143         if index in self.SlaveNodesSlaveNodes.keys():
nico@207: 00144             self.SlaveNodesSlaveNodes.pop(index)
nico@207: 00145             return None
nico@207: 00146         else:
nico@207: 00147             return "Node with \"0x%2.2X\" ID doesn't exist"
nico@207: 00148     
nico@207: 00149     def LoadMasterNode(self):
nico@207: 00150         masterpath = os.path.join(self.RootRoot, "master.od")
nico@207: 00151         if os.path.isfile(masterpath):
nico@207: 00152             self.ManagerManager.OpenFileInCurrent(masterpath)
nico@207: 00153         else:
nico@207: 00154             self.ManagerManager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"])
nico@207: 00155         return None
nico@207: 00156     
nico@207: 00157     def SaveMasterNode(self):
nico@207: 00158         masterpath = os.path.join(self.RootRoot, "master.od")
nico@207: 00159         if self.ManagerManager.SaveCurrentInFile(masterpath):
nico@207: 00160             return None
nico@207: 00161         else:
nico@207: 00162             return "Fail to save Master Node"
nico@207: 00163     
nico@207: 00164     def LoadSlaveNodes(self):
nico@207: 00165         cpjpath = os.path.join(self.RootRoot, "nodelist.cpj")
nico@207: 00166         if os.path.isfile(cpjpath):
nico@207: 00167             try:
nico@207: 00168                 networks = eds_utils.ParseCPJFile(cpjpath)
nico@207: 00169                 if len(networks) > 0:
nico@207: 00170                     self.NetworkNameNetworkName = networks[0]["Name"]
nico@207: 00171                     for nodeid, node in networks[0]["Nodes"].items():
nico@207: 00172                         if node["Present"] == 1:
nico@207: 00173                             result = self.AddSlaveNodeAddSlaveNode(node["Name"], nodeid, node["DCFName"])
nico@207: 00174                             if result != None:
nico@207: 00175                                 return result
nico@207: 00176             except SyntaxError, message:
nico@207: 00177                 return "Unable to load CPJ file\n%s"%message
nico@207: 00178         return None
nico@207: 00179     
nico@207: 00180     def SaveNodeList(self):
nico@207: 00181         cpjpath = os.path.join(self.RootRoot, "nodelist.cpj")
nico@207: 00182         content = eds_utils.GenerateCPJContent(self)
nico@207: 00183         file = open(cpjpath, "w")
nico@207: 00184         file.write(content)
nico@207: 00185         file.close()
nico@207: 00186     
nico@207: 00187     def GetSlaveNodeEntry(self, nodeid, index, subindex = None):
nico@207: 00188         if nodeid in self.SlaveNodesSlaveNodes.keys():
nico@207: 00189             self.SlaveNodesSlaveNodes[nodeid]["Node"].SetNodeID(nodeid)
nico@207: 00190             return self.SlaveNodesSlaveNodes[nodeid]["Node"].GetEntry(index, subindex)
nico@207: 00191         else:
nico@207: 00192             return "Node 0x%2.2X doesn't exist"%nodeid
nico@207: 00193 
nico@207: 00194     def GetMasterNodeEntry(self, index, subindex = None):
nico@207: 00195         return self.ManagerManager.GetCurrentEntry(index, subindex)
nico@207: 00196         
nico@207: 00197     def SetMasterNodeEntry(self, index, subindex = None, value = None):
nico@207: 00198         self.ManagerManager.SetCurrentEntry(index, subindex, value)
nico@207: 00199     
nico@207: 00200     def GetOrderNumber(self, nodeid):
nico@207: 00201         nodeindexes = self.SlaveNodesSlaveNodes.keys()
nico@207: 00202         nodeindexes.sort()
nico@207: 00203         return nodeindexes.index(nodeid) + 1
nico@207: 00204     
nico@207: 00205     def GetNodeByOrder(self, order):
nico@207: 00206         if order > 0:
nico@207: 00207             nodeindexes = self.SlaveNodesSlaveNodes.keys()
nico@207: 00208             nodeindexes.sort()
nico@207: 00209             print nodeindexes
nico@207: 00210             if order <= len(nodeindexes):
nico@207: 00211                 return self.SlaveNodesSlaveNodes[nodeindexes[order - 1]]["Node"]
nico@207: 00212         return None
nico@207: 00213     
nico@207: 00214     def IsCurrentEntry(self, index):
nico@207: 00215         if self.CurrentSelectedCurrentSelected != None:
nico@207: 00216             if self.CurrentSelectedCurrentSelected == 0:
nico@207: 00217                 return self.ManagerManager.IsCurrentEntry(index)
nico@207: 00218             else:
nico@207: 00219                 node = self.SlaveNodesSlaveNodes[self.CurrentSelectedCurrentSelected]["Node"]
nico@207: 00220                 if node:
nico@207: 00221                     return node.IsEntry(index)
nico@207: 00222         return False
nico@207: 00223     
nico@207: 00224     def GetEntryInfos(self, index):
nico@207: 00225         if self.CurrentSelectedCurrentSelected != None:
nico@207: 00226             if self.CurrentSelectedCurrentSelected == 0:
nico@207: 00227                 return self.ManagerManager.GetEntryInfos(index)
nico@207: 00228             else:
nico@207: 00229                 node = self.SlaveNodesSlaveNodes[self.CurrentSelectedCurrentSelected]["Node"]
nico@207: 00230                 if node:
nico@207: 00231                     return node.GetEntryInfos(index)
nico@207: 00232         return None
nico@207: 00233 
nico@207: 00234     def GetCurrentValidIndexes(self, min, max):
nico@207: 00235         if self.CurrentSelectedCurrentSelected != None:
nico@207: 00236             if self.CurrentSelectedCurrentSelected == 0:
nico@207: 00237                 return self.ManagerManager.GetCurrentValidIndexes(min, max)
nico@207: 00238             else:
nico@207: 00239                 node = self.SlaveNodesSlaveNodes[self.CurrentSelectedCurrentSelected]["Node"]
nico@207: 00240                 if node:
nico@207: 00241                     validindexes = []
nico@207: 00242                     for index in node.GetIndexes():
nico@207: 00243                         if min <= index <= max:
nico@207: 00244                             validindexes.append((node.GetEntryName(index), index))
nico@207: 00245                     return validindexes
nico@207: 00246                 else:
nico@207: 00247                     print "Can't find node"
nico@207: 00248         return []
nico@207: 00249     
nico@207: 00250     def GetCurrentEntryValues(self, index):
nico@207: 00251         if self.CurrentSelectedCurrentSelected != None:
nico@207: 00252             node = self.SlaveNodesSlaveNodes[self.CurrentSelectedCurrentSelected]["Node"]
nico@207: 00253             if node:
nico@207: 00254                 return self.ManagerManager.GetNodeEntryValues(node, index)
nico@207: 00255             else:
nico@207: 00256                 print "Can't find node"
nico@207: 00257         return [], []
nico@207: 00258     
nico@207: 00259 if __name__ == "__main__":
nico@207: 00260     from nodemanager import *
nico@207: 00261     import os, sys, shutil
nico@207: 00262     
nico@207: 00263     manager = NodeManager(sys.path[0])
nico@207: 00264     
nico@207: 00265     nodelist = NodeList(manager)
nico@207: 00266     
nico@207: 00267     result = nodelist.LoadProject("/home/laurent/test_nodelist")
nico@207: 00268     if result != None:
nico@207: 00269         print result
nico@207: 00270     else:
nico@207: 00271         print "MasterNode :"
nico@207: 00272         manager.CurrentNode.Print()
nico@207: 00273         print 
nico@207: 00274         for nodeid, node in nodelist.SlaveNodes.items():
nico@207: 00275             print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
nico@207: 00276             node["Node"].Print()
nico@207: 00277             print
nico@207: 00278 
nico@207: 

Generated on Mon Jun 4 16:29:06 2007 for CanFestival by  nico@207: nico@207: doxygen 1.5.1
nico@207: nico@207: