PLCControler.py
changeset 687 629680fb0582
parent 684 f10449b18dbe
child 692 e00ce7077bfc
equal deleted inserted replaced
686:3216bf5f711d 687:629680fb0582
   575                 return [words[1]]
   575                 return [words[1]]
   576             elif words[0] == 'R':
   576             elif words[0] == 'R':
   577                 return ["%s.%s" % (words[1], words[2])]
   577                 return ["%s.%s" % (words[1], words[2])]
   578         return []
   578         return []
   579     
   579     
   580     def RecursiveGetPouInstanceTagname(self, project, pou_type, parts):
   580     def RecursiveGetPouInstanceTagName(self, project, pou_type, parts):
   581         pou = project.getpou(pou_type)
   581         pou = project.getpou(pou_type)
   582         if pou is not None:
   582         if pou is not None:
   583             if len(parts) == 0:
   583             if len(parts) == 0:
   584                 return self.ComputePouName(pou_type)
   584                 return self.ComputePouName(pou_type)
   585             
   585             
   586             for varlist_type, varlist in pou.getvars():
   586             for varlist_type, varlist in pou.getvars():
   587                 for variable in varlist.getvariable():
   587                 for variable in varlist.getvariable():
   588                     vartype_content = variable.gettype().getcontent()
   588                     vartype_content = variable.gettype().getcontent()
   589                     if vartype_content["name"] == "derived":
   589                     if vartype_content["name"] == "derived":
   590                         return self.RecursiveGetPouInstanceTagname(
   590                         return self.RecursiveGetPouInstanceTagName(
   591                                         project, 
   591                                         project, 
   592                                         vartype_content["value"].getname(),
   592                                         vartype_content["value"].getname(),
   593                                         parts[1:])
   593                                         parts[1:])
   594         return None
   594         return None
   595     
   595     
   596     def GetPouInstanceTagname(self, instance_path, debug = False):
   596     def GetPouInstanceTagName(self, instance_path, debug = False):
   597         parts = instance_path.split(".")
   597         parts = instance_path.split(".")
   598         if len(parts) == 1:
   598         if len(parts) == 1:
   599             return self.ComputeConfigurationName(parts[0])
   599             return self.ComputeConfigurationName(parts[0])
   600         elif len(parts) == 2:
   600         elif len(parts) == 2:
   601             return self.ComputeConfigurationResourceName(parts[0], parts[1])
   601             return self.ComputeConfigurationResourceName(parts[0], parts[1])
   612                                 if pou_instance.getname() == parts[2]:
   612                                 if pou_instance.getname() == parts[2]:
   613                                     if len(parts) == 3:
   613                                     if len(parts) == 3:
   614                                         return self.ComputePouName(
   614                                         return self.ComputePouName(
   615                                                     pou_instance.gettypeName())
   615                                                     pou_instance.gettypeName())
   616                                     else:
   616                                     else:
   617                                         return self.RecursiveGetPouInstanceTagname(
   617                                         return self.RecursiveGetPouInstanceTagName(
   618                                                     project,
   618                                                     project,
   619                                                     pou_instance.gettypeName(),
   619                                                     pou_instance.gettypeName(),
   620                                                     parts[3:])
   620                                                     parts[3:])
   621         return None
   621         return None
   622     
   622     
   623     def GetInstanceInfos(self, instance_path):
   623     def GetInstanceInfos(self, instance_path, debug = False):
   624         tagname = self.GetPouInstanceTagName(instance_path)
   624         tagname = self.GetPouInstanceTagName(instance_path)
   625         if tagname is not None:
   625         if tagname is not None:
   626             return self.Controler.GetPouVariables(tagname, self.Debug)
   626             return self.GetPouVariables(tagname, debug)
   627         else:
   627         else:
   628             pou_path, var_name = tagname.rsplit(".", 1)
   628             pou_path, var_name = instance_path.rsplit(".", 1)
   629             tagname = self.Controler.GetPouInstanceTagName(pou_path)
   629             tagname = self.GetPouInstanceTagName(pou_path)
   630             if tagname is not None:
   630             if tagname is not None:
   631                 pou_infos = self.Controler.GetPouVariables(tagname, self.Debug)
   631                 pou_infos = self.GetPouVariables(tagname, debug)
   632                 for var_infos in pou_infos["variables"]:
   632                 for var_infos in pou_infos["variables"]:
   633                     if var_infos["name"] == var_name:
   633                     if var_infos["name"] == var_name:
   634                         return var_infos
   634                         return var_infos
   635         return None
   635         return None
   636         
   636     
   637     # Return project topology informations
       
   638     def GetProjectTopology(self, debug = False):
       
   639         project = self.GetProject(debug)
       
   640         if project is not None:
       
   641             infos = {"name": project.getname(), "type": ITEM_PROJECT, "values" : []}
       
   642             for config in project.getconfigurations():
       
   643                 config_infos = {"name" : config.getname(), "type": ITEM_CONFIGURATION, "values" : []}
       
   644                 for resource in config.getresource():
       
   645                     resource_infos = {"name" : resource.getname(), "type": ITEM_RESOURCE, "values": []}
       
   646                     for task in resource.gettask():
       
   647                         for pou in task.getpouInstance():
       
   648                             instance_infos = self.GetPouTopology(pou.getname(), pou.gettypeName(), debug=debug)
       
   649                             if instance_infos is not None:
       
   650                                 resource_infos["values"].append(instance_infos)
       
   651                     for pou in resource.getpouInstance():
       
   652                         instance_infos = self.GetPouTopology(pou.getname(), pou.gettypeName(), debug=debug)
       
   653                         if instance_infos is not None:
       
   654                             resource_infos["values"].append(instance_infos)
       
   655                     for varlist in resource.getglobalVars():
       
   656                         for variable in varlist.getvariable():
       
   657                             vartype_content = variable.gettype().getcontent()
       
   658                             if vartype_content["name"] == "derived":
       
   659                                 var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname(), True, debug)
       
   660                                 if var_infos is not None:
       
   661                                     resource_infos["values"].append(var_infos)
       
   662                             elif vartype_content["name"] in ["string", "wstring"]:
       
   663                                 resource_infos["values"].append({"name" : variable.getname(), 
       
   664                                                                  "elmt_type" : vartype_content["name"].upper(),
       
   665                                                                  "type" : ITEM_VAR_GLOBAL, "values" : []})
       
   666                             else:
       
   667                                 resource_infos["values"].append({"name" : variable.getname(), 
       
   668                                                                  "elmt_type" : vartype_content["name"], 
       
   669                                                                  "type" : ITEM_VAR_GLOBAL, "values" : []})
       
   670                     config_infos["values"].append(resource_infos)
       
   671                 for varlist in config.getglobalVars():
       
   672                     for variable in varlist.getvariable():
       
   673                         vartype_content = variable.gettype().getcontent()
       
   674                         if vartype_content["name"] == "derived":
       
   675                             var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname(), True, debug)
       
   676                             if var_infos is not None:
       
   677                                 config_infos["values"].append(var_infos)
       
   678                         elif vartype_content["name"] in ["string", "wstring"]:
       
   679                             config_infos["values"].append({"name" : variable.getname(), 
       
   680                                                            "elmt_type" : vartype_content["name"].upper(), 
       
   681                                                            "type" : ITEM_VAR_GLOBAL, "values" : []})
       
   682                         else:
       
   683                             config_infos["values"].append({"name" : variable.getname(),
       
   684                                                            "elmt_type" : vartype_content["name"], 
       
   685                                                            "type" : ITEM_VAR_GLOBAL, "values" : []})
       
   686                 infos["values"].append(config_infos)
       
   687             return infos
       
   688         return None
       
   689     
       
   690     # Return pou topology informations
       
   691     def GetPouTopology(self, name, type, global_var = False, debug = False):
       
   692         project = self.GetProject(debug)
       
   693         if project is not None:
       
   694             pou = project.getpou(type)
       
   695             if pou is not None:
       
   696                 pou_type = pou.getpouType()
       
   697                 if pou_type == "function":
       
   698                     return None
       
   699                 elif pou_type == "program":
       
   700                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_PROGRAM, 
       
   701                                  "tagname" : self.ComputePouName(pou.getname()), "values" : []}
       
   702                 else:
       
   703                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, 
       
   704                                  "tagname" : self.ComputePouName(pou.getname()), "values" : []}
       
   705                 if pou.getbodyType() == "SFC":
       
   706                     for transition in pou.gettransitionList():
       
   707                         pou_infos["values"].append({"name" : transition.getname(), 
       
   708                             "elmt_type" : "TRANSITION", "type" : ITEM_TRANSITION, 
       
   709                             "tagname" : self.ComputePouActionName(pou.getname(), transition.getname()),
       
   710                             "values" : []})
       
   711                     for action in pou.getactionList():
       
   712                         pou_infos["values"].append({"name": action.getname(), 
       
   713                             "elmt_type" : "ACTION", "type": ITEM_ACTION, 
       
   714                             "tagname" : self.ComputePouActionName(pou.getname(), action.getname()),
       
   715                             "values" : []})
       
   716                 if pou.interface:
       
   717                     # Extract variables from every varLists
       
   718                     for type, varlist in pou.getvars():
       
   719                         infos = VAR_CLASS_INFOS.get(type, None)
       
   720                         if infos is not None:
       
   721                             current_var_class = infos[1]
       
   722                         else:
       
   723                             current_var_class = ITEM_VAR_LOCAL
       
   724                         for variable in varlist.getvariable():
       
   725                             vartype_content = variable.gettype().getcontent()
       
   726                             if vartype_content["name"] == "derived":
       
   727                                 var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname())
       
   728                                 if var_infos is not None:
       
   729                                     pou_infos["values"].append(var_infos)
       
   730                             elif vartype_content["name"] in ["string", "wstring"]:
       
   731                                 pou_infos["values"].append({"name" : variable.getname(), 
       
   732                                                             "elmt_type" : vartype_content["name"].upper(), 
       
   733                                                             "type" : current_var_class, "values" : []})
       
   734                             else:
       
   735                                 pou_infos["values"].append({"name" : variable.getname(), 
       
   736                                                             "elmt_type" : vartype_content["name"], 
       
   737                                                             "type" : current_var_class, "values" : []})
       
   738                 return pou_infos
       
   739             block_infos = self.GetBlockType(type, debug = debug)
       
   740             if block_infos is not None:
       
   741                 if block_infos["type"] == "function":
       
   742                     return None
       
   743                 elif block_infos["type"] == "program":
       
   744                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_PROGRAM, "values" : []}
       
   745                 else:
       
   746                     pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, "values" : []}
       
   747                 for varname, vartype, varmodifier in block_infos["inputs"]:
       
   748                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
       
   749                 for varname, vartype, varmodifier in block_infos["outputs"]:
       
   750                     pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_OUTPUT, "values" : []})
       
   751                 return pou_infos
       
   752             
       
   753             if type in self.GetDataTypes(debug = debug):
       
   754                 if global_var:
       
   755                     return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_GLOBAL, "values" : []}
       
   756                 else:
       
   757                     return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_LOCAL, "values" : []}
       
   758         return None
       
   759 
       
   760     # Return if data type given by name is used by another data type or pou
   637     # Return if data type given by name is used by another data type or pou
   761     def DataTypeIsUsed(self, name, debug = False):
   638     def DataTypeIsUsed(self, name, debug = False):
   762         project = self.GetProject(debug)
   639         project = self.GetProject(debug)
   763         if project is not None:
   640         if project is not None:
   764             return project.ElementIsUsed(name) or project.DataTypeIsDerived(name)
   641             return project.ElementIsUsed(name) or project.DataTypeIsDerived(name)
   917     def ProjectRemovePou(self, pou_name):
   794     def ProjectRemovePou(self, pou_name):
   918         if self.Project is not None:
   795         if self.Project is not None:
   919             self.Project.removepou(pou_name)
   796             self.Project.removepou(pou_name)
   920             self.BufferProject()
   797             self.BufferProject()
   921     
   798     
       
   799     # Return the name of the configuration if only one exist
       
   800     def GetProjectMainConfigurationName(self):
       
   801         if self.Project is not None:
       
   802             # Found the configuration corresponding to old name and change its name to new name
       
   803             configurations = self.Project.getconfigurations()
       
   804             if len(configurations) == 1:
       
   805                 return configurations[0].getname()
       
   806         return None
       
   807                 
   922     # Add a configuration to Project
   808     # Add a configuration to Project
   923     def ProjectAddConfiguration(self, config_name):
   809     def ProjectAddConfiguration(self, config_name):
   924         if self.Project is not None:
   810         if self.Project is not None:
   925             self.Project.addconfiguration(config_name)
   811             self.Project.addconfiguration(config_name)
   926             self.BufferProject()
   812             self.BufferProject()