Skip to content

Commit

Permalink
Add ability to force a device disconnect
Browse files Browse the repository at this point in the history
Fix #116
  • Loading branch information
gfwilliams committed Jan 2, 2024
1 parent d642196 commit 9e6e7dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions lib/mqttclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
}
}
});

0 comments on commit 9e6e7dc

Please sign in to comment.