PLCControler.py
changeset 630 9d7e38e271cb
parent 629 ddf363817ffa
child 637 d8fe5a0f5403
equal deleted inserted replaced
629:ddf363817ffa 630:9d7e38e271cb
  1123                     tree.insert(0, ("ENO", "BOOL", ([], [])))
  1123                     tree.insert(0, ("ENO", "BOOL", ([], [])))
  1124                 if not en:
  1124                 if not en:
  1125                     tree.insert(0, ("EN", "BOOL", ([], [])))
  1125                     tree.insert(0, ("EN", "BOOL", ([], [])))
  1126                 return tree, []
  1126                 return tree, []
  1127             datatype = project.getdataType(typename)
  1127             datatype = project.getdataType(typename)
       
  1128             if datatype is None:
       
  1129                 datatype = self.GetPluginDataType(typename)
  1128             if datatype is not None:
  1130             if datatype is not None:
  1129                 tree = []
  1131                 tree = []
  1130                 basetype_content = datatype.baseType.getcontent()
  1132                 basetype_content = datatype.baseType.getcontent()
  1131                 if basetype_content["name"] == "derived":
  1133                 if basetype_content["name"] == "derived":
  1132                     return self.GenerateVarTree(basetype_content["value"].getname())
  1134                     return self.GenerateVarTree(basetype_content["value"].getname())
  1245                 else:
  1247                 else:
  1246                     return returntype_content["name"]
  1248                     return returntype_content["name"]
  1247         return None
  1249         return None
  1248 
  1250 
  1249     # Function that add a new plugin to the plugin list
  1251     # Function that add a new plugin to the plugin list
  1250     def AddPluginBlockList(self, blocklist):
  1252     def AddPluginTypesList(self, typeslist):
  1251         self.PluginTypes.extend(blocklist)
  1253         self.PluginTypes.extend(typeslist)
  1252         
  1254         
  1253     # Function that clear the plugin list
  1255     # Function that clear the plugin list
  1254     def ClearPluginTypes(self):
  1256     def ClearPluginTypes(self):
  1255         for i in xrange(len(self.PluginTypes)):
  1257         for i in xrange(len(self.PluginTypes)):
  1256             self.PluginTypes.pop(0)
  1258             self.PluginTypes.pop(0)
  1257 
  1259 
       
  1260     def GetPluginBlockTypes(self):
       
  1261         return [{"name": _("%s POUs") % plugintypes["name"],
       
  1262                  "list": plugintypes["types"].GetCustomBlockTypes()}
       
  1263                 for plugintypes in self.PluginTypes]
       
  1264         
       
  1265     def GetPluginDataTypes(self, exclude = ""):
       
  1266         return [{"name": _("%s Data Types") % plugintypes["name"],
       
  1267                  "list": [datatype["name"] for datatype in plugintypes["types"].GetCustomDataTypes(exclude, True)]}
       
  1268                 for plugintypes in self.PluginTypes]
       
  1269     
       
  1270     def GetPluginDataType(self, type):
       
  1271         for plugintype in self.PluginTypes:
       
  1272             datatype = plugintype["types"].getdataType(type)
       
  1273             if datatype is not None:
       
  1274                 return datatype
       
  1275         return None
       
  1276     
  1258     def GetVariableLocationTree(self):
  1277     def GetVariableLocationTree(self):
  1259         return []
  1278         return []
  1260 
  1279 
  1261     # Function that returns the block definition associated to the block type given
  1280     # Function that returns the block definition associated to the block type given
  1262     def GetBlockType(self, type, inputs = None, debug = False):
  1281     def GetBlockType(self, type, inputs = None, debug = False):
  1263         result_blocktype = None
  1282         result_blocktype = None
  1264         for category in BlockTypes + self.PluginTypes:
  1283         for category in BlockTypes + self.GetPluginBlockTypes():
  1265             for blocktype in category["list"]:
  1284             for blocktype in category["list"]:
  1266                 if blocktype["name"] == type:
  1285                 if blocktype["name"] == type:
  1267                     if inputs is not None and inputs != "undefined":
  1286                     if inputs is not None and inputs != "undefined":
  1268                         block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1287                         block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1269                         if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs))):
  1288                         if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs))):
  1293             if words[0] in ["P","T","A"]:
  1312             if words[0] in ["P","T","A"]:
  1294                 name = words[1]
  1313                 name = words[1]
  1295                 type = self.GetPouType(name, debug)
  1314                 type = self.GetPouType(name, debug)
  1296         if type == "function" or words[0] == "T":
  1315         if type == "function" or words[0] == "T":
  1297             blocktypes = []
  1316             blocktypes = []
  1298             for category in BlockTypes + self.PluginTypes:
  1317             for category in BlockTypes + self.GetPluginBlockTypes():
  1299                 cat = {"name" : category["name"], "list" : []}
  1318                 cat = {"name" : category["name"], "list" : []}
  1300                 for block in category["list"]:
  1319                 for block in category["list"]:
  1301                     if block["type"] == "function":
  1320                     if block["type"] == "function":
  1302                         cat["list"].append(block)
  1321                         cat["list"].append(block)
  1303                 if len(cat["list"]) > 0:
  1322                 if len(cat["list"]) > 0:
  1304                     blocktypes.append(cat)
  1323                     blocktypes.append(cat)
  1305         else:
  1324         else:
  1306             blocktypes = [category for category in BlockTypes + self.PluginTypes]
  1325             blocktypes = [category for category in BlockTypes + self.GetPluginBlockTypes()]
  1307         project = self.GetProject(debug)
  1326         project = self.GetProject(debug)
  1308         if project is not None:
  1327         if project is not None:
  1309             blocktypes.append({"name" : USER_DEFINED_POUS, "list": project.GetCustomBlockTypes(name, type == "function" or words[0] == "T")})
  1328             blocktypes.append({"name" : USER_DEFINED_POUS, "list": project.GetCustomBlockTypes(name, type == "function" or words[0] == "T")})
  1310         return blocktypes
  1329         return blocktypes
  1311 
  1330 
  1312     # Return Function Block types checking for recursion
  1331     # Return Function Block types checking for recursion
  1313     def GetFunctionBlockTypes(self, tagname = "", debug = False):
  1332     def GetFunctionBlockTypes(self, tagname = "", debug = False):
  1314         blocktypes = []
  1333         blocktypes = []
  1315         for category in BlockTypes + self.PluginTypes:
  1334         for category in BlockTypes + self.GetPluginBlockTypes():
  1316             for block in category["list"]:
  1335             for block in category["list"]:
  1317                 if block["type"] == "functionBlock":
  1336                 if block["type"] == "functionBlock":
  1318                     blocktypes.append(block["name"])
  1337                     blocktypes.append(block["name"])
  1319         project = self.GetProject(debug)
  1338         project = self.GetProject(debug)
  1320         if project is not None:
  1339         if project is not None:
  1347         if project is not None:
  1366         if project is not None:
  1348             name = ""
  1367             name = ""
  1349             words = tagname.split("::")
  1368             words = tagname.split("::")
  1350             if words[0] in ["D"]:
  1369             if words[0] in ["D"]:
  1351                 name = words[1]
  1370                 name = words[1]
  1352             datatypes.extend(project.GetCustomDataTypes(name, only_locatables))
  1371             datatypes.extend([datatype["name"] for datatype in project.GetCustomDataTypes(name, only_locatables)])
  1353         return datatypes
  1372         return datatypes
  1354 
  1373 
  1355     # Return Base Type of given possible derived type
  1374     # Return Base Type of given possible derived type
  1356     def GetBaseType(self, type, debug = False):
  1375     def GetBaseType(self, type, debug = False):
  1357         project = self.GetProject(debug)
  1376         project = self.GetProject(debug)
  1358         if project is not None:
  1377         if project is not None:
  1359             return project.GetBaseType(type)
  1378             result = project.GetBaseType(type)
       
  1379             if result is not None:
       
  1380                 return result
       
  1381         for plugintype in self.PluginTypes:
       
  1382             result = plugintype["types"].GetBaseType(type)
       
  1383             if result is not None:
       
  1384                 return result
  1360         return None
  1385         return None
  1361 
  1386 
  1362     def GetBaseTypes(self):
  1387     def GetBaseTypes(self):
  1363         '''
  1388         '''
  1364         return the list of datatypes defined in IEC 61131-3.
  1389         return the list of datatypes defined in IEC 61131-3.
  1366         which makes it easy for a user to find a type in a menu.
  1391         which makes it easy for a user to find a type in a menu.
  1367         '''
  1392         '''
  1368         return [x for x,y in TypeHierarchy_list if not x.startswith("ANY")]
  1393         return [x for x,y in TypeHierarchy_list if not x.startswith("ANY")]
  1369 
  1394 
  1370     def IsOfType(self, type, reference, debug = False):
  1395     def IsOfType(self, type, reference, debug = False):
  1371         project = self.GetProject(debug)
  1396         if reference is None:
  1372         if project is not None:
       
  1373             return project.IsOfType(type, reference)
       
  1374         elif reference is None:
       
  1375             return True
  1397             return True
  1376         elif type == reference:
  1398         elif type == reference:
  1377             return True
  1399             return True
       
  1400         elif type in TypeHierarchy:
       
  1401             return self.IsOfType(TypeHierarchy[type], reference)
  1378         else:
  1402         else:
  1379             if type in TypeHierarchy:
  1403             project = self.GetProject(debug)
  1380                 return self.IsOfType(TypeHierarchy[type], reference)
  1404             if project is not None and project.IsOfType(type, reference):
  1381         return None
  1405                 return True
       
  1406             for plugintype in self.PluginTypes:
       
  1407                 if plugintype["types"].IsOfType(type, reference):
       
  1408                     return True
       
  1409         return False
  1382     
  1410     
  1383     def IsEndType(self, type):
  1411     def IsEndType(self, type):
  1384         if type is not None:
  1412         if type is not None:
  1385             return not type.startswith("ANY")
  1413             return not type.startswith("ANY")
  1386         return True
  1414         return True
  1389         if isinstance(type, TupleType):
  1417         if isinstance(type, TupleType):
  1390             return False 
  1418             return False 
  1391         project = self.GetProject(debug)
  1419         project = self.GetProject(debug)
  1392         if project is not None:
  1420         if project is not None:
  1393             datatype = project.getdataType(type)
  1421             datatype = project.getdataType(type)
       
  1422             if datatype is None:
       
  1423                 datatype = self.GetPluginDataType(type)
  1394             if datatype is not None:
  1424             if datatype is not None:
  1395                 return project.IsLocatableType(datatype)
  1425                 return project.IsLocatableType(datatype)
  1396         return True
  1426         return True
  1397     
  1427     
  1398     def IsEnumeratedType(self, type, debug = False):
  1428     def IsEnumeratedType(self, type, debug = False):
  1399         project = self.GetProject(debug)
  1429         project = self.GetProject(debug)
  1400         if project is not None:
  1430         if project is not None:
  1401             datatype = project.getdataType(type)
  1431             datatype = project.getdataType(type)
       
  1432             if datatype is None:
       
  1433                 datatype = self.GetPluginDataType(type)
  1402             if datatype is not None:
  1434             if datatype is not None:
  1403                 basetype_content = datatype.baseType.getcontent()
  1435                 basetype_content = datatype.baseType.getcontent()
  1404                 return basetype_content["name"] == "enum"
  1436                 return basetype_content["name"] == "enum"
  1405         return False
  1437         return False
  1406 
  1438 
  1407     def GetDataTypeRange(self, type, debug = False):
  1439     def GetDataTypeRange(self, type, debug = False):
  1408         project = self.GetProject(debug)
  1440         if type in DataTypeRange:
  1409         if project is not None:
       
  1410             return project.GetDataTypeRange(type)
       
  1411         elif type in DataTypeRange:
       
  1412             return DataTypeRange[type]
  1441             return DataTypeRange[type]
       
  1442         else:
       
  1443             project = self.GetProject(debug)
       
  1444             if project is not None:
       
  1445                 result = project.GetDataTypeRange(type)
       
  1446                 if result is not None:
       
  1447                     return result
       
  1448             for plugintype in self.PluginTypes:
       
  1449                 result = plugintype["types"].GetDataTypeRange(type)
       
  1450                 if result is not None:
       
  1451                     return result
  1413         return None
  1452         return None
  1414     
  1453     
  1415     # Return Subrange types
  1454     # Return Subrange types
  1416     def GetSubrangeBaseTypes(self, exclude, debug = False):
  1455     def GetSubrangeBaseTypes(self, exclude, debug = False):
  1417         project = self.GetProject(debug)
  1456         subrange_basetypes = []
  1418         if project is not None:
  1457         project = self.GetProject(debug)
  1419             return project.GetSubrangeBaseTypes(exclude)
  1458         if project is not None:
  1420         return []
  1459             subrange_basetypes.extend(project.GetSubrangeBaseTypes(exclude))
       
  1460         for plugintype in self.PluginTypes:
       
  1461             subrange_basetypes.extend(plugintype["types"].GetSubrangeBaseTypes(exclude))
       
  1462         return DataTypeRange.keys() + subrange_basetypes
  1421     
  1463     
  1422     # Return Enumerated Values
  1464     # Return Enumerated Values
  1423     def GetEnumeratedDataValues(self, type = None, debug = False):
  1465     def GetEnumeratedDataValues(self, type = None, debug = False):
  1424         project = self.GetProject(debug)
  1466         values = []
  1425         if project is not None:
  1467         project = self.GetProject(debug)
  1426             return project.GetEnumeratedDataTypeValues(type)
  1468         if project is not None:
  1427         return []
  1469             values.extend(project.GetEnumeratedDataTypeValues(type))
       
  1470             if type is None and len(values) > 0:
       
  1471                 return values
       
  1472         for plugintype in self.PluginTypes:
       
  1473             values.extend(plugintype["types"].GetEnumeratedDataTypeValues(type))
       
  1474             if type is None and len(values) > 0:
       
  1475                 return values
       
  1476         return values
  1428 
  1477 
  1429 #-------------------------------------------------------------------------------
  1478 #-------------------------------------------------------------------------------
  1430 #                   Project Element tag name computation functions
  1479 #                   Project Element tag name computation functions
  1431 #-------------------------------------------------------------------------------
  1480 #-------------------------------------------------------------------------------
  1432     
  1481