tobof / openhab-addons

The next-generation open Home Automation Bus (openHAB)
Eclipse Public License 2.0
39 stars 30 forks source link

[mysensors] Multiple MQTT gateways not working #140

Open ftitius opened 4 years ago

ftitius commented 4 years ago

Expected Behavior

If defining multiple MQTT gateways and things the MQTT messages from broker should reach the correct gateway and be correctly assigned to the nodes.

Current Behavior

MQTT messages arriving at the wrong gateway and if the node id there exists are assigned to wrong nodes. Messages for node ids which not exist in gateway were ignored.

Possible Solution

Cause of the bug is that in MySensorsMqttConnection the PipedOutputStream and PipedInputStream were defined as static. Because they handle all in-/outgoing messages the last created MQTT gateway gets a connection with this streams. So all incomming messages go only to the last defined gateway. Fix is to remove the static attribute at both streams in MySensorsMqttConnection.

Second smaller problem is the caching of node ids in one file with name "given_ids.cached". Since each MQTT gateway can contain/define nodes with equal node ids this gets mixed on OH startup. Fix is to include the gateway thing uid in the cache filename in MySensorsBridgeHandler and use the filename parameter in MySensorsCacheFactory.

Steps to Reproduce (for Bugs)

  1. Have a system broker with name "mosquitto" defined via file "org.eclipse.smarthome.mqttbroker.cfg"
  2. Define two MySensors MQTT Bridges via mysensors.things file

    
    Bridge mysensors:bridge-mqtt:gwMQ01 [   brokerName="mosquitto", 
                                        topicPublish="mygw1-in",
                                        topicSubscribe="mygw1-out",
                                        startupCheckEnabled=false ] {
    
    power   powerThing      "power Thing"   [ nodeId="0", childId="1", smartSleep=false, revertState=false ]

}

Bridge mysensors:bridge-mqtt:gwMQ02 [ brokerName="mosquitto", topicPublish="mygw2-in", topicSubscribe="mygw2-out", startupCheckEnabled=false ] {

 temperature     temperature_2_1    "temperature thing"     [ nodeId="2", childId="1", smartSleep=false, revertState=false ]
 humidity        humidity_2_2       "humidity thing"        [ nodeId="2", childId="2", smartSleep=false, revertState=false ]

}

2. Define corresponding MySensors MQTT items via mysensors.items file

Number powerItem "power [%0.1f watt]" { channel="mysensors:power:gwMQ01:powerThing:watt" }

Number temperatureItem "temperature [%0.1f °C]" { channel="mysensors:temperature:gwMQ02:temperature_2_1:temp" } Number humidityItem "humidity [%0.1f %]" { channel="mysensors:humidity:gwMQ02:humidity_2_2:hum" }

3. Add org.openhab.binding.mysensors-2.5.0-SNAPSHOT.jar (from development branch "MySensors_Binding_bnd" with fix for #138 ) to addons folder
4. Clear (delete contents of) userdata/cache folder to abandon old versions of binding
5. Start OH2.5
6. Add missing dependencies via karaf console:

feature:install openhab-core-io-transport-mqtt feature:install openhab-transport-serial


7. Send MQ messages to broker 
mygw2-out/2/1/1/0/0 5.17
mygw2-out/2/1/1/0/0 21.35
mygw1-out/0/1/1/0/17 462.94
mygw2-out/2/1/1/0/0 5.37
mygw2-out/2/1/1/0/0 22.53
mygw1-out/0/1/1/0/17 422.47
mygw2-out/2/1/1/0/0 8.96
mygw2-out/2/1/1/0/0 14.43
8. See in event.log that only changes for mygw2 arrived
[vent.ItemStateChangedEvent] - temperatureItem changed from 7.76 to 5.17
[vent.ItemStateChangedEvent] - humidityItem changed from 92.55 to 21.35
[vent.ItemStateChangedEvent] - temperatureItem changed from 5.17 to 5.37
[vent.ItemStateChangedEvent] - humidityItem changed from 21.35 to 22.53
[vent.ItemStateChangedEvent] - temperatureItem changed from 5.37 to 8.96
[vent.ItemStateChangedEvent] - humidityItem changed from 22.53 to 14.43
## Your Environment
* OH 2.5 and MySensors binding 2.5 (from development branch "MySensors_Binding_bnd" with fix for #138)
* Java 8
* Windows 10 standalone or with Docker image "openhab/openhab:2.5.0-amd64-debian" on Linux