objdictgen/nodelist.py
author etisserant
Thu, 30 Aug 2007 17:03:34 +0200
changeset 262 f1c892f3f93c
parent 258 8f7725451453
child 265 36cc4c87cdaa
permissions -rw-r--r--
Added exclusion mechanism to TestMasterSlave finish code
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
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    26
import os, shutil
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
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    65
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    66
    def GetSlaveNumber(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    67
        return len(self.SlaveNodes)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    68
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    69
    def GetSlaveNames(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    70
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    71
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    72
        return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    73
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    74
    def GetSlaveIDs(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    75
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    76
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    77
        return nodes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    78
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    79
    def SetCurrentSelected(self, selected):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    80
        self.CurrentSelected = selected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    81
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    82
    def GetCurrentSelected(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    83
        return self.CurrentSelected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    84
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    85
    def LoadProject(self, root, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    86
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    87
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    88
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    89
        self.Root = root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    90
        if not os.path.exists(self.Root):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    91
            return "\"%s\" folder doesn't exist"%self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    92
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    93
        self.EDSFolder = os.path.join(self.Root, "eds")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    94
        if not os.path.exists(self.EDSFolder):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    95
            return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    96
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    97
        files = os.listdir(self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    98
        for file in files:
243
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
    99
            filepath = os.path.join(self.EDSFolder, file)
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   100
            if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".eds":
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   101
                result = self.LoadEDS(file)
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   102
                if result != None:
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   103
                    return result
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   104
                
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   105
        result = self.LoadMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   106
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   107
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   108
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   109
        result = self.LoadSlaveNodes(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   110
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   111
            return result
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   112
        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   113
        self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   114
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   115
    def SaveProject(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   116
        result = self.SaveMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   117
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   118
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   119
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   120
        result = self.SaveNodeList(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   121
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   122
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   123
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   124
    def ImportEDSFile(self, edspath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   125
        dir, file = os.path.split(edspath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   126
        eds = os.path.join(self.EDSFolder, file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   127
        if os.path.isfile(eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   128
            return "EDS file already imported"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   129
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   130
            shutil.copy(edspath, self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   131
            return self.LoadEDS(file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   132
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   133
    def LoadEDS(self, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   134
        edspath = os.path.join(self.EDSFolder, eds)
258
8f7725451453 Some bugs fixed:
lbessard
parents: 253
diff changeset
   135
        node = eds_utils.GenerateNode(edspath)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   136
        if isinstance(node, Node):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   137
            self.EDSNodes[eds] = node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   138
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   139
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   140
            return node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   141
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   142
    def AddSlaveNode(self, nodeName, nodeID, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   143
        if eds in self.EDSNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   144
            slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   145
            self.SlaveNodes[nodeID] = slave
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   146
            self.Changed = True
205
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 "\"%s\" EDS file is not available"%eds
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   150
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   151
    def RemoveSlaveNode(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   152
        if index in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   153
            self.SlaveNodes.pop(index)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   154
            OnCloseProjectMenu
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   155
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   156
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   157
            return "Node with \"0x%2.2X\" ID doesn't exist"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   158
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   159
    def LoadMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   160
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   161
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   162
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   163
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   164
        if os.path.isfile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   165
            self.Manager.OpenFileInCurrent(masterpath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   166
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   167
            self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   168
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   169
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   170
    def SaveMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   171
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   172
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   173
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   174
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   175
        if self.Manager.SaveCurrentInFile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   176
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   177
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   178
            return "Fail to save Master Node"
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 LoadSlaveNodes(self, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   181
        cpjpath = os.path.join(self.Root, "nodelist.cpj")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   182
        if os.path.isfile(cpjpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   183
            try:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   184
                networks = eds_utils.ParseCPJFile(cpjpath)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   185
                network = None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   186
                if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   187
                    for net in networks:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   188
                        if net["Name"] == netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   189
                            network = net
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   190
                    self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   191
                elif len(networks) > 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   192
                    network = networks[0]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   193
                    self.NetworkName = network["Name"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   194
                if network:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   195
                    for nodeid, node in network["Nodes"].items():
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   196
                        if node["Present"] == 1:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   197
                            result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   198
                            if result != None:
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   199
                                return result        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   200
                self.Changed = False
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   201
            except SyntaxError, message:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   202
                return "Unable to load CPJ file\n%s"%message
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   203
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   204
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   205
    def SaveNodeList(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   206
        try:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   207
            cpjpath = os.path.join(self.Root, "nodelist.cpj")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   208
            content = eds_utils.GenerateCPJContent(self)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   209
            if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   210
                file = open(cpjpath, "a")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   211
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   212
                file = open(cpjpath, "w")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   213
            file.write(content)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   214
            file.close()
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   215
            self.Changed = False
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   216
            return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   217
        except:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   218
            return "Fail to save node list"
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   219
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   220
    def GetSlaveNodeEntry(self, nodeid, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   221
        if nodeid in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   222
            self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   223
            return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   224
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   225
            return "Node 0x%2.2X doesn't exist"%nodeid
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   226
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   227
    def GetMasterNodeEntry(self, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   228
        return self.Manager.GetCurrentEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   229
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   230
    def SetMasterNodeEntry(self, index, subindex = None, value = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   231
        self.Manager.SetCurrentEntry(index, subindex, value)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   232
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   233
    def GetOrderNumber(self, nodeid):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   234
        nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   235
        nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   236
        return nodeindexes.index(nodeid) + 1
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   237
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   238
    def GetNodeByOrder(self, order):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   239
        if order > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   240
            nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   241
            nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   242
            print nodeindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   243
            if order <= len(nodeindexes):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   244
                return self.SlaveNodes[nodeindexes[order - 1]]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   245
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   246
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   247
    def IsCurrentEntry(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   248
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   249
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   250
                return self.Manager.IsCurrentEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   251
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   252
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   253
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   254
                    return node.IsEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   255
        return False
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   256
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   257
    def GetEntryInfos(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   258
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   259
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   260
                return self.Manager.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   261
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   262
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   263
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   264
                    return node.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   265
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   266
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   267
    def GetSubentryInfos(self, index, subindex):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   268
        if self.CurrentSelected != None:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   269
            if self.CurrentSelected == 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   270
                return self.Manager.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   271
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   272
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   273
                if node:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   274
                    return node.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   275
        return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   276
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   277
    def GetCurrentValidIndexes(self, min, max):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   278
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   279
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   280
                return self.Manager.GetCurrentValidIndexes(min, max)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   281
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   282
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   283
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   284
                    validindexes = []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   285
                    for index in node.GetIndexes():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   286
                        if min <= index <= max:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   287
                            validindexes.append((node.GetEntryName(index), index))
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   288
                    return validindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   289
                else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   290
                    print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   291
        return []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   292
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   293
    def GetCurrentEntryValues(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   294
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   295
            node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   296
            if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   297
                return self.Manager.GetNodeEntryValues(node, index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   298
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   299
                print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   300
        return [], []
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   301
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   302
if __name__ == "__main__":
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   303
    from nodemanager import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   304
    import os, sys, shutil
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   305
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   306
    manager = NodeManager(sys.path[0])
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   307
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   308
    nodelist = NodeList(manager)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   309
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   310
    result = nodelist.LoadProject("/home/laurent/test_nodelist")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   311
    if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   312
        print result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   313
    else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   314
        print "MasterNode :"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   315
        manager.CurrentNode.Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   316
        print 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   317
        for nodeid, node in nodelist.SlaveNodes.items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   318
            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   319
            node["Node"].Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   320
            print
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   321