diff --git a/README.md b/README.md index 897be36..1ed9114 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,7 @@ You can also connect to a device using MQTT packets: * `/ble/notify/DEVICE/SERVICE/CHARACTERISTIC` connects and starts notifications on the characteristic, which send data back on `/ble/data/DEVICE/SERVICE/CHARACTERISTIC` * `/ble/ping/DEVICE` connects, or maintains a connection to the device, and sends `/ble/pong/DEVICE` on success +* `/ble/disconnect/DEVICE` will force a disconnect and send `/ble/disconnected/DEVICE` on completion. This is not normally required as EspruinoHub will automatically disconnect after a period of inactivity (see `connection_timeout` and `connection_alive_on_notify` in the config file) `SERVICE` and `CHARACTERISTIC` are either known names from [attributes.js](https://github.com/espruino/EspruinoHub/blob/master/lib/attributes.js) such as `nus` and `nus_tx` or are of the form `6e400001b5a3f393e0a9e50e24dcca9e` for 128 bit uuids or `abcd` for 16 bit UUIDs. diff --git a/lib/mqttclient.js b/lib/mqttclient.js index 2dc0246..bad65dd 100644 --- a/lib/mqttclient.js +++ b/lib/mqttclient.js @@ -58,6 +58,7 @@ client.on("connect", function () { client.subscribe(config.mqtt_prefix + "/read/#"); client.subscribe(config.mqtt_prefix + "/notify/#"); client.subscribe(config.mqtt_prefix + "/ping/#"); + client.subscribe(config.mqtt_prefix + "/disconnect/#"); client.publish(`${config.mqtt_prefix}/state`, "online", {retain: true}); // Push out updated presence info discovery.sendMQTTPresence(); @@ -156,5 +157,16 @@ client.on("message", function (topic, message) { log("Ping " + id + " but not in range"); } } + if (path[1] === "disconnect") { // disconnect device + var id = devices.deviceToAddr(path[2]); + if (discovery.inRange[id]) { + var device = discovery.inRange[id].peripheral; + connect.disconnect(device, function () { + client.publish(config.mqtt_prefix + "/disconnected/" + path[2], message); + }); + } else { + log("Disconnect " + id + " but not in range"); + } + } } });