Skip to content

Commit

Permalink
Deconz stability improvements (Koenkk#186)
Browse files Browse the repository at this point in the history
* Don't throw promise.reject() when permit joins fails. But try again.

* Set network paramters according to configuration.yaml

* Speed up command queues

* Retry reset watchdog if it fails once

* Force RaspBee and ConBee to use 16bit NWK addr in data indications

* Fix wrong handling of 64bit src addresses in received data responses

* Remove unnecessary debug output

* Use given zclFrame.toBuffer() function instead of own implementation

* Don't throw error when unknown parameter is received

* Added writeparameter request to permit join request

* Removed unnecessary console.log

* Start on conbee

* Updates.

* conbee -> deconz. Koenkk/zigbee-herdsman#72

* Initial suppport for reading parameters

- Add basic frame parser
- Handle slip protocol and crc calculation
- Add queue for read and write parameters

* Add dummy function DeconzAdapter.supportsLED()

* Start working on write parameters

* supportsLED() returns false

* Implemented getCoordinator()

* Added deCONZ adapter dummy functions

* Implemented getCoordinatorVersion()

* Implemented getNetworkParameters()

* Added constants for APS layer

* Startet APS layer

Added readReceivedStateRequest, deviceStateRequest, deviceStateResponse,
readReceivedDataRequest

* Added APS layer parser functions

parseDeviceStateResponse, parseReadReceivedDataResponse, parseReceivedDataNotification,

* Added parser for APS responses

* Refactored some deCONZ adapter functions

* Added sendZclFrameNetworkAddress, sendZclFrameNetworkAddressWithResponse and sendZclFrameGroup

* Added constants for deCONZ adapter

* Added deCONZ driver functions for sending APS commands

* Added discoverRoute, supportsDiscoverRoute dummy functions

* Implemented NodeDescriptor

* Added constants

* Reworked debug messages and fixed send unnecessary device state requests

* Some smal fixes

* Implemented permit join

* Fixed check DeviceStatus variables

* implemented Active Endpoint request

* Implemented simple descriptor

* Removed reading request_id if data confirm fail

* Fixed reading payload of data indication

* Fix nodedescriptor devicetype

* Added waitFor dummy function

* Implemented lqi()

* Implemented routingTable()

* implemented bind()

* implmented unbind()

* Implemented removeDevice()

* Changes at driver.ts

* Fix for resolve aps request error

* ieeeAddr to String now add '0x' prefix

* Get correct APS data payload

* Fixes for some adapter functions

* Delete old code

* Added constant

* Added support of timeout for aps requests

* Refactored deCONZ adapter functions

* Added constants

* Refactored driver functions and debug logs

* Refactored frameparser debug logs

* Updated deCONZ adapter to newest adapter version

* Added device state intervall wand tweaked watchdog

* Removed unused code

* Removed unused code

* Added received msg event, removed discoverRoute

* Added catch for devicestaterequest

* Handle default response when sending zcl message

* Refactored debug print

* Added /* istanbul ignore file */

* Fixed/ implemented zclFrame.payload to Array (for dim and color commands)

* Implemented waitFor() and some changes for OTA update

* Update deconzAdapter.ts

* Fixed sending manufacturere specific commands

* Added some attributes to zclpayloadToArray conversion needed for e.g. configure reporting

TODO: this has to be reworked completely

* Fixed zclPayload to array function (used by configureReporting)

handle arrays of objects correctly

* Use APS Acks for bind and unbind requests

* Reset serial connection if too many timeouts occur

* Minor debug log and constants changes

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
  • Loading branch information
ChrisHae and Koenkk authored Jun 9, 2020
1 parent c61780f commit bdac446
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/adapter/deconz/adapter/deconzAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ class DeconzAdapter extends Adapter {
request.srcEndpoint = 0;
request.asduLength = zdpFrame.length;
request.asduPayload = zdpFrame;
request.txOptions = 0;
request.txOptions = 0x04; // 0x04 use APS ACKS
request.radius = PARAM.PARAM.txRadius.DEFAULT_RADIUS;
request.timeout = 30;

Expand Down Expand Up @@ -728,7 +728,7 @@ class DeconzAdapter extends Adapter {
request.srcEndpoint = 0;
request.asduLength = zdpFrame.length;
request.asduPayload = zdpFrame;
request.txOptions = 0;
request.txOptions = 0x04; // 0x04 use APS ACKS
request.radius = PARAM.PARAM.txRadius.DEFAULT_RADIUS;
request.timeout = 30;

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/deconz/driver/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const PARAM: {
PAN_ID: 0x05,
NWK_ADDRESS: 0x07,
EXT_PAN_ID: 0x08,
APS_EXT_PAN_ID: 0x0b,
CHANNEL_MASK: 0x0a,
APS_EXT_PAN_ID: 0x0b,
NETWORK_KEY: 0x18,
CHANNEL: 0x1c,
PERMIT_JOIN: 0x21,
Expand Down
31 changes: 28 additions & 3 deletions src/adapter/deconz/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var queue: Array<object> = [];
var busyQueue: Array<object> = [];
var apsQueue: Array<object> = [];
var apsBusyQueue: Array<object> = [];
var timeoutCounter = 0;
export { busyQueue, apsBusyQueue };

var frameParser = require('./frameParser');
Expand All @@ -38,12 +39,14 @@ class Driver extends events.EventEmitter {
private parser: Parser;
private frameParserEvent = frameParser.frameParserEvents;
private seqNumber: number;
private timeoutResetTimeout: any;

public constructor(path: string) {
super();
this.path = path;
this.initialized = false;
this.seqNumber = 0;
this.timeoutResetTimeout = null;

const that = this;
setInterval(() => { that.processQueue(); }, 50); //300
Expand Down Expand Up @@ -274,7 +277,7 @@ class Driver extends events.EventEmitter {
busyQueue.push(req);
}

private processBusyQueue() {
private async processBusyQueue() {
let i = busyQueue.length;
while (i--) {
const req: Request = busyQueue[i];
Expand All @@ -284,7 +287,21 @@ class Driver extends events.EventEmitter {
debug(`Timeout for request - CMD: 0x${req.commandId.toString(16)} seqNr: ${req.seqNumber}`);
//remove from busyQueue
busyQueue.splice(i, 1);
timeoutCounter++;
// after a timeout the timeoutcounter will be reset after 1 min. If another timeout happen then the timeoutcounter
// will not be reset
clearTimeout(this.timeoutResetTimeout);
this.timeoutResetTimeout = null;
this.resetTimeoutCounterAfter1min();
req.reject("TIMEOUT");
if (timeoutCounter >= 3) {
timeoutCounter = 0;
debug("to many timeouts - restart serial connecion");
if (this.serialPort.isOpen) {
this.serialPort.close();
}
await this.open();
}
}
}
}
Expand Down Expand Up @@ -440,8 +457,7 @@ class Driver extends events.EventEmitter {
dest += request.destEndpoint;
}

debug(`DATA_REQUEST - sending data request - destAddr: 0x${dest} SeqNr. ${seqNumber} request id: ${request.requestId}`);

debug(`DATA_REQUEST - destAddr: 0x${dest} SeqNr. ${seqNumber} request id: ${request.requestId}`);
const requestFrame = [PARAM.PARAM.APS.DATA_REQUEST, seqNumber, 0x00, frameLength & 0xff, (frameLength >> 8) & 0xff,
payloadLength & 0xff, (payloadLength >> 8) & 0xff,
request.requestId, 0x00, request.destAddrMode].concat(
Expand Down Expand Up @@ -549,6 +565,15 @@ class Driver extends events.EventEmitter {
private onParsed(frame: Uint8Array): void {
this.emit('rxFrame', frame);
}
private resetTimeoutCounterAfter1min() {
if (this.timeoutResetTimeout === null) {
this.timeoutResetTimeout = setTimeout(function(){
console.log("reset timeoutcounter");
timeoutCounter = 0;
this.timeoutResetTimeout = null;
}, 60000);
}
}
}

export default Driver;
12 changes: 6 additions & 6 deletions src/adapter/deconz/driver/frameParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function parseQuerySendDataStateResponse(view : DataView) : object {
response.status = view.getUint8(2);

if (response.status != 0) {
debug("DATA_CONFIRM Response - rejected - seqNr.: " + response.seqNr + " status: " + response.status);
debug("DATA_CONFIRM RESPONSE - seqNr.: " + response.seqNr + " status: " + response.status);
return null;
}

Expand Down Expand Up @@ -183,7 +183,7 @@ function parseQuerySendDataStateResponse(view : DataView) : object {
//remove from busyqueue
apsBusyQueue.splice(i, 1);

debug("DATA_CONFIRM Response - destAddr: 0x" + destAddr + " request id: " + response.requestId + " confirm status: " + response.confirmStatus + " new device state: " + newStatus);
debug("DATA_CONFIRM RESPONSE - destAddr: 0x" + destAddr + " request id: " + response.requestId + " confirm status: " + response.confirmStatus);
frameParserEvents.emit('receivedDataNotification', response.deviceState);
/*
const zclPayload: ZclDataPayload = {
Expand All @@ -206,7 +206,7 @@ function parseReadReceivedDataResponse(view : DataView) : object {
response.status = view.getUint8(2);

if (response.status != 0) {
debug("DATA_INDICATION Response - rejected - seqNr.: " + response.seqNr + " status: " + response.status);
debug("DATA_INDICATION RESPONSE - seqNr.: " + response.seqNr + " status: " + response.status);
return null;
}

Expand Down Expand Up @@ -273,8 +273,8 @@ function parseReadReceivedDataResponse(view : DataView) : object {
newStatus = "0" + newStatus;
}

debug("DATA_INDICATION Response - seqNr. " + response.seqNr + " srcAddr: 0x" + srcAddr + " destAddr: 0x" + destAddr + " profile id: 0x" + response.profileId.toString(16) + " cluster id: 0x" + response.clusterId.toString(16) + " new state: " + newStatus);
debug("payload: " + payload);
debug("DATA_INDICATION RESPONSE - seqNr. " + response.seqNr + " srcAddr: 0x" + srcAddr + " destAddr: 0x" + destAddr + " profile id: 0x" + response.profileId.toString(16) + " cluster id: 0x" + response.clusterId.toString(16));
debug("response payload: " + payload);
frameParserEvents.emit('receivedDataPayload', response);
frameParserEvents.emit('receivedDataNotification', response.deviceState);
return response;
Expand All @@ -284,7 +284,7 @@ function parseEnqueueSendDataResponse(view : DataView) : number {
const status = view.getUint8(2);
const requestId = view.getUint8(8);
const deviceState = view.getUint8(7);
debug("DATA_REQUEST Response - status: " + status + " request id: " + requestId + " new device state: " + deviceState.toString(2));
debug("DATA_REQUEST RESPONSE - request id: " + requestId + " status: " + status);
frameParserEvents.emit('receivedDataNotification', deviceState);
return deviceState;
}
Expand Down

0 comments on commit bdac446

Please sign in to comment.