etisserant@14: """ Edouard@728: Beremiz Project Controller etisserant@14: """ etisserant@178: import os,sys,traceback laurent@411: import time Edouard@721: import features etisserant@14: import shutil etisserant@22: import wx Edouard@725: import re, tempfile Edouard@725: from threading import Timer, Lock, Thread etisserant@20: from time import localtime etisserant@20: from datetime import datetime Edouard@725: from weakref import WeakKeyDictionary Edouard@725: Edouard@725: import targets Edouard@725: import connectors laurent@806: from util.misc import CheckPathPerm, GetClassImporter, IECCodeViewer Edouard@742: from util.MiniTextControler import MiniTextControler Edouard@726: from util.ProcessLogger import ProcessLogger laurent@782: from util.FileManagementPanel import FileManagementPanel laurent@738: from PLCControler import PLCControler etisserant@20: from TextViewer import TextViewer Edouard@725: from plcopen.structures import IEC_KEYWORDS Edouard@592: from targets.typemapping import DebugTypesSize Edouard@726: from util.discovery import DiscoveryDialog Edouard@725: from ConfigTreeNode import ConfigTreeNode laurent@738: from ProjectNodeEditor import ProjectNodeEditor laurent@782: from utils.BitmapLibrary import GetBitmap Edouard@725: Edouard@725: base_folder = os.path.split(sys.path[0])[0] etisserant@20: lbessard@356: MATIEC_ERROR_MODEL = re.compile(".*\.st:(\d+)-(\d+)\.\.(\d+)-(\d+): error : (.*)$") lbessard@356: edouard@578: DEBUG_RETRIES_WARN = 3 edouard@578: DEBUG_RETRIES_REREGISTER = 4 edouard@578: laurent@738: ITEM_CONFNODE = 25 laurent@738: Edouard@725: class ProjectController(ConfigTreeNode, PLCControler): etisserant@20: """ Edouard@717: This class define Root object of the confnode tree. etisserant@20: It is responsible of : etisserant@20: - Managing project directory etisserant@20: - Building project etisserant@20: - Handling PLCOpenEditor controler and view Edouard@718: - Loading user confnodes and instanciante them as children etisserant@20: - ... etisserant@20: etisserant@20: """ etisserant@13: Edouard@718: # For root object, available Children Types are modules of the confnode packages. Edouard@725: CTNChildrenTypes = [(n, GetClassImporter(c), d) for n,d,h,c in features.catalog] etisserant@13: etisserant@13: XSD = """ etisserant@13: etisserant@13: etisserant@13: lbessard@86: lbessard@86: lbessard@86: laurent@411: Edouard@733: """+targets.GetTargetChoices()+""" etisserant@106: etisserant@106: Edouard@770: Edouard@770: """+((""" laurent@730: Edouard@734: """+"\n".join(['' Edouard@731: for libname,lib in features.libraries])+""" Edouard@770: """) if len(features.libraries)>0 else '') + """ Edouard@770: lbessard@86: greg@204: Edouard@728: etisserant@13: etisserant@13: etisserant@13: etisserant@13: """ laurent@738: EditorType = ProjectNodeEditor laurent@738: etisserant@290: def __init__(self, frame, logger): lbessard@41: PLCControler.__init__(self) etisserant@227: etisserant@20: self.MandatoryParams = None laurent@417: self.SetAppFrame(frame, logger) etisserant@203: self._builder = None etisserant@203: self._connector = None etisserant@203: Edouard@725: self.iec2c_path = os.path.join(base_folder, "matiec", "iec2c"+(".exe" if wx.Platform == '__WXMSW__' else "")) greg@418: self.ieclib_path = os.path.join(base_folder, "matiec", "lib") greg@418: etisserant@203: # Setup debug information etisserant@227: self.IECdebug_datas = {} etisserant@227: self.IECdebug_lock = Lock() etisserant@222: etisserant@235: self.DebugTimer=None etisserant@203: self.ResetIECProgramsAndVariables() etisserant@203: etisserant@118: # In both new or load scenario, no need to save greg@350: self.ChangesToSave = False etisserant@23: # root have no parent Edouard@718: self.CTNParent = None Edouard@717: # Keep track of the confnode type name Edouard@718: self.CTNType = "Beremiz" Edouard@718: self.Children = {} laurent@738: self._View = None Edouard@717: # After __init__ root confnode is not valid etisserant@20: self.ProjectPath = None greg@427: self._setBuildPath(None) etisserant@286: self.DebugThread = None etisserant@286: self.debug_break = False greg@350: self.previous_plcstate = None Edouard@717: # copy ConfNodeMethods so that it can be later customized laurent@754: self.StatusMethods = [dic.copy() for dic in self.StatusMethods] Edouard@728: Edouard@728: def LoadLibraries(self): Edouard@728: self.Libraries = [] Edouard@728: TypeStack=[] Edouard@731: for libname,clsname in features.libraries: Edouard@731: if self.BeremizRoot.Libraries is None or getattr(self.BeremizRoot.Libraries, "Enable_"+libname+"_Library"): Edouard@732: Lib = GetClassImporter(clsname)()(self, libname, TypeStack) Edouard@728: TypeStack.append(Lib.GetTypes()) Edouard@728: self.Libraries.append(Lib) lbessard@325: laurent@395: def __del__(self): edouard@466: if self.DebugTimer: edouard@466: self.DebugTimer.cancel() edouard@466: self.KillDebugThread() laurent@738: laurent@417: def SetAppFrame(self, frame, logger): laurent@417: self.AppFrame = frame laurent@417: self.logger = logger laurent@417: self.StatusTimer = None laurent@417: laurent@417: if frame is not None: laurent@417: # Timer to pull PLC status laurent@417: ID_STATUSTIMER = wx.NewId() laurent@417: self.StatusTimer = wx.Timer(self.AppFrame, ID_STATUSTIMER) laurent@417: self.AppFrame.Bind(wx.EVT_TIMER, self.PullPLCStatusProc, self.StatusTimer) laurent@652: Edouard@717: self.RefreshConfNodesBlockLists() laurent@417: laurent@417: def ResetAppFrame(self, logger): laurent@417: if self.AppFrame is not None: laurent@417: self.AppFrame.Unbind(wx.EVT_TIMER, self.StatusTimer) laurent@417: self.StatusTimer = None laurent@417: self.AppFrame = None laurent@417: laurent@417: self.logger = logger laurent@417: laurent@738: def CTNName(self): laurent@738: return "Project" laurent@738: Edouard@718: def CTNTestModified(self): etisserant@118: return self.ChangesToSave or not self.ProjectIsSaved() etisserant@118: Edouard@718: def CTNFullName(self): laurent@656: return "" laurent@656: Edouard@718: def GetCTRoot(self): etisserant@23: return self etisserant@23: greg@418: def GetIECLibPath(self): greg@418: return self.ieclib_path greg@418: greg@418: def GetIEC2cPath(self): greg@418: return self.iec2c_path greg@418: etisserant@23: def GetCurrentLocation(self): etisserant@23: return () etisserant@47: etisserant@47: def GetCurrentName(self): etisserant@47: return "" etisserant@47: etisserant@47: def _GetCurrentName(self): etisserant@47: return "" etisserant@47: lbessard@17: def GetProjectPath(self): lbessard@17: return self.ProjectPath etisserant@51: etisserant@51: def GetProjectName(self): etisserant@51: return os.path.split(self.ProjectPath)[1] lbessard@17: laurent@781: def GetIconName(self): laurent@781: return "PROJECT" laurent@738: laurent@510: def GetDefaultTargetName(self): laurent@411: if wx.Platform == '__WXMSW__': laurent@510: return "Win32" laurent@411: else: laurent@510: return "Linux" laurent@510: laurent@510: def GetTarget(self): laurent@510: target = self.BeremizRoot.getTargetType() laurent@510: if target.getcontent() is None: laurent@510: target = self.Classes["BeremizRoot_TargetType"]() laurent@510: target_name = self.GetDefaultTargetName() laurent@510: target.setcontent({"name": target_name, "value": self.Classes["TargetType_%s"%target_name]()}) laurent@411: return target laurent@411: laurent@411: def GetParamsAttributes(self, path = None): Edouard@717: params = ConfigTreeNode.GetParamsAttributes(self, path) laurent@411: if params[0]["name"] == "BeremizRoot": laurent@411: for child in params[0]["children"]: laurent@411: if child["name"] == "TargetType" and child["value"] == '': laurent@510: child.update(self.GetTarget().getElementInfos("TargetType")) laurent@411: return params laurent@411: laurent@411: def SetParamsAttribute(self, path, value): laurent@411: if path.startswith("BeremizRoot.TargetType.") and self.BeremizRoot.getTargetType().getcontent() is None: laurent@607: self.BeremizRoot.setTargetType(self.GetTarget()) Edouard@717: return ConfigTreeNode.SetParamsAttribute(self, path, value) greg@427: greg@427: # helper func to check project path write permission greg@427: def CheckProjectPathPerm(self, dosave=True): greg@427: if CheckPathPerm(self.ProjectPath): greg@427: return True greg@427: dialog = wx.MessageDialog(self.AppFrame, laurent@428: _('You must have permission to work on the project\nWork on a project copy ?'), greg@427: _('Error'), greg@427: wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) greg@427: answer = dialog.ShowModal() greg@427: dialog.Destroy() greg@427: if answer == wx.ID_YES: greg@427: if self.SaveProjectAs(): greg@427: self.AppFrame.RefreshTitle() laurent@534: self.AppFrame.RefreshFileMenu() laurent@738: self.AppFrame.RefreshPageTitles() greg@427: return True greg@427: return False etisserant@20: laurent@757: def _getProjectFilesPath(self): laurent@757: projectfiles_path = os.path.join(self.GetProjectPath(), "project_files") laurent@757: if not os.path.exists(projectfiles_path): laurent@757: os.mkdir(projectfiles_path) laurent@757: return projectfiles_path laurent@757: greg@256: def NewProject(self, ProjectPath, BuildPath=None): lbessard@17: """ lbessard@17: Create a new project in an empty folder lbessard@17: @param ProjectPath: path of the folder where project have to be created lbessard@17: @param PLCParams: properties of the PLCOpen program created lbessard@17: """ laurent@415: # Verify that chosen folder is empty lbessard@17: if not os.path.isdir(ProjectPath) or len(os.listdir(ProjectPath)) > 0: laurent@415: return _("Chosen folder isn't empty. You can't use it for a new project!") etisserant@20: lbessard@41: # Create PLCOpen program laurent@738: self.CreateNewProject( laurent@738: {"projectName": _("Unnamed"), laurent@738: "productName": _("Unnamed"), laurent@801: "productVersion": "1", laurent@738: "companyName": _("Unknown"), laurent@738: "creationDateTime": datetime(*localtime()[:6])}) laurent@738: self.ProjectAddConfiguration("config") laurent@738: self.ProjectAddConfigurationResource("config", "resource1") laurent@738: etisserant@13: # Change XSD into class members etisserant@13: self._AddParamsMembers() Edouard@718: self.Children = {} Edouard@717: # Keep track of the root confnode (i.e. project path) lbessard@17: self.ProjectPath = ProjectPath greg@427: self._setBuildPath(BuildPath) Edouard@717: # get confnodes bloclist (is that usefull at project creation?) Edouard@717: self.RefreshConfNodesBlockLists() etisserant@114: # this will create files base XML files etisserant@114: self.SaveProject() lbessard@17: return None lbessard@17: greg@256: def LoadProject(self, ProjectPath, BuildPath=None): lbessard@17: """ lbessard@17: Load a project contained in a folder lbessard@17: @param ProjectPath: path of the project folder lbessard@17: """ lbessard@190: if os.path.basename(ProjectPath) == "": lbessard@190: ProjectPath = os.path.dirname(ProjectPath) etisserant@203: # Verify that project contains a PLCOpen program lbessard@17: plc_file = os.path.join(ProjectPath, "plc.xml") lbessard@17: if not os.path.isfile(plc_file): laurent@415: return _("Chosen folder doesn't contain a program. It's not a valid project!") lbessard@17: # Load PLCOpen file lbessard@41: result = self.OpenXMLFile(plc_file) lbessard@17: if result: lbessard@17: return result lbessard@17: # Change XSD into class members lbessard@17: self._AddParamsMembers() Edouard@718: self.Children = {} Edouard@717: # Keep track of the root confnode (i.e. project path) lbessard@17: self.ProjectPath = ProjectPath greg@427: self._setBuildPath(BuildPath) etisserant@13: # If dir have already be made, and file exist Edouard@718: if os.path.isdir(self.CTNPath()) and os.path.isfile(self.ConfNodeXmlFilePath()): Edouard@717: #Load the confnode.xml file into parameters members etisserant@203: result = self.LoadXMLParams() lbessard@17: if result: lbessard@17: return result Edouard@718: #Load and init all the children Edouard@718: self.LoadChildren() Edouard@717: self.RefreshConfNodesBlockLists() etisserant@203: etisserant@203: if os.path.exists(self._getBuildPath()): etisserant@203: self.EnableMethod("_Clean", True) etisserant@203: etisserant@203: if os.path.isfile(self._getIECrawcodepath()): etisserant@203: self.ShowMethod("_showIECcode", True) etisserant@203: lbessard@17: return None lbessard@17: laurent@738: def RecursiveConfNodeInfos(self, confnode): laurent@738: values = [] laurent@738: for CTNChild in confnode.IECSortedChildren(): laurent@738: values.append( laurent@738: {"name": "%s: %s" % (CTNChild.GetFullIEC_Channel(), laurent@738: CTNChild.CTNName()), laurent@738: "type": ITEM_CONFNODE, laurent@738: "confnode": CTNChild, laurent@781: "icon": CTNChild.GetIconName(), laurent@738: "values": self.RecursiveConfNodeInfos(CTNChild)}) laurent@738: return values laurent@738: laurent@738: def GetProjectInfos(self): laurent@738: infos = PLCControler.GetProjectInfos(self) laurent@738: configurations = infos["values"].pop(-1) laurent@738: resources = None laurent@738: for config_infos in configurations["values"]: laurent@738: if resources is None: laurent@738: resources = config_infos["values"][0] laurent@738: else: laurent@738: resources["values"].extend(config_infos["values"][0]["values"]) laurent@738: if resources is not None: laurent@738: infos["values"].append(resources) laurent@738: infos["values"].extend(self.RecursiveConfNodeInfos(self)) laurent@738: return infos laurent@738: laurent@403: def CloseProject(self): Edouard@718: self.ClearChildren() laurent@417: self.ResetAppFrame(None) laurent@417: lbessard@17: def SaveProject(self): greg@427: if self.CheckProjectPathPerm(False): lbessard@41: self.SaveXMLFile(os.path.join(self.ProjectPath, 'plc.xml')) Edouard@718: result = self.CTNRequestSave() greg@427: if result: greg@427: self.logger.write_error(result) greg@427: greg@427: def SaveProjectAs(self, dosave=True): greg@427: # Ask user to choose a path with write permissions laurent@529: if wx.Platform == '__WXMSW__': laurent@529: path = os.getenv("USERPROFILE") laurent@529: else: laurent@529: path = os.getenv("HOME") laurent@529: dirdialog = wx.DirDialog(self.AppFrame , _("Choose a directory to save project"), path, wx.DD_NEW_DIR_BUTTON) greg@427: answer = dirdialog.ShowModal() greg@427: dirdialog.Destroy() greg@427: if answer == wx.ID_OK: greg@427: newprojectpath = dirdialog.GetPath() greg@427: if os.path.isdir(newprojectpath): greg@427: self.ProjectPath = newprojectpath greg@427: if dosave: greg@427: self.SaveProject() greg@427: self._setBuildPath(self.BuildPath) greg@427: return True greg@427: return False Edouard@728: Edouard@728: def GetLibrariesTypes(self): Edouard@728: self.LoadLibraries() Edouard@728: return [ lib.GetTypes() for lib in self.Libraries ] Edouard@728: Edouard@728: def GetLibrariesSTCode(self): Edouard@728: return "\n".join([ lib.GetSTCode() for lib in self.Libraries ]) Edouard@728: Edouard@728: def GetLibrariesCCode(self, buildpath): Edouard@770: if len(self.Libraries)==0: Edouard@770: return [],[],() Edouard@728: self.GetIECProgramsAndVariables() Edouard@728: LibIECCflags = '"-I%s"'%os.path.abspath(self.GetIECLibPath()) Edouard@728: LocatedCCodeAndFlags=[] Edouard@728: Extras=[] Edouard@728: for lib in self.Libraries: Edouard@728: res=lib.Generate_C(buildpath,self._VariablesList,LibIECCflags) Edouard@728: LocatedCCodeAndFlags.append(res[:2]) Edouard@728: if len(res)>2: Edouard@728: Extras.append(res[2:]) Edouard@758: return map(list,zip(*LocatedCCodeAndFlags))+[tuple(*Extras)] lbessard@17: Edouard@717: # Update PLCOpenEditor ConfNode Block types from loaded confnodes Edouard@717: def RefreshConfNodesBlockLists(self): Edouard@718: if getattr(self, "Children", None) is not None: Edouard@717: self.ClearConfNodeTypes() Edouard@728: self.AddConfNodeTypesList(self.GetLibrariesTypes()) laurent@395: if self.AppFrame is not None: laurent@716: self.AppFrame.RefreshLibraryPanel() laurent@395: self.AppFrame.RefreshEditor() lbessard@41: laurent@443: # Update a PLCOpenEditor Pou variable location laurent@443: def UpdateProjectVariableLocation(self, old_leading, new_leading): laurent@443: self.Project.updateElementAddress(old_leading, new_leading) laurent@443: self.BufferProject() laurent@443: if self.AppFrame is not None: laurent@468: self.AppFrame.RefreshTitle() laurent@730: self.AppFrame.RefreshPouInstanceVariablesPanel() laurent@468: self.AppFrame.RefreshFileMenu() laurent@468: self.AppFrame.RefreshEditMenu() laurent@443: self.AppFrame.RefreshEditor() laurent@443: laurent@401: def GetVariableLocationTree(self): laurent@411: ''' Edouard@717: This function is meant to be overridden by confnodes. laurent@411: laurent@411: It should returns an list of dictionaries laurent@411: laurent@411: - IEC_type is an IEC type like BOOL/BYTE/SINT/... laurent@411: - location is a string of this variable's location, like "%IX0.0.0" laurent@411: ''' laurent@411: children = [] Edouard@718: for child in self.IECSortedChildren(): laurent@411: children.append(child.GetVariableLocationTree()) laurent@411: return children laurent@401: Edouard@717: def ConfNodePath(self): Edouard@721: return os.path.split(__file__)[0] laurent@363: Edouard@718: def CTNPath(self, CTNName=None): etisserant@13: return self.ProjectPath lbessard@17: Edouard@718: def ConfNodeXmlFilePath(self, CTNName=None): Edouard@718: return os.path.join(self.CTNPath(CTNName), "beremiz.xml") etisserant@18: laurent@669: def ParentsTypesFactory(self): Edouard@717: return self.ConfNodeTypesFactory() laurent@363: greg@427: def _setBuildPath(self, buildpath): greg@427: if CheckPathPerm(buildpath): greg@427: self.BuildPath = buildpath greg@427: else: greg@427: self.BuildPath = None greg@427: self.BuildPath = buildpath greg@427: self.DefaultBuildPath = None greg@427: if self._builder is not None: greg@427: self._builder.SetBuildPath(self._getBuildPath()) greg@427: etisserant@20: def _getBuildPath(self): greg@427: # BuildPath is defined by user greg@427: if self.BuildPath is not None: greg@427: return self.BuildPath greg@427: # BuildPath isn't defined by user but already created by default greg@427: if self.DefaultBuildPath is not None: greg@427: return self.DefaultBuildPath greg@427: # Create a build path in project folder if user has permissions greg@427: if CheckPathPerm(self.ProjectPath): greg@427: self.DefaultBuildPath = os.path.join(self.ProjectPath, "build") greg@427: # Create a build path in temp folder greg@427: else: greg@427: self.DefaultBuildPath = os.path.join(tempfile.mkdtemp(), os.path.basename(self.ProjectPath), "build") greg@427: greg@427: if not os.path.exists(self.DefaultBuildPath): greg@427: os.makedirs(self.DefaultBuildPath) greg@427: return self.DefaultBuildPath etisserant@20: etisserant@203: def _getExtraFilesPath(self): etisserant@203: return os.path.join(self._getBuildPath(), "extra_files") etisserant@203: etisserant@20: def _getIECcodepath(self): etisserant@20: # define name for IEC code file etisserant@20: return os.path.join(self._getBuildPath(), "plc.st") etisserant@20: lbessard@65: def _getIECgeneratedcodepath(self): lbessard@65: # define name for IEC generated code file lbessard@65: return os.path.join(self._getBuildPath(), "generated_plc.st") lbessard@65: lbessard@65: def _getIECrawcodepath(self): lbessard@65: # define name for IEC raw code file Edouard@718: return os.path.join(self.CTNPath(), "raw_plc.st") lbessard@65: lbessard@97: def GetLocations(self): lbessard@97: locations = [] lbessard@97: filepath = os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h") lbessard@97: if os.path.isfile(filepath): lbessard@97: # IEC2C compiler generate a list of located variables : LOCATED_VARIABLES.h lbessard@97: location_file = open(os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h")) lbessard@97: # each line of LOCATED_VARIABLES.h declares a located variable lbessard@97: lines = [line.strip() for line in location_file.readlines()] lbessard@97: # This regular expression parses the lines genereated by IEC2C lbessard@348: LOCATED_MODEL = re.compile("__LOCATED_VAR\((?P[A-Z]*),(?P[_A-Za-z0-9]*),(?P[QMI])(?:,(?P[XBWDL]))?,(?P[,0-9]*)\)") lbessard@97: for line in lines: lbessard@97: # If line match RE, lbessard@97: result = LOCATED_MODEL.match(line) lbessard@97: if result: lbessard@97: # Get the resulting dict lbessard@97: resdict = result.groupdict() lbessard@97: # rewrite string for variadic location as a tuple of integers lbessard@97: resdict['LOC'] = tuple(map(int,resdict['LOC'].split(','))) lbessard@97: # set located size to 'X' if not given lbessard@97: if not resdict['SIZE']: lbessard@97: resdict['SIZE'] = 'X' lbessard@97: # finally store into located variable list lbessard@97: locations.append(resdict) lbessard@97: return locations lbessard@97: etisserant@203: def _Generate_SoftPLC(self): etisserant@20: """ lbessard@64: Generate SoftPLC ST/IL/SFC code out of PLCOpenEditor controller, and compile it with IEC2C etisserant@20: @param buildpath: path where files should be created etisserant@20: """ etisserant@20: Edouard@717: # Update PLCOpenEditor ConfNode Block types before generate ST code Edouard@717: self.RefreshConfNodesBlockLists() lbessard@41: laurent@361: self.logger.write(_("Generating SoftPLC IEC-61131 ST/IL/SFC code...\n")) etisserant@20: buildpath = self._getBuildPath() etisserant@20: # ask PLCOpenEditor controller to write ST/IL/SFC code file lbessard@309: program, errors, warnings = self.GenerateProgram(self._getIECgeneratedcodepath()) lbessard@309: if len(warnings) > 0: laurent@361: self.logger.write_warning(_("Warnings in ST/IL/SFC code generator :\n")) lbessard@309: for warning in warnings: lbessard@309: self.logger.write_warning("%s\n"%warning) lbessard@309: if len(errors) > 0: etisserant@20: # Failed ! laurent@361: self.logger.write_error(_("Error in ST/IL/SFC code generator :\n%s\n")%errors[0]) etisserant@20: return False lbessard@65: plc_file = open(self._getIECcodepath(), "w") Edouard@717: # Add ST Library from confnodes Edouard@728: plc_file.write(self.GetLibrariesSTCode()) lbessard@65: if os.path.isfile(self._getIECrawcodepath()): lbessard@65: plc_file.write(open(self._getIECrawcodepath(), "r").read()) lbessard@65: plc_file.write("\n") lbessard@356: plc_file.close() lbessard@356: plc_file = open(self._getIECcodepath(), "r") lbessard@356: self.ProgramOffset = 0 lbessard@356: for line in plc_file.xreadlines(): lbessard@356: self.ProgramOffset += 1 lbessard@356: plc_file.close() lbessard@356: plc_file = open(self._getIECcodepath(), "a") lbessard@65: plc_file.write(open(self._getIECgeneratedcodepath(), "r").read()) lbessard@65: plc_file.close() laurent@415: laurent@415: self.logger.write(_("Compiling IEC Program into C code...\n")) laurent@415: etisserant@20: # Now compile IEC code into many C files etisserant@20: # files are listed to stdout, and errors to stderr. etisserant@110: status, result, err_result = ProcessLogger( etisserant@203: self.logger, lbessard@351: "\"%s\" -f -I \"%s\" -T \"%s\" \"%s\""%( greg@418: self.iec2c_path, greg@418: self.ieclib_path, lbessard@351: buildpath, lbessard@351: self._getIECcodepath()), lbessard@356: no_stdout=True, no_stderr=True).spin() etisserant@20: if status: etisserant@20: # Failed ! lbessard@356: lbessard@356: # parse iec2c's error message. if it contains a line number, lbessard@356: # then print those lines from the generated IEC file. lbessard@356: for err_line in err_result.split('\n'): lbessard@356: self.logger.write_warning(err_line + "\n") lbessard@356: lbessard@356: m_result = MATIEC_ERROR_MODEL.match(err_line) lbessard@356: if m_result is not None: lbessard@356: first_line, first_column, last_line, last_column, error = m_result.groups() lbessard@356: first_line, last_line = int(first_line), int(last_line) lbessard@356: lbessard@356: last_section = None lbessard@356: f = open(self._getIECcodepath()) lbessard@356: lbessard@356: for i, line in enumerate(f.readlines()): laurent@661: i = i + 1 lbessard@356: if line[0] not in '\t \r\n': lbessard@356: last_section = line lbessard@356: lbessard@356: if first_line <= i <= last_line: lbessard@356: if last_section is not None: lbessard@356: self.logger.write_warning("In section: " + last_section) lbessard@356: last_section = None # only write section once lbessard@356: self.logger.write_warning("%04d: %s" % (i, line)) lbessard@356: lbessard@356: f.close() lbessard@356: laurent@361: self.logger.write_error(_("Error : IEC to C compiler returned %d\n")%status) etisserant@20: return False lbessard@356: etisserant@20: # Now extract C files of stdout etisserant@113: C_files = [ fname for fname in result.splitlines() if fname[-2:]==".c" or fname[-2:]==".C" ] etisserant@20: # remove those that are not to be compiled because included by others etisserant@20: C_files.remove("POUS.c") etisserant@115: if not C_files: laurent@415: self.logger.write_error(_("Error : At least one configuration and one resource must be declared in PLC !\n")) etisserant@115: return False etisserant@20: # transform those base names to full names with path etisserant@23: C_files = map(lambda filename:os.path.join(buildpath, filename), C_files) laurent@361: self.logger.write(_("Extracting Located Variables...\n")) lbessard@97: # Keep track of generated located variables for later use by self._Generate_C lbessard@97: self.PLCGeneratedLocatedVars = self.GetLocations() Edouard@718: # Keep track of generated C files for later use by self.CTNGenerate_C etisserant@18: self.PLCGeneratedCFiles = C_files etisserant@49: # compute CFLAGS for plc greg@418: self.plcCFLAGS = "\"-I"+self.ieclib_path+"\"" etisserant@18: return True etisserant@18: etisserant@203: def GetBuilder(self): etisserant@203: """ etisserant@203: Return a Builder (compile C code into machine code) etisserant@203: """ etisserant@203: # Get target, module and class name laurent@510: targetname = self.GetTarget().getcontent()["name"] Edouard@733: targetclass = targets.GetBuilder(targetname) etisserant@203: etisserant@203: # if target already etisserant@203: if self._builder is None or not isinstance(self._builder,targetclass): etisserant@203: # Get classname instance etisserant@203: self._builder = targetclass(self) etisserant@203: return self._builder etisserant@203: laurent@677: def ResetBuildMD5(self): laurent@677: builder=self.GetBuilder() laurent@677: if builder is not None: laurent@677: builder.ResetBinaryCodeMD5() laurent@677: self.EnableMethod("_Transfer", False) laurent@677: etisserant@203: def GetLastBuildMD5(self): etisserant@203: builder=self.GetBuilder() etisserant@203: if builder is not None: etisserant@203: return builder.GetBinaryCodeMD5() etisserant@203: else: etisserant@203: return None etisserant@203: etisserant@203: ####################################################################### etisserant@203: # etisserant@203: # C CODE GENERATION METHODS etisserant@203: # etisserant@203: ####################################################################### etisserant@203: Edouard@718: def CTNGenerate_C(self, buildpath, locations): etisserant@203: """ etisserant@203: Return C code generated by iec2c compiler etisserant@203: when _generate_softPLC have been called etisserant@203: @param locations: ignored etisserant@203: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@203: """ etisserant@283: laurent@366: return ([(C_file_name, self.plcCFLAGS) etisserant@283: for C_file_name in self.PLCGeneratedCFiles ], etisserant@283: "", # no ldflags etisserant@283: False) # do not expose retreive/publish calls etisserant@203: etisserant@203: def ResetIECProgramsAndVariables(self): etisserant@203: """ etisserant@203: Reset variable and program list that are parsed from etisserant@203: CSV file generated by IEC2C compiler. etisserant@203: """ etisserant@203: self._ProgramList = None etisserant@203: self._VariablesList = None edouard@532: self._IECPathToIdx = {} laurent@670: self._Ticktime = 0 etisserant@235: self.TracedIECPath = [] etisserant@235: etisserant@203: def GetIECProgramsAndVariables(self): etisserant@203: """ etisserant@203: Parse CSV-like file VARIABLES.csv resulting from IEC2C compiler. etisserant@203: Each section is marked with a line staring with '//' etisserant@203: list of all variables used in various POUs etisserant@203: """ etisserant@203: if self._ProgramList is None or self._VariablesList is None: etisserant@203: try: etisserant@203: csvfile = os.path.join(self._getBuildPath(),"VARIABLES.csv") etisserant@203: # describes CSV columns etisserant@203: ProgramsListAttributeName = ["num", "C_path", "type"] etisserant@203: VariablesListAttributeName = ["num", "vartype", "IEC_path", "C_path", "type"] etisserant@203: self._ProgramList = [] etisserant@203: self._VariablesList = [] etisserant@203: self._IECPathToIdx = {} etisserant@203: etisserant@203: # Separate sections etisserant@203: ListGroup = [] etisserant@203: for line in open(csvfile,'r').xreadlines(): etisserant@203: strippedline = line.strip() etisserant@203: if strippedline.startswith("//"): etisserant@203: # Start new section etisserant@203: ListGroup.append([]) etisserant@203: elif len(strippedline) > 0 and len(ListGroup) > 0: etisserant@203: # append to this section etisserant@203: ListGroup[-1].append(strippedline) etisserant@203: etisserant@203: # first section contains programs etisserant@203: for line in ListGroup[0]: etisserant@203: # Split and Maps each field to dictionnary entries etisserant@203: attrs = dict(zip(ProgramsListAttributeName,line.strip().split(';'))) etisserant@203: # Truncate "C_path" to remove conf an ressources names etisserant@203: attrs["C_path"] = '__'.join(attrs["C_path"].split(".",2)[1:]) etisserant@203: # Push this dictionnary into result. etisserant@203: self._ProgramList.append(attrs) etisserant@203: etisserant@203: # second section contains all variables etisserant@203: for line in ListGroup[1]: etisserant@203: # Split and Maps each field to dictionnary entries etisserant@203: attrs = dict(zip(VariablesListAttributeName,line.strip().split(';'))) etisserant@203: # Truncate "C_path" to remove conf an ressources names laurent@639: parts = attrs["C_path"].split(".",2) laurent@639: if len(parts) > 2: laurent@639: attrs["C_path"] = '__'.join(parts[1:]) laurent@639: else: laurent@639: attrs["C_path"] = '__'.join(parts) etisserant@203: # Push this dictionnary into result. etisserant@203: self._VariablesList.append(attrs) etisserant@203: # Fill in IEC<->C translation dicts etisserant@203: IEC_path=attrs["IEC_path"] etisserant@203: Idx=int(attrs["num"]) edouard@450: self._IECPathToIdx[IEC_path]=(Idx, attrs["type"]) laurent@670: laurent@670: # third section contains ticktime laurent@670: if len(ListGroup) > 2: laurent@670: self._Ticktime = int(ListGroup[2][0]) laurent@670: etisserant@203: except Exception,e: laurent@361: self.logger.write_error(_("Cannot open/parse VARIABLES.csv!\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: self.ResetIECProgramsAndVariables() etisserant@203: return False etisserant@203: etisserant@203: return True etisserant@203: etisserant@203: def Generate_plc_debugger(self): etisserant@203: """ etisserant@203: Generate trace/debug code out of PLC variable list etisserant@203: """ etisserant@203: self.GetIECProgramsAndVariables() etisserant@203: etisserant@203: # prepare debug code Edouard@733: debug_code = targets.GetCode("plc_debug") % { greg@335: "buffer_size": reduce(lambda x, y: x + y, [DebugTypesSize.get(v["type"], 0) for v in self._VariablesList], 0), etisserant@203: "programs_declarations": etisserant@203: "\n".join(["extern %(type)s %(C_path)s;"%p for p in self._ProgramList]), etisserant@203: "extern_variables_declarations":"\n".join([ laurent@506: {"EXT":"extern __IEC_%(type)s_p %(C_path)s;", laurent@506: "IN":"extern __IEC_%(type)s_p %(C_path)s;", laurent@601: "MEM":"extern __IEC_%(type)s_p %(C_path)s;", laurent@506: "OUT":"extern __IEC_%(type)s_p %(C_path)s;", laurent@463: "VAR":"extern __IEC_%(type)s_t %(C_path)s;"}[v["vartype"]]%v lbessard@275: for v in self._VariablesList if v["vartype"] != "FB" and v["C_path"].find('.')<0]), edouard@450: "for_each_variable_do_code":"\n".join([ laurent@506: {"EXT":" (*fp)((void*)&%(C_path)s,%(type)s_P_ENUM);\n", laurent@506: "IN":" (*fp)((void*)&%(C_path)s,%(type)s_P_ENUM);\n", Edouard@604: "MEM":" (*fp)((void*)&%(C_path)s,%(type)s_O_ENUM);\n", laurent@511: "OUT":" (*fp)((void*)&%(C_path)s,%(type)s_O_ENUM);\n", edouard@450: "VAR":" (*fp)((void*)&%(C_path)s,%(type)s_ENUM);\n"}[v["vartype"]]%v Edouard@592: for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ]), edouard@450: "find_variable_case_code":"\n".join([ edouard@450: " case %(num)s:\n"%v+ edouard@458: " *varp = (void*)&%(C_path)s;\n"%v+ laurent@506: {"EXT":" return %(type)s_P_ENUM;\n", laurent@506: "IN":" return %(type)s_P_ENUM;\n", Edouard@604: "MEM":" return %(type)s_O_ENUM;\n", laurent@511: "OUT":" return %(type)s_O_ENUM;\n", edouard@450: "VAR":" return %(type)s_ENUM;\n"}[v["vartype"]]%v Edouard@592: for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ])} etisserant@203: etisserant@203: return debug_code etisserant@203: etisserant@203: def Generate_plc_common_main(self): etisserant@203: """ Edouard@717: Use confnodes layout given in LocationCFilesAndCFLAGS to Edouard@717: generate glue code that dispatch calls to all confnodes etisserant@203: """ etisserant@203: # filter location that are related to code that will be called etisserant@203: # in retreive, publish, init, cleanup etisserant@203: locstrs = map(lambda x:"_".join(map(str,x)), etisserant@203: [loc for loc,Cfiles,DoCalls in self.LocationCFilesAndCFLAGS if loc and DoCalls]) etisserant@203: etisserant@203: # Generate main, based on template Edouard@728: if not self.BeremizRoot.getDisable_Extensions(): Edouard@733: plc_main_code = targets.GetCode("plc_common_main") % { greg@338: "calls_prototypes":"\n".join([( greg@338: "int __init_%(s)s(int argc,char **argv);\n"+ greg@418: "void __cleanup_%(s)s(void);\n"+ greg@418: "void __retrieve_%(s)s(void);\n"+ greg@418: "void __publish_%(s)s(void);")%{'s':locstr} for locstr in locstrs]), greg@338: "retrieve_calls":"\n ".join([ laurent@694: "__retrieve_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)]), greg@338: "publish_calls":"\n ".join([ #Call publish in reverse order laurent@694: "__publish_%s();"%locstr for locstr in locstrs]), greg@338: "init_calls":"\n ".join([ greg@338: "init_level=%d; "%(i+1)+ greg@423: "if((res = __init_%s(argc,argv))){"%locstr + greg@338: #"printf(\"%s\"); "%locstr + #for debug greg@338: "return res;}" for i,locstr in enumerate(locstrs)]), greg@338: "cleanup_calls":"\n ".join([ greg@338: "if(init_level >= %d) "%i+ greg@338: "__cleanup_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)]) greg@338: } greg@338: else: Edouard@733: plc_main_code = targets.GetCode("plc_common_main") % { greg@338: "calls_prototypes":"\n", greg@338: "retrieve_calls":"\n", greg@338: "publish_calls":"\n", greg@338: "init_calls":"\n", greg@338: "cleanup_calls":"\n" greg@338: } Edouard@733: plc_main_code += targets.GetTargetCode(self.GetTarget().getcontent()["name"]) etisserant@203: return plc_main_code etisserant@203: etisserant@203: Edouard@623: def _Build(self): etisserant@20: """ Edouard@717: Method called by user to (re)build SoftPLC and confnode tree etisserant@20: """ laurent@395: if self.AppFrame is not None: laurent@395: self.AppFrame.ClearErrors() laurent@774: self._CloseView(self._IECCodeView) lbessard@202: etisserant@20: buildpath = self._getBuildPath() etisserant@20: etisserant@20: # Eventually create build dir etisserant@18: if not os.path.exists(buildpath): etisserant@18: os.mkdir(buildpath) etisserant@203: # There is something to clean etisserant@110: self.EnableMethod("_Clean", True) etisserant@203: etisserant@203: self.logger.flush() laurent@361: self.logger.write(_("Start build in %s\n") % buildpath) etisserant@203: etisserant@203: # Generate SoftPLC IEC code etisserant@203: IECGenRes = self._Generate_SoftPLC() etisserant@203: self.ShowMethod("_showIECcode", True) etisserant@203: etisserant@203: # If IEC code gen fail, bail out. etisserant@203: if not IECGenRes: laurent@361: self.logger.write_error(_("IEC-61131-3 code generation failed !\n")) laurent@677: self.ResetBuildMD5() etisserant@20: return False etisserant@20: etisserant@203: # Reset variable and program list that are parsed from etisserant@203: # CSV file generated by IEC2C compiler. etisserant@203: self.ResetIECProgramsAndVariables() etisserant@18: Edouard@717: # Generate C code and compilation params from confnode hierarchy etisserant@24: try: Edouard@728: CTNLocationCFilesAndCFLAGS, CTNLDFLAGS, CTNExtraFiles = self._Generate_C( etisserant@24: buildpath, etisserant@203: self.PLCGeneratedLocatedVars) etisserant@178: except Exception, exc: Edouard@721: self.logger.write_error(_("Runtime extensions C code generation failed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) laurent@677: self.ResetBuildMD5() etisserant@24: return False etisserant@18: Edouard@728: # Generate C code and compilation params from liraries Edouard@728: try: Edouard@728: LibCFilesAndCFLAGS, LibLDFLAGS, LibExtraFiles = self.GetLibrariesCCode(buildpath) Edouard@728: except Exception, exc: Edouard@728: self.logger.write_error(_("Runtime extensions C code generation failed !\n")) Edouard@728: self.logger.write_error(traceback.format_exc()) Edouard@728: self.ResetBuildMD5() Edouard@728: return False Edouard@728: Edouard@728: self.LocationCFilesAndCFLAGS = CTNLocationCFilesAndCFLAGS + LibCFilesAndCFLAGS Edouard@728: self.LDFLAGS = CTNLDFLAGS + LibLDFLAGS Edouard@728: ExtraFiles = CTNExtraFiles + LibExtraFiles Edouard@728: laurent@361: # Get temporary directory path etisserant@203: extrafilespath = self._getExtraFilesPath() etisserant@203: # Remove old directory etisserant@203: if os.path.exists(extrafilespath): etisserant@203: shutil.rmtree(extrafilespath) etisserant@203: # Recreate directory etisserant@203: os.mkdir(extrafilespath) etisserant@203: # Then write the files etisserant@203: for fname,fobject in ExtraFiles: etisserant@203: fpath = os.path.join(extrafilespath,fname) etisserant@203: open(fpath, "wb").write(fobject.read()) etisserant@203: # Now we can forget ExtraFiles (will close files object) etisserant@203: del ExtraFiles laurent@510: etisserant@203: # Template based part of C code generation Edouard@717: # files are stacked at the beginning, as files of confnode tree root etisserant@203: for generator, filename, name in [ etisserant@203: # debugger code etisserant@203: (self.Generate_plc_debugger, "plc_debugger.c", "Debugger"), etisserant@203: # init/cleanup/retrieve/publish, run and align code etisserant@203: (self.Generate_plc_common_main,"plc_common_main.c","Common runtime")]: etisserant@203: try: etisserant@203: # Do generate etisserant@203: code = generator() greg@335: if code is None: greg@335: raise etisserant@203: code_path = os.path.join(buildpath,filename) etisserant@203: open(code_path, "w").write(code) Edouard@717: # Insert this file as first file to be compiled at root confnode etisserant@203: self.LocationCFilesAndCFLAGS[0][1].insert(0,(code_path, self.plcCFLAGS)) etisserant@203: except Exception, exc: laurent@361: self.logger.write_error(name+_(" generation failed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) laurent@677: self.ResetBuildMD5() etisserant@203: return False etisserant@203: laurent@361: self.logger.write(_("C code generated successfully.\n")) etisserant@203: etisserant@203: # Get current or fresh builder etisserant@203: builder = self.GetBuilder() etisserant@203: if builder is None: laurent@361: self.logger.write_error(_("Fatal : cannot get builder.\n")) laurent@677: self.ResetBuildMD5() etisserant@51: return False etisserant@203: etisserant@203: # Build etisserant@203: try: etisserant@203: if not builder.build() : laurent@361: self.logger.write_error(_("C Build failed.\n")) etisserant@203: return False etisserant@203: except Exception, exc: laurent@361: self.logger.write_error(_("C Build crashed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) laurent@677: self.ResetBuildMD5() etisserant@203: return False etisserant@203: Edouard@624: self.logger.write(_("Successfully built.\n")) etisserant@203: # Update GUI status about need for transfer etisserant@203: self.CompareLocalAndRemotePLC() etisserant@49: return True lbessard@202: lbessard@202: def ShowError(self, logger, from_location, to_location): lbessard@202: chunk_infos = self.GetChunkInfos(from_location, to_location) lbessard@202: for infos, (start_row, start_col) in chunk_infos: lbessard@202: start = (from_location[0] - start_row, from_location[1] - start_col) lbessard@202: end = (to_location[0] - start_row, to_location[1] - start_col) laurent@707: #print from_location, to_location, start_row, start_col, start, end laurent@396: if self.AppFrame is not None: laurent@396: self.AppFrame.ShowError(infos, start, end) laurent@774: laurent@774: _IECCodeView = None etisserant@203: def _showIECcode(self): laurent@716: self._OpenView("IEC code") laurent@716: laurent@774: _IECRawCodeView = None laurent@716: def _editIECrawcode(self): laurent@716: self._OpenView("IEC raw code") laurent@782: laurent@782: _ProjectFilesView = None laurent@782: def _OpenProjectFiles(self): laurent@782: self._OpenView("Project files") laurent@782: laurent@784: _FileEditors = {} laurent@784: def _OpenFileEditor(self, filepath): laurent@784: self._OpenView(filepath) laurent@784: laurent@782: def _OpenView(self, name=None, onlyopened=False): laurent@716: if name == "IEC code": laurent@782: if self._IECCodeView is None: laurent@774: plc_file = self._getIECcodepath() laurent@774: laurent@806: self._IECCodeView = IECCodeViewer(self.AppFrame.TabsOpened, "", None, None, instancepath=name) laurent@774: self._IECCodeView.SetTextSyntax("ALL") laurent@774: self._IECCodeView.SetKeywords(IEC_KEYWORDS) laurent@774: try: laurent@774: text = file(plc_file).read() laurent@774: except: laurent@774: text = '(* No IEC code have been generated at that time ! *)' laurent@774: self._IECCodeView.SetText(text = text) laurent@782: self._IECCodeView.SetIcon(GetBitmap("ST")) laurent@806: setattr(self._IECCodeView, "_OnClose", self.OnCloseEditor) laurent@782: laurent@784: if self._IECCodeView is not None: laurent@774: self.AppFrame.EditProjectElement(self._IECCodeView, name) laurent@782: laurent@774: return self._IECCodeView laurent@716: laurent@716: elif name == "IEC raw code": laurent@782: if self._IECRawCodeView is None: laurent@806: controler = MiniTextControler(self._getIECrawcodepath(), self) laurent@716: laurent@806: self._IECRawCodeView = IECCodeViewer(self.AppFrame.TabsOpened, "", None, controler, instancepath=name) laurent@782: self._IECRawCodeView.SetTextSyntax("ALL") laurent@782: self._IECRawCodeView.SetKeywords(IEC_KEYWORDS) laurent@782: self._IECRawCodeView.RefreshView() laurent@782: self._IECRawCodeView.SetIcon(GetBitmap("ST")) laurent@806: setattr(self._IECRawCodeView, "_OnClose", self.OnCloseEditor) laurent@784: laurent@784: if self._IECRawCodeView is not None: laurent@782: self.AppFrame.EditProjectElement(self._IECRawCodeView, name) laurent@782: laurent@782: return self._IECRawCodeView laurent@782: laurent@782: elif name == "Project files": laurent@782: if self._ProjectFilesView is None: laurent@782: self._ProjectFilesView = FileManagementPanel(self.AppFrame.TabsOpened, self, name, self._getProjectFilesPath(), True) laurent@782: laurent@784: extensions = [] laurent@784: for extension, name, editor in features.file_editors: laurent@784: if extension not in extensions: laurent@784: extensions.append(extension) laurent@784: self._ProjectFilesView.SetEditableFileExtensions(extensions) laurent@784: laurent@784: if self._ProjectFilesView is not None: laurent@782: self.AppFrame.EditProjectElement(self._ProjectFilesView, name) laurent@782: laurent@784: return self._ProjectFilesView laurent@784: laurent@789: elif name is not None and name.find("::") != -1: laurent@789: filepath, editor_name = name.split("::") laurent@789: if not self._FileEditors.has_key(filepath): laurent@789: if os.path.isfile(filepath): laurent@789: file_extension = os.path.splitext(filepath)[1] laurent@789: laurent@796: editors = dict([(edit_name, edit_class) laurent@796: for extension, edit_name, edit_class in features.file_editors laurent@789: if extension == file_extension]) laurent@789: laurent@789: if editor_name == "": laurent@789: if len(editors) == 1: laurent@789: editor_name = editors.keys()[0] laurent@789: elif len(editors) > 0: laurent@789: names = editors.keys() laurent@789: dialog = wx.SingleChoiceDialog(self.AppFrame, laurent@789: _("Select an editor:"), _("Editor selection"), laurent@789: names, wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL) laurent@789: if dialog.ShowModal() == wx.ID_OK: laurent@789: editor_name = names[dialog.GetSelection()] laurent@789: dialog.Destroy() laurent@789: laurent@789: if editor_name != "": laurent@789: name = "::".join([filepath, editor_name]) laurent@789: laurent@789: editor = editors[editor_name]() laurent@789: self._FileEditors[filepath] = editor(self.AppFrame.TabsOpened, self, name, self.AppFrame) laurent@789: self._FileEditors[filepath].SetIcon(GetBitmap("FILE")) laurent@789: laurent@789: if self._FileEditors.has_key(filepath): laurent@789: editor = self._FileEditors[filepath] laurent@789: self.AppFrame.EditProjectElement(editor, editor.GetTagName()) laurent@784: laurent@789: return self._FileEditors.get(filepath) laurent@738: else: laurent@786: return ConfigTreeNode._OpenView(self, self.CTNName(), onlyopened) etisserant@20: laurent@774: def OnCloseEditor(self, view): laurent@774: ConfigTreeNode.OnCloseEditor(self, view) laurent@774: if self._IECCodeView == view: laurent@774: self._IECCodeView = None laurent@774: if self._IECRawCodeView == view: laurent@774: self._IECRawCodeView = None laurent@782: if self._ProjectFilesView == view: laurent@782: self._ProjectFilesView = None laurent@784: if view in self._FileEditors.values(): laurent@792: self._FileEditors.pop(view.GetFilePath()) laurent@774: etisserant@203: def _Clean(self): laurent@774: self._CloseView(self._IECCodeView) greg@108: if os.path.isdir(os.path.join(self._getBuildPath())): laurent@361: self.logger.write(_("Cleaning the build directory\n")) greg@108: shutil.rmtree(os.path.join(self._getBuildPath())) greg@108: else: laurent@361: self.logger.write_error(_("Build directory already clean\n")) etisserant@203: self.ShowMethod("_showIECcode", False) etisserant@110: self.EnableMethod("_Clean", False) etisserant@286: # kill the builder etisserant@286: self._builder = None etisserant@203: self.CompareLocalAndRemotePLC() etisserant@203: etisserant@203: ############# Real PLC object access ############# etisserant@203: def UpdateMethodsFromPLCStatus(self): etisserant@203: # Get PLC state : Running or Stopped etisserant@203: # TODO : use explicit status instead of boolean laurent@486: status = None etisserant@203: if self._connector is not None: etisserant@203: status = self._connector.GetPLCstatus() laurent@486: if status is None: laurent@516: self._connector = None etisserant@203: status = "Disconnected" ed@446: if(self.previous_plcstate != status): ed@446: for args in { ed@446: "Started" : [("_Run", False), ed@446: ("_Stop", True)], ed@446: "Stopped" : [("_Run", True), ed@446: ("_Stop", False)], ed@446: "Empty" : [("_Run", False), ed@446: ("_Stop", False)], ed@446: "Broken" : [], ed@446: "Disconnected" :[("_Run", False), ed@446: ("_Stop", False), ed@446: ("_Transfer", False), ed@446: ("_Connect", True), ed@446: ("_Disconnect", False)], ed@446: }.get(status,[]): ed@446: self.ShowMethod(*args) ed@446: self.previous_plcstate = status ed@446: return True ed@446: return False ed@446: ed@446: def PullPLCStatusProc(self, event): greg@355: if self._connector is None: greg@355: self.StatusTimer.Stop() ed@446: if self.UpdateMethodsFromPLCStatus(): laurent@486: ed@446: status = _(self.previous_plcstate) ed@446: {"Broken": self.logger.write_error, ed@446: None: lambda x: None}.get( ed@446: self.previous_plcstate, self.logger.write)(_("PLC is %s\n")%status) laurent@738: self.AppFrame.RefreshStatusToolBar() greg@355: etisserant@239: def RegisterDebugVarToConnector(self): etisserant@239: self.DebugTimer=None etisserant@239: Idxs = [] etisserant@239: self.TracedIECPath = [] etisserant@239: if self._connector is not None: etisserant@239: self.IECdebug_lock.acquire() etisserant@239: IECPathsToPop = [] etisserant@239: for IECPath,data_tuple in self.IECdebug_datas.iteritems(): laurent@474: WeakCallableDict, data_log, status, fvalue = data_tuple etisserant@239: if len(WeakCallableDict) == 0: etisserant@239: # Callable Dict is empty. etisserant@239: # This variable is not needed anymore! etisserant@239: #print "Unused : " + IECPath etisserant@239: IECPathsToPop.append(IECPath) greg@355: elif IECPath != "__tick__": etisserant@239: # Convert edouard@450: Idx, IEC_Type = self._IECPathToIdx.get(IECPath,(None,None)) etisserant@239: if Idx is not None: Edouard@592: if IEC_Type in DebugTypesSize: Edouard@592: Idxs.append((Idx, IEC_Type, fvalue, IECPath)) Edouard@592: else: laurent@801: self.logger.write_warning(_("Debug: Unsupported type to debug '%s'\n")%IEC_Type) etisserant@239: else: laurent@801: self.logger.write_warning(_("Debug: Unknown variable '%s'\n")%IECPath) etisserant@239: for IECPathToPop in IECPathsToPop: etisserant@239: self.IECdebug_datas.pop(IECPathToPop) etisserant@239: ed@457: if Idxs: ed@457: Idxs.sort() laurent@475: self.TracedIECPath = zip(*Idxs)[3] laurent@474: self._connector.SetTraceVariablesList(zip(*zip(*Idxs)[0:3])) edouard@465: else: edouard@465: self.TracedIECPath = [] edouard@465: self._connector.SetTraceVariablesList([]) etisserant@239: self.IECdebug_lock.release() lbessard@243: lbessard@243: #for IEC_path, IECdebug_data in self.IECdebug_datas.iteritems(): lbessard@243: # print IEC_path, IECdebug_data[0].keys() lbessard@243: lbessard@243: def ReArmDebugRegisterTimer(self): lbessard@243: if self.DebugTimer is not None: lbessard@243: self.DebugTimer.cancel() lbessard@243: edouard@466: # Timer to prevent rapid-fire when registering many variables edouard@466: # use wx.CallAfter use keep using same thread. TODO : use wx.Timer instead edouard@466: self.DebugTimer=Timer(0.5,wx.CallAfter,args = [self.RegisterDebugVarToConnector]) edouard@466: # Rearm anti-rapid-fire timer edouard@466: self.DebugTimer.start() lbessard@243: laurent@463: def GetDebugIECVariableType(self, IECPath): laurent@463: Idx, IEC_Type = self._IECPathToIdx.get(IECPath,(None,None)) laurent@463: return IEC_Type etisserant@239: etisserant@239: def SubscribeDebugIECVariable(self, IECPath, callableobj, *args, **kwargs): etisserant@239: """ etisserant@239: Dispatching use a dictionnary linking IEC variable paths etisserant@239: to a WeakKeyDictionary linking etisserant@239: weakly referenced callables to optionnal args etisserant@239: """ edouard@450: if IECPath != "__tick__" and not self._IECPathToIdx.has_key(IECPath): lbessard@246: return None lbessard@246: etisserant@239: self.IECdebug_lock.acquire() etisserant@239: # If no entry exist, create a new one with a fresh WeakKeyDictionary etisserant@239: IECdebug_data = self.IECdebug_datas.get(IECPath, None) etisserant@239: if IECdebug_data is None: etisserant@239: IECdebug_data = [ etisserant@239: WeakKeyDictionary(), # Callables etisserant@239: [], # Data storage [(tick, data),...] laurent@474: "Registered", # Variable status laurent@474: None] # Forced value etisserant@239: self.IECdebug_datas[IECPath] = IECdebug_data etisserant@239: etisserant@239: IECdebug_data[0][callableobj]=(args, kwargs) etisserant@239: etisserant@239: self.IECdebug_lock.release() lbessard@243: lbessard@243: self.ReArmDebugRegisterTimer() lbessard@243: etisserant@239: return IECdebug_data[1] etisserant@239: etisserant@239: def UnsubscribeDebugIECVariable(self, IECPath, callableobj): lbessard@243: #print "Unsubscribe", IECPath, callableobj lbessard@243: self.IECdebug_lock.acquire() etisserant@239: IECdebug_data = self.IECdebug_datas.get(IECPath, None) lbessard@243: if IECdebug_data is not None: etisserant@239: IECdebug_data[0].pop(callableobj,None) lbessard@243: self.IECdebug_lock.release() lbessard@243: lbessard@243: self.ReArmDebugRegisterTimer() etisserant@239: lbessard@334: def UnsubscribeAllDebugIECVariable(self): lbessard@334: self.IECdebug_lock.acquire() lbessard@334: IECdebug_data = {} lbessard@334: self.IECdebug_lock.release() lbessard@334: greg@355: self.ReArmDebugRegisterTimer() greg@355: laurent@474: def ForceDebugIECVariable(self, IECPath, fvalue): laurent@474: if not self.IECdebug_datas.has_key(IECPath): laurent@474: return laurent@474: laurent@474: self.IECdebug_lock.acquire() laurent@474: laurent@474: # If no entry exist, create a new one with a fresh WeakKeyDictionary laurent@474: IECdebug_data = self.IECdebug_datas.get(IECPath, None) laurent@474: IECdebug_data[2] = "Forced" laurent@474: IECdebug_data[3] = fvalue laurent@474: laurent@474: self.IECdebug_lock.release() laurent@474: laurent@474: self.ReArmDebugRegisterTimer() laurent@463: laurent@463: def ReleaseDebugIECVariable(self, IECPath): laurent@474: if not self.IECdebug_datas.has_key(IECPath): laurent@474: return laurent@474: laurent@474: self.IECdebug_lock.acquire() laurent@474: laurent@474: # If no entry exist, create a new one with a fresh WeakKeyDictionary laurent@474: IECdebug_data = self.IECdebug_datas.get(IECPath, None) laurent@474: IECdebug_data[2] = "Registered" laurent@474: IECdebug_data[3] = None laurent@474: laurent@474: self.IECdebug_lock.release() laurent@474: laurent@474: self.ReArmDebugRegisterTimer() laurent@474: greg@355: def CallWeakcallables(self, IECPath, function_name, *cargs): greg@355: data_tuple = self.IECdebug_datas.get(IECPath, None) greg@355: if data_tuple is not None: laurent@474: WeakCallableDict, data_log, status, fvalue = data_tuple greg@355: #data_log.append((debug_tick, value)) greg@355: for weakcallable,(args,kwargs) in WeakCallableDict.iteritems(): greg@355: #print weakcallable, value, args, kwargs greg@355: function = getattr(weakcallable, function_name, None) greg@355: if function is not None: laurent@481: if status == "Forced" and cargs[1] == fvalue: laurent@476: function(*(cargs + (True,) + args), **kwargs) laurent@474: else: laurent@474: function(*(cargs + args), **kwargs) greg@355: # This will block thread if more than one call is waiting lbessard@334: laurent@670: def GetTicktime(self): laurent@670: return self._Ticktime laurent@670: laurent@699: def RemoteExec(self, script, **kwargs): laurent@699: if self._connector is None: laurent@703: return -1, "No runtime connected!" laurent@699: return self._connector.RemoteExec(script, **kwargs) laurent@699: etisserant@235: def DebugThreadProc(self): etisserant@239: """ etisserant@239: This thread waid PLC debug data, and dispatch them to subscribers etisserant@239: """ etisserant@286: self.debug_break = False edouard@461: debug_getvar_retry = 0 etisserant@286: while (not self.debug_break) and (self._connector is not None): Edouard@674: Trace = self._connector.GetTraceVariables() Edouard@674: if(Trace): Edouard@674: plc_status, debug_tick, debug_vars = Trace Edouard@674: else: Edouard@674: plc_status = None edouard@578: debug_getvar_retry += 1 etisserant@239: #print debug_tick, debug_vars edouard@578: if plc_status == "Started": edouard@578: self.IECdebug_lock.acquire() ed@446: if len(debug_vars) == len(self.TracedIECPath): edouard@578: if debug_getvar_retry > DEBUG_RETRIES_WARN: Edouard@689: self.logger.write(_("... debugger recovered\n")) edouard@578: debug_getvar_retry = 0 edouard@673: for IECPath,value in zip(self.TracedIECPath, debug_vars): edouard@673: if value is not None: edouard@673: self.CallWeakcallables(IECPath, "NewValue", debug_tick, value) edouard@673: self.CallWeakcallables("__tick__", "NewDataAvailable") edouard@578: self.IECdebug_lock.release() edouard@578: if debug_getvar_retry == DEBUG_RETRIES_WARN: Edouard@689: self.logger.write(_("Waiting debugger to recover...\n")) edouard@578: if debug_getvar_retry == DEBUG_RETRIES_REREGISTER: edouard@578: # re-register debug registry to PLC edouard@578: wx.CallAfter(self.RegisterDebugVarToConnector) edouard@578: if debug_getvar_retry != 0: ed@446: # Be patient, tollerate PLC to come up before debugging edouard@460: time.sleep(0.1) edouard@578: else: edouard@578: self.debug_break = True Edouard@689: self.logger.write(_("Debugger disabled\n")) Edouard@689: self.DebugThread = None etisserant@235: etisserant@286: def KillDebugThread(self): laurent@703: tmp_debugthread = self.DebugThread etisserant@286: self.debug_break = True laurent@703: if tmp_debugthread is not None: Edouard@624: self.logger.writeyield(_("Stopping debugger...\n")) laurent@703: tmp_debugthread.join(timeout=5) laurent@703: if tmp_debugthread.isAlive() and self.logger: Edouard@624: self.logger.write_warning(_("Couldn't stop debugger.\n")) edouard@578: else: Edouard@624: self.logger.write(_("Debugger stopped.\n")) etisserant@286: self.DebugThread = None etisserant@286: edouard@465: def _connect_debug(self): edouard@465: if self.AppFrame: edouard@465: self.AppFrame.ResetGraphicViewers() edouard@465: self.RegisterDebugVarToConnector() edouard@578: if self.DebugThread is None: edouard@578: self.DebugThread = Thread(target=self.DebugThreadProc) edouard@578: self.DebugThread.start() edouard@465: edouard@462: def _Run(self): etisserant@203: """ edouard@464: Start PLC etisserant@203: """ greg@350: if self.GetIECProgramsAndVariables(): edouard@462: self._connector.StartPLC() edouard@464: self.logger.write(_("Starting PLC\n")) edouard@465: self._connect_debug() etisserant@203: else: edouard@464: self.logger.write_error(_("Couldn't start PLC !\n")) laurent@675: wx.CallAfter(self.UpdateMethodsFromPLCStatus) etisserant@203: etisserant@203: def _Stop(self): etisserant@203: """ etisserant@203: Stop PLC etisserant@203: """ edouard@483: if self._connector is not None and not self._connector.StopPLC(): edouard@483: self.logger.write_error(_("Couldn't stop PLC !\n")) edouard@483: Edouard@689: # debugthread should die on his own Edouard@689: #self.KillDebugThread() etisserant@286: laurent@675: wx.CallAfter(self.UpdateMethodsFromPLCStatus) etisserant@203: etisserant@203: def _Connect(self): etisserant@203: # don't accept re-connetion is already connected etisserant@203: if self._connector is not None: laurent@361: self.logger.write_error(_("Already connected. Please disconnect\n")) etisserant@203: return etisserant@203: etisserant@203: # Get connector uri etisserant@203: uri = self.\ etisserant@203: BeremizRoot.\ etisserant@203: getURI_location().\ etisserant@203: strip() etisserant@203: etisserant@203: # if uri is empty launch discovery dialog etisserant@203: if uri == "": Edouard@740: try: Edouard@740: # Launch Service Discovery dialog Edouard@740: dialog = DiscoveryDialog(self.AppFrame) Edouard@740: answer = dialog.ShowModal() Edouard@740: uri = dialog.GetURI() Edouard@740: dialog.Destroy() Edouard@740: except: laurent@801: self.logger.write_error(_("Local service discovery failed!\n")) Edouard@763: self.logger.write_error(traceback.format_exc()) Edouard@740: uri = None laurent@392: etisserant@203: # Nothing choosed or cancel button laurent@392: if uri is None or answer == wx.ID_CANCEL: laurent@392: self.logger.write_error(_("Connection canceled!\n")) etisserant@203: return etisserant@203: else: etisserant@203: self.\ etisserant@203: BeremizRoot.\ etisserant@203: setURI_location(uri) laurent@764: self.ChangesToSave = True laurent@746: if self._View is not None: laurent@746: self._View.RefreshView() laurent@789: if self.AppFrame is not None: laurent@764: self.AppFrame.RefreshTitle() laurent@764: self.AppFrame.RefreshFileMenu() laurent@764: self.AppFrame.RefreshEditMenu() laurent@764: self.AppFrame.RefreshPageTitles() laurent@764: etisserant@203: # Get connector from uri etisserant@203: try: etisserant@203: self._connector = connectors.ConnectorFactory(uri, self) etisserant@203: except Exception, msg: laurent@361: self.logger.write_error(_("Exception while connecting %s!\n")%uri) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: etisserant@203: # Did connection success ? etisserant@203: if self._connector is None: etisserant@203: # Oups. laurent@361: self.logger.write_error(_("Connection failed to %s!\n")%uri) etisserant@203: else: etisserant@203: self.ShowMethod("_Connect", False) etisserant@203: self.ShowMethod("_Disconnect", True) etisserant@203: self.ShowMethod("_Transfer", True) etisserant@203: etisserant@203: self.CompareLocalAndRemotePLC() greg@350: greg@350: # Init with actual PLC status and print it ed@446: self.UpdateMethodsFromPLCStatus() laurent@361: if self.previous_plcstate is not None: laurent@361: status = _(self.previous_plcstate) laurent@361: else: laurent@361: status = "" laurent@361: self.logger.write(_("PLC is %s\n")%status) greg@350: greg@350: # Start the status Timer greg@350: self.StatusTimer.Start(milliseconds=500, oneShot=False) edouard@465: edouard@465: if self.previous_plcstate=="Started": edouard@465: if self.DebugAvailable() and self.GetIECProgramsAndVariables(): edouard@465: self.logger.write(_("Debug connect matching running PLC\n")) edouard@465: self._connect_debug() edouard@465: else: edouard@465: self.logger.write_warning(_("Debug do not match PLC - stop/transfert/start to re-enable\n")) etisserant@203: etisserant@203: def CompareLocalAndRemotePLC(self): etisserant@203: if self._connector is None: etisserant@203: return etisserant@203: # We are now connected. Update button status etisserant@203: MD5 = self.GetLastBuildMD5() etisserant@203: # Check remote target PLC correspondance to that md5 etisserant@203: if MD5 is not None: etisserant@203: if not self._connector.MatchMD5(MD5): Lolitech@544: # self.logger.write_warning( Lolitech@544: # _("Latest build does not match with target, please transfer.\n")) etisserant@203: self.EnableMethod("_Transfer", True) etisserant@203: else: Lolitech@544: # self.logger.write( Lolitech@544: # _("Latest build matches target, no transfer needed.\n")) etisserant@203: self.EnableMethod("_Transfer", True) edouard@465: # warns controller that program match edouard@465: self.ProgramTransferred() etisserant@203: #self.EnableMethod("_Transfer", False) etisserant@203: else: Lolitech@544: # self.logger.write_warning( Lolitech@544: # _("Cannot compare latest build to target. Please build.\n")) etisserant@203: self.EnableMethod("_Transfer", False) etisserant@203: etisserant@203: etisserant@203: def _Disconnect(self): etisserant@203: self._connector = None greg@350: self.StatusTimer.Stop() laurent@675: wx.CallAfter(self.UpdateMethodsFromPLCStatus) etisserant@203: etisserant@203: def _Transfer(self): etisserant@203: # Get the last build PLC's etisserant@203: MD5 = self.GetLastBuildMD5() etisserant@203: etisserant@203: # Check if md5 file is empty : ask user to build PLC etisserant@203: if MD5 is None : laurent@361: self.logger.write_error(_("Failed : Must build before transfer.\n")) etisserant@203: return False etisserant@203: etisserant@203: # Compare PLC project with PLC on target etisserant@203: if self._connector.MatchMD5(MD5): etisserant@203: self.logger.write( laurent@415: _("Latest build already matches current target. Transfering anyway...\n")) etisserant@203: etisserant@203: # Get temprary directory path laurent@757: extrafiles = [] laurent@757: for extrafilespath in [self._getExtraFilesPath(), laurent@757: self._getProjectFilesPath()]: laurent@757: laurent@757: extrafiles.extend( laurent@757: [(name, open(os.path.join(extrafilespath, name), etisserant@203: 'rb').read()) \ laurent@757: for name in os.listdir(extrafilespath)]) laurent@757: etisserant@203: # Send PLC on target etisserant@203: builder = self.GetBuilder() etisserant@203: if builder is not None: etisserant@203: data = builder.GetBinaryCode() etisserant@203: if data is not None : laurent@707: if self._connector.NewPLC(MD5, data, extrafiles) and self.GetIECProgramsAndVariables(): lbessard@334: self.UnsubscribeAllDebugIECVariable() lbessard@246: self.ProgramTransferred() laurent@692: if self.AppFrame is not None: laurent@692: self.AppFrame.CloseObsoleteDebugTabs() laurent@746: self.AppFrame.RefreshPouInstanceVariablesPanel() laurent@361: self.logger.write(_("Transfer completed successfully.\n")) etisserant@203: else: laurent@361: self.logger.write_error(_("Transfer failed\n")) etisserant@203: else: laurent@415: self.logger.write_error(_("No PLC to transfer (did build succeed ?)\n")) laurent@415: laurent@675: wx.CallAfter(self.UpdateMethodsFromPLCStatus) etisserant@105: laurent@738: StatusMethods = [ Edouard@734: {"bitmap" : "Build", laurent@361: "name" : _("Build"), laurent@361: "tooltip" : _("Build project into build folder"), Edouard@623: "method" : "_Build"}, Edouard@734: {"bitmap" : "Clean", laurent@361: "name" : _("Clean"), etisserant@203: "enabled" : False, laurent@361: "tooltip" : _("Clean project build folder"), etisserant@105: "method" : "_Clean"}, Edouard@734: {"bitmap" : "Run", laurent@361: "name" : _("Run"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Start PLC"), etisserant@105: "method" : "_Run"}, Edouard@734: {"bitmap" : "Stop", laurent@361: "name" : _("Stop"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Stop Running PLC"), etisserant@105: "method" : "_Stop"}, Edouard@734: {"bitmap" : "Connect", laurent@361: "name" : _("Connect"), laurent@361: "tooltip" : _("Connect to the target PLC"), etisserant@203: "method" : "_Connect"}, Edouard@734: {"bitmap" : "Transfer", laurent@361: "name" : _("Transfer"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Transfer PLC"), etisserant@203: "method" : "_Transfer"}, Edouard@734: {"bitmap" : "Disconnect", laurent@361: "name" : _("Disconnect"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Disconnect from PLC"), etisserant@203: "method" : "_Disconnect"}, Edouard@734: {"bitmap" : "ShowIECcode", laurent@361: "name" : _("Show code"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Show IEC code generated by PLCGenerator"), etisserant@105: "method" : "_showIECcode"}, laurent@738: ] laurent@738: laurent@738: ConfNodeMethods = [ Edouard@734: {"bitmap" : "editIECrawcode", laurent@361: "name" : _("Raw IEC code"), laurent@361: "tooltip" : _("Edit raw IEC code added to code generated by PLCGenerator"), etisserant@203: "method" : "_editIECrawcode"}, laurent@782: {"bitmap" : "ManageFolder", laurent@782: "name" : _("Project Files"), laurent@782: "tooltip" : _("Open a file explorer to manage project files"), laurent@782: "method" : "_OpenProjectFiles"}, lbessard@65: ] laurent@738: laurent@738: laurent@738: def EnableMethod(self, method, value): laurent@738: for d in self.StatusMethods: laurent@738: if d["method"]==method: laurent@738: d["enabled"]=value laurent@738: return True laurent@738: return False laurent@738: laurent@738: def ShowMethod(self, method, value): laurent@738: for d in self.StatusMethods: laurent@738: if d["method"]==method: laurent@738: d["shown"]=value laurent@738: return True laurent@738: return False laurent@738: laurent@738: def CallMethod(self, method): laurent@738: for d in self.StatusMethods: laurent@738: if d["method"]==method and d.get("enabled", True) and d.get("shown", True): laurent@738: getattr(self, method)()