import re import system reDefault = re.compile(r'\[[^[]+\]') def _extractParameter(params, parameter, path): val = params[parameter].value if hasattr(val, "bindType"): return system.tag.readBlocking([path+"/Parameters."+parameter])[0].value else: return val def _isGroupedUDT(obj): for tag in obj["tags"]: if tag["name"] in ["Graphics", "StatusText"]: return False return True def getSiblingDevices(tagPath, filterType=None, filterTagPath=True): devices = [] def extractStatusText(obj, path): # Scan for status text tag: for tag in obj["tags"]: if tag["name"] == "StatusText": return "[ignition]" + reDefault.sub("", path) + "/StatusText" # Couldn't find it, so return None: return None def scan(obj, parentPath): if isinstance(obj, list): for tag in obj: scan(tag, parentPath) else: path = parentPath + "/" + obj["name"] if str(obj["tagType"]) == "Folder" or (str(obj["tagType"]) == "UdtInstance" and (tagPath <> path or not filterTagPath) and _isGroupedUDT(obj)): for tag in obj["tags"]: scan(tag, path) elif str(obj["tagType"]) == "UdtInstance" and (tagPath <> path or not filterTagPath): params = obj["parameters"] # Make sure this is a renderable device: if "DeviceType" in params: deviceType = _extractParameter(params, "DeviceType", path) if filterType == None or deviceType in filterType: labelFull = _extractParameter(params, "LabelFull", path) devices.append({ "deviceType": deviceType, "tagPath": path, "device": labelFull,#"[ignition]" + reDefault.sub("", path) + "/Parameters.LabelFull", "status": extractStatusText(obj, path) }) # Scan devices: if tagPath not in [None, ""]: tagPath = reDefault.sub("", tagPath) # Remove [default] from tagpath parentPath = "/".join(tagPath.split("/")[:-1]) config = system.tag.getConfiguration(parentPath, True) #system.perspective.print(parentPath) #system.perspective.print(config) if len(config) > 0 and "tags" in config[0]: scan(config[0]["tags"], parentPath) devices.sort(key=lambda x: x["device"]) return devices def _isTagOPC(path): try: return system.tag.readBlocking([path+".ValueSource"])[0].value == "opc" except: return False def _isTagEnabled(path): try: return system.tag.readBlocking([path+".Enabled"])[0].value except: return False def getOPCTags(tagPath): tags = [] def scan(obj, parentPath, relPath=""): if isinstance(obj, list): for tag in obj: scan(tag, parentPath, relPath) elif "name" in obj: path = parentPath + "/" + obj["name"] if "tags" in obj: # Scan deeper: relPath = relPath + obj["name"] + "/" for tag in obj["tags"]: scan(tag, path, relPath) else: if _isTagEnabled(path) and _isTagOPC(path):#and obj["name"] not in ["Graphics"]: # Found an OPC tag: tags.append({ "tagName": relPath + obj["name"], "tagType": "opc", "tagPath": "[ignition]" + path + ".OpcItemPath", "tagValue": "[ignition]" + path, "tagTooltip": "[ignition]" + path + ".Tooltip", "tagDocumentation": "[ignition]" + path + ".Documentation" }) # Scan devices: if tagPath not in [None, ""]: tagPath = reDefault.sub("", tagPath) # Remove [default] from tagpath config = system.tag.getConfiguration(tagPath, True) if len(config) > 0 and "tags" in config[0]: scan(config[0]["tags"], tagPath) tags.sort(key=lambda x: x["tagName"]) return tags