objdictgen/nodelist.py
author etisserant
Fri, 01 Feb 2008 18:01:36 +0100
changeset 377 bea5a1576cbd
parent 337 9aab477d8a8a
child 505 88956169c8b5
permissions -rw-r--r--
Enhanced cleanup of TCP CAN client/server
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     3
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     4
#This file is part of CanFestival, a library implementing CanOpen Stack. 
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     5
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     6
#Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     7
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     8
#See COPYING file for copyrights details.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     9
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    10
#This library is free software; you can redistribute it and/or
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    11
#modify it under the terms of the GNU Lesser General Public
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    12
#License as published by the Free Software Foundation; either
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    13
#version 2.1 of the License, or (at your option) any later version.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    14
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    15
#This library is distributed in the hope that it will be useful,
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    18
#Lesser General Public License for more details.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    19
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    20
#You should have received a copy of the GNU Lesser General Public
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    21
#License along with this library; if not, write to the Free Software
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    22
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    23
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    24
from node import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    25
import eds_utils
268
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
    26
import os, shutil, types
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    27
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    28
#-------------------------------------------------------------------------------
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    29
#                          Definition of NodeList Object
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    30
#-------------------------------------------------------------------------------
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    31
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    32
"""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    33
Class recording a node list for a CANOpen network.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    34
"""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    35
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    36
class NodeList:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    37
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    38
    def __init__(self, manager, netname = ""):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    39
        self.Root = ""
258
8f7725451453 Some bugs fixed:
lbessard
parents: 253
diff changeset
    40
        self.EDSFolder = ""
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    41
        self.Manager = manager
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    42
        self.NetworkName = netname
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    43
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    44
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    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
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    53
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    54
    def GetNetworkName(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    55
        return self.NetworkName
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    56
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    57
    def SetNetworkName(self, name):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    58
        self.NetworkName = name
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    59
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    60
    def GetManager(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    61
        return self.Manager
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    62
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    63
    def GetRoot(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    64
        return self.Root
272
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    65
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    66
    def SetRoot(self, newrootpath):
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    67
        """
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    68
        Define a new path for the CanOpen network project
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    69
        !!! Does not check if new path is valid !!!
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    70
        """
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    71
        self.Root = newrootpath
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    72
        self.Manager.SetCurrentFilePath(os.path.join(self.Root, "master.od"))
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    73
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    74
    def GetSlaveNumber(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    75
        return len(self.SlaveNodes)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    76
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    77
    def GetSlaveNames(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    78
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    79
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    80
        return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    81
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    82
    def GetSlaveIDs(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    83
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    84
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    85
        return nodes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    86
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    87
    def SetCurrentSelected(self, selected):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    88
        self.CurrentSelected = selected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    89
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    90
    def GetCurrentSelected(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    91
        return self.CurrentSelected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    92
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    93
    def LoadProject(self, root, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    94
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    95
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    96
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    97
        self.Root = root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    98
        if not os.path.exists(self.Root):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    99
            return "\"%s\" folder doesn't exist"%self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   100
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   101
        self.EDSFolder = os.path.join(self.Root, "eds")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   102
        if not os.path.exists(self.EDSFolder):
265
36cc4c87cdaa Create "eds" folder if it doesn't exist in any case
lbessard
parents: 258
diff changeset
   103
            os.mkdir(self.EDSFolder)
36cc4c87cdaa Create "eds" folder if it doesn't exist in any case
lbessard
parents: 258
diff changeset
   104
            #return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   105
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   106
        files = os.listdir(self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   107
        for file in files:
243
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   108
            filepath = os.path.join(self.EDSFolder, file)
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   109
            if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".eds":
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   110
                result = self.LoadEDS(file)
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   111
                if result != None:
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   112
                    return result
270
178030c1dff8 Removed debug prints
etisserant
parents: 268
diff changeset
   113
                
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   114
        result = self.LoadMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   115
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   116
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   117
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   118
        result = self.LoadSlaveNodes(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   119
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   120
            return result
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   121
        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   122
        self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   123
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   124
    def SaveProject(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   125
        result = self.SaveMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   126
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   127
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   128
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   129
        result = self.SaveNodeList(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   130
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   131
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   132
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   133
    def ImportEDSFile(self, edspath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   134
        dir, file = os.path.split(edspath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   135
        eds = os.path.join(self.EDSFolder, file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   136
        if os.path.isfile(eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   137
            return "EDS file already imported"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   138
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   139
            shutil.copy(edspath, self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   140
            return self.LoadEDS(file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   141
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   142
    def LoadEDS(self, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   143
        edspath = os.path.join(self.EDSFolder, eds)
258
8f7725451453 Some bugs fixed:
lbessard
parents: 253
diff changeset
   144
        node = eds_utils.GenerateNode(edspath)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   145
        if isinstance(node, Node):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   146
            self.EDSNodes[eds] = node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   147
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   148
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   149
            return node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   150
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   151
    def AddSlaveNode(self, nodeName, nodeID, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   152
        if eds in self.EDSNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   153
            slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   154
            self.SlaveNodes[nodeID] = slave
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   155
            self.Changed = True
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   156
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   157
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   158
            return "\"%s\" EDS file is not available"%eds
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   159
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   160
    def RemoveSlaveNode(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   161
        if index in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   162
            self.SlaveNodes.pop(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   163
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   164
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   165
            return "Node with \"0x%2.2X\" ID doesn't exist"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   166
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   167
    def LoadMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   168
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   169
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   170
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   171
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   172
        if os.path.isfile(masterpath):
268
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   173
            result = self.Manager.OpenFileInCurrent(masterpath)
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   174
        else:
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   175
            result = self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"])
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   176
        if not isinstance(result, types.IntType):
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   177
            return result
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   178
        return None
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   179
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   180
    def SaveMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   181
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   182
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   183
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   184
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   185
        if self.Manager.SaveCurrentInFile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   186
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   187
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   188
            return "Fail to save Master Node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   189
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   190
    def LoadSlaveNodes(self, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   191
        cpjpath = os.path.join(self.Root, "nodelist.cpj")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   192
        if os.path.isfile(cpjpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   193
            try:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   194
                networks = eds_utils.ParseCPJFile(cpjpath)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   195
                network = None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   196
                if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   197
                    for net in networks:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   198
                        if net["Name"] == netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   199
                            network = net
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   200
                    self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   201
                elif len(networks) > 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   202
                    network = networks[0]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   203
                    self.NetworkName = network["Name"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   204
                if network:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   205
                    for nodeid, node in network["Nodes"].items():
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   206
                        if node["Present"] == 1:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   207
                            result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   208
                            if result != None:
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   209
                                return result        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   210
                self.Changed = False
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   211
            except SyntaxError, message:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   212
                return "Unable to load CPJ file\n%s"%message
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   213
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   214
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   215
    def SaveNodeList(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   216
        try:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   217
            cpjpath = os.path.join(self.Root, "nodelist.cpj")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   218
            content = eds_utils.GenerateCPJContent(self)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   219
            if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   220
                file = open(cpjpath, "a")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   221
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   222
                file = open(cpjpath, "w")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   223
            file.write(content)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   224
            file.close()
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   225
            self.Changed = False
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   226
            return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   227
        except:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   228
            return "Fail to save node list"
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   229
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   230
    def GetSlaveNodeEntry(self, nodeid, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   231
        if nodeid in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   232
            self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   233
            return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   234
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   235
            return "Node 0x%2.2X doesn't exist"%nodeid
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   236
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   237
    def GetMasterNodeEntry(self, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   238
        return self.Manager.GetCurrentEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   239
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   240
    def SetMasterNodeEntry(self, index, subindex = None, value = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   241
        self.Manager.SetCurrentEntry(index, subindex, value)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   242
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   243
    def GetOrderNumber(self, nodeid):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   244
        nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   245
        nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   246
        return nodeindexes.index(nodeid) + 1
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   247
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   248
    def GetNodeByOrder(self, order):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   249
        if order > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   250
            nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   251
            nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   252
            if order <= len(nodeindexes):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   253
                return self.SlaveNodes[nodeindexes[order - 1]]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   254
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   255
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   256
    def IsCurrentEntry(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   257
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   258
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   259
                return self.Manager.IsCurrentEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   260
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   261
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   262
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   263
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   264
                    return node.IsEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   265
        return False
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   266
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   267
    def GetEntryInfos(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   268
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   269
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   270
                return self.Manager.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   271
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   272
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   273
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   274
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   275
                    return node.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   276
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   277
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   278
    def GetSubentryInfos(self, index, subindex):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   279
        if self.CurrentSelected != None:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   280
            if self.CurrentSelected == 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   281
                return self.Manager.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   282
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   283
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   284
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   285
                    node.SetNodeID(self.CurrentSelected)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   286
                    return node.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   287
        return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   288
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   289
    def GetCurrentValidIndexes(self, min, max):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   290
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   291
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   292
                return self.Manager.GetCurrentValidIndexes(min, max)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   293
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   294
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   295
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   296
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   297
                    validindexes = []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   298
                    for index in node.GetIndexes():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   299
                        if min <= index <= max:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   300
                            validindexes.append((node.GetEntryName(index), index))
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   301
                    return validindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   302
                else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   303
                    print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   304
        return []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   305
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   306
    def GetCurrentEntryValues(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   307
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   308
            node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   309
            if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   310
                node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   311
                return self.Manager.GetNodeEntryValues(node, index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   312
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   313
                print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   314
        return [], []
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   315
    
327
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   316
    def AddToMasterDCF(self, node_id, index, subindex, size, value):
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   317
        # Adding DCF entry into Master node
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   318
        if not self.Manager.IsCurrentEntry(0x1F22):
337
9aab477d8a8a Bug on DCF edition fixed
lbessard
parents: 327
diff changeset
   319
            self.Manager.ManageEntriesOfCurrent([0x1F22], [])
327
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   320
        self.Manager.AddSubentriesToCurrent(0x1F22, 127)
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   321
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   322
        self.Manager.AddToDCF(node_id, index, subindex, size, value)
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   323
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   324
if __name__ == "__main__":
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   325
    from nodemanager import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   326
    import os, sys, shutil
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   327
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   328
    manager = NodeManager(sys.path[0])
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   329
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   330
    nodelist = NodeList(manager)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   331
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   332
    result = nodelist.LoadProject("/home/laurent/test_nodelist")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   333
    if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   334
        print result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   335
    else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   336
        print "MasterNode :"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   337
        manager.CurrentNode.Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   338
        print 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   339
        for nodeid, node in nodelist.SlaveNodes.items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   340
            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   341
            node["Node"].Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   342
            print
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   343