tobof / openhab-addons

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

[MySensors] No MQTT connection with OH2.5 #138

Open ftitius opened 4 years ago

ftitius commented 4 years ago

Expected Behavior

After starting OH or adding the MySensors binding jar to addon folder a MQTT connection to the MQTT broker should be establiehed.

Current Behavior

With OH 2.5 and MySensors binding as osgi bundle its not possible to get a working connection to MQTT broker. In the log you get:

[ERROR] [rotocol.mqtt.MySensorsMqttConnection] - MqttService is null!
[ERROR] [rotocol.mqtt.MySensorsMqttConnection] - Failed connecting to bridge...next retry in 10 seconds (Retry No.:0)

No connection could be establiehed.

Possible Solution

See https://community.openhab.org/t/oh-2-5-snapshot-and-mysensors-binding/86580/29. Until clarified why the osgi dependency injection for MqttService does not work we can use a workaround in MySensorsMqttConnection.establishConnection():

       boolean connectionEstablished = false;

        //Start workaround
        if (MySensorsMqttService.getMqttService() == null) {
            BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
            final ServiceReference sr = bc.getServiceReference(MqttService.class.getName());
            if (sr != null) {
                final MqttService mqtt = (MqttService) bc.getService(sr);
                if (mqtt != null) {
                    new MySensorsMqttService().setMqttService(mqtt);
                }
            }
        }
        //End workaround

        if (MySensorsMqttService.getMqttService() == null) {

Shortly after starting with this binding you see in the log:

        [INFO ] [rotocol.mqtt.MySensorsMqttConnection] - Successfully connected to MySensors Bridge.

and the things are going online in paperui.

Steps to Reproduce (for Bugs)

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

    Bridge mysensors:bridge-mqtt:gwMQ01 [   brokerName="mosquitto", 
                                        topicPublish="mygw1-in",
                                        topicSubscribe="mygw1-out",
                                        startupCheckEnabled=false ] {
    
    // no things yet
    }
  3. Use development branch "MySensors_Binding_bnd" and enable MQTT again in MySensorsBridgeHandler.java:
        } else if (bridgeuid.equals(THING_TYPE_BRIDGE_MQTT)) {
            gatewayConfig.setGatewayType(MySensorsGatewayType.MQTT);
            gatewayConfig.setBrokerName(conf.brokerName);
  4. Build org.openhab.binding.mysensors-2.5.0-SNAPSHOT.jar and add it to addons folder
  5. Clear (delete contents of) userdata/cache folder to abandon old versions of binding
  6. Start OH2.5
  7. Add missing dependencies via karaf console:
    feature:install openhab-core-io-transport-mqtt
    feature:install openhab-transport-serial
  8. See errors in log (see above) or in paperui the

Context

Since changing to osgi bundling of the MySensors binding the dependency injection of the MQTT service ist not working.

Your Environment