detect serial disconnect

This commit is contained in:
liamcottle 2025-02-11 23:36:49 +13:00
parent 995e740347
commit af6403bf6a
2 changed files with 12 additions and 0 deletions

View file

@ -142,18 +142,21 @@
async askForSerialPort() {
this.connection = await SerialConnection.open();
this.connection.on("connected", () => this.onConnected());
this.connection.on("disconnected", () => this.onDisconnected());
// this.connection.on("tx", (data) => console.log("tx", data));
// this.connection.on("rx", (data) => console.log("rx", data));
},
async askForBleDevice() {
this.connection = await BleConnection.open();
this.connection.on("connected", () => this.onConnected());
this.connection.on("disconnected", () => this.onDisconnected());
// this.connection.on("tx", (data) => console.log("tx", data));
// this.connection.on("rx", (data) => console.log("rx", data));
},
async disconnect() {
if(this.connection){
await this.connection.close();
this.onDisconnected();
this.connection = null;
}
},
@ -166,6 +169,10 @@
await this.loadContacts();
});
},
onDisconnected() {
console.log("disconnected");
this.connection = null;
},
async loadSelfInfo() {
this.selfInfo = await this.connection.getSelfInfo();
},

View file

@ -15,6 +15,11 @@ class SerialConnection extends Connection {
this.readBuffer = [];
this.readLoop();
// listen for disconnect
this.serialPort.addEventListener("disconnect", () => {
this.emit("disconnected");
});
// fire connected callback after constructor has returned
setTimeout(() => {
this.onConnected();