kamilfb / mqtt-spy

Please use the new Eclipse Paho GitHub repo instead.
https://github.com/eclipse/paho.mqtt-spy/wiki
345 stars 68 forks source link

Setting payload in onMessage doesn't work properly #97

Closed NicolSpies closed 8 years ago

NicolSpies commented 8 years ago

Hi, @kamilfb, thank you for an excellent tool which I am starting to explore. The message payloads I receive is in JSON and I need to reformat it to XML before it is saved in the log1.messages file. I have started by trying to get the example in the ScriptingSubscriptions Wiki to run.

I have saved the example javascript

function onMessage() { receivedMessage.setPayload("<tag>" + receivedMessage.getPayload() + "- modified :)</tag>"); return true; }

in a file message.js in the MQTTSpy directory and entered the file name in the script column of the subscriptions tab of the topic of my connection. I saved, closed and re-opened the connection.

I am expecting the payload message to be modified in the browser and the log1.messages file. When a message is received it nothing appears to happen and the json payload is unchanged. I am using version 0.52.

What am I doing wrong, please point me in the right direction. Regards

kamilfb commented 8 years ago

Hi @NicolSpies, could you attach your configuration file and the mqtt-spy logs?

NicolSpies commented 8 years ago

Hi Kamil, this problem relates to #99 as I am trying to accomplish the same. The solution in #99 is actually better suited as I need to do it for all the subscribed topics, hundreds of them. It will however be great to link an external .js file similar as what is done for the individual subscriptions.

I will attach the files when I get back to that test platform. Thanks

kamilfb commented 8 years ago

@NicolSpies, have you now got the onMessage being called properly or still not? I'm asking because I've seen the other issues you raised where it seems it is fine. Please advise.

NicolSpies commented 8 years ago

@kamilfb, I have tried the onMessage and could not get it working, hence I tried the formatter as a workaround.

kamilfb commented 8 years ago

@NicolSpies, I see. I will need your config and logs to help further though...

NicolSpies commented 8 years ago

I appreciate your help, to make it simple I will quickly create a fresh log with the javascript referenced in a subscription. Thanks

NicolSpies commented 8 years ago

The clean log file is: mqtt-spy.txt

The absolute path of the javascript file containing the onMessage function is : C:\Program Files (x86)\MQTTSpy\scripts\subscriptions\message.js

The onMessage function is as detailed in the first entry of this issue

NicolSpies commented 8 years ago

Rest of the config files

mqtt-spy-configuration.txt mqtt-spy-stats.txt

NicolSpies commented 8 years ago

The received JSON message payload:

{"type":"test","name":"Oos-Moot","cfg3":{"monthly":{"monthly":0,"time":"2016-08-15T23:00:00Z","deltp":0},"daily":{"daily":0,"time":"2016-08-15T23:00:00Z","deltp":0},"weekly":{"time":"2016-08-15T23:00:00Z","weekly":0,"deltp":0},"hourly":{"hourly":0,"time":"null","deltp":0}},"customer":"Oasis","time":"2016-08-15T23:51:11Z"}

and the messages log indicating that the onMessage did not modify the message payload: log1messages.txt

kamilfb commented 8 years ago

I'd recommend deleting the following from the config:

<BackgroundScript autoStart="false" repeat="false">
                <File>put your script location here...</File>
</BackgroundScript>
NicolSpies commented 8 years ago

I have tried it, still the same, the message in the log still the same with no modifications as expected

kamilfb commented 8 years ago

Yay - I was able to reproduce it. Indeed, the payload in the log doesn't get modified. Also, the "All" tab doesn't get updated with the modified payload either. However, the specific subscription tab has payloads with the modified value! Clearly a bug :)

NicolSpies commented 8 years ago

I have closed mqtt spy and reopened, get the error that the configuration file cannot be processed. mqtt-spy - Copy (2).txt

kamilfb commented 8 years ago

@NicolSpies, delete the whole BackgroundScript tag

NicolSpies commented 8 years ago

Thanks, it is running again

kamilfb commented 8 years ago

@NicolSpies, I will start working on a fix for setting the payload... - thanks for reporting it!

NicolSpies commented 8 years ago

Thank you for all your effort and an excellent product

kamilfb commented 8 years ago

Could you try the latest beta? http://tinyurl.com/mqtt-spy-download/mqtt-spy-0.6.0-beta-b52-jar-with-dependencies.jar

NicolSpies commented 8 years ago

Will gladly do

NicolSpies commented 8 years ago

Hi Kamil, from the testing so far, the onMessage method not triggers correctly and the script linked is executed. Using a script the JSON payload is converted to XML wrapped in CDATA tags which I remove using post processing. Thank you

kamilfb commented 8 years ago

@NicolSpies - thank you for confirming it's fine now! Cheers