Add serial disconnect event

This commit is contained in:
GHOSCHT 2021-08-27 20:03:26 +02:00
parent 41461139c3
commit a60acb1381
3 changed files with 13 additions and 2 deletions

View file

@ -7,9 +7,12 @@ class PortController {
parser: any;
onCloseCallback: (error:Error)=>void;
constructor() {
this.path = null;
this.port = null;
this.onCloseCallback = () => {};
}
get isOpen() {
@ -29,7 +32,7 @@ class PortController {
this.parser = this.port.pipe(new parsers.Readline({ delimiter: "\r\n" }));
}
open(callback: (error:Error | null | undefined)=>void) {
open(callback: (error:Error | null | undefined)=>void, onCloseCallback:(error:Error)=>void) {
if (this.isOpen) {
throw new Error("Port already open");
}
@ -39,11 +42,14 @@ class PortController {
}
this.port.open((error) => callback(error));
this.port.on("close", onCloseCallback);
this.onCloseCallback = onCloseCallback;
}
close() {
if (this.port) {
this.port.close();
this.port.removeListener("data", this.onCloseCallback);
}
}
}

View file

@ -24,6 +24,11 @@ const connect = (): ThunkResult<void> => async (dispatch, getState) => {
} else {
dispatch(connectionFailure(error));
}
}, (error) => {
if (error.message === "Reading from COM port (ReadIOCompletion): Access denied") {
dispatch(connectionFailure(new Error("Device disconnected")));
dispatch(connectionEnd());
}
});
};

View file

@ -45,9 +45,9 @@ const SerialConnectionReducer = createReducer(initialState)
...state,
portController: null,
status: {
...state.status,
connecting: false,
connected: false,
error: null,
},
}))
.handleType(SerialConnectionActionTypes.SET_PORT_CONTROLLER, (state, action) => ({