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