Skip to content

Commit 9e6e7dc

Browse files
committed
Add ability to force a device disconnect
Fix #116
1 parent d642196 commit 9e6e7dc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ You can also connect to a device using MQTT packets:
298298
* `/ble/notify/DEVICE/SERVICE/CHARACTERISTIC` connects and starts notifications on the characteristic, which
299299
send data back on `/ble/data/DEVICE/SERVICE/CHARACTERISTIC`
300300
* `/ble/ping/DEVICE` connects, or maintains a connection to the device, and sends `/ble/pong/DEVICE` on success
301+
* `/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)
301302

302303
`SERVICE` and `CHARACTERISTIC` are either known names from [attributes.js](https://github.com/espruino/EspruinoHub/blob/master/lib/attributes.js)
303304
such as `nus` and `nus_tx` or are of the form `6e400001b5a3f393e0a9e50e24dcca9e` for 128 bit uuids or `abcd` for 16 bit UUIDs.

lib/mqttclient.js

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ client.on("connect", function () {
5858
client.subscribe(config.mqtt_prefix + "/read/#");
5959
client.subscribe(config.mqtt_prefix + "/notify/#");
6060
client.subscribe(config.mqtt_prefix + "/ping/#");
61+
client.subscribe(config.mqtt_prefix + "/disconnect/#");
6162
client.publish(`${config.mqtt_prefix}/state`, "online", {retain: true});
6263
// Push out updated presence info
6364
discovery.sendMQTTPresence();
@@ -156,5 +157,16 @@ client.on("message", function (topic, message) {
156157
log("Ping " + id + " but not in range");
157158
}
158159
}
160+
if (path[1] === "disconnect") { // disconnect device
161+
var id = devices.deviceToAddr(path[2]);
162+
if (discovery.inRange[id]) {
163+
var device = discovery.inRange[id].peripheral;
164+
connect.disconnect(device, function () {
165+
client.publish(config.mqtt_prefix + "/disconnected/" + path[2], message);
166+
});
167+
} else {
168+
log("Disconnect " + id + " but not in range");
169+
}
170+
}
159171
}
160172
});

0 commit comments

Comments
 (0)