I've written a script for performing an http request that I execute every 60s. I'm requesting data from a server that I then set to tag values in my PLC. I run 3 instance of this script (I know it's inefficient to be performing so many tag writes, but it's been requested that the separate subsystems I'm polling are treated independently).
2 of my 3 scripts are running fine, but the one problem script is an enigma. I know the data packet I'm recieving from the request is a list of dictionaries and I know the dictionary keys. When I try to copy the one of vlaues at key 'data'
, Ignition is throwing a KeyError
exception. I've wrapped everything in a try block and I'm throwing the results into my logs, and I am completely stumped.
First my script:
payload = {'names': tagPathUtil.cryoParams,'props': tagPathUtil.cryoProps,'ppmuser':'0'}
subSys = 'cryo'
directory = "/list/numeric/valueAndTime"
headers = {'Accept': 'application/json'}
response = mcrUtil.mcrGetRequest(payload, subSys, directory, tagPathUtil.cryoErrTags, headers=headers)
if response.good:
y = response.json #Set y to the json interpretation of the response object (y is a list)
tags = [] #Create an empty list for the tag value
#Iterate through the response json list
try:
for i in range(len(y)):
tagData = y[i]['data'][0] #Set tagData to the value of the parameter from the http request
tags.append(tagData) #Append tagData to the tag values list
except Exception as e:
message = 'Good http request. Failed on tag assignment. Exception type: %s. ' % e
if isinstance(y,list):
message += 'List returned. '
if isinstance(y[0],dict):
message += 'List of dictionaries. Dictionary keys: %s' % (y[0].keys())
else:
message += 'List of %s.' % (type(y[0]))
else:
message = 'Good http request. Failed on tag assignment. Type returned: .' % (type(y))
system.util.getLogger('Data Logger').info(message)
else:
system.tag.writeBlocking(tagPathUtil.cryoTagPaths, tags)
And now an entry from my logs:
If it's hard to read, it says: "Good http request. Failed on tag assignment. Exception type: 'data'*. List returned. List of dicitonaries. Dictionary keys: ['data','type','entryid','tsource','property','device','tstamp']"
*I thought 'data' would read KeyError since the exception should be the KeyError
Some things that I have noticed:
- If I don't have a successful http request, it's a client side error (typically 400, but I've seen 406).
- If I have a successful http request, I get this exception.
- In the other 2 instances of this script, I am seeing consistent successful tansfers.
- If I run this script in the scripting console, I get Error_TypeConversion feedback in the console.
I've attached my logs below.
wrapper.log (5.2 MB)