PLCControler.py
changeset 684 f10449b18dbe
parent 681 c141dad94ff4
child 687 629680fb0582
equal deleted inserted replaced
683:37882f34f9cb 684:f10449b18dbe
    72                    "Temp" :     (plcopen.interface_tempVars,     ITEM_VAR_TEMP),
    72                    "Temp" :     (plcopen.interface_tempVars,     ITEM_VAR_TEMP),
    73                    "Input" :    (plcopen.interface_inputVars,    ITEM_VAR_INPUT),
    73                    "Input" :    (plcopen.interface_inputVars,    ITEM_VAR_INPUT),
    74                    "Output" :   (plcopen.interface_outputVars,   ITEM_VAR_OUTPUT),
    74                    "Output" :   (plcopen.interface_outputVars,   ITEM_VAR_OUTPUT),
    75                    "InOut" :    (plcopen.interface_inOutVars,    ITEM_VAR_INOUT)
    75                    "InOut" :    (plcopen.interface_inOutVars,    ITEM_VAR_INOUT)
    76                   }
    76                   }
       
    77 
       
    78 POU_TYPES = {"program": ITEM_PROGRAM,
       
    79              "functionBlock": ITEM_FUNCTIONBLOCK,
       
    80              "function": ITEM_FUNCTION,
       
    81             }
    77 
    82 
    78 LOCATIONS_ITEMS = [LOCATION_CONFNODE,
    83 LOCATIONS_ITEMS = [LOCATION_CONFNODE,
    79                    LOCATION_MODULE,
    84                    LOCATION_MODULE,
    80                    LOCATION_GROUP,
    85                    LOCATION_GROUP,
    81                    LOCATION_VAR_INPUT,
    86                    LOCATION_VAR_INPUT,
   374                         "tagname": self.ComputeConfigurationResourceName(config.getname(), resource.getname()), 
   379                         "tagname": self.ComputeConfigurationResourceName(config.getname(), resource.getname()), 
   375                         "values": []}
   380                         "values": []}
   376                     resources["values"].append(resource_infos)
   381                     resources["values"].append(resource_infos)
   377                 config_infos["values"] = [resources]
   382                 config_infos["values"] = [resources]
   378                 configurations["values"].append(config_infos)
   383                 configurations["values"].append(config_infos)
   379             infos["values"] = [{"name": PROPERTIES, "type": ITEM_PROPERTIES, "values": []},
   384             infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"], 
   380                                datatypes, pou_types["function"], pou_types["functionBlock"], 
       
   381                                pou_types["program"], configurations]
   385                                pou_types["program"], configurations]
   382             return infos
   386             return infos
   383         return None
   387         return None
   384 
   388 
       
   389     def GetPouVariableInfos(self, project, variable, var_class, debug=False):
       
   390         vartype_content = variable.gettype().getcontent()
       
   391         if vartype_content["name"] == "derived":
       
   392             var_type = vartype_content["value"].getname()
       
   393             pou_type = None
       
   394             pou = project.getpou(var_type)
       
   395             if pou is not None:
       
   396                 pou_type = pou.getpouType()
       
   397             edit = debug = pou_type is not None
       
   398             if pou_type is None:
       
   399                 block_infos = self.GetBlockType(var_type, debug = debug)
       
   400                 pou_type = block_infos["type"]
       
   401             if pou_type is not None:
       
   402                 var_class = None
       
   403                 if pou_type == "program":
       
   404                     var_class = ITEM_PROGRAM
       
   405                 elif pou_type != "function":
       
   406                     var_class = ITEM_FUNCTIONBLOCK
       
   407                 if var_class is not None:
       
   408                     return {"name": variable.getname(), 
       
   409                             "type": var_type, 
       
   410                             "class": var_class,
       
   411                             "edit": edit,
       
   412                             "debug": debug}
       
   413             elif var_type in self.GetDataTypes(debug = debug):
       
   414                 return {"name": variable.getname(), 
       
   415                         "type": var_type, 
       
   416                         "class": var_class,
       
   417                         "edit": False,
       
   418                         "debug": False}
       
   419         elif vartype_content["name"] in ["string", "wstring"]:
       
   420             return {"name": variable.getname(), 
       
   421                     "type": vartype_content["name"].upper(), 
       
   422                     "class": var_class,
       
   423                     "edit": False,
       
   424                     "debug": True}
       
   425         else:
       
   426             return {"name": variable.getname(),
       
   427                     "type": vartype_content["name"], 
       
   428                     "class": var_class,
       
   429                     "edit": False,
       
   430                     "debug": True}
       
   431         return None
       
   432 
       
   433     def GetPouVariables(self, tagname, debug = False):
       
   434         vars = []
       
   435         pou_type = None
       
   436         project = self.GetProject(debug)
       
   437         if project is not None:
       
   438             words = tagname.split("::")
       
   439             if words[0] == "P":
       
   440                 pou = project.getpou(words[1])
       
   441                 if pou is not None:
       
   442                     pou_type = pou.getpouType()
       
   443                     if (pou_type in ["program", "functionBlock"] and 
       
   444                         pou.interface is not None):
       
   445                         # Extract variables from every varLists
       
   446                         for varlist_type, varlist in pou.getvars():
       
   447                             var_infos = VAR_CLASS_INFOS.get(varlist_type, None)
       
   448                             if var_infos is not None:
       
   449                                 var_class = var_infos[1]
       
   450                             else:
       
   451                                 var_class = ITEM_VAR_LOCAL
       
   452                             for variable in varlist.getvariable():
       
   453                                 var_infos = self.GetPouVariableInfos(project, variable, var_class, debug)
       
   454                                 if var_infos is not None:
       
   455                                     vars.append(var_infos)
       
   456                         return {"class": POU_TYPES[pou_type],
       
   457                                 "type": words[1],
       
   458                                 "variables": vars,
       
   459                                 "edit": True,
       
   460                                 "debug": True}
       
   461                 else:
       
   462                     block_infos = self.GetBlockType(words[1], debug = debug)
       
   463                     if (block_infos is not None and 
       
   464                         block_infos["type"] in ["program", "functionBlock"]):
       
   465                         for varname, vartype, varmodifier in block_infos["inputs"]:
       
   466                             vars.append({"name" : varname, 
       
   467                                          "type" : vartype, 
       
   468                                          "class" : ITEM_VAR_INPUT,
       
   469                                          "edit": False,
       
   470                                          "debug": True})
       
   471                         for varname, vartype, varmodifier in block_infos["outputs"]:
       
   472                             vars.append({"name" : varname, 
       
   473                                          "type" : vartype, 
       
   474                                          "class" : ITEM_VAR_OUTPUT,
       
   475                                          "edit": False,
       
   476                                          "debug": True})
       
   477                         return {"class": POU_TYPES[block_infos["type"]],
       
   478                                 "type": None,
       
   479                                 "variables": vars,
       
   480                                 "edit": False,
       
   481                                 "debug": False}
       
   482             elif words[0] in ['C', 'R']:
       
   483                 if words[0] == 'C':
       
   484                     pou_type = ITEM_CONFIGURATION
       
   485                     element = project.getconfiguration(words[1])
       
   486                     for resource in element.getresource():
       
   487                         vars.append({"name": resource.getname(),
       
   488                                      "type": None,
       
   489                                      "class": ITEM_RESOURCE,
       
   490                                      "edit": True,
       
   491                                      "debug": False})
       
   492                 elif words[0] == 'R':
       
   493                     pou_type = ITEM_RESOURCE
       
   494                     element = project.getconfigurationResource(words[1], words[2])
       
   495                     for task in element.gettask():
       
   496                         for pou in task.getpouInstance():
       
   497                             vars.append({"name": pou.getname(),
       
   498                                          "type": pou.gettypeName(),
       
   499                                          "class": ITEM_PROGRAM,
       
   500                                          "edit": True,
       
   501                                          "debug": True})
       
   502                     for pou in element.getpouInstance():
       
   503                         vars.append({"name": pou.getname(),
       
   504                                      "type": pou.gettypeName(),
       
   505                                      "class": ITEM_PROGRAM,
       
   506                                      "edit": True,
       
   507                                      "debug": True})
       
   508                 for varlist in element.getglobalVars():
       
   509                     for variable in varlist.getvariable():
       
   510                         var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
       
   511                         if var_infos is not None:
       
   512                             vars.append(var_infos)
       
   513                 return {"class": pou_type,
       
   514                         "type": None,
       
   515                         "variables": vars,
       
   516                         "edit": True,
       
   517                         "debug": False}
       
   518         return None
       
   519 
       
   520     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
       
   521         instances = []
       
   522         for varlist in varlists:
       
   523             for variable in varlist.getvariable():
       
   524                 vartype_content = variable.gettype().getcontent()
       
   525                 if vartype_content["name"] == "derived":
       
   526                     var_path = "%s.%s" % (parent_path, variable.getname())
       
   527                     var_type = vartype_content["value"].getname()
       
   528                     if var_type == pou_type:
       
   529                         instances.append(var_path)
       
   530                     else:
       
   531                         pou = project.getpou(var_type)
       
   532                         if pou is not None:
       
   533                             instances.extend(
       
   534                                 self.RecursiveSearchPouInstances(
       
   535                                     project, pou_type, var_path, 
       
   536                                     [varlist for type, varlist in pou.getvars()], 
       
   537                                     debug))
       
   538         return instances
       
   539                         
       
   540     def SearchPouInstances(self, tagname, debug = False):
       
   541         project = self.GetProject(debug)
       
   542         if project is not None:
       
   543             words = tagname.split("::")
       
   544             if words[0] == "P":
       
   545                 instances = []
       
   546                 for config in project.getconfigurations():
       
   547                     config_name = config.getname()
       
   548                     instances.extend(
       
   549                         self.RecursiveSearchPouInstances(
       
   550                             project, words[1], config_name, 
       
   551                             config.getglobalVars(), debug))
       
   552                     for resource in config.getresource():
       
   553                         res_path = "%s.%s" % (config_name, resource.getname())
       
   554                         instances.extend(
       
   555                             self.RecursiveSearchPouInstances(
       
   556                                 project, words[1], res_path, 
       
   557                                 resource.getglobalVars(), debug))
       
   558                         pou_instances = resource.getpouInstance()[:]
       
   559                         for task in resource.gettask():
       
   560                             pou_instances.extend(task.getpouInstance())
       
   561                         for pou_instance in pou_instances:
       
   562                             pou_path = "%s.%s" % (res_path, pou_instance.getname())
       
   563                             pou_type = pou_instance.gettypeName()
       
   564                             if pou_type == words[1]:
       
   565                                 instances.append(pou_path)
       
   566                             pou = project.getpou(pou_type)
       
   567                             if pou is not None:
       
   568                                 instances.extend(
       
   569                                     self.RecursiveSearchPouInstances(
       
   570                                         project, words[1], pou_path, 
       
   571                                         [varlist for type, varlist in pou.getvars()], 
       
   572                                         debug))
       
   573                 return instances
       
   574             elif words[0] == 'C':
       
   575                 return [words[1]]
       
   576             elif words[0] == 'R':
       
   577                 return ["%s.%s" % (words[1], words[2])]
       
   578         return []
       
   579     
       
   580     def RecursiveGetPouInstanceTagname(self, project, pou_type, parts):
       
   581         pou = project.getpou(pou_type)
       
   582         if pou is not None:
       
   583             if len(parts) == 0:
       
   584                 return self.ComputePouName(pou_type)
       
   585             
       
   586             for varlist_type, varlist in pou.getvars():
       
   587                 for variable in varlist.getvariable():
       
   588                     vartype_content = variable.gettype().getcontent()
       
   589                     if vartype_content["name"] == "derived":
       
   590                         return self.RecursiveGetPouInstanceTagname(
       
   591                                         project, 
       
   592                                         vartype_content["value"].getname(),
       
   593                                         parts[1:])
       
   594         return None
       
   595     
       
   596     def GetPouInstanceTagname(self, instance_path, debug = False):
       
   597         parts = instance_path.split(".")
       
   598         if len(parts) == 1:
       
   599             return self.ComputeConfigurationName(parts[0])
       
   600         elif len(parts) == 2:
       
   601             return self.ComputeConfigurationResourceName(parts[0], parts[1])
       
   602         else:
       
   603             project = self.GetProject(debug)
       
   604             for config in project.getconfigurations():
       
   605                 if config.getname() == parts[0]:
       
   606                     for resource in config.getresource():
       
   607                         if resource.getname() == parts[1]:
       
   608                             pou_instances = resource.getpouInstance()[:]
       
   609                             for task in resource.gettask():
       
   610                                 pou_instances.extend(task.getpouInstance())
       
   611                             for pou_instance in pou_instances:
       
   612                                 if pou_instance.getname() == parts[2]:
       
   613                                     if len(parts) == 3:
       
   614                                         return self.ComputePouName(
       
   615                                                     pou_instance.gettypeName())
       
   616                                     else:
       
   617                                         return self.RecursiveGetPouInstanceTagname(
       
   618                                                     project,
       
   619                                                     pou_instance.gettypeName(),
       
   620                                                     parts[3:])
       
   621         return None
       
   622     
       
   623     def GetInstanceInfos(self, instance_path):
       
   624         tagname = self.GetPouInstanceTagName(instance_path)
       
   625         if tagname is not None:
       
   626             return self.Controler.GetPouVariables(tagname, self.Debug)
       
   627         else:
       
   628             pou_path, var_name = tagname.rsplit(".", 1)
       
   629             tagname = self.Controler.GetPouInstanceTagName(pou_path)
       
   630             if tagname is not None:
       
   631                 pou_infos = self.Controler.GetPouVariables(tagname, self.Debug)
       
   632                 for var_infos in pou_infos["variables"]:
       
   633                     if var_infos["name"] == var_name:
       
   634                         return var_infos
       
   635         return None
       
   636         
   385     # Return project topology informations
   637     # Return project topology informations
   386     def GetProjectTopology(self, debug = False):
   638     def GetProjectTopology(self, debug = False):
   387         project = self.GetProject(debug)
   639         project = self.GetProject(debug)
   388         if project is not None:
   640         if project is not None:
   389             infos = {"name": project.getname(), "type": ITEM_PROJECT, "values" : []}
   641             infos = {"name": project.getname(), "type": ITEM_PROJECT, "values" : []}
   493                 else:
   745                 else:
   494                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, "values" : []}
   746                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, "values" : []}
   495                 for varname, vartype, varmodifier in block_infos["inputs"]:
   747                 for varname, vartype, varmodifier in block_infos["inputs"]:
   496                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
   748                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
   497                 for varname, vartype, varmodifier in block_infos["outputs"]:
   749                 for varname, vartype, varmodifier in block_infos["outputs"]:
   498                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
   750                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_OUTPUT, "values" : []})
   499                 return pou_infos
   751                 return pou_infos
   500             
   752             
   501             if type in self.GetDataTypes(debug = debug):
   753             if type in self.GetDataTypes(debug = debug):
   502                 if global_var:
   754                 if global_var:
   503                     return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_GLOBAL, "values" : []}
   755                     return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_GLOBAL, "values" : []}
  2737         
  2989         
  2738         self.Project = plcopen.project()
  2990         self.Project = plcopen.project()
  2739         for child in tree.childNodes:
  2991         for child in tree.childNodes:
  2740             if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "project":
  2992             if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "project":
  2741                 try:
  2993                 try:
  2742                     result = self.Project.loadXMLTree(child, ["xmlns", "xmlns:xhtml", "xmlns:xsi", "xsi:schemaLocation"])
  2994                     result = self.Project.loadXMLTree(child)
  2743                 except ValueError, e:
  2995                 except ValueError, e:
  2744                     return _("Project file syntax error:\n\n") + str(e)
  2996                     return _("Project file syntax error:\n\n") + str(e)
  2745                 self.SetFilePath(filepath)
  2997                 self.SetFilePath(filepath)
  2746                 self.Project.RefreshElementUsingTree()
  2998                 self.Project.RefreshElementUsingTree()
  2747                 self.Project.RefreshDataTypeHierarchy()
  2999                 self.Project.RefreshDataTypeHierarchy()