Skip to content

Commit

Permalink
feat(bacnet-client): rework unconfirmed services to utilize even emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
fh1ch committed Jun 18, 2017
1 parent abb6a75 commit b5b8d78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
34 changes: 18 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ module.exports = function(settings) {
// Public enums
self.enum = baEnum;

client.events.on('iAm', function(address, deviceId, maxAdpu, segmentation, vendorId) {

/**
* @event bacstack.iAm
* @param {string} address - The IP address of the detected device.
* @param {number} deviceId - The BACNET device-id of the detected device.
* @param {number} maxAdpu - The max ADPU size the detected device is supporting.
* @param {number} segmentation - The type of segmentation the detected device is supporting.
* @param {number} vendorId - The BACNET vendor-id of the detected device.
* @example
* client.on('iAm', function(address, deviceId, maxAdpu, segmentation, vendorId) {
* console.log('address: ', address, ' - deviceId: ', deviceId, ' - maxAdpu: ', maxAdpu, ' - segmentation: ', segmentation, ' - vendorId: ', vendorId);
* });
*/
self.emit('iAm', address, deviceId, maxAdpu, segmentation, vendorId);
});

/**
* The whoIs command discovers all BACNET devices in a network.
* @function bacstack.whoIs
Expand All @@ -51,22 +68,7 @@ module.exports = function(settings) {
* client.whoIs();
*/
self.whoIs = function(lowLimit, highLimit, address) {
client.whoIs(lowLimit, highLimit, address, function(address, deviceId, maxAdpu, segmentation, vendorId) {

/**
* @event bacstack.iAm
* @param {string} address - The IP address of the detected device.
* @param {number} deviceId - The BACNET device-id of the detected device.
* @param {number} maxAdpu - The max ADPU size the detected device is supporting.
* @param {number} segmentation - The type of segmentation the detected device is supporting.
* @param {number} vendorId - The BACNET vendor-id of the detected device.
* @example
* client.on('iAm', function(address, deviceId, maxAdpu, segmentation, vendorId) {
* console.log('address: ', address, ' - deviceId: ', deviceId, ' - maxAdpu: ', maxAdpu, ' - segmentation: ', segmentation, ' - vendorId: ', vendorId);
* });
*/
self.emit('iAm', address, deviceId, maxAdpu, segmentation, vendorId);
});
client.whoIs(lowLimit, highLimit, address);
};

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/bacnet-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Util Modules
var events = require('events');
var debug = require('debug')('bacstack');

// Local Modules
Expand Down Expand Up @@ -29,7 +30,7 @@ module.exports = function(settings) {
});

// Local Handlers
var whoIsHandler;
self.events = new events.EventEmitter();

// Helper utils
var getInvokeId = function() {
Expand Down Expand Up @@ -135,7 +136,7 @@ module.exports = function(settings) {
if (service === baEnum.BacnetUnconfirmedServices.SERVICE_UNCONFIRMED_I_AM) {
var result = baServices.decodeIamBroadcast(buffer, offset);
if (!result) return debug('Received invalid iAm message');
whoIsHandler(address, result.deviceId, result.maxApdu, result.segmentation, result.vendorId);
self.events.emit('iAm', address, result.deviceId, result.maxApdu, result.segmentation, result.vendorId);
} else if (service === baEnum.BacnetUnconfirmedServices.SERVICE_UNCONFIRMED_WHO_IS) {
return debug('TODO: Implement');
// TODO: Implement
Expand Down Expand Up @@ -229,8 +230,7 @@ module.exports = function(settings) {
};

// Public Functions
self.whoIs = function(lowLimit, highLimit, address, handler) {
whoIsHandler = handler;
self.whoIs = function(lowLimit, highLimit, address) {
var buffer = getBuffer();
address = address || transport.getBroadcastAddress();
baNpdu.Encode(buffer, baEnum.BacnetNpduControls.PRIORITY_NORMAL_MESSAGE, address, null, DEFAULT_HOP_COUNT, baEnum.BacnetNetworkMessageTypes.NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK, 0);
Expand Down

0 comments on commit b5b8d78

Please sign in to comment.