Closed Greg-Belcher closed 8 years ago
Hi @Greg-Belcher!
Many thanks for reporting this. Some time ago I did some refactoring and forgot to update the wiki :( Could change your scripts to follow the new example in the https://github.com/kamilfb/mqtt-spy/wiki/ScriptingSubscriptions wiki?
The main difference is that now you need to name your method onMessage()
, not call it from anywhere, and best to use mqtt.publish rather than mqttspy.publish
I hope it works - let me know!
Hi Kamil,
Many thanks for your rapid reply. I am pleased to confirm that having implemented your suggested changes, the script is now working as intended. :)
With regard to your Auto-reply example...
function onMessage()
{
mqtt.publish(
"/reply", "
Do you have any additional information about the XML structure you have used in this example? Is there some object model or API document that defines the CDATA and XML tags used here? Apologies for my ignorance.
regards Greg
Cool - glad to hear it works!
The example is just some pretty random XML with a CDATA tag, so that it is still valid XML if your payload is XML itself. You don't need to use XML in reply - totally up to you. The mqtt.publish works the same as for a normal publication, so whatever topic and payload you want. Does this answer your question?
Hi Kamil,
Yes thanks - your answer is clear to me. Thanks again for this great test tool.
regards Greg
Good afternoon. It would be desirable to express gratitude for the MQTT_SPY. Very help in work. Alex.
@Greg-Belcher @elAlexOm - that's great you like it! Maybe you would consider making a small contribution to UNICEF at https://www.justgiving.com/mqtt-spy/ ? Don't forget to star it on GitHub and follow mqtt-spy on Twitter! :)
Hi Kamil, I'm trying to write a simple script that will execute when a subscription is received. It will read some data from the received payload and then publish it to a new topic. I have assigned the script to the script tab in the subscriptions topic of the connection management configuration dialog window. When I re-open the connection, the log shows the script failed with "unable to reference receivedMessage" object error. During my fault finding efforts, I've copied your publishReply() script example and it too fails with this "unable to reference receivedMessage" error.
The following script works fine if I inject it manually (using the scripted publications list where I can right click on the selected script and select Start).
function mirrorSinePVtoSPmanual() { // get the current PV value from the received message payload and write it back to the SP parameter for the point Sine // PV = process variable parameter // SP = setpoint parameter // received message payload is in the form {"id":"ASCEPKS.Points.SinePV","val":4352}
// subscribed topic is "iotgateway/Sine" // published topic is "iotgateway/write"
var topic = "iotgateway/write"; var rxMsg = "{\"id\":\"ASCEPKS.Points.SinePV\",\"val\":4352}"; // inject fixed received message payload stub for testing var rxVal = JSON.parse(rxMsg); var PV = rxVal.val; // SP = setpoint parameter of the point Sine var SP = "[{\"id\":\"ASCEPKS.Points.SineSP\",\"v\":\"" + PV + "\"}]"; // write received PV back to the SP parameter mqttspy.publish(topic, SP, 0, false); return true; } mirrorSinePVtoSPmanual();
The following version of the script fails with the "unable to reference "receivedMessage" object in script" error when I assign it to the script tab of the iotgateway/Sine subscription topic. You will see that the only difference between the two scripts is that this one refers to the ReceivedMessage object in place of the previous fixed test string and, of course, the alternate triggering method.
function mirrorSinePVtoSPauto() { // get the current PV value from the received message payload and write it back to the SP parameter for the point Sine // PV = process variable parameter // SP = setpoint parameter // received message payload is in the form {"id":"ASCEPKS.Points.SinePV","val":4352}
// subscribed topic is "iotgateway/Sine" // published topic is "iotgateway/write"
var topic = "iotgateway/write"; var rxMsg = receivedMessage.getPayload(); // use the actual received message object var rxVal = JSON.parse(rxMsg); var PV = rxVal.val; // SP = setpoint parameter of the point Sine var SP = "[{\"id\":\"ASCEPKS.Points.SineSP\",\"v\":\"" + PV + "\"}]"; // write received PV back to the SP parameter mqttspy.publish(topic, SP, 0, false); return true; } mirrorSinePVtoSPauto();
Any clues about why this error is occurring? Is there a problem with publishing a reply to a different topic from which the message was received?
Thanks for your assistance. Greg