Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Unable to use JSON structured mqtt message payload in the lwt property of MQTTService #5197

Closed
doandzhi opened this issue Mar 7, 2018 · 5 comments

Comments

@doandzhi
Copy link
Contributor

doandzhi commented Mar 7, 2018

As Last Will Testament is configured with the lwt property of the MqttService it has a specific formatting that uses which doesn't allow JSON structured messages since the separation between parametters is done using :. I see it as a valid use case to use JSON structure in my LWT message but the property is split by : which makes it impossible. Does it make sense to change the : to something else or it is against some ESH guidelines that I don't know yet?

example usage of lwt property:
org.eclipse.smarthome.mqtt:mosquitto.lwt=mqttTopic:mqttMessagePayload:QoS:retainFlag

wanted usage of lwt property:
org.eclipse.smarthome.mqtt:mosquitto.lwt=mqtt/clients/client1:{"clientID":"client1","status":"online"}:2:1

It takes "client1" as Quality Of Service and throws Exception because it is not a number at all.

Any thoughts on this one?

@doandzhi doandzhi changed the title Unable to use JSON structured message in the lwt property of MQTTService Unable to use JSON structured mqtt message payload in the lwt property of MQTTService Mar 7, 2018
@maggu2810
Copy link
Contributor

AFAIK also the topic is allowed to contain :.

What about this one:

  • If there is a property lwt.topic, use that topic
  • If there is a property lwt.message, use that message
  • If there is a property lwt.qos, use that QoS
  • If there is a property lwt.retain, use that retain flag
  • If some of the properties above are missing, try to parse the lwt property and use the missing part encoded in that property.

@doandzhi
Copy link
Contributor Author

doandzhi commented Mar 7, 2018

Thank you for the response @maggu2810 ! Having a look in the MqttWillAndTestament I see that this fromString(String string) method splits the given parameter by : . Concerning the last will setting after the session has been started I already mentioned this in #5162 . So this is impossible to build any logic around this at the moment (parsing the parameter and setting the lwt later) since it is set when the connection is established.

Sorry my ignorace if I don't understand something properly but at the moment it is impossible for me to use the : in my message payload because the property is not processed properly and there seems to be no alternative to use instead of this configuration property.

@maggu2810
Copy link
Contributor

But there is also a constructor that could be used:

public MqttWillAndTestament(String topic, byte[] payload, int qos, boolean retain)

I have just a very short look at the code, but IMHO it should be possible to change the "addBrokerConnection" method in the MqttService class to implement the above logic.

@doandzhi
Copy link
Contributor Author

doandzhi commented Mar 8, 2018

Yes, valid point @maggu2810 ! As I described in #5162 I tried to use this constructor at first but then it requires some connection handling and my thoughts are in the direction of using already implemented logic of ESH with configuring the MqttService and then getting the broker connection where I need it since it is working as required for almost everything I am doing till now.

Just to clear out, I opened this issue to concern only the lwt property parsing mechanism and propose a change to extend its use cases.

@maggu2810
Copy link
Contributor

Just to clear out, I opened this issue to concern only the lwt property parsing mechanism and propose a change to extend its use cases.

I am fine with a change similar to the above described one. At least if it is backward compatible and allows to use all allowed characters for topic and message.

kaikreuzer pushed a commit that referenced this issue Mar 14, 2018
* MQTT transport: use a broker connection config class
* MQTT transport: improve "MQTT will and testament" class
* MQTT transport: add support to configure lwt parts

Fixes: #5197
Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants