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

View file

@ -24,6 +24,11 @@ const connect = (): ThunkResult<void> => async (dispatch, getState) => {
} else { } else {
dispatch(connectionFailure(error)); 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, ...state,
portController: null, portController: null,
status: { status: {
...state.status,
connecting: false, connecting: false,
connected: false, connected: false,
error: null,
}, },
})) }))
.handleType(SerialConnectionActionTypes.SET_PORT_CONTROLLER, (state, action) => ({ .handleType(SerialConnectionActionTypes.SET_PORT_CONTROLLER, (state, action) => ({