PLCControler.py
changeset 1290 13ee5f4ab612
parent 1284 abf63a310532
child 1291 42ea51d083ce
equal deleted inserted replaced
1286:adda406d3960 1290:13ee5f4ab612
    27 import cPickle
    27 import cPickle
    28 import os,sys,re
    28 import os,sys,re
    29 import datetime
    29 import datetime
    30 from time import localtime
    30 from time import localtime
    31 
    31 
    32 from plcopen import plcopen
    32 from plcopen import PLCOpenParser, LoadProject, SaveProject
    33 from plcopen.structures import *
    33 from plcopen.structures import *
    34 from graphics.GraphicCommons import *
    34 from graphics.GraphicCommons import *
    35 from PLCGenerator import *
    35 from PLCGenerator import *
    36 
    36 
    37 duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:\.[0-9]*)?)ms)?")
    37 duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:\.[0-9]*)?)ms)?")
    64                   ITEM_VAR_INPUT,
    64                   ITEM_VAR_INPUT,
    65                   ITEM_VAR_OUTPUT,
    65                   ITEM_VAR_OUTPUT,
    66                   ITEM_VAR_INOUT
    66                   ITEM_VAR_INOUT
    67                  ] = range(17, 24)
    67                  ] = range(17, 24)
    68 
    68 
    69 VAR_CLASS_INFOS = {"Local" :    (plcopen.interface_localVars,    ITEM_VAR_LOCAL),
    69 VAR_CLASS_INFOS = dict(
    70                    "Global" :   (plcopen.interface_globalVars,   ITEM_VAR_GLOBAL),
    70     [(name, (PLCOpenParser.GetElementClass(classname, "interface"), symbol))
    71                    "External" : (plcopen.interface_externalVars, ITEM_VAR_EXTERNAL),
    71      for name, classname, symbol in 
    72                    "Temp" :     (plcopen.interface_tempVars,     ITEM_VAR_TEMP),
    72      [("Local",    "localVars",    ITEM_VAR_LOCAL),
    73                    "Input" :    (plcopen.interface_inputVars,    ITEM_VAR_INPUT),
    73       ("Global",   "globalVars",   ITEM_VAR_GLOBAL),
    74                    "Output" :   (plcopen.interface_outputVars,   ITEM_VAR_OUTPUT),
    74       ("External", "externalVars", ITEM_VAR_EXTERNAL),
    75                    "InOut" :    (plcopen.interface_inOutVars,    ITEM_VAR_INOUT)
    75       ("Temp",     "tempVars",     ITEM_VAR_TEMP),
    76                   }
    76       ("Input",    "inputVars",    ITEM_VAR_INPUT),
       
    77       ("Output",   "outputVars",   ITEM_VAR_OUTPUT),
       
    78       ("InOut",    "inOutVars",    ITEM_VAR_INOUT)]])
    77 
    79 
    78 POU_TYPES = {"program": ITEM_PROGRAM,
    80 POU_TYPES = {"program": ITEM_PROGRAM,
    79              "functionBlock": ITEM_FUNCTIONBLOCK,
    81              "functionBlock": ITEM_FUNCTIONBLOCK,
    80              "function": ITEM_FUNCTION,
    82              "function": ITEM_FUNCTION,
    81             }
    83             }
   232         return self.Project is not None
   234         return self.Project is not None
   233 
   235 
   234     # Create a new project by replacing the current one
   236     # Create a new project by replacing the current one
   235     def CreateNewProject(self, properties):
   237     def CreateNewProject(self, properties):
   236         # Create the project
   238         # Create the project
   237         self.Project = plcopen.project()
   239         self.Project = PLCOpenParser.CreateRoot()
   238         properties["creationDateTime"] = datetime.datetime(*localtime()[:6])
   240         properties["creationDateTime"] = datetime.datetime(*localtime()[:6])
   239         self.Project.setfileHeader(properties)
   241         self.Project.setfileHeader(properties)
   240         self.Project.setcontentHeader(properties)
   242         self.Project.setcontentHeader(properties)
   241         self.SetFilePath("")
   243         self.SetFilePath("")
   242         # Initialize the project buffer
   244         # Initialize the project buffer
  3134                 new_instance["Task"] = ""
  3136                 new_instance["Task"] = ""
  3135                 instances_data.append(new_instance)
  3137                 instances_data.append(new_instance)
  3136             return tasks_data, instances_data
  3138             return tasks_data, instances_data
  3137 
  3139 
  3138     def OpenXMLFile(self, filepath):
  3140     def OpenXMLFile(self, filepath):
  3139         xmlfile = open(filepath, 'r')
  3141         try:
  3140         tree = minidom.parse(xmlfile)
  3142             self.Project = LoadProject(filepath)
  3141         xmlfile.close()
  3143         except Exception, e:
       
  3144             return _("Project file syntax error:\n\n") + str(e)
       
  3145         self.SetFilePath(filepath)
       
  3146         self.Project.RefreshElementUsingTree()
       
  3147         self.Project.RefreshDataTypeHierarchy()
       
  3148         self.Project.RefreshCustomBlockTypes()
  3142         
  3149         
  3143         self.Project = plcopen.project()
  3150         ## To remove when project buffering ready
  3144         for child in tree.childNodes:
  3151         self.ProjectBufferEnabled = False
  3145             if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "project":
  3152         
  3146                 try:
  3153         self.CreateProjectBuffer(True)
  3147                     result = self.Project.loadXMLTree(child)
  3154         self.ProgramChunks = []
  3148                 except ValueError, e:
  3155         self.ProgramOffset = 0
  3149                     return _("Project file syntax error:\n\n") + str(e)
  3156         self.NextCompiledProject = self.Project ## self.Copy(self.Project)
  3150                 self.SetFilePath(filepath)
  3157         self.CurrentCompiledProject = None
  3151                 self.Project.RefreshElementUsingTree()
  3158         self.Buffering = False
  3152                 self.Project.RefreshDataTypeHierarchy()
  3159         self.CurrentElementEditing = None
  3153                 self.Project.RefreshCustomBlockTypes()
  3160         return None
  3154                 self.CreateProjectBuffer(True)
  3161         
  3155                 self.ProgramChunks = []
       
  3156                 self.ProgramOffset = 0
       
  3157                 self.NextCompiledProject = self.Copy(self.Project)
       
  3158                 self.CurrentCompiledProject = None
       
  3159                 self.Buffering = False
       
  3160                 self.CurrentElementEditing = None
       
  3161                 return None
       
  3162         return _("No PLC project found")
       
  3163 
       
  3164     def SaveXMLFile(self, filepath = None):
  3162     def SaveXMLFile(self, filepath = None):
  3165         if not filepath and self.FilePath == "":
  3163         if not filepath and self.FilePath == "":
  3166             return False
  3164             return False
  3167         else:
  3165         else:
  3168             contentheader = {"modificationDateTime": datetime.datetime(*localtime()[:6])}
  3166             contentheader = {"modificationDateTime": datetime.datetime(*localtime()[:6])}
  3169             self.Project.setcontentHeader(contentheader)
  3167             self.Project.setcontentHeader(contentheader)
  3170             
  3168             
  3171             text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  3169             if filepath:
  3172             extras = {"xmlns" : "http://www.plcopen.org/xml/tc6.xsd",
  3170                 SaveProject(self.Project, filepath)
  3173                       "xmlns:xhtml" : "http://www.w3.org/1999/xhtml",
  3171             else:
  3174                       "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
  3172                 SaveProject(self.Project, self.FilePath)
  3175                       "xsi:schemaLocation" : "http://www.plcopen.org/xml/tc6.xsd"}
       
  3176             text += self.Project.generateXMLText("project", 0, extras)
       
  3177             
  3173             
  3178             if filepath:
       
  3179                 xmlfile = open(filepath,"w")
       
  3180             else:
       
  3181                 xmlfile = open(self.FilePath,"w")
       
  3182             xmlfile.write(text.encode("utf-8"))
       
  3183             xmlfile.close()
       
  3184             self.MarkProjectAsSaved()
  3174             self.MarkProjectAsSaved()
  3185             if filepath:
  3175             if filepath:
  3186                 self.SetFilePath(filepath)
  3176                 self.SetFilePath(filepath)
  3187             return True
  3177             return True
  3188 
  3178