stjohnjohnson / smartthings-mqtt-bridge

Bridge between SmartThings and MQTT
https://hub.docker.com/r/stjohnjohnson/smartthings-mqtt-bridge/
MIT License
363 stars 242 forks source link

SmartApp triggers ok, but bridge.deviceNotification(json) not triggered #205

Open dcolley opened 5 years ago

dcolley commented 5 years ago

I [mostly] followed the README with mosquitto running natively on rpi3. pub and sub work from other computers in the house, to it seems to be fine.

The server.js is also running on the rpi3, listening on port 8085.

I created a Sim Switch, and linked the SmartApp to the on/off Switch

// Receive an event from a device
def inputHandler(evt) {
    log.debug "MBA: inputHandler firing..."
    //log.debug "  name: '${evt.displayName}'"
    //log.debug "  value: '${evt.value}'"
    //log.debug "  type: '${evt.name}'"

    if (
        state.ignoreEvent
        && state.ignoreEvent.name == evt.displayName
        && state.ignoreEvent.type == evt.name
        && state.ignoreEvent.value == evt.value
    ) {
        log.debug "Ignoring event ${state.ignoreEvent}"
        state.ignoreEvent = false;
    } else {
        def json = new JsonOutput().toJson([
            path: "/push",
            body: [
                name: evt.displayName,
                value: evt.value,
                type: evt.name
            ]
        ])

        //String ip = bridge.getConf("ip")
        //log.debug "MBA: inputHandler: bridge.ip='${ip}'"
        //log.debug "bridge:"
        //log.debug "${bridge}"
        log.debug "Forwarding device event to bridge: ${json}"
        if(bridge == null) {
            log.debug "MBA: bridge == null !!!!"
        } else {
            bridge.deviceNotification(json)
        }
    }
}

Ok, the bridge is not NULL, but it's just the name of the bridge... I would expect it to be an object

When I trigger a Sim Switch, I can see the ST live log

12d065a0-ac58-4d40-b06b-5b4ab5180756  23:14:59: debug Forwarding device event to bridge: {"path":"/push","body":{"name":"Sim Switch","value":"on","type":"switch"}}
12d065a0-ac58-4d40-b06b-5b4ab5180756  23:14:59: debug MQTT Device
12d065a0-ac58-4d40-b06b-5b4ab5180756  23:14:59: debug bridge:
12d065a0-ac58-4d40-b06b-5b4ab5180756  23:14:59: debug MBA: inputHandler firing...

As you can see, 'forwarding to device' debug message is logged.

However, in the device handler - there is nothing logged.

// Send message to the Bridge
def deviceNotification(message) {
    log.debug "MBH: deviceNotification: '${message}'" // <<==== WE NEVER GET HERE
    if (device.hub == null) {
        log.error "Hub is null, must set the hub in the device settings so we can get local hub IP and port"
        return
    }

    log.debug "Sending '${message}' to device"
    setNetworkAddress()

    def slurper = new JsonSlurper()
    def parsed = slurper.parseText(message)

    if (parsed.path == '/subscribe') {
        parsed.body.callback = device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
    }

    def headers = [:]
    headers.put("HOST", "$ip:$port")
    headers.put("Content-Type", "application/json")

    // this action will "POST" the "parsed.body" to the MQTT server "parsed.path"
    log.debug "MBH: deviceNotification(): parsed.path='${parsed.path}'"
    log.debug "MBH: deviceNotification(): parsed.body='${parsed.body}'"
    def hubAction = new physicalgraph.device.HubAction(
        method: "POST",
        path: parsed.path,
        headers: headers,
        body: parsed.body,
        callback: calledBackHandler  // I ADDED THIS, but it's not triggered
    )
    hubAction
}

How can I force / debug the connection from the SmartApp to the DeviceHandler?

dcolley commented 5 years ago

If anyone is interested, I also asked the question here: https://community.smartthings.com/t/mqtt-bridge-smartapp-does-not-trigger-dh/167834