-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.js
597 lines (485 loc) · 29 KB
/
program.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// CAS BACnet Stack NodeJS Client Example
// More information can be found here: https://github.com/chipkin/BACnetClientExampleNodeJS
// Start with the "function main()"
//
// Written by: Steven Smethurst
// Last updated: 2023-July-11
//
var CASBACnetStack = require('./CASBACnetStackAdapter'); // CAS BACnet stack
var ffi = require('ffi-napi'); // DLL interface. https://github.com/node-ffi/node-ffi
var ref = require('ref-napi'); // DLL Data types. https://github.com/TooTallNate/ref
var dequeue = require('dequeue'); // Creates a FIFO buffer. https://github.com/lleo/node-dequeue/
const dgram = require('dgram'); // UDP server
const os = require('os'); // Retrieve network info
// Logger
const loggerObj = require('./logging');
const logger = loggerObj.child({ label: 'BACnetClientExample' });
// Settings
const SETTING_BACNET_PORT = 47808; // Default BACnet IP UDP Port.
const SETTING_DEVICE_INSTANCE = 389005; // This BACnet client device instance.
const SETTING_IP_ADDRESS = []; // Set IP Address to use for BACnet, set to [] to discover... [192,168,2,101]
const SETTING_SUBNET_MASK = []; // Set Subnet Mask to use for BACnet, set to [] to discover... [255,255,255,0]
const SETTING_TIMER_DISCOVERY_SECONDS = 30;
const SETTING_TIMER_READ_PROPERTY_SECONDS = 15;
const SETTING_TIMER_PRINT_DATABASE_SECONDS = 3;
// Constants
const APPLICATION_VERSION = '0.0.4';
const CONNECTION_STRING_SIZE = 6; // The size of the connection string in bytes.
// Globals
var fifoRecvBuffer = new dequeue();
var server = dgram.createSocket('udp4');
var database = require('./database.js');
const { setTimeout } = require('timers/promises');
var subnetMask = [];
var localAddress = [];
// Callback Functions
// ------------------------------------------------------------------------
// FFI types
var uint8Ptr = ref.refType('uint8 *');
// Message Callback Functions
var FuncPtrCallbackSendMessage = ffi.Callback('uint16', [uint8Ptr, 'uint16', uint8Ptr, 'uint8', 'uint8', 'bool'], CallbackSendMessage);
var FuncPtrCallbackReceiveMessage = ffi.Callback('uint16', [uint8Ptr, 'uint16', uint8Ptr, 'uint8', uint8Ptr, uint8Ptr], CallbackRecvMessage);
// System Callback Functions
var FuncPtrCallbackGetSystemTime = ffi.Callback('uint64', [], CallbackGetSystemTime);
// Get Property Callback Functions
var FuncPtrCallbackGetPropertyCharacterString = ffi.Callback('bool', ['uint32', 'uint16', 'uint32', 'uint32', 'char*', 'uint32*', 'uint32', 'uint8', 'bool', 'uint32'], GetPropertyCharacterString);
// Debug
var FuncPtrCallbackLogDebugMessage = ffi.Callback('void', ['uint8*', 'uint16', 'uint8'], LogDebugMessage);
// Helper Functions
// ------------------------------------------------------------------------
// CreateStringFromCharPointer converts a char pointer (from the CAS BACnet Stack) into a string useable by NodeJS.
function CreateStringFromCharPointer(charPointer, length) {
let messageToRead = ref.reinterpret(charPointer, length, 0);
let workingString = '';
for (let offset = 0; offset < length; offset++) {
workingString += String.fromCharCode(messageToRead.readUInt8(offset));
}
return workingString;
}
// Helper function to get a key name from an array by the value.
function HelperGetKeyByValue(object, value) {
// https://www.geeksforgeeks.org/how-to-get-a-key-in-a-javascript-object-by-its-value/
return Object.keys(object).find((key) => object[key] === value);
}
// Helper function to convert a connection string ("192.168.1.101:47808" or "192.168.1.101") into a buffer.
// That can be used by the CAS BACnet Stack.
function HelperGetConnectionStringAsBuffer(connectionString) {
// Convert the connection string as a text into a buffer that the CAS BACnet Stack expects.
// The connection string is in the format of "192.168.1.101:47808" or "192.168.1.101"
var newConnectionString = Buffer.allocUnsafe(CONNECTION_STRING_SIZE);
var ipAddress = connectionString.substring(0, connectionString.indexOf(':')).split('.').map(Number);
var port = parseInt(connectionString.substring(connectionString.indexOf(':') + 1));
// If the port doesn't exist then set it to the default port.
if (isNaN(port)) {
port = SETTING_BACNET_PORT;
}
newConnectionString.writeUInt8(ipAddress[0], 0);
newConnectionString.writeUInt8(ipAddress[1], 1);
newConnectionString.writeUInt8(ipAddress[2], 2);
newConnectionString.writeUInt8(ipAddress[3], 3);
newConnectionString.writeUInt8(port / 256, 4);
newConnectionString.writeUInt8(port % 256, 5);
// Return the connection string as a buffer.
return newConnectionString;
}
// Callback functions
// ------------------------------------------------------------------------
// This callback function is used when the CAS BACnet stack wants to send a message out onto the network.
function CallbackSendMessage(message, messageLength, connectionString, connectionStringLength, networkType, broadcast) {
// Convert the connection string to a buffer.
var newConnectionString = ref.reinterpret(connectionString, connectionStringLength, 0);
var ipAddress = "";
// If the message needs to be sent as a broadcast message then find the broadcast address
if (broadcast) {
// Use the subnetMask to calculate the broadcast address
var broadcastAddress = [];
for (var i = 0; i < 4; i++) {
broadcastAddress.push((localAddress[i] & subnetMask[i]) | (~subnetMask[i] & 0xFF));
}
ipAddress = broadcastAddress[0] + '.' + broadcastAddress[1] + '.' + broadcastAddress[2] + '.' + broadcastAddress[3];
} else {
ipAddress = newConnectionString.readUInt8(0) + '.' + newConnectionString.readUInt8(1) + '.' + newConnectionString.readUInt8(2) + '.' + newConnectionString.readUInt8(3);
}
var port = newConnectionString.readUInt8(4) * 256 + newConnectionString.readUInt8(5);
// Check to see if the port is valid.
if (port <= 0 || port >= 65536) {
logger.error('Failed to CallbackSendMessage. Bad port: ' + port);
return 0;
}
logger.debug('>> CallbackSendMessage. messageLength: ' + messageLength + ", broadcast: " + broadcast + ", networkType: " + networkType + ", ipAddress: " + ipAddress + ", port: " + port + ', message: ' + message.toString('hex'));
// Check to make sure that we are currently connected to the udp port before sending a message.
if (server.address() === null) {
logger.error('Error: UDP port is not open. Cannot send message.');
return 0;
}
// copy the message to the sendBuffer.
var newMessage = ref.reinterpret(message, messageLength, 0);
sendBuffer = Buffer.alloc(messageLength);
for (var offset = 0; offset < messageLength; ++offset) {
sendBuffer.writeUInt8(newMessage[offset], offset);
}
// Send the message.
try {
// TODO: server.send is async, for now we trigger unelegant error-out if an error occurs when server is trying to send
server.send(sendBuffer, port, ipAddress, function (error) {
if (error) {
logger.error('Error: Could not send message');
server.close();
} else {
return newMessage.length;
}
});
} catch (e) {
logger.error('Exception: ' + e.stack);
}
return newMessage.length;
}
// This callback fundtion is used when the CAS BACnet stack wants to check to see if there are any incomming messages
function CallbackRecvMessage(message, maxMessageLength, receivedConnectionString, maxConnectionStringLength, receivedConnectionStringLength, networkType) {
// Check to see if there are any messages waiting on the buffer.
if (fifoRecvBuffer.length > 0) {
// There is at lest one message on the buffer.
var msg = fifoRecvBuffer.shift();
const receivedMessage = msg[0];
logger.debug('<< CallbackRecvMessage Got message. Length: ' + receivedMessage.length + ', From:' + msg[1] + ', Message: ' + receivedMessage.toString('hex'));
if (receivedMessage.length > maxMessageLength) {
logger.error('Error: Message too large to fit into buffer on Recv. Dumping message. msg[0].length=' + receivedMessage.length + ', maxMessageLength=' + maxMessageLength);
return 0;
}
// Received Connection String
// --------------------------------------------------------------------
// Extract address and port
var receivedAddress = msg[1].substring(0, msg[1].indexOf(':')).split('.').map(Number);
var receivedPort = parseInt(msg[1].substring(msg[1].indexOf(':') + 1));
// Reinterpret the receivedConnectionString parameter with a the max buffer size.
var newReceivedConnectionString = ref.reinterpret(receivedConnectionString, maxConnectionStringLength, 0);
newReceivedConnectionString.writeUInt8(receivedAddress[0], 0);
newReceivedConnectionString.writeUInt8(receivedAddress[1], 1);
newReceivedConnectionString.writeUInt8(receivedAddress[2], 2);
newReceivedConnectionString.writeUInt8(receivedAddress[3], 3);
newReceivedConnectionString.writeUInt8(receivedPort / 256, 4);
newReceivedConnectionString.writeUInt8(receivedPort % 256, 5);
// Connection string length
receivedConnectionStringLength.writeUInt8(CONNECTION_STRING_SIZE, 0); // Ensure its null terminated.
// Received message
// --------------------------------------------------------------------
// Reinterpret the message parameter with a the max buffer size.
var newMessage = ref.reinterpret(message, maxMessageLength, 0);
for (var offset = 0; offset < receivedMessage.length; offset++) {
newMessage.writeUInt8(receivedMessage[offset], offset);
}
// Pass the recived message to the process function.
ProcessBACnetMessage(msg[1], newMessage);
// Return the length of the message.
return receivedMessage.length;
}
return 0;
}
// This callback is used to determin the current system time.
function CallbackGetSystemTime() {
// https://stackoverflow.com/a/9456144/58456
var d = new Date();
return d.getTime() / 1000;
}
function GetPropertyCharacterString(deviceInstance, objectType, objectInstance, propertyIdentifier, value, valueElementCount, maxElementCount, encodingType, useArrayIndex, propertyArrayIndex) {
logger.debug('GetPropertyCharacterString - deviceInstance: ' + deviceInstance + ', objectType: ' + objectType + ', objectInstance: ' + objectInstance + ', propertyIdentifier: ' + propertyIdentifier + ', useArrayIndex: ' + useArrayIndex + ', propertyArrayIndex: ' + propertyArrayIndex + ', maxElementCount: ' + maxElementCount + ', encodingType: ' + encodingType);
var newValue = ref.reinterpret(value, maxElementCount, 0);
// Convert the enumerated values to human readable strings.
var resultPropertyIdentifier = HelperGetKeyByValue(CASBACnetStack.PROPERTY_IDENTIFIER, propertyIdentifier).toLowerCase();
var resultObjectType = HelperGetKeyByValue(CASBACnetStack.OBJECT_TYPE, objectType).toLowerCase();
// Example of getting object Name property
if (propertyIdentifier === CASBACnetStack.PROPERTY_IDENTIFIER.OBJECT_NAME && objectType === CASBACnetStack.OBJECT_TYPE.DEVICE) {
// The property has been defined.
// Convert the property to the requested data type and return success.
charValue = "Example name"
newValue.write(charValue, 0, 'utf8');
valueElementCount.writeInt32LE(charValue.length, 0); // Null terminated string.
return true;
}
// Could not find the value in the database.
logger.error('GetPropertyCharacterString error. Could not find this value in the application database');
return false;
}
function LogDebugMessage(message, messageLength, messageType) {
// This callback is called when the CAS BACnet Stack logs an error or info message
// In this callback, you will be able to access this debug message.
if (messageType) {
logger.info(CreateStringFromCharPointer(message, messageLength));
} else {
logger.error(CreateStringFromCharPointer(message, messageLength));
}
}
// This function decodes and processes the BACnet message.
// This is where most of your bussiness logic will be.
function ProcessBACnetMessage(connectionString, BACnetMessageAsBuffer) {
const MAX_JSON_MESSAGE_LENGTH = 1024 * 20;
var messageAsJSONBuffer = Buffer.allocUnsafe(MAX_JSON_MESSAGE_LENGTH);
var messageAsJSONBufferLength = CASBACnetStack.stack.BACnetStack_DecodeAsJSON(BACnetMessageAsBuffer, BACnetMessageAsBuffer.length, messageAsJSONBuffer, MAX_JSON_MESSAGE_LENGTH, CASBACnetStack.CONSTANTS.NETWORK_TYPE_BACNET_IP);
messageAsJSONBuffer = messageAsJSONBuffer.subarray(0, messageAsJSONBufferLength); // Remove the end of the buffer.
var messageAsJSON = '';
try {
messageAsJSON = JSON.parse(messageAsJSONBuffer.toString('utf8'));
} catch (e) {
logger.error('Could not parse JSON. Exception: ' + e.message + ", messageAsJSONBuffer: " + messageAsJSONBuffer.toString('utf8'));
return;
}
logger.info(JSON.stringify(messageAsJSON));
// In this example we are only processing two types of messages:
// I-am, and read-property-multiple.
// All messages will have a NPDU, section that has network information.
var sourceNetwork = messageAsJSON?.BACnetPacket?.NPDU?.SourceNetwork;
var sourceAddress = '';
if (messageAsJSON?.BACnetPacket?.NPDU?.SourceAddress?._length > 0) {
// This message is for a different network.
// The sourceAddress is in this format 0x0493E9
sourceAddress = messageAsJSON?.BACnetPacket?.NPDU?.SourceAddress?._text.split('x')[1];
}
// # I-Am
// An I-Am message is sent by a BACnet device on response from a Who-Is message.
// The I-Am are used for discovery of a BACnet device.
// Use JSON Path to extract the data from the message. BACnetPacket/UnconfirmedRequestPDU/_serviceChoice = "iAm"
//
// Example message:
// {"BACnetPacket":{"_networkType":"IP","BVLL":{"_function":"originalBroadcastNPDU"},
// "NPDU":{"_control":"0x28","_version":"1","DestinationNetwork":"65535","DestinationAddress":{"_length":"0"},
// "HopCount":"254","SourceNetwork":"3000","SourceAddress":{"_text":"0x0493E9","_length":"3"}},
// "UnconfirmedRequestPDU":{"_serviceChoice":"iAm","IAmRequest":{"IAmDeviceIdentifier":{"_text":"device, 300009",
// "_datatype":"12","_objectInstance":"300009","_objectType":"8"},"MaxAPDULengthAccepted":{"_text":"1476","_datatype":"2",
// "_value":"1476"},"SegmentationSupported":{"_text":"noSegmentation","_datatype":"9","_value":"3"},"VendorId":{"_text":"389",
// "_datatype":"2","_value":"389"}}}}}
//
//
var serviceChoice = messageAsJSON?.BACnetPacket?.UnconfirmedRequestPDU?._serviceChoice;
if (serviceChoice === "iAm") {
// Extract the data from the message.
var deviceInstance = messageAsJSON?.BACnetPacket?.UnconfirmedRequestPDU?.IAmRequest?.IAmDeviceIdentifier?._objectInstance;
var vendorId = messageAsJSON?.BACnetPacket?.UnconfirmedRequestPDU?.IAmRequest?.VendorId?._value;
// Print the information to the console for debugging.
logger.info('I-Am message. Device Instance: ' + deviceInstance + ', VendorId: ' + vendorId + ', sourceNetwork: ' + sourceNetwork + ', sourceAddress: ' + sourceAddress);
database.AddDevice(deviceInstance, vendorId, sourceNetwork, sourceAddress, connectionString);
return; // We are done processing this message.
}
// # Read-Property-Multiple
// A Read-Property-Multiple message is sent by a BACnet device on response from a Read-Property-Multiple message.
var serviceChoice = messageAsJSON?.BACnetPacket?.ComplexACKPDU?._serviceChoice;
if (serviceChoice === "readPropertyMultiple") {
// Get the _originalInvokeId from the message to match up to the request.
var originalInvokeId = messageAsJSON?.BACnetPacket?.ComplexACKPDU?._originalInvokeId;
var originalRequest = database.FindOriginalRequest(connectionString, originalInvokeId, true);
if (originalRequest == undefined) {
logger.error("Could not find original request using originalInvokeId: " + originalInvokeId + ", connectionString: " + connectionString);
return;
}
logger.info("originalInvokeId: " + originalInvokeId + ", originalRequest: " + JSON.stringify(originalRequest));
// Loop thought the results adding them to the database one at a time.
var ReadAccessResults = messageAsJSON?.BACnetPacket?.ComplexACKPDU?.ReadPropertyMultipleACK?.ListOfReadAccessResults?.ReadAccessResult?.ListOfResults?.ReadResult;
logger.info("Found ReadAccessResults: " + ReadAccessResults.length);
for (var i = 0; i < ReadAccessResults.length; i++) {
var propertyIdentifier = ReadAccessResults[i]?.PropertyIdentifier?._text;
var propertyValue = ReadAccessResults[i]?.PropertyValue;
// PropertyValue.UnsignedInteger._text || PropertyValue.CharacterString._text etc...
// The middle is the data type and should be a whild card.
// Extract the _text from the PropertyValue
var propertyValueText = '';
for (var key in propertyValue) {
if (key !== '_text') {
propertyValueText = propertyValue[key]._text;
}
}
logger.info('ReadPropertyMultiple message. originalInvokeId: ' + originalInvokeId + ', Device Instance: ' + originalRequest.deviceInstance + ', PropertyIdentifier: ' + propertyIdentifier + ', PropertyValue: ' + propertyValueText);
database.AddObjectProperty(originalRequest.deviceInstance, originalRequest.objectType, originalRequest.objectInstance, propertyIdentifier, propertyValueText, originalRequest.sourceNetwork, originalRequest.sourceAddress, connectionString);
}
return;
}
// Catch and produce a better error message for AbortPDU - segmentationNotSupported
// {"BACnetPacket":{"_networkType":"IP","BVLL":{"_function":"originalUnicastNPDU"},"NPDU":{"_control":"0x00","_version":"1"},
// "AbortPDU":{"_abortReason":"segmentationNotSupported","_originalInvokeId":"180","_server":"1"}}}
var serviceChoice = messageAsJSON?.BACnetPacket?.AbortPDU?._abortReason;
if (serviceChoice === "segmentationNotSupported") {
var _originalInvokeId = messageAsJSON?.BACnetPacket?.AbortPDU?._originalInvokeId;
logger.error('AbortPDU - segmentationNotSupported. The response to this request is larger then the max APDU size. Reduce the size of the request to include fewer objects or properties. _originalInvokeId: ' + _originalInvokeId);
database.RemoveRequest(connectionString, _originalInvokeId);
return;
}
}
function main() {
// Print version information
// ------------------------------------------------------------------------
logger.info('BACnet Client Example NodeJS https://github.com/chipkin/BACnetClientExampleNodeJS');
logger.info('GetLibaryPath: ' + CASBACnetStack.GetLibaryPath());
logger.info('Application Version: ' + APPLICATION_VERSION + ', BACnetStack_Version: ' + CASBACnetStack.stack.BACnetStack_GetAPIMajorVersion() + '.' + CASBACnetStack.stack.BACnetStack_GetAPIMinorVersion() + '.' + CASBACnetStack.stack.BACnetStack_GetAPIPatchVersion() + '.' + CASBACnetStack.stack.BACnetStack_GetAPIBuildVersion() + ', BACnetStackAdapter_Version: ' + CASBACnetStack.GetAdapterVersion());
// Setup the callback functions
// ------------------------------------------------------------------------
logger.info('Setting up callback functions...');
// Message Callback Functions
CASBACnetStack.stack.BACnetStack_RegisterCallbackSendMessage(FuncPtrCallbackSendMessage);
CASBACnetStack.stack.BACnetStack_RegisterCallbackReceiveMessage(FuncPtrCallbackReceiveMessage);
// System Time Callback Functions
CASBACnetStack.stack.BACnetStack_RegisterCallbackGetSystemTime(FuncPtrCallbackGetSystemTime);
// Get Property Callback Functions
CASBACnetStack.stack.BACnetStack_RegisterCallbackGetPropertyCharacterString(FuncPtrCallbackGetPropertyCharacterString);
// Debug Callback Function
CASBACnetStack.stack.BACnetStack_RegisterCallbackLogDebugMessage(FuncPtrCallbackLogDebugMessage);
// Setup the BACnet device.
// ------------------------------------------------------------------------
logger.info('Setting up BACnet device...');
logger.info('BACnet device instance: ' + SETTING_DEVICE_INSTANCE);
CASBACnetStack.stack.BACnetStack_AddDevice(SETTING_DEVICE_INSTANCE);
// Set up the BACnet services
// ------------------------------------------------------------------------
// By default only the required servics are enabled.
logger.info('Enabling IAm... ');
CASBACnetStack.stack.BACnetStack_SetServiceEnabled(SETTING_DEVICE_INSTANCE, CASBACnetStack.SERVICES_SUPPORTED.I_AM, true);
CASBACnetStack.stack.BACnetStack_SetServiceEnabled(SETTING_DEVICE_INSTANCE, CASBACnetStack.SERVICES_SUPPORTED.WHO_IS, true);
logger.info('Enabling ReadPropertyMultiple... ');
if (!CASBACnetStack.stack.BACnetStack_SetServiceEnabled(SETTING_DEVICE_INSTANCE, CASBACnetStack.SERVICES_SUPPORTED.READ_PROPERTY_MULTIPLE, true)) {
logger.error('Failed to enable the ReadPropertyMultiple service');
return false;
}
// Setup the UDP socket
// ------------------------------------------------------------------------
logger.info('Setting up BACnet UDP port. Port: ' + SETTING_BACNET_PORT);
server.on('error', (err) => {
logger.error(`UDP.Server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
// logger.info(`UDP.Server message. From: ${rinfo.address}:${rinfo.port}, Message:`, msg);
// When we recive a message we are storing it into a FIFO buffer.
// When the CAS BACnet Stack tick function is called. it will call the CallbackRecvMessage function.
// In this function we will read the message from the FIFO buffer and pass it to the CAS BACnet Stack.
fifoRecvBuffer.push([msg, rinfo.address + ':' + rinfo.port]);
// Note: If the fifoRecvBuffer ever gets too big you may wish to trim it or call BACnetStack_Tick more frequently.
// No need to wait for the tick function to be called. We can call it now.
CASBACnetStack.stack.BACnetStack_Tick();
});
server.on('listening', () => {
const address = server.address();
logger.info(`UDP.Server listening ${address.address}:${address.port}`);
DoDiscovery(); // Now that the UDP port is open, we can send a WhoIs message.
});
server.on('exit', () => {
logger.info(`UDP.Server Exit`);
});
// Get the system network information
// ------------------------------------------------------------------------
// Get netmask and ip address if not set
var localAddressString = '';
if (SETTING_IP_ADDRESS.length !== 4 || SETTING_SUBNET_MASK.length !== 4) {
const networkInterfaces = os.networkInterfaces();
logger.info('Found the following networks: \n' + JSON.stringify(networkInterfaces, null, 2));
// Find the FIRST network that satisfies the conditions.
var foundNetwork = false;
for (localNetwork in networkInterfaces) {
networkInterfaces[localNetwork].forEach(function (adapter) {
// NOTE: Specify your network here with your own filters
// Internal must be false and family must be IPv4
if (adapter.internal || adapter.family !== 'IPv4' || foundNetwork) {
return;
}
localAddress = adapter.address.split('.').map(Number);
subnetMask = adapter.netmask.split('.').map(Number);
foundNetwork = true;
return;
});
}
} else {
localAddress = SETTING_IP_ADDRESS;
subnetMask = SETTING_SUBNET_MASK;
}
logger.info('IP Address: ' + localAddress + ', SubnetMask: ' + subnetMask);
localAddressString = localAddress[0] + '.' + localAddress[1] + '.' + localAddress[2] + '.' + localAddress[3];
// Bind to the specific adapter and BACnet UDP port
server.bind({
address: localAddressString,
port: SETTING_BACNET_PORT
});
// Main program loop
// ------------------------------------------------------------------------
var intervalCount = 1;
logger.info('Starting main program loop... ');
// Process the BACnet stack
// The tick function must run every 100ms or so.
// It will process the BACnet stack and send out any messages.
// Run any timers, do any retransmits, etc.
function BACnetStackLoop() {
while (CASBACnetStack.stack.BACnetStack_Tick()) {
intervalCount += 1;
}
}
setInterval(BACnetStackLoop, 100); // Run every 100ms
// Print database
// ------------------------------------------------------------------------
setInterval(() => {
logger.info(database.Print());
}, 1000 * SETTING_TIMER_PRINT_DATABASE_SECONDS);
// Send a WhoIs message
// ------------------------------------------------------------------------
function DoDiscovery() {
logger.info('Sending WhoIs... ');
var newConnectionString = Buffer.allocUnsafe(CONNECTION_STRING_SIZE);
// The IP address of the connection string doesn't matter as it will be replaced by the broadcast address.
// Update the 4 and 5th bytes with the port number.
newConnectionString[4] = SETTING_BACNET_PORT / 256;
newConnectionString[5] = SETTING_BACNET_PORT % 256;
try {
// Note: Only one of these WhoIs should be enabled at a time.
// This is a unlimited Who-Is. All devices that recive this message will respond with an I-Am message.
// CASBACnetStack.stack.BACnetStack_SendWhoIs(newConnectionString, CONNECTION_STRING_SIZE, CASBACnetStack.CONSTANTS.NETWORK_TYPE_BACNET_IP, true, 65535, null, 0);
// This is a limited Who-Iss. Only devices within the range will respond with an I-Am message.
const WHO_IS_RANGE_LOW = 8800;
const WHO_IS_RANGE_HIGH = 8810;
CASBACnetStack.stack.BACnetStack_SendWhoIsWithLimits(WHO_IS_RANGE_LOW, WHO_IS_RANGE_HIGH, newConnectionString, CONNECTION_STRING_SIZE, CASBACnetStack.CONSTANTS.NETWORK_TYPE_BACNET_IP, true, 65535, null, 0);
} catch (e) {
logger.error('Send Who-is Exception: ' + e.stack);
}
}
setInterval(DoDiscovery, 1000 * SETTING_TIMER_DISCOVERY_SECONDS);
// Send the ReadPropertyMultiple message
// ------------------------------------------------------------------------
function DoReadProperty() {
// For each device in the database. The device key is (deviceInstance)
Object.keys(database.database.devices).forEach(function (deviceIdentifier) {
var deviceObject = database.database.devices[deviceIdentifier];
// Create the connection string
var newConnectionString = HelperGetConnectionStringAsBuffer(deviceObject.connectionString);
// Build the request
// Note: We are hard coding the object type and property identifier for this example.
// In your application you may want to read the object type and property identifier from a database.
var objectType = CASBACnetStack.OBJECT_TYPE.DEVICE;
const USE_READ_PROPERTY_MULTIPLE_ALL = true;
if (USE_READ_PROPERTY_MULTIPLE_ALL) {
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.ALL, false, 0);
} else {
// If the downstream device has too many objects or properties then the response will be segmented.
// Instead you could use the ReadPropertyMultiple service with fewer objects or properties.
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.OBJECT_NAME, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.VENDOR_NAME, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.VENDOR_IDENTIFIER, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.MODEL_NAME, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.FIRMWAREREVISION, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.APPLICATIONSOFTWAREVERSION, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.PROTOCOLREVISION, false, 0);
CASBACnetStack.stack.BACnetStack_BuildReadProperty(objectType, deviceIdentifier, CASBACnetStack.PROPERTY_IDENTIFIER.PROTOCOLSERVICESSUPPORTED, false, 0);
}
// Send the request
var destinationNetwork = deviceObject.sourceNetwork;
var destinationAddressLength = 0;
var destinationAddress = Buffer.allocUnsafe(1);
if (destinationNetwork != 0) {
// This device is on a different network.
// Convert the destinationAddress as hex to a buffer.
destinationAddress = Buffer.from(deviceObject.sourceAddress, 'hex');
destinationAddressLength = destinationAddress.length;
}
var sentInvokeIdAsBuffer = Buffer.allocUnsafe(1); // The invoke id is returned by the CAS BACnet Stack after the message is sent.
CASBACnetStack.stack.BACnetStack_SendReadProperty(sentInvokeIdAsBuffer, newConnectionString, CONNECTION_STRING_SIZE, CASBACnetStack.CONSTANTS.NETWORK_TYPE_BACNET_IP, destinationNetwork, destinationAddress, destinationAddressLength);
logger.info('Sent ReadPropertyMultiple to device: ' + deviceIdentifier + ", connectionString: " + deviceObject.connectionString + ", sentInvokeId: " + sentInvokeIdAsBuffer.readUint8());
database.AddNewRequest(deviceObject.connectionString, sentInvokeIdAsBuffer.readUint8(), destinationNetwork, destinationAddress, deviceIdentifier, objectType, deviceIdentifier);
});
}
setInterval(DoReadProperty, 1000 * SETTING_TIMER_READ_PROPERTY_SECONDS);
}
// Start the application.
// ------------------------------------------------------------------------
main();