plugger.py
author greg
Mon, 18 May 2009 07:58:41 +0200
changeset 345 a3520d75a722
parent 338 e19c78831d41
child 348 054fbf1ae0f8
permissions -rw-r--r--
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     1
"""
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     2
Base definitions for beremiz plugins
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     3
"""
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     4
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 176
diff changeset
     5
import os,sys,traceback
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
     6
import plugins
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     7
import types
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     8
import shutil
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     9
from xml.dom import minidom
22
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
    10
import wx
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
    11
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
    12
#Quick hack to be able to find Beremiz IEC tools. Should be config params.
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
    13
base_folder = os.path.split(sys.path[0])[0]
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
    14
sys.path.append(os.path.join(base_folder, "plcopeneditor"))
126
034979cb02b0 add use of docutils for pdf
greg
parents: 125
diff changeset
    15
sys.path.append(os.path.join(base_folder, "docutils"))
034979cb02b0 add use of docutils for pdf
greg
parents: 125
diff changeset
    16
034979cb02b0 add use of docutils for pdf
greg
parents: 125
diff changeset
    17
from docpdf import *
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    18
from xmlclass import GenerateClassesFromXSDstring
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
    19
from wxPopen import ProcessLogger
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    20
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
    21
from PLCControler import PLCControler
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
    22
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    23
_BaseParamsClass = GenerateClassesFromXSDstring("""<?xml version="1.0" encoding="ISO-8859-1" ?>
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    24
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    25
          <xsd:element name="BaseParams">
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    26
            <xsd:complexType>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    27
              <xsd:attribute name="Name" type="xsd:string" use="optional" default="__unnamed__"/>
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
    28
              <xsd:attribute name="IEC_Channel" type="xsd:integer" use="required"/>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    29
              <xsd:attribute name="Enabled" type="xsd:boolean" use="optional" default="true"/>
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    30
            </xsd:complexType>
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    31
          </xsd:element>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    32
        </xsd:schema>""")["BaseParams"]
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    33
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    34
NameTypeSeparator = '@'
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    35
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    36
class MiniTextControler:
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    37
    
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    38
    def __init__(self, filepath):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    39
        self.FilePath = filepath
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    40
        
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    41
    def SetEditedElementText(self, tagname, text):
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    42
        file = open(self.FilePath, "w")
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    43
        file.write(text)
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    44
        file.close()
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    45
        
273
d15a20eb2b8b Bug with debug mode when displaying code fixed
greg
parents: 256
diff changeset
    46
    def GetEditedElementText(self, tagname, debug = False):
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    47
        if os.path.isfile(self.FilePath):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    48
            file = open(self.FilePath, "r")
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    49
            text = file.read()
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    50
            file.close()
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    51
            return text
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    52
        return ""
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    53
    
273
d15a20eb2b8b Bug with debug mode when displaying code fixed
greg
parents: 256
diff changeset
    54
    def GetEditedElementInterfaceVars(self, tagname, debug = False):
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    55
        return []
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    56
    
273
d15a20eb2b8b Bug with debug mode when displaying code fixed
greg
parents: 256
diff changeset
    57
    def GetEditedElementType(self, tagname, debug = False):
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    58
        return "program"
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    59
    
273
d15a20eb2b8b Bug with debug mode when displaying code fixed
greg
parents: 256
diff changeset
    60
    def GetBlockTypes(self, tagname = "", debug = False):
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    61
        return []
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    62
    
273
d15a20eb2b8b Bug with debug mode when displaying code fixed
greg
parents: 256
diff changeset
    63
    def GetEnumeratedDataValues(self, debug = False):
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    64
        return []
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
    65
    
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    66
    def StartBuffering(self):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    67
        pass
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    68
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    69
    def EndBuffering(self):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    70
        pass
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    71
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    72
    def BufferProject(self):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    73
        pass
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
    74
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
    75
# helper func to get path to images
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
    76
def opjimg(imgname):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
    77
    return os.path.join("images",imgname)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
    78
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    79
class PlugTemplate:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    80
    """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    81
    This class is the one that define plugins.
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    82
    """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    83
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    84
    XSD = None
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    85
    PlugChildsTypes = []
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    86
    PlugMaxCount = None
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    87
    PluginMethods = []
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
    88
    LibraryControler = None
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    89
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    90
    def _AddParamsMembers(self):
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
    91
        self.PlugParams = None
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    92
        if self.XSD:
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    93
            Classes = GenerateClassesFromXSDstring(self.XSD)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    94
            Classes = [(name, XSDclass) for name, XSDclass in Classes.items() if XSDclass.IsBaseClass]
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    95
            if len(Classes) == 1:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    96
                name, XSDclass = Classes[0]
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    97
                obj = XSDclass()
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    98
                self.PlugParams = (name, obj)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    99
                setattr(self, name, obj)
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   100
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   101
    def __init__(self):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   102
        # Create BaseParam 
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   103
        self.BaseParams = _BaseParamsClass()
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   104
        self.MandatoryParams = ("BaseParams", self.BaseParams)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   105
        self._AddParamsMembers()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   106
        self.PluggedChilds = {}
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   107
        # copy PluginMethods so that it can be later customized
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   108
        self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
325
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   109
        self.LoadSTLibrary()
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   110
        
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   111
    def PluginBaseXmlFilePath(self, PlugName=None):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   112
        return os.path.join(self.PlugPath(PlugName), "baseplugin.xml")
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   113
    
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   114
    def PluginXmlFilePath(self, PlugName=None):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   115
        return os.path.join(self.PlugPath(PlugName), "plugin.xml")
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   116
325
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   117
    def PluginLibraryFilePath(self):
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   118
        return os.path.join(os.path.join(os.path.split(__file__)[0], "plugins", self.PlugType, "pous.xml"))
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   119
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   120
    def PlugPath(self,PlugName=None):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   121
        if not PlugName:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   122
            PlugName = self.BaseParams.getName()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   123
        return os.path.join(self.PlugParent.PlugPath(),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   124
                            PlugName + NameTypeSeparator + self.PlugType)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   125
    
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   126
    def PlugTestModified(self):
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   127
        return self.ChangesToSave
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   128
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   129
    def ProjectTestModified(self):
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   130
        """
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   131
        recursively check modified status
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   132
        """
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   133
        if self.PlugTestModified():
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   134
            return True
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   135
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   136
        for PlugChild in self.IterChilds():
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   137
            if PlugChild.ProjectTestModified():
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   138
                return True
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   139
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   140
        return False
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   141
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   142
    def OnPlugSave(self):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   143
        #Default, do nothing and return success
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   144
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   145
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   146
    def GetParamsAttributes(self, path = None):
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   147
        if path:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   148
            parts = path.split(".", 1)
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   149
            if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   150
                return self.MandatoryParams[1].getElementInfos(parts[0], parts[1])
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   151
            elif self.PlugParams and parts[0] == self.PlugParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   152
                return self.PlugParams[1].getElementInfos(parts[0], parts[1])
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   153
        else:
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   154
            params = []
82
d7b4dd1f543f Beremiz layout improved for wx2.8 by inserting all control in TreeCtrl
lbessard
parents: 81
diff changeset
   155
            if wx.VERSION < (2, 8, 0) and self.MandatoryParams:
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   156
                params.append(self.MandatoryParams[1].getElementInfos(self.MandatoryParams[0]))
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   157
            if self.PlugParams:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   158
                params.append(self.PlugParams[1].getElementInfos(self.PlugParams[0]))
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   159
            return params
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   160
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   161
    def SetParamsAttribute(self, path, value):
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   162
        self.ChangesToSave = True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   163
        # Filter IEC_Channel and Name, that have specific behavior
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   164
        if path == "BaseParams.IEC_Channel":
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   165
            return self.FindNewIEC_Channel(value), True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   166
        elif path == "BaseParams.Name":
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   167
            res = self.FindNewName(value)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   168
            self.PlugRequestSave()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   169
            return res, True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   170
        
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   171
        parts = path.split(".", 1)
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   172
        if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   173
            self.MandatoryParams[1].setElementValue(parts[1], value)
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   174
        elif self.PlugParams and parts[0] == self.PlugParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   175
            self.PlugParams[1].setElementValue(parts[1], value)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   176
        return value, False
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   177
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   178
    def PlugRequestSave(self):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   179
        # If plugin do not have corresponding directory
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   180
        plugpath = self.PlugPath()
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   181
        if not os.path.isdir(plugpath):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   182
            # Create it
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   183
            os.mkdir(plugpath)
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   184
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   185
        # generate XML for base XML parameters controller of the plugin
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   186
        if self.MandatoryParams:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   187
            BaseXMLFile = open(self.PluginBaseXmlFilePath(),'w')
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   188
            BaseXMLFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   189
            BaseXMLFile.write(self.MandatoryParams[1].generateXMLText(self.MandatoryParams[0], 0))
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   190
            BaseXMLFile.close()
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   191
        
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   192
        # generate XML for XML parameters controller of the plugin
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   193
        if self.PlugParams:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   194
            XMLFile = open(self.PluginXmlFilePath(),'w')
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   195
            XMLFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   196
            XMLFile.write(self.PlugParams[1].generateXMLText(self.PlugParams[0], 0))
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   197
            XMLFile.close()
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   198
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   199
        # Call the plugin specific OnPlugSave method
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   200
        result = self.OnPlugSave()
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   201
        if not result:
250
01963beca027 Bug with return value of nodelist saving fixed
lbessard
parents: 248
diff changeset
   202
            return "Error while saving \"%s\"\n"%self.PlugPath()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   203
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   204
        # mark plugin as saved
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   205
        self.ChangesToSave = False        
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   206
        # go through all childs and do the same
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   207
        for PlugChild in self.IterChilds():
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   208
            result = PlugChild.PlugRequestSave()
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   209
            if result:
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   210
                return result
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   211
        return None
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   212
    
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   213
    def PlugImport(self, src_PlugPath):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   214
        shutil.copytree(src_PlugPath, self.PlugPath)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   215
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   216
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   217
    def PlugGenerate_C(self, buildpath, locations):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   218
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   219
        Generate C code
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   220
        @param locations: List of complete variables locations \
22
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   221
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   222
            "NAME" : name of the variable (generally "__IW0_1_2" style)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   223
            "DIR" : direction "Q","I" or "M"
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   224
            "SIZE" : size "X", "B", "W", "D", "L"
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   225
            "LOC" : tuple of interger for IEC location (0,1,2,...)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   226
            }, ...]
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   227
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   228
        """
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   229
        self.logger.write_warning(".".join(map(lambda x:str(x), self.GetCurrentLocation())) + " -> Nothing to do\n")
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   230
        return [],"",False
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   231
    
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   232
    def _Generate_C(self, buildpath, locations):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   233
        # Generate plugins [(Cfiles, CFLAGS)], LDFLAGS, DoCalls, extra_files
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   234
        # extra_files = [(fname,fobject), ...]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   235
        gen_result = self.PlugGenerate_C(buildpath, locations)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   236
        PlugCFilesAndCFLAGS, PlugLDFLAGS, DoCalls = gen_result[:3]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   237
        extra_files = gen_result[3:]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   238
        # if some files heve been generated put them in the list with their location
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   239
        if PlugCFilesAndCFLAGS:
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   240
            LocationCFilesAndCFLAGS = [(self.GetCurrentLocation(), PlugCFilesAndCFLAGS, DoCalls)]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   241
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   242
            LocationCFilesAndCFLAGS = []
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   243
115
9ffdac9cf609 Fixed empty build exception.
etisserant
parents: 114
diff changeset
   244
        # plugin asks for some LDFLAGS
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   245
        if PlugLDFLAGS:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   246
            # LDFLAGS can be either string
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   247
            if type(PlugLDFLAGS)==type(str()):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   248
                LDFLAGS=[PlugLDFLAGS]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   249
            #or list of strings
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   250
            elif type(PlugLDFLAGS)==type(list()):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   251
                LDFLAGS=PlugLDFLAGS[:]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   252
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   253
            LDFLAGS=[]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   254
        
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   255
        # recurse through all childs, and stack their results
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   256
        for PlugChild in self.IECSortedChilds():
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   257
            new_location = PlugChild.GetCurrentLocation()
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   258
            # How deep are we in the tree ?
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   259
            depth=len(new_location)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   260
            _LocationCFilesAndCFLAGS, _LDFLAGS, _extra_files = \
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   261
                PlugChild._Generate_C(
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   262
                    #keep the same path
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   263
                    buildpath,
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   264
                    # filter locations that start with current IEC location
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   265
                    [loc for loc in locations if loc["LOC"][0:depth] == new_location ])
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   266
            # stack the result
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   267
            LocationCFilesAndCFLAGS += _LocationCFilesAndCFLAGS
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   268
            LDFLAGS += _LDFLAGS
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   269
            extra_files += _extra_files
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   270
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   271
        return LocationCFilesAndCFLAGS, LDFLAGS, extra_files
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   272
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   273
    def BlockTypesFactory(self):
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   274
        if self.LibraryControler is not None:
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   275
            return [{"name" : "%s POUs" % self.PlugType, "list": self.LibraryControler.Project.GetCustomBlockTypes()}]
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   276
        return []
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   277
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   278
    def STLibraryFactory(self):
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   279
        if self.LibraryControler is not None:
309
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   280
            program, errors, warnings = self.LibraryControler.GenerateProgram()
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   281
            return program
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   282
        return ""
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   283
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   284
    def IterChilds(self):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   285
        for PlugType, PluggedChilds in self.PluggedChilds.items():
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   286
            for PlugInstance in PluggedChilds:
250
01963beca027 Bug with return value of nodelist saving fixed
lbessard
parents: 248
diff changeset
   287
                yield PlugInstance
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   288
    
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   289
    def IECSortedChilds(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   290
        # reorder childs by IEC_channels
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   291
        ordered = [(chld.BaseParams.getIEC_Channel(),chld) for chld in self.IterChilds()]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   292
        if ordered:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   293
            ordered.sort()
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   294
            return zip(*ordered)[1]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   295
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   296
            return []
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   297
    
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   298
    def _GetChildBySomething(self, something, toks):
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   299
        for PlugInstance in self.IterChilds():
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   300
            # if match component of the name
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   301
            if getattr(PlugInstance.BaseParams, something) == toks[0]:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   302
                # if Name have other components
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   303
                if len(toks) >= 2:
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   304
                    # Recurse in order to find the latest object
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   305
                    return PlugInstance._GetChildBySomething( something, toks[1:])
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   306
                # No sub name -> found
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   307
                return PlugInstance
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   308
        # Not found
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   309
        return None
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   310
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   311
    def GetChildByName(self, Name):
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   312
        if Name:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   313
            toks = Name.split('.')
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   314
            return self._GetChildBySomething("Name", toks)
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   315
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   316
            return self
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   317
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   318
    def GetChildByIECLocation(self, Location):
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   319
        if Location:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   320
            return self._GetChildBySomething("IEC_Channel", Location)
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   321
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   322
            return self
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   323
    
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   324
    def GetCurrentLocation(self):
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   325
        """
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   326
        @return:  Tupple containing plugin IEC location of current plugin : %I0.0.4.5 => (0,0,4,5)
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   327
        """
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   328
        return self.PlugParent.GetCurrentLocation() + (self.BaseParams.getIEC_Channel(),)
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   329
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   330
    def GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   331
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   332
        @return:  String "ParentParentName.ParentName.Name"
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   333
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   334
        return  self.PlugParent._GetCurrentName() + self.BaseParams.getName()
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   335
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   336
    def _GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   337
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   338
        @return:  String "ParentParentName.ParentName.Name."
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   339
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   340
        return  self.PlugParent._GetCurrentName() + self.BaseParams.getName() + "."
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   341
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   342
    def GetPlugRoot(self):
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   343
        return self.PlugParent.GetPlugRoot()
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   344
97
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   345
    def GetFullIEC_Channel(self):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   346
        return ".".join([str(i) for i in self.GetCurrentLocation()]) + ".x"
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   347
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   348
    def GetLocations(self):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   349
        location = self.GetCurrentLocation()
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   350
        return [loc for loc in self.PlugParent.GetLocations() if loc["LOC"][0:len(location)] == location]
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   351
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   352
    def GetPlugInfos(self):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   353
        childs = []
33
59b84ab7bf8b Enhanced bahavior of plugin tree representation when changing IEC channel
etisserant
parents: 29
diff changeset
   354
        # reorder childs by IEC_channels
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   355
        for child in self.IECSortedChilds():
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   356
            childs.append(child.GetPlugInfos())
82
d7b4dd1f543f Beremiz layout improved for wx2.8 by inserting all control in TreeCtrl
lbessard
parents: 81
diff changeset
   357
        if wx.VERSION < (2, 8, 0):
d7b4dd1f543f Beremiz layout improved for wx2.8 by inserting all control in TreeCtrl
lbessard
parents: 81
diff changeset
   358
            return {"name" : "%d-%s"%(self.BaseParams.getIEC_Channel(),self.BaseParams.getName()), "type" : self.BaseParams.getName(), "values" : childs}
d7b4dd1f543f Beremiz layout improved for wx2.8 by inserting all control in TreeCtrl
lbessard
parents: 81
diff changeset
   359
        else:
d7b4dd1f543f Beremiz layout improved for wx2.8 by inserting all control in TreeCtrl
lbessard
parents: 81
diff changeset
   360
            return {"name" : self.BaseParams.getName(), "channel" : self.BaseParams.getIEC_Channel(), "enabled" : self.BaseParams.getEnabled(), "parent" : len(self.PlugChildsTypes) > 0, "type" : self.BaseParams.getName(), "values" : childs}
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   361
    
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   362
    def FindNewName(self, DesiredName):
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   363
        """
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   364
        Changes Name to DesiredName if available, Name-N if not.
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   365
        @param DesiredName: The desired Name (string)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   366
        """
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   367
        # Get Current Name
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   368
        CurrentName = self.BaseParams.getName()
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   369
        # Do nothing if no change
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   370
        #if CurrentName == DesiredName: return CurrentName
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   371
        # Build a list of used Name out of parent's PluggedChilds
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   372
        AllNames=[]
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   373
        for PlugInstance in self.PlugParent.IterChilds():
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   374
            if PlugInstance != self:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   375
                AllNames.append(PlugInstance.BaseParams.getName())
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   376
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   377
        # Find a free name, eventually appending digit
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   378
        res = DesiredName
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   379
        suffix = 1
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   380
        while res in AllNames:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   381
            res = "%s-%d"%(DesiredName, suffix)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   382
            suffix += 1
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   383
        
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   384
        # Get old path
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   385
        oldname = self.PlugPath()
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   386
        # Check previous plugin existance
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   387
        dontexist = self.BaseParams.getName() == "__unnamed__"
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   388
        # Set the new name
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   389
        self.BaseParams.setName(res)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   390
        # Rename plugin dir if exist
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   391
        if not dontexist:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   392
            shutil.move(oldname, self.PlugPath())
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   393
        # warn user he has two left hands
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   394
        if DesiredName != res:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   395
            self.logger.write_warning("A child names \"%s\" already exist -> \"%s\"\n"%(DesiredName,res))
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   396
        return res
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   397
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   398
    def FindNewIEC_Channel(self, DesiredChannel):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   399
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   400
        Changes IEC Channel number to DesiredChannel if available, nearest available if not.
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   401
        @param DesiredChannel: The desired IEC channel (int)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   402
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   403
        # Get Current IEC channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   404
        CurrentChannel = self.BaseParams.getIEC_Channel()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   405
        # Do nothing if no change
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   406
        #if CurrentChannel == DesiredChannel: return CurrentChannel
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   407
        # Build a list of used Channels out of parent's PluggedChilds
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   408
        AllChannels=[]
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   409
        for PlugInstance in self.PlugParent.IterChilds():
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   410
            if PlugInstance != self:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   411
                AllChannels.append(PlugInstance.BaseParams.getIEC_Channel())
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   412
        AllChannels.sort()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   413
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   414
        # Now, try to guess the nearest available channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   415
        res = DesiredChannel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   416
        while res in AllChannels: # While channel not free
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   417
            if res < CurrentChannel: # Want to go down ?
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   418
                res -=  1 # Test for n-1
33
59b84ab7bf8b Enhanced bahavior of plugin tree representation when changing IEC channel
etisserant
parents: 29
diff changeset
   419
                if res < 0 :
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   420
                    self.logger.write_warning("Cannot find lower free IEC channel than %d\n"%CurrentChannel)
33
59b84ab7bf8b Enhanced bahavior of plugin tree representation when changing IEC channel
etisserant
parents: 29
diff changeset
   421
                    return CurrentChannel # Can't go bellow 0, do nothing
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   422
            else : # Want to go up ?
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   423
                res +=  1 # Test for n-1
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   424
        # Finally set IEC Channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   425
        self.BaseParams.setIEC_Channel(res)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   426
        if DesiredChannel != res:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   427
            self.logger.write_warning("A child with IEC channel %d already exist -> %d\n"%(DesiredChannel,res))
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   428
        return res
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   429
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   430
    def OnPlugClose(self):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   431
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   432
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   433
    def _doRemoveChild(self, PlugInstance):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   434
        # Remove all childs of child
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   435
        for SubPlugInstance in PlugInstance.IterChilds():
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   436
            PlugInstance._doRemoveChild(SubPlugInstance)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   437
        # Call the OnCloseMethod
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   438
        PlugInstance.OnPlugClose()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   439
        # Delete plugin dir
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   440
        shutil.rmtree(PlugInstance.PlugPath())
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   441
        # Remove child of PluggedChilds
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   442
        self.PluggedChilds[PlugInstance.PlugType].remove(PlugInstance)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   443
        # Forget it... (View have to refresh)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   444
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   445
    def PlugRemove(self):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   446
        # Fetch the plugin
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   447
        #PlugInstance = self.GetChildByName(PlugName)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   448
        # Ask to his parent to remove it
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   449
        self.PlugParent._doRemoveChild(self)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   450
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   451
    def PlugAddChild(self, PlugName, PlugType):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   452
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   453
        Create the plugins that may be added as child to this node self
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   454
        @param PlugType: string desining the plugin class name (get name from PlugChildsTypes)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   455
        @param PlugName: string for the name of the plugin instance
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   456
        """
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   457
        # reorgabize self.PlugChildsTypes tuples from (name, PlugClass, Help)
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   458
        # to ( name, (PlugClass, Help)), an make a dict
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   459
        transpose = zip(*self.PlugChildsTypes)
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   460
        PlugChildsTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2])))
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   461
        # Check that adding this plugin is allowed
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   462
        try:
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   463
            PlugClass, PlugHelp = PlugChildsTypes[PlugType]
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   464
        except KeyError:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   465
            raise Exception, "Cannot create child %s of type %s "%(PlugName, PlugType)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   466
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   467
        # if PlugClass is a class factory, call it. (prevent unneeded imports)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   468
        if type(PlugClass) == types.FunctionType:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   469
            PlugClass = PlugClass()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   470
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   471
        # Eventualy Initialize child instance list for this class of plugin
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   472
        PluggedChildsWithSameClass = self.PluggedChilds.setdefault(PlugType, list())
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   473
        # Check count
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   474
        if getattr(PlugClass, "PlugMaxCount", None) and len(PluggedChildsWithSameClass) >= PlugClass.PlugMaxCount:
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   475
            raise Exception, "Max count (%d) reached for this plugin of type %s "%(PlugClass.PlugMaxCount, PlugType)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   476
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   477
        # create the final class, derived of provided plugin and template
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   478
        class FinalPlugClass(PlugClass, PlugTemplate):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   479
            """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   480
            Plugin class is derivated into FinalPlugClass before being instanciated
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   481
            This way __init__ is overloaded to ensure PlugTemplate.__init__ is called 
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   482
            before PlugClass.__init__, and to do the file related stuff.
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   483
            """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   484
            def __init__(_self):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   485
                # self is the parent
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   486
                _self.PlugParent = self
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   487
                # self is the parent
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   488
                _self.logger = self.logger
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   489
                # Keep track of the plugin type name
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   490
                _self.PlugType = PlugType
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   491
                # remind the help string, for more fancy display
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   492
                _self.PlugHelp = PlugHelp
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   493
                # Call the base plugin template init - change XSD into class members
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   494
                PlugTemplate.__init__(_self)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   495
                # check name is unique
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   496
                NewPlugName = _self.FindNewName(PlugName)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   497
                # If dir have already be made, and file exist
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   498
                if os.path.isdir(_self.PlugPath(NewPlugName)): #and os.path.isfile(_self.PluginXmlFilePath(PlugName)):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   499
                    #Load the plugin.xml file into parameters members
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   500
                    _self.LoadXMLParams(NewPlugName)
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   501
                    # Basic check. Better to fail immediately.
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   502
                    if (_self.BaseParams.getName() != NewPlugName):
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   503
                        raise Exception, "Project tree layout do not match plugin.xml %s!=%s "%(NewPlugName, _self.BaseParams.getName())
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   504
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   505
                    # Now, self.PlugPath() should be OK
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   506
                    
15
7a473efc4530 More precise design for plugins.... to be continued...
etisserant
parents: 14
diff changeset
   507
                    # Check that IEC_Channel is not already in use.
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   508
                    _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel())
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   509
                    # Call the plugin real __init__
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   510
                    if getattr(PlugClass, "__init__", None):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   511
                        PlugClass.__init__(_self)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   512
                    #Load and init all the childs
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   513
                    _self.LoadChilds()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   514
                    #just loaded, nothing to saved
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   515
                    _self.ChangesToSave = False
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   516
                else:
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   517
                    # If plugin do not have corresponding file/dirs - they will be created on Save
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   518
                    os.mkdir(_self.PlugPath())
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   519
                    # Find an IEC number
253
b1a9d37bb84c fixed : findiecchannel call
greg
parents: 250
diff changeset
   520
                    _self.FindNewIEC_Channel(0)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   521
                    # Call the plugin real __init__
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   522
                    if getattr(PlugClass, "__init__", None):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   523
                        PlugClass.__init__(_self)
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   524
                    _self.PlugRequestSave()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   525
                    #just created, must be saved
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   526
                    _self.ChangesToSave = True
77
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   527
            
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   528
            def _getBuildPath(_self):
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   529
                return self._getBuildPath()
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   530
            
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   531
        # Create the object out of the resulting class
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   532
        newPluginOpj = FinalPlugClass()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   533
        # Store it in PluggedChils
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   534
        PluggedChildsWithSameClass.append(newPluginOpj)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   535
        
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   536
        return newPluginOpj
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   537
            
325
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   538
    def LoadSTLibrary(self):
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   539
        # Get library blocks if plcopen library exist
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   540
        library_path = self.PluginLibraryFilePath()
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   541
        if os.path.isfile(library_path):
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   542
            self.LibraryControler = PLCControler()
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   543
            self.LibraryControler.OpenXMLFile(library_path)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   544
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   545
    def LoadXMLParams(self, PlugName = None):
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
   546
        methode_name = os.path.join(self.PlugPath(PlugName), "methods.py")
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
   547
        if os.path.isfile(methode_name):
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
   548
            execfile(methode_name)
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   549
        
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   550
        # Get the base xml tree
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   551
        if self.MandatoryParams:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   552
            try:
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   553
                basexmlfile = open(self.PluginBaseXmlFilePath(PlugName), 'r')
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   554
                basetree = minidom.parse(basexmlfile)
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   555
                self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0])
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   556
                basexmlfile.close()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   557
            except Exception, exc:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   558
                self.logger.write_error("Couldn't load plugin base parameters %s :\n %s" % (PlugName, str(exc)))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   559
                self.logger.write_error(traceback.format_exc())
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   560
        
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   561
        # Get the xml tree
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   562
        if self.PlugParams:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   563
            try:
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   564
                xmlfile = open(self.PluginXmlFilePath(PlugName), 'r')
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   565
                tree = minidom.parse(xmlfile)
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   566
                self.PlugParams[1].loadXMLTree(tree.childNodes[0])
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   567
                xmlfile.close()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   568
            except Exception, exc:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   569
                self.logger.write_error("Couldn't load plugin parameters %s :\n %s" % (PlugName, str(exc)))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   570
                self.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   571
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   572
    def LoadChilds(self):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   573
        # Iterate over all PlugName@PlugType in plugin directory, and try to open them
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   574
        for PlugDir in os.listdir(self.PlugPath()):
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   575
            if os.path.isdir(os.path.join(self.PlugPath(), PlugDir)) and \
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   576
               PlugDir.count(NameTypeSeparator) == 1:
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   577
                pname, ptype = PlugDir.split(NameTypeSeparator)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   578
                try:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   579
                    self.PlugAddChild(pname, ptype)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   580
                except Exception, exc:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   581
                    self.logger.write_error("Could not add child \"%s\", type %s :\n%s\n"%(pname, ptype, str(exc)))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   582
                    self.logger.write_error(traceback.format_exc())
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   583
109
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   584
    def EnableMethod(self, method, value):
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   585
        for d in self.PluginMethods:
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   586
            if d["method"]==method:
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   587
                d["enabled"]=value
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   588
                return True
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   589
        return False
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   590
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   591
    def ShowMethod(self, method, value):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   592
        for d in self.PluginMethods:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   593
            if d["method"]==method:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   594
                d["shown"]=value
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   595
                return True
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   596
        return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   597
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   598
def _GetClassFunction(name):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   599
    def GetRootClass():
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   600
        return getattr(__import__("plugins." + name), name).RootClass
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   601
    return GetRootClass
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   602
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   603
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   604
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   605
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   606
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   607
###################################   ROOT    ######################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   608
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   609
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   610
####################################################################################
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   611
81
3a34b580f141 Windows related enhancements
etisserant
parents: 77
diff changeset
   612
if wx.Platform == '__WXMSW__':
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 74
diff changeset
   613
    exe_ext=".exe"
9ad18a387a96 Windows related enhancements
etisserant
parents: 74
diff changeset
   614
else:
9ad18a387a96 Windows related enhancements
etisserant
parents: 74
diff changeset
   615
    exe_ext=""
9ad18a387a96 Windows related enhancements
etisserant
parents: 74
diff changeset
   616
9ad18a387a96 Windows related enhancements
etisserant
parents: 74
diff changeset
   617
iec2c_path = os.path.join(base_folder, "matiec", "iec2c"+exe_ext)
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   618
ieclib_path = os.path.join(base_folder, "matiec", "lib")
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   619
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   620
# import for project creation timestamping
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   621
from threading import Timer, Lock, Thread, Semaphore
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   622
from time import localtime
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   623
from datetime import datetime
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   624
# import necessary stuff from PLCOpenEditor
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   625
from PLCOpenEditor import PLCOpenEditor, ProjectDialog
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   626
from TextViewer import TextViewer
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   627
from plcopen.structures import IEC_KEYWORDS, TypeHierarchy_list
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   628
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   629
# Construct debugger natively supported types
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   630
DebugTypes = [t for t in zip(*TypeHierarchy_list)[0] if not t.startswith("ANY")] + \
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   631
    ["STEP","TRANSITION","ACTION"]
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   632
DebugTypesSize =  {"BOOL" :       1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   633
                   "STEP" :       1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   634
                   "TRANSITION" : 1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   635
                   "ACTION" :     1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   636
                   "SINT" :       1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   637
                   "USINT" :      1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   638
                   "BYTE" :       1,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   639
                   "STRING" :     128,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   640
                   "INT" :        2,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   641
                   "UINT" :       2,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   642
                   "WORD" :       2,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   643
                   "WSTRING" :    0, #TODO
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   644
                   "DINT" :       4,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   645
                   "UDINT" :      4,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   646
                   "DWORD" :      4,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   647
                   "LINT" :       4,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   648
                   "ULINT" :      8,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   649
                   "LWORD" :      8,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   650
                   "REAL" :       4,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   651
                   "LREAL" :      8,
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
   652
                  } 
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   653
22
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   654
import re
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   655
import targets
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   656
import connectors
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   657
from discovery import DiscoveryDialog
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
   658
from weakref import WeakKeyDictionary
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   659
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   660
class PluginsRoot(PlugTemplate, PLCControler):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   661
    """
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   662
    This class define Root object of the plugin tree. 
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   663
    It is responsible of :
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   664
    - Managing project directory
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   665
    - Building project
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   666
    - Handling PLCOpenEditor controler and view
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   667
    - Loading user plugins and instanciante them as childs
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   668
    - ...
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   669
    
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   670
    """
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   671
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   672
    # For root object, available Childs Types are modules of the plugin packages.
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   673
    PlugChildsTypes = [(name, _GetClassFunction(name), help) for name, help in zip(plugins.__all__,plugins.helps)]
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   674
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   675
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   676
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   677
      <xsd:element name="BeremizRoot">
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   678
        <xsd:complexType>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
   679
          <xsd:sequence>
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
   680
            <xsd:element name="TargetType">
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
   681
              <xsd:complexType>
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
   682
                <xsd:choice>
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   683
                """+targets.targetchoices+"""
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   684
                </xsd:choice>
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   685
              </xsd:complexType>
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   686
            </xsd:element>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
   687
          </xsd:sequence>
204
f572ab819769 remove URI_location from XSD targets and add to pluginroot XSD
greg
parents: 203
diff changeset
   688
          <xsd:attribute name="URI_location" type="xsd:string" use="optional" default=""/>
338
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
   689
          <xsd:attribute name="Enable_Plugins" type="xsd:boolean" use="optional" default="true"/>
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   690
        </xsd:complexType>
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   691
      </xsd:element>
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   692
    </xsd:schema>
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   693
    """
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   694
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 288
diff changeset
   695
    def __init__(self, frame, logger):
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   696
        PLCControler.__init__(self)
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 222
diff changeset
   697
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   698
        self.MandatoryParams = None
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   699
        self.AppFrame = frame
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   700
        self.logger = logger
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   701
        self._builder = None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   702
        self._connector = None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   703
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   704
        # Setup debug information
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 222
diff changeset
   705
        self.IECdebug_datas = {}
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 222
diff changeset
   706
        self.IECdebug_lock = Lock()
222
d0f7d36bf241 Added lock to avoid variable subsciption concurrent to transmission to PLC
etisserant
parents: 217
diff changeset
   707
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
   708
        self.DebugTimer=None
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   709
        self.ResetIECProgramsAndVariables()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   710
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   711
        #This method are not called here... but in NewProject and OpenProject
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   712
        #self._AddParamsMembers()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   713
        #self.PluggedChilds = {}
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   714
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   715
        # In both new or load scenario, no need to save
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   716
        self.ChangesToSave = False        
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   717
        # root have no parent
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   718
        self.PlugParent = None
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   719
        # Keep track of the plugin type name
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   720
        self.PlugType = "Beremiz"
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   721
        # After __init__ root plugin is not valid
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   722
        self.ProjectPath = None
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   723
        self.BuildPath = None
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   724
        self.PLCEditor = None
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
   725
        self.PLCDebug = None
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
   726
        self.DebugThread = None
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
   727
        self.debug_break = False
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   728
        # copy PluginMethods so that it can be later customized
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   729
        self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
325
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   730
        self.LoadSTLibrary()
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   731
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   732
    def PluginLibraryFilePath(self):
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
   733
        return os.path.join(os.path.split(__file__)[0], "pous.xml")
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   734
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   735
    def PlugTestModified(self):
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   736
         return self.ChangesToSave or not self.ProjectIsSaved()
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   737
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   738
    def GetPlugRoot(self):
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   739
        return self
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   740
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   741
    def GetCurrentLocation(self):
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   742
        return ()
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   743
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   744
    def GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   745
        return ""
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   746
    
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   747
    def _GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   748
        return ""
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   749
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   750
    def GetProjectPath(self):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   751
        return self.ProjectPath
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   752
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   753
    def GetProjectName(self):
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   754
        return os.path.split(self.ProjectPath)[1]
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   755
    
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   756
    def GetPlugInfos(self):
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   757
        childs = []
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   758
        for child in self.IterChilds():
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   759
            childs.append(child.GetPlugInfos())
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   760
        return {"name" : "PLC (%s)"%self.GetProjectName(), "type" : None, "values" : childs}
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   761
    
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   762
    def NewProject(self, ProjectPath, BuildPath=None):
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   763
        """
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   764
        Create a new project in an empty folder
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   765
        @param ProjectPath: path of the folder where project have to be created
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   766
        @param PLCParams: properties of the PLCOpen program created
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   767
        """
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   768
        # Verify that choosen folder is empty
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   769
        if not os.path.isdir(ProjectPath) or len(os.listdir(ProjectPath)) > 0:
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   770
            return "Folder choosen isn't empty. You can't use it for a new project!"
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   771
        
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   772
        dialog = ProjectDialog(self.AppFrame)
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   773
        if dialog.ShowModal() == wx.ID_OK:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   774
            values = dialog.GetValues()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   775
            values["creationDateTime"] = datetime(*localtime()[:6])
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   776
            dialog.Destroy()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   777
        else:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   778
            dialog.Destroy()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   779
            return "Project not created"
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   780
        
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   781
        # Create PLCOpen program
113
78a92a18f8d6 Fixed new project creation bug
etisserant
parents: 110
diff changeset
   782
        self.CreateNewProject(values)
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   783
        # Change XSD into class members
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   784
        self._AddParamsMembers()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   785
        self.PluggedChilds = {}
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   786
        # Keep track of the root plugin (i.e. project path)
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   787
        self.ProjectPath = ProjectPath
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   788
        self.BuildPath = BuildPath
114
2e3d8d4480e7 Now .xml files are automatically created when creating a new project no need to save explicitely.
etisserant
parents: 113
diff changeset
   789
        # get plugins bloclist (is that usefull at project creation?)
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   790
        self.RefreshPluginsBlockLists()
114
2e3d8d4480e7 Now .xml files are automatically created when creating a new project no need to save explicitely.
etisserant
parents: 113
diff changeset
   791
        # this will create files base XML files
2e3d8d4480e7 Now .xml files are automatically created when creating a new project no need to save explicitely.
etisserant
parents: 113
diff changeset
   792
        self.SaveProject()
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   793
        return None
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   794
        
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   795
    def LoadProject(self, ProjectPath, BuildPath=None):
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   796
        """
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   797
        Load a project contained in a folder
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   798
        @param ProjectPath: path of the project folder
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   799
        """
190
be969d4694c7 Bug with folder path ending by '/' or '\' fixed
lbessard
parents: 178
diff changeset
   800
        if os.path.basename(ProjectPath) == "":
be969d4694c7 Bug with folder path ending by '/' or '\' fixed
lbessard
parents: 178
diff changeset
   801
            ProjectPath = os.path.dirname(ProjectPath)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   802
		# Verify that project contains a PLCOpen program
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   803
        plc_file = os.path.join(ProjectPath, "plc.xml")
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   804
        if not os.path.isfile(plc_file):
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   805
            return "Folder choosen doesn't contain a program. It's not a valid project!"
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   806
        # Load PLCOpen file
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   807
        result = self.OpenXMLFile(plc_file)
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   808
        if result:
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   809
            return result
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   810
        # Change XSD into class members
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   811
        self._AddParamsMembers()
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   812
        self.PluggedChilds = {}
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   813
        # Keep track of the root plugin (i.e. project path)
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   814
        self.ProjectPath = ProjectPath
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   815
        self.BuildPath = BuildPath
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   816
        # If dir have already be made, and file exist
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   817
        if os.path.isdir(self.PlugPath()) and os.path.isfile(self.PluginXmlFilePath()):
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   818
            #Load the plugin.xml file into parameters members
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   819
            result = self.LoadXMLParams()
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   820
            if result:
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   821
                return result
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   822
            #Load and init all the childs
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   823
            self.LoadChilds()
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   824
        self.RefreshPluginsBlockLists()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   825
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   826
        if os.path.exists(self._getBuildPath()):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   827
            self.EnableMethod("_Clean", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   828
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   829
        if os.path.isfile(self._getIECrawcodepath()):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   830
            self.ShowMethod("_showIECcode", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   831
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   832
        return None
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   833
    
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   834
    def SaveProject(self):
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   835
        if not self.SaveXMLFile():
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   836
            self.SaveXMLFile(os.path.join(self.ProjectPath, 'plc.xml'))
25
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
   837
        if self.PLCEditor:
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
   838
            self.PLCEditor.RefreshTitle()
250
01963beca027 Bug with return value of nodelist saving fixed
lbessard
parents: 248
diff changeset
   839
        result = self.PlugRequestSave()
01963beca027 Bug with return value of nodelist saving fixed
lbessard
parents: 248
diff changeset
   840
        if result:
01963beca027 Bug with return value of nodelist saving fixed
lbessard
parents: 248
diff changeset
   841
            self.logger.write_error(result)
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   842
    
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   843
    # Update PLCOpenEditor Plugin Block types from loaded plugins
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   844
    def RefreshPluginsBlockLists(self):
62
ddf0cdd71558 Adding support for refresh block list where beremiz loose focus
lbessard
parents: 57
diff changeset
   845
        if getattr(self, "PluggedChilds", None) is not None:
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
   846
            self.ClearPluginTypes()
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
   847
            self.AddPluginBlockList(self.BlockTypesFactory())
62
ddf0cdd71558 Adding support for refresh block list where beremiz loose focus
lbessard
parents: 57
diff changeset
   848
            for child in self.IterChilds():
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
   849
                self.AddPluginBlockList(child.BlockTypesFactory())
62
ddf0cdd71558 Adding support for refresh block list where beremiz loose focus
lbessard
parents: 57
diff changeset
   850
        if self.PLCEditor is not None:
ddf0cdd71558 Adding support for refresh block list where beremiz loose focus
lbessard
parents: 57
diff changeset
   851
            self.PLCEditor.RefreshEditor()
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   852
    
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   853
    def PlugPath(self, PlugName=None):
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   854
        return self.ProjectPath
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   855
    
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   856
    def PluginXmlFilePath(self, PlugName=None):
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   857
        return os.path.join(self.PlugPath(PlugName), "beremiz.xml")
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   858
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   859
    def _getBuildPath(self):
256
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   860
        if self.BuildPath is None:
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   861
            return os.path.join(self.ProjectPath, "build")
1da137b99948 add buildpath parameter for beremiz
greg
parents: 253
diff changeset
   862
        return self.BuildPath
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   863
    
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   864
    def _getExtraFilesPath(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   865
        return os.path.join(self._getBuildPath(), "extra_files")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   866
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   867
    def _getIECcodepath(self):
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   868
        # define name for IEC code file
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   869
        return os.path.join(self._getBuildPath(), "plc.st")
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   870
    
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   871
    def _getIECgeneratedcodepath(self):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   872
        # define name for IEC generated code file
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   873
        return os.path.join(self._getBuildPath(), "generated_plc.st")
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   874
    
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   875
    def _getIECrawcodepath(self):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   876
        # define name for IEC raw code file
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   877
        return os.path.join(self.PlugPath(), "raw_plc.st")
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   878
    
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   879
    def _getPYTHONcodepath(self):
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   880
        # define name for IEC raw code file
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   881
        return os.path.join(self.PlugPath(), "runtime.py")
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   882
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
   883
    def _getWXGLADEpath(self):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
   884
        # define name for IEC raw code file
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
   885
        return os.path.join(self.PlugPath(), "hmi.wxg")
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
   886
97
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   887
    def GetLocations(self):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   888
        locations = []
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   889
        filepath = os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h")
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   890
        if os.path.isfile(filepath):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   891
            # IEC2C compiler generate a list of located variables : LOCATED_VARIABLES.h
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   892
            location_file = open(os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h"))
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   893
            # each line of LOCATED_VARIABLES.h declares a located variable
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   894
            lines = [line.strip() for line in location_file.readlines()]
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   895
            # This regular expression parses the lines genereated by IEC2C
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   896
            LOCATED_MODEL = re.compile("__LOCATED_VAR\((?P<IEC_TYPE>[A-Z]*),(?P<NAME>[_A-Za-z0-9]*),(?P<DIR>[QMI])(?:,(?P<SIZE>[XBWD]))?,(?P<LOC>[,0-9]*)\)")
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   897
            for line in lines:
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   898
                # If line match RE, 
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   899
                result = LOCATED_MODEL.match(line)
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   900
                if result:
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   901
                    # Get the resulting dict
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   902
                    resdict = result.groupdict()
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   903
                    # rewrite string for variadic location as a tuple of integers
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   904
                    resdict['LOC'] = tuple(map(int,resdict['LOC'].split(',')))
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   905
                    # set located size to 'X' if not given 
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   906
                    if not resdict['SIZE']:
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   907
                        resdict['SIZE'] = 'X'
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   908
                    # finally store into located variable list
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   909
                    locations.append(resdict)
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   910
        return locations
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   911
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   912
    def _Generate_SoftPLC(self):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   913
        """
64
531e6a834d7e Change generate_cc to generate_c
lbessard
parents: 62
diff changeset
   914
        Generate SoftPLC ST/IL/SFC code out of PLCOpenEditor controller, and compile it with IEC2C
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   915
        @param buildpath: path where files should be created
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   916
        """
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   917
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   918
        # Update PLCOpenEditor Plugin Block types before generate ST code
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   919
        self.RefreshPluginsBlockLists()
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
   920
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   921
        self.logger.write("Generating SoftPLC IEC-61131 ST/IL/SFC code...\n")
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   922
        buildpath = self._getBuildPath()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   923
        # ask PLCOpenEditor controller to write ST/IL/SFC code file
309
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   924
        program, errors, warnings = self.GenerateProgram(self._getIECgeneratedcodepath())
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   925
        if len(warnings) > 0:
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   926
            self.logger.write_warning("Warnings in ST/IL/SFC code generator :\n")
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   927
            for warning in warnings:
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   928
                self.logger.write_warning("%s\n"%warning)
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   929
        if len(errors) > 0:
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   930
            # Failed !
309
6eb074f0dae9 Adding support for cancelling code generation of function with no input connected
lbessard
parents: 304
diff changeset
   931
            self.logger.write_error("Error in ST/IL/SFC code generator :\n%s\n"%errors[0])
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   932
            return False
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   933
        plc_file = open(self._getIECcodepath(), "w")
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   934
        if getattr(self, "PluggedChilds", None) is not None:
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   935
            # Add ST Library from plugins
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   936
            plc_file.write(self.STLibraryFactory())
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   937
            plc_file.write("\n")
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   938
            for child in self.IterChilds():
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   939
                plc_file.write(child.STLibraryFactory())
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   940
                plc_file.write("\n")
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   941
        if os.path.isfile(self._getIECrawcodepath()):
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   942
            plc_file.write(open(self._getIECrawcodepath(), "r").read())
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   943
            plc_file.write("\n")
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   944
        plc_file.write(open(self._getIECgeneratedcodepath(), "r").read())
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
   945
        plc_file.close()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   946
        self.logger.write("Compiling IEC Program in to C code...\n")
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   947
        # Now compile IEC code into many C files
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   948
        # files are listed to stdout, and errors to stderr. 
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
   949
        status, result, err_result = ProcessLogger(
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   950
               self.logger,
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
   951
               "\"%s\" -f \"%s\" -I \"%s\" \"%s\""%(
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
   952
                         iec2c_path,
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
   953
                         self._getIECcodepath(),
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
   954
                         ieclib_path, buildpath),
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
   955
               no_stdout=True).spin()
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   956
        if status:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   957
            # Failed !
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   958
            self.logger.write_error("Error : IEC to C compiler returned %d\n"%status)
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   959
            return False
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   960
        # Now extract C files of stdout
113
78a92a18f8d6 Fixed new project creation bug
etisserant
parents: 110
diff changeset
   961
        C_files = [ fname for fname in result.splitlines() if fname[-2:]==".c" or fname[-2:]==".C" ]
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   962
        # remove those that are not to be compiled because included by others
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   963
        C_files.remove("POUS.c")
115
9ffdac9cf609 Fixed empty build exception.
etisserant
parents: 114
diff changeset
   964
        if not C_files:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   965
            self.logger.write_error("Error : At least one configuration and one ressource must be declared in PLC !\n")
115
9ffdac9cf609 Fixed empty build exception.
etisserant
parents: 114
diff changeset
   966
            return False
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   967
        # transform those base names to full names with path
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   968
        C_files = map(lambda filename:os.path.join(buildpath, filename), C_files)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   969
        self.logger.write("Extracting Located Variables...\n")
97
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   970
        # Keep track of generated located variables for later use by self._Generate_C
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   971
        self.PLCGeneratedLocatedVars = self.GetLocations()
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   972
        # Keep track of generated C files for later use by self.PlugGenerate_C
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   973
        self.PLCGeneratedCFiles = C_files
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents: 47
diff changeset
   974
        # compute CFLAGS for plc
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   975
        self.plcCFLAGS = "\"-I"+ieclib_path+"\""
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   976
        return True
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   977
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   978
    def GetBuilder(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   979
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   980
        Return a Builder (compile C code into machine code)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   981
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   982
        # Get target, module and class name
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   983
        targetname = self.BeremizRoot.getTargetType().getcontent()["name"]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   984
        modulename = "targets." + targetname
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   985
        classname = targetname + "_target"
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   986
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   987
        # Get module reference
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   988
        try :
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   989
            targetmodule = getattr(__import__(modulename), targetname)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   990
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   991
        except Exception, msg:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   992
            self.logger.write_error("Can't find module for target %s!\n"%targetname)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   993
            self.logger.write_error(str(msg))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   994
            return None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   995
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   996
        # Get target class
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   997
        targetclass = getattr(targetmodule, classname)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   998
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   999
        # if target already 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1000
        if self._builder is None or not isinstance(self._builder,targetclass):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1001
            # Get classname instance
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1002
            self._builder = targetclass(self)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1003
        return self._builder
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1004
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1005
    def GetLastBuildMD5(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1006
        builder=self.GetBuilder()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1007
        if builder is not None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1008
            return builder.GetBinaryCodeMD5()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1009
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1010
            return None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1011
310
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1012
    def launch_wxglade(self, options, wait=False):
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1013
        from wxglade import __file__ as fileName
310
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1014
        path = os.path.dirname(fileName)
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1015
        glade = os.path.join(path, 'wxglade.py')
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1016
        if wx.Platform == '__WXMSW__':
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1017
            glade = "\"%s\""%glade
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1018
        mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait]
310
11a82fbbac46 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 309
diff changeset
  1019
        os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options)
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1020
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1021
    #######################################################################
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1022
    #
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1023
    #                C CODE GENERATION METHODS
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1024
    #
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1025
    #######################################################################
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1026
    
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1027
    def PlugGenerate_C(self, buildpath, locations):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1028
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1029
        Return C code generated by iec2c compiler 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1030
        when _generate_softPLC have been called
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1031
        @param locations: ignored
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1032
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1033
        """
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1034
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1035
        res = ([(C_file_name, self.plcCFLAGS) 
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1036
                for C_file_name in self.PLCGeneratedCFiles ], 
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1037
               "", # no ldflags
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1038
               False) # do not expose retreive/publish calls
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1039
        
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1040
        pyfile=self._getPYTHONcodepath()
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1041
        if os.path.exists(pyfile):
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1042
            res += (("runtime.py", file(pyfile,"rb")),)
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1043
        wxgfile=self._getWXGLADEpath()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1044
        if os.path.exists(wxgfile):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1045
            hmipyfile=os.path.join(self._getBuildPath(),"hmi.py")
312
8a702213ce51 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 310
diff changeset
  1046
            if wx.Platform == '__WXMSW__':
8a702213ce51 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 310
diff changeset
  1047
                wxgfile = "\"%s\""%wxgfile
313
ef0b303d9535 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 312
diff changeset
  1048
                _hmipyfile = "\"%s\""%hmipyfile
ef0b303d9535 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 312
diff changeset
  1049
            else:
ef0b303d9535 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 312
diff changeset
  1050
                _hmipyfile = hmipyfile
ef0b303d9535 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 312
diff changeset
  1051
            self.launch_wxglade(['-o', _hmipyfile, '-g', 'python', wxgfile], wait=True)
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1052
            res += (("hmi.py", file(hmipyfile,"rb")),)
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1053
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1054
        return res
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1055
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1056
    
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1057
    def ResetIECProgramsAndVariables(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1058
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1059
        Reset variable and program list that are parsed from
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1060
        CSV file generated by IEC2C compiler.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1061
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1062
        self._ProgramList = None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1063
        self._VariablesList = None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1064
        self._IECPathToIdx = None
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1065
        self.TracedIECPath = []
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1066
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1067
    def GetIECProgramsAndVariables(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1068
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1069
        Parse CSV-like file  VARIABLES.csv resulting from IEC2C compiler.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1070
        Each section is marked with a line staring with '//'
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1071
        list of all variables used in various POUs
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1072
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1073
        if self._ProgramList is None or self._VariablesList is None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1074
            try:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1075
                csvfile = os.path.join(self._getBuildPath(),"VARIABLES.csv")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1076
                # describes CSV columns
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1077
                ProgramsListAttributeName = ["num", "C_path", "type"]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1078
                VariablesListAttributeName = ["num", "vartype", "IEC_path", "C_path", "type"]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1079
                self._ProgramList = []
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1080
                self._VariablesList = []
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1081
                self._IECPathToIdx = {}
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1082
                
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1083
                # Separate sections
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1084
                ListGroup = []
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1085
                for line in open(csvfile,'r').xreadlines():
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1086
                    strippedline = line.strip()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1087
                    if strippedline.startswith("//"):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1088
                        # Start new section
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1089
                        ListGroup.append([])
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1090
                    elif len(strippedline) > 0 and len(ListGroup) > 0:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1091
                        # append to this section
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1092
                        ListGroup[-1].append(strippedline)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1093
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1094
                # first section contains programs
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1095
                for line in ListGroup[0]:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1096
                    # Split and Maps each field to dictionnary entries
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1097
                    attrs = dict(zip(ProgramsListAttributeName,line.strip().split(';')))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1098
                    # Truncate "C_path" to remove conf an ressources names
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1099
                    attrs["C_path"] = '__'.join(attrs["C_path"].split(".",2)[1:])
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1100
                    # Push this dictionnary into result.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1101
                    self._ProgramList.append(attrs)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1102
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1103
                # second section contains all variables
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1104
                for line in ListGroup[1]:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1105
                    # Split and Maps each field to dictionnary entries
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1106
                    attrs = dict(zip(VariablesListAttributeName,line.strip().split(';')))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1107
                    # Truncate "C_path" to remove conf an ressources names
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1108
                    attrs["C_path"] = '__'.join(attrs["C_path"].split(".",2)[1:])
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1109
                    # Push this dictionnary into result.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1110
                    self._VariablesList.append(attrs)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1111
                    # Fill in IEC<->C translation dicts
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1112
                    IEC_path=attrs["IEC_path"]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1113
                    Idx=int(attrs["num"])
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1114
                    self._IECPathToIdx[IEC_path]=Idx
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1115
            except Exception,e:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1116
                self.logger.write_error("Cannot open/parse VARIABLES.csv!\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1117
                self.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1118
                self.ResetIECProgramsAndVariables()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1119
                return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1120
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1121
        return True
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1122
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1123
    def Generate_plc_debugger(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1124
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1125
        Generate trace/debug code out of PLC variable list
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1126
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1127
        self.GetIECProgramsAndVariables()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1128
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1129
        # prepare debug code
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 206
diff changeset
  1130
        debug_code = targets.code("plc_debug") % {
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
  1131
           "buffer_size": reduce(lambda x, y: x + y, [DebugTypesSize.get(v["type"], 0) for v in self._VariablesList], 0),
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1132
           "programs_declarations":
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1133
               "\n".join(["extern %(type)s %(C_path)s;"%p for p in self._ProgramList]),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1134
           "extern_variables_declarations":"\n".join([
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1135
              {"PT":"extern %(type)s *%(C_path)s;",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1136
               "VAR":"extern %(type)s %(C_path)s;"}[v["vartype"]]%v 
275
ff7c8eb3f362 Adding Function Blocks in VARIABLES.csv
lbessard
parents: 274
diff changeset
  1137
               for v in self._VariablesList if v["vartype"] != "FB" and v["C_path"].find('.')<0]),
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1138
           "subscription_table_count":
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1139
               len(self._VariablesList),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1140
           "variables_pointer_type_table_count":
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1141
               len(self._VariablesList),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1142
           "variables_pointer_type_table_initializer":"\n".join([
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1143
               {"PT":"    variable_table[%(num)s].ptrvalue = (void*)(%(C_path)s);\n",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1144
                "VAR":"    variable_table[%(num)s].ptrvalue = (void*)(&%(C_path)s);\n"}[v["vartype"]]%v + 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1145
                "    variable_table[%(num)s].type = %(type)s_ENUM;\n"%v
275
ff7c8eb3f362 Adding Function Blocks in VARIABLES.csv
lbessard
parents: 274
diff changeset
  1146
                for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypes ])}
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1147
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1148
        return debug_code
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1149
        
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1150
    def Generate_plc_python(self):
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1151
        """
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1152
        Generate trace/debug code out of PLC variable list
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1153
        """
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1154
        self.GetIECProgramsAndVariables()
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1155
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1156
        python_eval_fb_list = []
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1157
        for v in self._VariablesList :
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1158
            if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL","PYTHON_POLL"]:
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1159
                python_eval_fb_list.append(v)
288
7ee96191c476 Warning on modulo by zero fixed
lbessard
parents: 286
diff changeset
  1160
        python_eval_fb_count = max(1, len(python_eval_fb_list))
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1161
        
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1162
        # prepare debug code
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1163
        python_code = targets.code("plc_python") % {
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1164
           "python_eval_fb_count": python_eval_fb_count}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1165
        return python_code
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1166
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1167
    def Generate_plc_common_main(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1168
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1169
        Use plugins layout given in LocationCFilesAndCFLAGS to
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1170
        generate glue code that dispatch calls to all plugins
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1171
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1172
        # filter location that are related to code that will be called
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1173
        # in retreive, publish, init, cleanup
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1174
        locstrs = map(lambda x:"_".join(map(str,x)),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1175
           [loc for loc,Cfiles,DoCalls in self.LocationCFilesAndCFLAGS if loc and DoCalls])
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1176
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1177
        # Generate main, based on template
338
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1178
        if self.BeremizRoot.getEnable_Plugins():
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1179
            plc_main_code = targets.code("plc_common_main") % {
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1180
                "calls_prototypes":"\n".join([(
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1181
                      "int __init_%(s)s(int argc,char **argv);\n"+
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1182
                      "void __cleanup_%(s)s();\n"+
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1183
                      "void __retrieve_%(s)s();\n"+
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1184
                      "void __publish_%(s)s();")%{'s':locstr} for locstr in locstrs]),
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1185
                "retrieve_calls":"\n    ".join([
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1186
                      "__retrieve_%s();"%locstr for locstr in locstrs]),
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1187
                "publish_calls":"\n    ".join([ #Call publish in reverse order
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1188
                      "__publish_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)]),
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1189
                "init_calls":"\n    ".join([
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1190
                      "init_level=%d; "%(i+1)+
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1191
                      "if(res = __init_%s(argc,argv)){"%locstr +
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1192
                      #"printf(\"%s\"); "%locstr + #for debug
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1193
                      "return res;}" for i,locstr in enumerate(locstrs)]),
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1194
                "cleanup_calls":"\n    ".join([
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1195
                      "if(init_level >= %d) "%i+
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1196
                      "__cleanup_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)])
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1197
                }
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1198
        else:
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1199
            plc_main_code = targets.code("plc_common_main") % {
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1200
                "calls_prototypes":"\n",
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1201
                "retrieve_calls":"\n",
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1202
                "publish_calls":"\n",
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1203
                "init_calls":"\n",
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1204
                "cleanup_calls":"\n"
e19c78831d41 Adding support for enable/Disable plugins
greg
parents: 335
diff changeset
  1205
                }
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1206
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1207
        target_name = self.BeremizRoot.getTargetType().getcontent()["name"]
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 206
diff changeset
  1208
        plc_main_code += targets.targetcode(target_name)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1209
        return plc_main_code
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1210
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1211
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1212
    def _build(self):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1213
        """
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1214
        Method called by user to (re)build SoftPLC and plugin tree
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1215
        """
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1216
        if self.PLCEditor is not None:
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1217
            self.PLCEditor.ClearErrors()
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1218
        
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1219
        buildpath = self._getBuildPath()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1220
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1221
        # Eventually create build dir
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
  1222
        if not os.path.exists(buildpath):
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
  1223
            os.mkdir(buildpath)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1224
        # There is something to clean
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
  1225
        self.EnableMethod("_Clean", True)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1226
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1227
        self.logger.flush()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1228
        self.logger.write("Start build in %s\n" % buildpath)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1229
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1230
        # Generate SoftPLC IEC code
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1231
        IECGenRes = self._Generate_SoftPLC()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1232
        self.ShowMethod("_showIECcode", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1233
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1234
        # If IEC code gen fail, bail out.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1235
        if not IECGenRes:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1236
            self.logger.write_error("IEC-61131-3 code generation failed !\n")
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1237
            return False
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1238
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1239
        # Reset variable and program list that are parsed from
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1240
        # CSV file generated by IEC2C compiler.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1241
        self.ResetIECProgramsAndVariables()
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
  1242
        
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1243
        # Generate C code and compilation params from plugin hierarchy
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1244
        self.logger.write("Generating plugins C code\n")
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
  1245
        try:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1246
            self.LocationCFilesAndCFLAGS, self.LDFLAGS, ExtraFiles = self._Generate_C(
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
  1247
                buildpath, 
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1248
                self.PLCGeneratedLocatedVars)
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 176
diff changeset
  1249
        except Exception, exc:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1250
            self.logger.write_error("Plugins code generation failed !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1251
            self.logger.write_error(traceback.format_exc())
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
  1252
            return False
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
  1253
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1254
        # Get temprary directory path
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1255
        extrafilespath = self._getExtraFilesPath()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1256
        # Remove old directory
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1257
        if os.path.exists(extrafilespath):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1258
            shutil.rmtree(extrafilespath)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1259
        # Recreate directory
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1260
        os.mkdir(extrafilespath)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1261
        # Then write the files
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1262
        for fname,fobject in ExtraFiles:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1263
            fpath = os.path.join(extrafilespath,fname)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1264
            open(fpath, "wb").write(fobject.read())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1265
        # Now we can forget ExtraFiles (will close files object)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1266
        del ExtraFiles
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1267
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1268
        # Template based part of C code generation
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1269
        # files are stacked at the beginning, as files of plugin tree root
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1270
        for generator, filename, name in [
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1271
           # debugger code
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1272
           (self.Generate_plc_debugger, "plc_debugger.c", "Debugger"),
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1273
           # IEC<->python gateway code
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1274
           (self.Generate_plc_python, "plc_python.c", "IEC-Python gateway"),
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1275
           # init/cleanup/retrieve/publish, run and align code
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1276
           (self.Generate_plc_common_main,"plc_common_main.c","Common runtime")]:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1277
            try:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1278
                # Do generate
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1279
                code = generator()
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
  1280
                if code is None:
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 334
diff changeset
  1281
                     raise
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1282
                code_path = os.path.join(buildpath,filename)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1283
                open(code_path, "w").write(code)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1284
                # Insert this file as first file to be compiled at root plugin
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1285
                self.LocationCFilesAndCFLAGS[0][1].insert(0,(code_path, self.plcCFLAGS))
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1286
            except Exception, exc:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1287
                self.logger.write_error(name+" generation failed !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1288
                self.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1289
                return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1290
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1291
        self.logger.write("C code generated successfully.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1292
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1293
        # Get current or fresh builder
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1294
        builder = self.GetBuilder()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1295
        if builder is None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1296
            self.logger.write_error("Fatal : cannot get builder.\n")
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
  1297
            return False
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1298
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1299
        # Build
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1300
        try:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1301
            if not builder.build() :
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1302
                self.logger.write_error("C Build failed.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1303
                return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1304
        except Exception, exc:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1305
            self.logger.write_error("C Build crashed !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1306
            self.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1307
            return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1308
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1309
        # Update GUI status about need for transfer
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1310
        self.CompareLocalAndRemotePLC()
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents: 47
diff changeset
  1311
        return True
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1312
    
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1313
    def ShowError(self, logger, from_location, to_location):
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1314
        chunk_infos = self.GetChunkInfos(from_location, to_location)
215
0a98f2308107 Bugs on Opening PLCOpenEditor fixed
lbessard
parents: 209
diff changeset
  1315
        self._EditPLC()
202
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1316
        for infos, (start_row, start_col) in chunk_infos:
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1317
            start = (from_location[0] - start_row, from_location[1] - start_col)
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1318
            end = (to_location[0] - start_row, to_location[1] - start_col)
cd81a7a6e55c Adding support for highlighing compiling errors into PLCOpenEditor
lbessard
parents: 199
diff changeset
  1319
            self.PLCEditor.ShowError(infos, start, end)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1320
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1321
    def _showIECcode(self):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1322
        plc_file = self._getIECcodepath()
173
2a9c4eec8645 Bug on Beremiz close with and IECcode and IECrawcode frames opened fixed
lbessard
parents: 158
diff changeset
  1323
        new_dialog = wx.Frame(self.AppFrame)
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
  1324
        ST_viewer = TextViewer(new_dialog, "", None, None)
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1325
        #ST_viewer.Enable(False)
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1326
        ST_viewer.SetKeywords(IEC_KEYWORDS)
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1327
        try:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1328
            text = file(plc_file).read()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1329
        except:
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1330
            text = '(* No IEC code have been generated at that time ! *)'
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1331
        ST_viewer.SetText(text = text)
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1332
            
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1333
        new_dialog.Show()
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1334
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1335
    def _editIECrawcode(self):
173
2a9c4eec8645 Bug on Beremiz close with and IECcode and IECrawcode frames opened fixed
lbessard
parents: 158
diff changeset
  1336
        new_dialog = wx.Frame(self.AppFrame)
66
b46237718b27 Bug with raw code edit fixed
lbessard
parents: 65
diff changeset
  1337
        
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1338
        controler = MiniTextControler(self._getIECrawcodepath())
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
  1339
        ST_viewer = TextViewer(new_dialog, "", None, controler)
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1340
        #ST_viewer.Enable(False)
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1341
        ST_viewer.SetKeywords(IEC_KEYWORDS)
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1342
        ST_viewer.RefreshView()
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1343
            
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1344
        new_dialog.Show()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1345
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1346
    def _editPYTHONcode(self):
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1347
        from PythonSTC import PythonCodePanel
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1348
        new_dialog = wx.Frame(self.AppFrame)
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1349
        
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1350
        PYTHON_viewer = PythonCodePanel(new_dialog, self.AppFrame)
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1351
        #ST_viewer.Enable(False)
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1352
        pyfile=self._getPYTHONcodepath()
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1353
        PYTHON_viewer.LoadSourceFile(pyfile)
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1354
            
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1355
        new_dialog.Show()
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1356
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1357
    def _editWXGLADE(self):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1358
        wxg_filename = self._getWXGLADEpath()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1359
        if not os.path.exists(wxg_filename):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1360
            open(wxg_filename,"w").write("""<?xml version="1.0"?>
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1361
<application path="" name="" class="" option="0" language="python" top_window="frame_1" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0">
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1362
    <object class="HMIFrame" name="frame_1" base="EditFrame">
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1363
        <style>wxDEFAULT_FRAME_STYLE</style>
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1364
        <title>frame_1</title>
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1365
    </object>
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1366
</application>
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1367
""")
312
8a702213ce51 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 310
diff changeset
  1368
        if wx.Platform == '__WXMSW__':
8a702213ce51 Bug when try to launch wxGlade on Windows fixed
lbessard
parents: 310
diff changeset
  1369
            wxg_filename = "\"%s\""%wxg_filename
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1370
        self.launch_wxglade([wxg_filename])
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1371
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1372
    def _EditPLC(self):
62
ddf0cdd71558 Adding support for refresh block list where beremiz loose focus
lbessard
parents: 57
diff changeset
  1373
        if self.PLCEditor is None:
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
  1374
            self.RefreshPluginsBlockLists()
25
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1375
            def _onclose():
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1376
                self.PLCEditor = None
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1377
            def _onsave():
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1378
                self.SaveProject()
41
1608a434fb8c Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 40
diff changeset
  1379
            self.PLCEditor = PLCOpenEditor(self.AppFrame, self)
25
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1380
            self.PLCEditor._onclose = _onclose
fa7503684c28 Adding support for using Networkedit et PLCOpenEditor in Beremiz
lbessard
parents: 24
diff changeset
  1381
            self.PLCEditor._onsave = _onsave
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1382
            self.PLCEditor.Show()
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
  1383
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1384
    def _Clean(self):
108
280b458e42e5 add clean implementation
greg
parents: 107
diff changeset
  1385
        if os.path.isdir(os.path.join(self._getBuildPath())):
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1386
            self.logger.write("Cleaning the build directory\n")
108
280b458e42e5 add clean implementation
greg
parents: 107
diff changeset
  1387
            shutil.rmtree(os.path.join(self._getBuildPath()))
280b458e42e5 add clean implementation
greg
parents: 107
diff changeset
  1388
        else:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1389
            self.logger.write_error("Build directory already clean\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1390
        self.ShowMethod("_showIECcode", False)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 109
diff changeset
  1391
        self.EnableMethod("_Clean", False)
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1392
        # kill the builder
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1393
        self._builder = None
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1394
        self.CompareLocalAndRemotePLC()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1395
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1396
    ############# Real PLC object access #############
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1397
    def UpdateMethodsFromPLCStatus(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1398
        # Get PLC state : Running or Stopped
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1399
        # TODO : use explicit status instead of boolean
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1400
        if self._connector is not None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1401
            status = self._connector.GetPLCstatus()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1402
            self.logger.write("PLC is %s\n"%status)
107
65fe90d49bf7 add Run and Stop implementation
greg
parents: 106
diff changeset
  1403
        else:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1404
            status = "Disconnected"
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1405
        for args in {
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1406
               "Started":[("_Run", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1407
                          ("_Debug", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1408
                          ("_Stop", True)],
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1409
               "Stopped":[("_Run", True),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1410
                          ("_Debug", True),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1411
                          ("_Stop", False)],
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1412
               "Empty":  [("_Run", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1413
                          ("_Debug", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1414
                          ("_Stop", False)],
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1415
               "Dirty":  [("_Run", True),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1416
                          ("_Debug", True),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1417
                          ("_Stop", False)],
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1418
               "Disconnected":  [("_Run", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1419
                                 ("_Debug", False),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1420
                                 ("_Stop", False)],
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1421
               }.get(status,[]):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1422
            self.ShowMethod(*args)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1423
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1424
    def _Run(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1425
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1426
        Start PLC
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1427
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1428
        if self._connector.StartPLC():
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1429
            self.logger.write("Starting PLC\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1430
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1431
            self.logger.write_error("Couldn't start PLC !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1432
        self.UpdateMethodsFromPLCStatus()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1433
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1434
    def RegisterDebugVarToConnector(self):
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1435
        self.DebugTimer=None
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1436
        Idxs = []
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1437
        self.TracedIECPath = []
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1438
        if self._connector is not None:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1439
            self.IECdebug_lock.acquire()
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1440
            IECPathsToPop = []
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1441
            for IECPath,data_tuple in self.IECdebug_datas.iteritems():
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1442
                WeakCallableDict, data_log, status = data_tuple
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1443
                if len(WeakCallableDict) == 0:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1444
                    # Callable Dict is empty.
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1445
                    # This variable is not needed anymore!
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1446
                    #print "Unused : " + IECPath
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1447
                    IECPathsToPop.append(IECPath)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1448
                else:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1449
                    # Convert 
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1450
                    Idx = self._IECPathToIdx.get(IECPath,None)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1451
                    if Idx is not None:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1452
                        Idxs.append(Idx)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1453
                        self.TracedIECPath.append(IECPath)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1454
                    else:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1455
                        self.logger.write_warning("Debug : Unknown variable %s\n"%IECPath)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1456
            for IECPathToPop in IECPathsToPop:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1457
                self.IECdebug_datas.pop(IECPathToPop)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1458
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1459
            self._connector.SetTraceVariablesList(Idxs)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1460
            self.IECdebug_lock.release()
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1461
            
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1462
            #for IEC_path, IECdebug_data in self.IECdebug_datas.iteritems():
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1463
            #    print IEC_path, IECdebug_data[0].keys()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1464
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1465
    def ReArmDebugRegisterTimer(self):
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1466
        if self.DebugTimer is not None:
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1467
            self.DebugTimer.cancel()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1468
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1469
        # Timer to prevent rapid-fire when registering many variables
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1470
        # use wx.CallAfter use keep using same thread. TODO : use wx.Timer instead
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1471
        self.DebugTimer=Timer(0.5,wx.CallAfter,args = [self.RegisterDebugVarToConnector])
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1472
        # Rearm anti-rapid-fire timer
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1473
        self.DebugTimer.start()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1474
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1475
        
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1476
    def SubscribeDebugIECVariable(self, IECPath, callableobj, *args, **kwargs):
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1477
        """
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1478
        Dispatching use a dictionnary linking IEC variable paths
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1479
        to a WeakKeyDictionary linking 
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1480
        weakly referenced callables to optionnal args
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1481
        """
246
3613c6ee9e0e Adding support for Debugging in PLCOpenEditor
lbessard
parents: 244
diff changeset
  1482
        if self._IECPathToIdx.get(IECPath, None) is None:
3613c6ee9e0e Adding support for Debugging in PLCOpenEditor
lbessard
parents: 244
diff changeset
  1483
            return None
3613c6ee9e0e Adding support for Debugging in PLCOpenEditor
lbessard
parents: 244
diff changeset
  1484
        
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1485
        self.IECdebug_lock.acquire()
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1486
        # If no entry exist, create a new one with a fresh WeakKeyDictionary
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1487
        IECdebug_data = self.IECdebug_datas.get(IECPath, None)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1488
        if IECdebug_data is None:
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1489
            IECdebug_data  = [
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1490
                    WeakKeyDictionary(), # Callables
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1491
                    [],                  # Data storage [(tick, data),...]
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1492
                    "Registered"]        # Variable status
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1493
            self.IECdebug_datas[IECPath] = IECdebug_data
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1494
        
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1495
        IECdebug_data[0][callableobj]=(args, kwargs)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1496
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1497
        self.IECdebug_lock.release()
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1498
        
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1499
        self.ReArmDebugRegisterTimer()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1500
        
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1501
        return IECdebug_data[1]
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1502
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1503
    def UnsubscribeDebugIECVariable(self, IECPath, callableobj):
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1504
        #print "Unsubscribe", IECPath, callableobj
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1505
        self.IECdebug_lock.acquire()
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1506
        IECdebug_data = self.IECdebug_datas.get(IECPath, None)
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1507
        if IECdebug_data is not None:
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1508
            IECdebug_data[0].pop(callableobj,None)
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1509
        self.IECdebug_lock.release()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1510
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1511
        self.ReArmDebugRegisterTimer()
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1512
334
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1513
    def UnsubscribeAllDebugIECVariable(self):
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1514
        self.IECdebug_lock.acquire()
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1515
        IECdebug_data = {}
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1516
        self.IECdebug_lock.release()
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1517
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1518
        self.ReArmDebugRegisterTimer()        
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1519
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1520
    def DebugThreadProc(self):
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1521
        """
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1522
        This thread waid PLC debug data, and dispatch them to subscribers
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1523
        """
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1524
        # This lock is used to avoid flooding wx event stack calling callafter
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1525
        self.DebugThreadSlowDownLock = Semaphore(0)
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1526
        self.debug_break = False
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1527
        while (not self.debug_break) and (self._connector is not None):
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1528
            debug_tick, debug_vars = self._connector.GetTraceVariables()
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1529
            #print debug_tick, debug_vars
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1530
            self.IECdebug_lock.acquire()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1531
            if debug_vars is not None and \
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1532
               len(debug_vars) == len(self.TracedIECPath):
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1533
                for IECPath,value in zip(self.TracedIECPath, debug_vars):
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1534
                    if value is not None:
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1535
	                    data_tuple = self.IECdebug_datas.get(IECPath, None)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1536
	                    if data_tuple is not None:
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1537
	                        WeakCallableDict, data_log, status = data_tuple
328
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1538
	                        #data_log.append((debug_tick, value))
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1539
	                        for weakcallable,(args,kwargs) in WeakCallableDict.iteritems():
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1540
	                            # delegate call to wx event loop
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1541
	                            #print weakcallable, value, args, kwargs
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1542
	                            if getattr(weakcallable, "SetValue", None) is not None:
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1543
	                                wx.CallAfter(weakcallable.SetValue, value, *args, **kwargs)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1544
	                            elif getattr(weakcallable, "AddPoint", None) is not None:
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1545
	                                wx.CallAfter(weakcallable.AddPoint, debug_tick, value, *args, **kwargs)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents: 313
diff changeset
  1546
	                            # This will block thread if more than one call is waiting
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1547
            elif debug_vars is not None:
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1548
                wx.CallAfter(self.logger.write_warning, 
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 275
diff changeset
  1549
                             "Debug data not coherent %d != %d\n"%(len(debug_vars), len(self.TracedIECPath)))
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1550
            elif debug_tick == -1:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1551
                #wx.CallAfter(self.logger.write, "Debugger unavailable\n")
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1552
                pass
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1553
            else:
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1554
                wx.CallAfter(self.logger.write, "Debugger disabled\n")
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1555
                self.debug_break = True
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1556
            self.IECdebug_lock.release()
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 243
diff changeset
  1557
            wx.CallAfter(self.DebugThreadSlowDownLock.release)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 243
diff changeset
  1558
            self.DebugThreadSlowDownLock.acquire()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1559
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1560
    def KillDebugThread(self):
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1561
        self.debug_break = True
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1562
        self.DebugThreadSlowDownLock.release()
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1563
        self.DebugThread.join(timeout=1)
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1564
        if self.DebugThread.isAlive():
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1565
            self.logger.write_warning("Debug Thread couldn't be killed")
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1566
        self.DebugThread = None
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1567
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1568
    def _Debug(self):
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1569
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1570
        Start PLC (Debug Mode)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1571
        """
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1572
        if self.GetIECProgramsAndVariables() and \
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1573
           self._connector.StartPLC(debug=True):
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1574
            self.logger.write("Starting PLC (debug mode)\n")
243
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1575
            if self.PLCDebug is None:
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1576
                self.RefreshPluginsBlockLists()
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1577
                def _onclose():
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1578
                    self.PLCDebug = None
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1579
                self.PLCDebug = PLCOpenEditor(self.AppFrame, self, debug=True)
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1580
                self.PLCDebug._onclose = _onclose
90db933fe956 Adding support for Debugging with PLCOpenEditor
lbessard
parents: 239
diff changeset
  1581
                self.PLCDebug.Show()
328
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1582
            else:
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1583
                self.PLCDebug.ResetGraphicViewers()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1584
            self.DebugThread = Thread(target=self.DebugThreadProc)
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1585
            self.DebugThread.start()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1586
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1587
            self.logger.write_error("Couldn't start PLC debug !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1588
        self.UpdateMethodsFromPLCStatus()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1589
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1590
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1591
#    def _Do_Test_Debug(self):
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1592
#        # debug code
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1593
#        self.temporary_non_weak_callable_refs = []
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1594
#        for IEC_Path, idx in self._IECPathToIdx.iteritems():
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1595
#            class tmpcls:
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1596
#                def __init__(_self):
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 243
diff changeset
  1597
#                    _self.buf = None
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1598
#                def setbuf(_self,buf):
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 243
diff changeset
  1599
#                    _self.buf = buf
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1600
#                def SetValue(_self, value, idx, name):
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1601
#                    self.logger.write("debug call: %s %d %s\n"%(repr(value), idx, name))
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
  1602
#                    #self.logger.write("debug call: %s %d %s %s\n"%(repr(value), idx, name, repr(self.buf)))
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1603
#            a = tmpcls()
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1604
#            res = self.SubscribeDebugIECVariable(IEC_Path, a, idx, IEC_Path)
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1605
#            a.setbuf(res)
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1606
#            self.temporary_non_weak_callable_refs.append(a)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1607
       
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1608
    def _Stop(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1609
        """
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1610
        Stop PLC
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1611
        """
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1612
        if self.DebugThread is not None:
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1613
            self.logger.write("Stopping debug\n")
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1614
            self.KillDebugThread()
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
  1615
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1616
        if self._connector.StopPLC():
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1617
            self.logger.write("Stopping PLC\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1618
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1619
            self.logger.write_error("Couldn't stop PLC !\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1620
        self.UpdateMethodsFromPLCStatus()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1621
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1622
    def _Connect(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1623
        # don't accept re-connetion is already connected
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1624
        if self._connector is not None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1625
            self.logger.write_error("Already connected. Please disconnect\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1626
            return
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1627
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1628
        # Get connector uri
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1629
        uri = self.\
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1630
              BeremizRoot.\
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1631
              getURI_location().\
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1632
              strip()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1633
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1634
        # if uri is empty launch discovery dialog
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1635
        if uri == "":
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1636
            # Launch Service Discovery dialog
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1637
            dia = DiscoveryDialog(self.AppFrame)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1638
            dia.ShowModal()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1639
            uri = dia.GetResult()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1640
            # Nothing choosed or cancel button
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1641
            if uri is None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1642
                return
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1643
            else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1644
                self.\
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1645
                BeremizRoot.\
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1646
                setURI_location(uri)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1647
       
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1648
        # Get connector from uri
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1649
        try:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1650
            self._connector = connectors.ConnectorFactory(uri, self)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1651
        except Exception, msg:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1652
            self.logger.write_error("Exception while connecting %s!\n"%uri)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1653
            self.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1654
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1655
        # Did connection success ?
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1656
        if self._connector is None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1657
            # Oups.
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1658
            self.logger.write_error("Connection failed to %s!\n"%uri)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1659
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1660
            self.ShowMethod("_Connect", False)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1661
            self.ShowMethod("_Disconnect", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1662
            self.ShowMethod("_Transfer", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1663
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1664
            self.CompareLocalAndRemotePLC()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1665
            self.UpdateMethodsFromPLCStatus()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1666
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1667
    def CompareLocalAndRemotePLC(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1668
        if self._connector is None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1669
            return
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1670
        # We are now connected. Update button status
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1671
        MD5 = self.GetLastBuildMD5()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1672
        # Check remote target PLC correspondance to that md5
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1673
        if MD5 is not None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1674
            if not self._connector.MatchMD5(MD5):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1675
                self.logger.write_warning(
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1676
                   "Latest build do not match with target, please transfer.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1677
                self.EnableMethod("_Transfer", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1678
            else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1679
                self.logger.write(
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1680
                   "Latest build match target, no transfer needed.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1681
                self.EnableMethod("_Transfer", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1682
                #self.EnableMethod("_Transfer", False)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1683
        else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1684
            self.logger.write_warning(
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1685
                "Cannot compare latest build to target. Please build.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1686
            self.EnableMethod("_Transfer", False)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1687
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1688
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1689
    def _Disconnect(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1690
        self._connector = None
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1691
        self.ShowMethod("_Transfer", False)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1692
        self.ShowMethod("_Connect", True)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1693
        self.ShowMethod("_Disconnect", False)
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1694
        self.UpdateMethodsFromPLCStatus()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1695
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1696
    def _Transfer(self):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1697
        # Get the last build PLC's 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1698
        MD5 = self.GetLastBuildMD5()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1699
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1700
        # Check if md5 file is empty : ask user to build PLC 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1701
        if MD5 is None :
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1702
            self.logger.write_error("Failed : Must build before transfer.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1703
            return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1704
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1705
        # Compare PLC project with PLC on target
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1706
        if self._connector.MatchMD5(MD5):
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1707
            self.logger.write(
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1708
                "Latest build already match current target. Transfering anyway...\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1709
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1710
        # Get temprary directory path
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1711
        extrafilespath = self._getExtraFilesPath()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1712
        extrafiles = [(name, open(os.path.join(extrafilespath, name), 
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1713
                                  'rb').read()) \
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1714
                      for name in os.listdir(extrafilespath) \
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1715
                      if not name=="CVS"]
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1716
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1717
        # Send PLC on target
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1718
        builder = self.GetBuilder()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1719
        if builder is not None:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1720
            data = builder.GetBinaryCode()
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1721
            if data is not None :
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1722
                if self._connector.NewPLC(MD5, data, extrafiles):
328
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1723
                    if self.PLCDebug is not None:
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1724
                        self.PLCDebug.Close()
c23daa6996c2 Close Debug Window when program transfered
lbessard
parents: 325
diff changeset
  1725
                        self.PLCDebug = None
334
b4131e5d10a4 Adding support for unsubscribe all variables while transferring
lbessard
parents: 328
diff changeset
  1726
                    self.UnsubscribeAllDebugIECVariable()
246
3613c6ee9e0e Adding support for Debugging in PLCOpenEditor
lbessard
parents: 244
diff changeset
  1727
                    self.ProgramTransferred()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1728
                    self.logger.write("Transfer completed successfully.\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1729
                else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1730
                    self.logger.write_error("Transfer failed\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1731
            else:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1732
                self.logger.write_error("No PLC to transfer (did build success ?)\n")
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1733
        self.UpdateMethodsFromPLCStatus()
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1734
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1735
    PluginMethods = [
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1736
        {"bitmap" : opjimg("editPLC"),
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1737
         "name" : "Edit PLC",
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1738
         "tooltip" : "Edit PLC program with PLCOpenEditor",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1739
         "method" : "_EditPLC"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1740
        {"bitmap" : opjimg("Build"),
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1741
         "name" : "Build",
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1742
         "tooltip" : "Build project into build folder",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1743
         "method" : "_build"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1744
        {"bitmap" : opjimg("Clean"),
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1745
         "name" : "Clean",
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1746
         "enabled" : False,
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1747
         "tooltip" : "Clean project build folder",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1748
         "method" : "_Clean"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1749
        {"bitmap" : opjimg("Run"),
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1750
         "name" : "Run",
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1751
         "shown" : False,
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1752
         "tooltip" : "Start PLC",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1753
         "method" : "_Run"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1754
        {"bitmap" : opjimg("Debug"),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1755
         "name" : "Debug",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1756
         "shown" : False,
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1757
         "tooltip" : "Start PLC (debug mode)",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1758
         "method" : "_Debug"},
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1759
#        {"bitmap" : opjimg("Debug"),
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1760
#         "name" : "Do_Test_Debug",
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1761
#         "tooltip" : "Test debug mode)",
a66e150f2888 Improved debug data feedback.
etisserant
parents: 228
diff changeset
  1762
#         "method" : "_Do_Test_Debug"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1763
        {"bitmap" : opjimg("Stop"),
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1764
         "name" : "Stop",
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1765
         "shown" : False,
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1766
         "tooltip" : "Stop Running PLC",
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1767
         "method" : "_Stop"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1768
        {"bitmap" : opjimg("Connect"),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1769
         "name" : "Connect",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1770
         "tooltip" : "Connect to the target PLC",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1771
         "method" : "_Connect"},
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1772
        {"bitmap" : opjimg("Transfer"),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1773
         "name" : "Transfer",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1774
         "shown" : False,
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1775
         "tooltip" : "Transfer PLC",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1776
         "method" : "_Transfer"},
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1777
        {"bitmap" : opjimg("Disconnect"),
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1778
         "name" : "Disconnect",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1779
         "shown" : False,
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1780
         "tooltip" : "Disconnect from PLC",
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1781
         "method" : "_Disconnect"},
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1782
        {"bitmap" : opjimg("ShowIECcode"),
199
aa5f43bafad4 minor gui improvements :
etisserant
parents: 190
diff changeset
  1783
         "name" : "Show code",
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1784
         "shown" : False,
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1785
         "tooltip" : "Show IEC code generated by PLCGenerator",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
  1786
         "method" : "_showIECcode"},
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1787
        {"bitmap" : opjimg("editIECrawcode"),
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1788
         "name" : "Raw IEC code",
74
8a726a62fbfc Some bugs fixed
lbessard
parents: 66
diff changeset
  1789
         "tooltip" : "Edit raw IEC code added to code generated by PLCGenerator",
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
  1790
         "method" : "_editIECrawcode"},
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1791
        {"bitmap" : opjimg("editPYTHONcode"),
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1792
         "name" : "Python code",
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1793
         "tooltip" : "Write Python runtime code, for use with python_eval FBs",
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
  1794
         "method" : "_editPYTHONcode"},
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1795
        {"bitmap" : opjimg("editWXGLADE"),
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1796
         "name" : "WXGLADE GUI",
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1797
         "tooltip" : "Edit a WxWidgets GUI with WXGlade",
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 298
diff changeset
  1798
         "method" : "_editWXGLADE"},
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 64
diff changeset
  1799
    ]