Hello,
I'm trying to push data of ignition. I found a tutorial online but the code doesn't work for me. Does anyone see any glaring issues? This is in my Gateway scripting events and in the Gateway library I have the path to this script in a tag change.
import json
def publishOnMqtt(initialChange, executionCount, newValue, previousValue, event):
# log change event with the new value and the tag path
logger = system.util.getLogger("MqttLogger")
tagPath = event.getTagPath().toString()
topic = tagPath.split("/")[-1] # extract tag path
value = newValue.getValue()
logger.trace("TAGCHANGE: {} changed to {}".format(topic, value))
payload = {
"value": value,
"timestamp": system.date.format(newValue.getTimestamp(), "dd-MM-yyyy"), # correct format
"quality": str(newValue.getQuality())
}
qos = 1 # quality of service
retain = False
payloadStr = json.dumps(payload)
# publish the message to broker
system.cirruslink.engine.publish("Chariot SCADA", topic, payload, qos, retain)
My bad, I'll explain in more detail. Though I've saved the project, it's as if the script doesn't exist. There is nothing connected to it in the logs and and when I check ignition it says there is no script currently running. I've checked the application I'm trying to send the data to and it's empty. I figured there must be something wrong within the script.
I copied it’s path to put in a script in a tag change and made the tag a folder in my tag browser. The tags in question are changing every second. This is what is calling the script. Thanks for the reply.
Do you have the project you're using set as the gateway scripting project in the configuration of your gateway? If not, depending on where you're calling this from, it could be that it can't get to the script because of this.
You're logging at the trace level, make sure the logger is set up to record logs of that level:
Go to the logs page on the gateway interface, and click the settings wheel:
Find your logger and make sure the level is set to "trace"
Otherwise you won't ever see what you're logging.
Alternatively, change the logger.trace call in your script to a higher level that will show in the logs.
Next, I suggest commenting out the whole script and just logging something to make sure that the script is actually called when you think it is.
Then we can start tracking issues.
For easy debugging: Use try/except and log the error using str(sys.exc_info())
And/or call your function from the script console and sprinkle some print('at LOC 12'), ... lines all over it.
Also: just use jsonEncode
And one more thing: Use a gateway tag change script.
If you don't wrap your code in try/except clauses, the errors will bubble up to the top level and show up in the logs anyway. Unless something else catches it before it reaches the surface, of course.
For debugging, I'd recommend not wrapping the code and letting the "natural" process do its job, at least for the first round.