Skip to content

Commit

Permalink
fix: Revert Deconz changes of #859 in an attempt to fix Koenkk/zigbee…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Feb 13, 2024
1 parent 472bce9 commit 818f2cc
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions src/adapter/deconz/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ class Driver extends events.EventEmitter {
return paths.length > 0 ? paths[0] : null;
}

private onPortError(error: Error): void {
debug(`Port error: ${error}`);
}

private onPortClose(): void {
debug('Port closed');
this.initialized = false;
Expand All @@ -188,7 +184,7 @@ class Driver extends events.EventEmitter {
return this.portType === 'serial' ? this.openSerialPort(baudrate) : this.openSocketPort();
}

private async openSerialPort(baudrate: number): Promise<void> {
public openSerialPort(baudrate: number): Promise<void> {
debug(`Opening with ${this.path}`);
this.serialPort = new SerialPort({path: this.path, baudRate: baudrate, autoOpen: false}); //38400 RaspBee //115200 ConBee3

Expand All @@ -200,23 +196,21 @@ class Driver extends events.EventEmitter {
this.serialPort.pipe(this.parser);
this.parser.on('parsed', this.onParsed);

try {
await this.serialPort.asyncOpen();
debug('Serialport opened');

this.serialPort.once('close', this.onPortClose.bind(this));
this.serialPort.once('error', this.onPortError.bind(this));

this.initialized = true;
} catch (error) {
this.initialized = false;

if (this.serialPort.isOpen) {
this.serialPort.close();
}

throw error;
}
return new Promise((resolve, reject): void => {
this.serialPort.open(async (error: object): Promise<void> => {
if (error) {
reject(new Error(`Error while opening serialport '${error}'`));
this.initialized = false;
if (this.serialPort.isOpen) {
this.serialPort.close();
}
} else {
debug('Serialport opened');
this.initialized = true;
resolve();
}
});
});
}

private async openSocketPort(): Promise<void> {
Expand Down Expand Up @@ -246,7 +240,7 @@ class Driver extends events.EventEmitter {
resolve();
});

this.socketPort.once('close', this.onPortClose.bind(this));
this.socketPort.once('close', this.onPortClose);

this.socketPort.on('error', function () {
debug('Socket error');
Expand All @@ -258,27 +252,29 @@ class Driver extends events.EventEmitter {
});
}

public async close(): Promise<void> {
debug('closing');
queue = [];

if (this.initialized) {
this.initialized = false;

if (this.portType === 'serial') {
try {
await this.serialPort.asyncFlushAndClose();
} catch (error) {
this.emit('close');

throw error;
public close(): Promise<void> {
return new Promise((resolve, reject): void => {
if (this.initialized) {
if (this.portType === 'serial') {
this.serialPort.flush((): void => {
this.serialPort.close((error): void => {
this.initialized = false;
error == null ?
resolve() :
reject(new Error(`Error while closing serialport '${error}'`));
this.emit('close');
});
});
} else {
this.socketPort.destroy();
resolve();
}
} else {
this.socketPort.destroy();
resolve();
this.emit('close');
}
}
});

this.emit('close');
}

public readParameterRequest(parameterId: number) : Promise<Command> {
Expand Down

1 comment on commit 818f2cc

@SilverPhoenix99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, this is the line that's causing the problem when Conbee II seems to crash: https://github.com/serialport/node-serialport/blob/main/packages/stream/lib/index.ts#L352

This is the error I get from the library:

serialport/stream disconnected [Error: bad file descriptor]
serialport/stream #close
serialport/stream _read queueing _read for after open
serialport/stream binding.close finished
zigbee-herdsman:deconz:driver Port closed

Unfortunately not very informative, and looks like a hardware issue, but there's still some possibility that it's triggered by ZH, although I can't immediately see how, just that it only happens after the refactor was introduced.

When the disconnect happens, the library emits a close event that includes a DisconnectError as the 1st parameter: https://github.com/serialport/node-serialport/blob/main/packages/stream/lib/index.ts#L330

Although it would still hide the underlying issue, it should be possible to adapt onPortClose to detect when the disconnect happens and try to reopen the port (or give up).

Please sign in to comment.