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

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

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