I have seen this topic come up a few times recently and I thought I’d share our solution to expose tag data to external systems via the Webdev module.
The example is kind of raw and very specific for our UDT structures; it should be used with caution , since it can traverse through the tag path provided and has the potential to do some damage in large systems…
def tagsToDict(tagsPath):
# TODO: Batch tag reads with system.tag.readAll()
# https://support.inductiveautomation.com/usermanuals/ignition/index.html?system_tag_read.htm
tags = system.tag.browseTags(parentPath=tagsPath)
# Leaf node
if not tags:
tag = system.tag.read(tagsPath)
return {
'value': tag.value,
'quality': tag.quality.toString(),
'timestamp': tag.timestamp,
}
tagDict = {}
for tag in tags:
tagDict[tag.name] = tagsToDict(tag.fullPath)
return tagDict
tagPathParts = []
pathParts = ['site', 'area', 'line', 'cell', 'tag']
params = request['params']
for part in pathParts:
partVal = params.get(part)
if not partVal:
break
tagPathParts.append(partVal)
tagPath = 'Enterprise/' + '/'.join(tagPathParts)
response = {
'params': dict(zip(pathParts, tagPathParts)),
'data': tagsToDict(tagPath),
}
return { 'json': system.util.jsonEncode(response) }
#Endpoint
http://GatewayHost/main/system/webdev/api/core/get_tags
#Example Requests
http://GatewayHost/main/system/webdev/api/core/get_tags?site=New%20Albany&area=Assembly&line=M98
http://GatewayHost/main/system/webdev/api/core/get_tags?site=New%20Albany&area=Assembly&line=M98&cell=Assembly_Array
http://GatewayHost/main/system/webdev/api/core/get_tags?site=New%20Albany&area=Assembly&line=M98&cell=Assembly_Array&tag=Accumulated%20Parts
Response (JSON)
{
“data”: {
“Assembly_Array”: {
“Accumulated Pins Used”: {
“value”: 134389334,
“quality”: “Good”,
“timestamp”: “Wed Oct 18 07:14:57 EDT 2017”
},
…
},
},
“params”: {
“area”: “Assembly”,
“site”: “New Albany”,
“line”: “M98”
}
}