Skip to content

Commit

Permalink
feat(bacnet-client): expose BACNET errors and aborts to user
Browse files Browse the repository at this point in the history
  • Loading branch information
fh1ch committed Jun 29, 2017
1 parent 2c7971d commit 8cda7de
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/bacnet-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ module.exports = function(settings) {
return id - 1;
};

var invokeCallback = function(result) {
var callback = invokeStore[result.result.invokeId];
if (callback) return callback(null, result);
debug('InvokeId ', result.result.invokeId, ' not found -> drop package');
var invokeCallback = function(id, err, result) {
var callback = invokeStore[id];
if (callback) return callback(err, result);
debug('InvokeId ', id, ' not found -> drop package');
};

var addCallback = function(id, callback) {
Expand All @@ -69,10 +69,14 @@ module.exports = function(settings) {
};

// Service Handlers
var ProcessError = function(adr, type, service, invokeId, buffer, offset, length) {
debug('Handle Error');
var processError = function(invokeId, buffer, offset, length) {
var result = baServices.DecodeError(buffer, offset, length);
if (!result) return debug('Couldn`t decode Error');
invokeCallback(invokeId, new Error('BacnetAbort - Class:', result.class, ' - Code:', result.code));
};

var processAbort = function(invokeId, reason) {
invokeCallback(invokeId, new Error('BacnetAbort - Reason:', reason));
};

var segmentAckResponse = function(receiver, negative, server, originalInvokeId, sequencenumber, actualWindowSize) {
Expand Down Expand Up @@ -181,12 +185,12 @@ module.exports = function(settings) {
result = baAdpu.decodeSimpleAck(buffer, offset);
offset += result.len;
length -= result.len;
invokeCallback({result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
invokeCallback(result.invokeId, null, {result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
break;
case baEnum.BacnetPduTypes.PDU_TYPE_COMPLEX_ACK:
result = baAdpu.decodeComplexAck(buffer, offset);
if ((type & baEnum.BacnetPduTypes.SEGMENTED_MESSAGE) === 0) {
invokeCallback({result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
invokeCallback(result.invokeId, null, {result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
} else {
processSegment(address, result.type, result.service, result.invokeId, baEnum.BacnetMaxSegments.MAX_SEG0, baEnum.BacnetMaxAdpu.MAX_baAdpu50, false, result.sequencenumber, result.proposedWindowNumber, buffer, offset + result.len, length - result.len);
}
Expand All @@ -198,12 +202,12 @@ module.exports = function(settings) {
break;
case baEnum.BacnetPduTypes.PDU_TYPE_ERROR:
result = baAdpu.decodeError(buffer, offset);
//processError(address, result.type, result.service, result.invokeId, buffer, offset + result.len, length - result.len);
processError(result.invokeId, buffer, offset + result.len, length - result.len);
break;
case baEnum.BacnetPduTypes.PDU_TYPE_REJECT:
case baEnum.BacnetPduTypes.PDU_TYPE_ABORT:
result = baAdpu.decodeAbort(buffer, offset);
//processAbort(address, result.type, result.invokeId, result.reason, buffer, offset + result.len, length - result.len);
processAbort(result.invokeId, result.reason);
break;
case baEnum.BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST:
result = baAdpu.decodeConfirmedServiceRequest(buffer, offset);
Expand Down

0 comments on commit 8cda7de

Please sign in to comment.