Skip to content

Commit

Permalink
feat: implement write-property-multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
fh1ch committed Jul 11, 2017
1 parent cbb3d13 commit 5f9f7a0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,38 @@ module.exports = function(settings) {
client.readPropertyMultiple(address, propertyIdAndArrayIndex, next);
};

/**
* The writePropertyMultiple command writes multiple properties in multiple objects to a device.
* @function bacstack.writePropertyMultiple
* @param {string} address - IP address of the target device.
* @param {object[]} valueList - List of object and property specifications to be written.
* @param {object} valueList.objectIdentifier - Specifies which object to read.
* @param {number} valueList.objectIdentifier.type - The BACNET object type to read.
* @param {number} valueList.objectIdentifier.instance - The BACNET object instance to read.
* @param {object[]} valueList.values - List of properties to be written.
* @param {object} valueList.values.property - Property specifications to be written.
* @param {number} valueList.values.property.propertyIdentifier - The BACNET property id in the specified object to write.
* @param {number} valueList.values.property.propertyArrayIndex - The array index of the property to be written.
* @param {object[]} valueList.values.value - A list of values to be written to the specified property.
* @param {number} valueList.values.value.Tag - The data-type of the value to be written. Has to be a BacnetApplicationTags declaration as specified in lib/bacnet-enum.js.
* @param {object} valueList.values.value.Value - The actual value to be written.
* @param {number} valueList.values.priority - The priority to be used for writing to the property.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* var valueList = [
* {objectIdentifier: {type: 8, instance: 44301}, values: [
* {property: {propertyIdentifier: 28, propertyArrayIndex: 12}, value: [{Tag: 1, Value: true}], priority: 8}
* ]}
* ];
* client.readPropertyMultiple('192.168.1.43', valueList, function(err, value) {
* console.log('value: ', value);
* });
*/
self.writePropertyMultiple = function(address, valueList, next) {
client.writePropertyMultiple(address, valueList, next);
};

/**
* The deviceCommunicationControl command enables or disables network communication of the target device.
* @function bacstack.deviceCommunicationControl
Expand Down
17 changes: 17 additions & 0 deletions lib/bacnet-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ module.exports = function(settings) {
});
};

self.writePropertyMultiple = function(address, valueList, next) {
var maxSegments = baEnum.BacnetMaxSegments.MAX_SEG65;
var buffer = getBuffer();
var invokeId = getInvokeId();
baNpdu.Encode(buffer, baEnum.BacnetNpduControls.PRIORITY_NORMAL_MESSAGE | baEnum.BacnetNpduControls.EXPECTING_REPLY, address);
baAdpu.encodeConfirmedServiceRequest(buffer, baEnum.BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST, baEnum.BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROP_MULTIPLE, maxSegments, baEnum.BacnetMaxAdpu.MAX_APDU1476, invokeId);
baServices.EncodeWriteObjectMultiple(buffer, valueList);
baBvlc.encode(buffer.buffer, baEnum.BacnetBvlcFunctions.BVLC_ORIGINAL_UNICAST_NPDU, buffer.offset);
transport.send(buffer.buffer, buffer.offset, address);
addCallback(invokeId, function(err, data) {
if (err) return next(err);
// TODO: Handle simple-ack
var result = true;
next(null, result);
});
};

self.deviceCommunicationControl = function(address, timeDuration, enableDisable, password, next) {
var maxSegments = baEnum.BacnetMaxSegments.MAX_SEG65;
var buffer = getBuffer();
Expand Down
4 changes: 2 additions & 2 deletions lib/bacnet-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ module.exports.DecodeWritePropertyMultiple = function(buffer, offset, apduLen) {
};

module.exports.EncodeWriteObjectMultiple = function(buffer, valueList) {
valueList.forEach(function(rValue) {
EncodeWritePropertyMultiple(buffer, rValue.objectIdentifier, rValue.values);
valueList.forEach(function(object) {
EncodeWritePropertyMultiple(buffer, object.objectIdentifier, object.values);
});
};

Expand Down
19 changes: 19 additions & 0 deletions test/integration/write-property-multiple.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var expect = require('chai').expect;
var utils = require('./utils');

describe('bacstack - writePropertyMultiple integration', function() {
it('should return a timeout error if no device is available', function(next) {
var client = new utils.bacnetClient({adpuTimeout: 200});
var valueList = [
{objectIdentifier: {type: 8, instance: 44301}, values: [
{property: {propertyIdentifier: 28, propertyArrayIndex: 12}, value: [{Tag: 1, Value: true}], priority: 8}
]}
];
client.writePropertyMultiple('127.0.0.1', valueList, function(err, value) {
expect(err).to.eql(new Error('ERR_TIMEOUT'));
expect(value).to.eql(undefined);
client.close();
next();
});
});
});

0 comments on commit 5f9f7a0

Please sign in to comment.