forked from BiancoRoyal/node-bacstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
1305 lines (1240 loc) · 71.7 KB
/
client.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// Util Modules
const EventEmitter = require('events').EventEmitter;
const debug = require('debug')('bacstack');
// Local Modules
const baTransport = require('./transport');
const baServices = require('./services');
const baAsn1 = require('./asn1');
const baApdu = require('./apdu');
const baNpdu = require('./npdu');
const baBvlc = require('./bvlc');
const baEnum = require('./enum');
const DEFAULT_HOP_COUNT = 0xFF;
const BVLC_HEADER_LENGTH = 4;
/**
* To be able to communicate to BACNET devices, you have to initialize a new bacstack instance.
* @class bacstack
* @param {object=} this._settings - The options object used for parameterizing the bacstack.
* @param {number=} [options.port=47808] - BACNET communication port for listening and sending.
* @param {string=} options.interface - Specific BACNET communication interface if different from primary one.
* @param {string=} [options.broadcastAddress=255.255.255.255] - The address used for broadcast messages.
* @param {number=} [options.apduTimeout=3000] - The timeout in milliseconds until a transaction should be interpreted as error.
* @example
* const bacnet = require('bacstack');
*
* const client = new bacnet({
* port: 47809, // Use BAC1 as communication port
* interface: '192.168.251.10', // Listen on a specific interface
* broadcastAddress: '192.168.251.255', // Use the subnet broadcast address
* apduTimeout: 6000 // Wait twice as long for response
* });
*/
class Client extends EventEmitter {
constructor(options) {
super();
options = options || {};
this._invokeCounter = 1;
this._invokeStore = {};
this._lastSequenceNumber = 0;
this._segmentStore = [];
this._settings = {
port: options.port || 47808,
interface: options.interface,
transport: options.transport,
broadcastAddress: options.broadcastAddress || '255.255.255.255',
apduTimeout: options.apduTimeout || 3000
};
this._transport = this._settings.transport || new baTransport({
port: this._settings.port,
interface: this._settings.interface,
broadcastAddress: this._settings.broadcastAddress
});
// Setup code
this._transport.on('message', this._receiveData.bind(this));
this._transport.on('error', this._receiveError.bind(this));
this._transport.open();
}
// Helper utils
_getInvokeId() {
const id = this._invokeCounter++;
if (id >= 256) this._invokeCounter = 1;
return id - 1;
}
_invokeCallback(id, err, result) {
const callback = this._invokeStore[id];
if (callback) return callback(err, result);
debug('InvokeId ', id, ' not found -> drop package');
}
_addCallback(id, callback) {
const timeout = setTimeout(() => {
delete this._invokeStore[id];
callback(new Error('ERR_TIMEOUT'));
}, this._settings.apduTimeout);
this._invokeStore[id] = (err, data) => {
clearTimeout(timeout);
delete this._invokeStore[id];
callback(err, data);
};
}
_getBuffer() {
return {
buffer: Buffer.alloc(this._transport.getMaxPayload()),
offset: BVLC_HEADER_LENGTH
};
}
// Service Handlers
_processError(invokeId, buffer, offset, length) {
const result = baServices.error.decode(buffer, offset, length);
if (!result) return debug('Couldn`t decode Error');
this._invokeCallback(invokeId, new Error('BacnetError - Class:' + result.class + ' - Code:' + result.code));
}
_processAbort(invokeId, reason) {
this._invokeCallback(invokeId, new Error('BacnetAbort - Reason:' + reason));
}
_segmentAckResponse(receiver, negative, server, originalInvokeId, sequencenumber, actualWindowSize) {
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE, receiver, null, DEFAULT_HOP_COUNT, baEnum.NetworkLayerMessageType.WHO_IS_ROUTER_TO_NETWORK, 0);
baApdu.encodeSegmentAck(buffer, baEnum.PduTypes.SEGMENT_ACK | (negative ? baEnum.PduSegAckBits.NEGATIVE_ACK : 0) | (server ? baEnum.PduSegAckBits.SERVER : 0), originalInvokeId, sequencenumber, actualWindowSize);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, receiver);
}
_performDefaultSegmentHandling(sender, adr, type, service, invokeId, maxSegments, maxApdu, sequencenumber, first, moreFollows, buffer, offset, length) {
if (first) {
this._segmentStore = [];
type &= ~baEnum.PduConReqBits.SEGMENTED_MESSAGE;
let apduHeaderLen = 3;
if ((type & baEnum.PDU_TYPE_MASK) === baEnum.PduTypes.CONFIRMED_REQUEST) {
apduHeaderLen = 4;
}
const apdubuffer = this._getBuffer();
apdubuffer.offset = 0;
buffer.copy(apdubuffer.buffer, apduHeaderLen, offset, offset + length);
if ((type & baEnum.PDU_TYPE_MASK) === baEnum.PduTypes.CONFIRMED_REQUEST) {
baApdu.encodeConfirmedServiceRequest(apdubuffer, type, service, maxSegments, maxApdu, invokeId, 0, 0);
} else {
baApdu.encodeComplexAck(apdubuffer, type, service, invokeId, 0, 0);
}
this._segmentStore.push(apdubuffer.buffer.slice(0, length + apduHeaderLen));
} else {
this._segmentStore.push(buffer.slice(offset, offset + length));
}
if (!moreFollows) {
const apduBuffer = Buffer.concat(this._segmentStore);
this._segmentStore = [];
type &= ~baEnum.PduConReqBits.SEGMENTED_MESSAGE;
this._handlePdu(adr, type, apduBuffer, 0, apduBuffer.length);
}
}
_processSegment(receiver, type, service, invokeId, maxSegments, maxApdu, server, sequencenumber, proposedWindowNumber, buffer, offset, length) {
let first = false;
if (sequencenumber === 0 && this._lastSequenceNumber === 0) {
first = true;
} else {
if (sequencenumber !== this._lastSequenceNumber + 1) {
return this._segmentAckResponse(receiver, true, server, invokeId, this._lastSequenceNumber, proposedWindowNumber);
}
}
this._lastSequenceNumber = sequencenumber;
const moreFollows = type & baEnum.PduConReqBits.MORE_FOLLOWS;
if (!moreFollows) {
this._lastSequenceNumber = 0;
}
if ((sequencenumber % proposedWindowNumber) === 0 || !moreFollows) {
this._segmentAckResponse(receiver, false, server, invokeId, sequencenumber, proposedWindowNumber);
}
this._performDefaultSegmentHandling(this, receiver, type, service, invokeId, maxSegments, maxApdu, sequencenumber, first, moreFollows, buffer, offset, length);
}
_processConfirmedServiceRequest(address, type, service, maxSegments, maxApdu, invokeId, buffer, offset, length) {
let result;
debug('Handle this._processConfirmedServiceRequest');
if (service === baEnum.ConfirmedServiceChoice.READ_PROPERTY) {
result = baServices.readProperty.decode(buffer, offset, length);
if (!result) return debug('Received invalid readProperty message');
this.emit('readProperty', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.WRITE_PROPERTY) {
result = baServices.writeProperty.decode(buffer, offset, length);
if (!result) return debug('Received invalid writeProperty message');
this.emit('writeProperty', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.READ_PROPERTY_MULTIPLE) {
result = baServices.readPropertyMultiple.decode(buffer, offset, length);
if (!result) return debug('Received invalid readPropertyMultiple message');
this.emit('readPropertyMultiple', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.WRITE_PROPERTY_MULTIPLE) {
result = baServices.writePropertyMultiple.decode(buffer, offset, length);
if (!result) return debug('Received invalid writePropertyMultiple message');
this.emit('writePropertyMultiple', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.CONFIRMED_COV_NOTIFICATION) {
result = baServices.covNotify.decode(buffer, offset, length);
if (!result) return debug('Received invalid covNotify message');
this.emit('covNotify', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.ATOMIC_WRITE_FILE) {
result = baServices.atomicWriteFile.decode(buffer, offset, length);
if (!result) return debug('Received invalid atomicWriteFile message');
this.emit('atomicWriteFile', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.ATOMIC_READ_FILE) {
result = baServices.atomicReadFile.decode(buffer, offset, length);
if (!result) return debug('Received invalid atomicReadFile message');
this.emit('atomicReadFile', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.SUBSCRIBE_COV) {
result = baServices.subscribeCov.decode(buffer, offset, length);
if (!result) return debug('Received invalid subscribeCOV message');
this.emit('subscribeCOV', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.SUBSCRIBE_COV_PROPERTY) {
result = baServices.subscribeProperty.decode(buffer, offset, length);
if (!result) return debug('Received invalid subscribeProperty message');
this.emit('subscribeProperty', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.DEVICE_COMMUNICATION_CONTROL) {
result = baServices.deviceCommunicationControl.decode(buffer, offset, length);
if (!result) return debug('Received invalid deviceCommunicationControl message');
this.emit('deviceCommunicationControl', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.REINITIALIZE_DEVICE) {
result = baServices.reinitializeDevice.decode(buffer, offset, length);
if (!result) return debug('Received invalid reinitializeDevice message');
this.emit('reinitializeDevice', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.CONFIRMED_EVENT_NOTIFICATION) {
result = baServices.eventNotifyData.decode(buffer, offset, length);
if (!result) return debug('Received invalid eventNotifyData message');
this.emit('eventNotifyData', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.READ_RANGE) {
result = baServices.readRange.decode(buffer, offset, length);
if (!result) return debug('Received invalid readRange message');
this.emit('readRange', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.CREATE_OBJECT) {
result = baServices.createObject.decode(buffer, offset, length);
if (!result) return debug('Received invalid createObject message');
this.emit('createObject', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.DELETE_OBJECT) {
result = baServices.deleteObject.decode(buffer, offset, length);
if (!result) return debug('Received invalid deleteObject message');
this.emit('deleteObject', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.ACKNOWLEDGE_ALARM) {
result = baServices.alarmAcknowledge.decode(buffer, offset, length);
if (!result) return debug('Received invalid alarmAcknowledge message');
this.emit('alarmAcknowledge', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.GET_ALARM_SUMMARY) {
this.emit('getAlarmSummary', {address: address, invokeId: invokeId});
} else if (service === baEnum.ConfirmedServiceChoice.GET_ENROLLMENT_SUMMARY) {
result = baServices.getEnrollmentSummary.decode(buffer, offset, length);
if (!result) return debug('Received invalid getEntrollmentSummary message');
this.emit('getEntrollmentSummary', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.GET_EVENT_INFORMATION) {
result = baServices.getEventInformation.decode(buffer, offset, length);
if (!result) return debug('Received invalid getEventInformation message');
this.emit('getEventInformation', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.LIFE_SAFETY_OPERATION) {
result = baServices.lifeSafetyOperation.decode(buffer, offset, length);
if (!result) return debug('Received invalid lifeSafetyOperation message');
this.emit('lifeSafetyOperation', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.ADD_LIST_ELEMENT) {
result = baServices.addListElement.decode(buffer, offset, length);
if (!result) return debug('Received invalid addListElement message');
this.emit('addListElement', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.REMOVE_LIST_ELEMENT) {
result = baServices.addListElement.decode(buffer, offset, length);
if (!result) return debug('Received invalid removeListElement message');
this.emit('removeListElement', {address: address, invokeId: invokeId, request: result});
} else if (service === baEnum.ConfirmedServiceChoice.CONFIRMED_PRIVATE_TRANSFER) {
result = baServices.privateTransfer.decode(buffer, offset, length);
if (!result) return debug('Received invalid privateTransfer message');
this.emit('privateTransfer', {address: address, invokeId: invokeId, request: result});
} else {
debug('Received unsupported confirmed service request');
}
}
_processUnconfirmedServiceRequest(address, type, service, buffer, offset, length) {
let result;
debug('Handle this._processUnconfirmedServiceRequest');
if (service === baEnum.UnconfirmedServiceChoice.I_AM) {
result = baServices.iAmBroadcast.decode(buffer, offset);
if (!result) return debug('Received invalid iAm message');
/**
* The iAm event represents the response to a whoIs request to detect all devices in a BACNET network.
* @event bacstack.iAm
* @param {object} device - An object representing the detected device.
* @param {string} device.address - The IP address of the detected device.
* @param {number} device.deviceId - The BACNET device-id of the detected device.
* @param {number} device.maxApdu - The max APDU size the detected device is supporting.
* @param {number} device.segmentation - The type of segmentation the detected device is supporting.
* @param {number} device.vendorId - The BACNET vendor-id of the detected device.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.on('iAm', (device) => {
* console.log('address: ', device.address, ' - deviceId: ', device.deviceId, ' - maxApdu: ', device.maxApdu, ' - segmentation: ', device.segmentation, ' - vendorId: ', device.vendorId);
* });
*/
this.emit('iAm', {address: address, deviceId: result.deviceId, maxApdu: result.maxApdu, segmentation: result.segmentation, vendorId: result.vendorId});
} else if (service === baEnum.UnconfirmedServiceChoice.WHO_IS) {
result = baServices.whoIs.decode(buffer, offset, length);
if (!result) return debug('Received invalid WhoIs message');
/**
* The whoIs event represents the request for an IAm reponse to detect all devices in a BACNET network.
* @event bacstack.whoIs
* @param {object} request - An object representing the received request.
* @param {string} request.address - The IP address of the device sending the request.
* @param {number=} request.lowLimit - The lower limit of the BACNET device-id.
* @param {number=} request.highLimit - The higher limit of the BACNET device-id.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.on('whoIs', (request) => {
* console.log('address: ', device.address, ' - lowLimit: ', device.lowLimit, ' - highLimit: ', device.highLimit);
* });
*/
this.emit('whoIs', {address: address, lowLimit: result.lowLimit, highLimit: result.highLimit});
} else if (service === baEnum.UnconfirmedServiceChoice.WHO_HAS) {
result = baServices.whoHas.decode(buffer, offset, length);
if (!result) return debug('Received invalid WhoHas message');
this.emit('whoHas', {address: address, lowLimit: result.lowLimit, highLimit: result.highLimit, objectId: result.objectId, objectName: result.objectName});
} else if (service === baEnum.UnconfirmedServiceChoice.UNCONFIRMED_COV_NOTIFICATION) {
result = baServices.covNotify.decode(buffer, offset, length);
if (!result) return debug('Received invalid covNotifyUnconfirmed message');
this.emit('covNotifyUnconfirmed', {address: address, request: result});
} else if (service === baEnum.UnconfirmedServiceChoice.TIME_SYNCHRONIZATION) {
result = baServices.timeSync.decode(buffer, offset, length);
if (!result) return debug('Received invalid TimeSync message');
/**
* The timeSync event represents the request to synchronize the local time to the received time.
* @event bacstack.timeSync
* @param {object} request - An object representing the received request.
* @param {string} request.address - The IP address of the device sending the request.
* @param {date} request.dateTime - The time to be synchronized to.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.on('timeSync', (request) => {
* console.log('address: ', device.address, ' - dateTime: ', device.dateTime);
* });
*/
this.emit('timeSync', {address: address, dateTime: result.dateTime});
} else if (service === baEnum.UnconfirmedServiceChoice.UTC_TIME_SYNCHRONIZATION) {
result = baServices.timeSync.decode(buffer, offset, length);
if (!result) return debug('Received invalid TimeSyncUTC message');
/**
* The timeSyncUTC event represents the request to synchronize the local time to the received UTC time.
* @event bacstack.timeSyncUTC
* @param {object} request - An object representing the received request.
* @param {string} request.address - The IP address of the device sending the request.
* @param {date} request.dateTime - The time to be synchronized to.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.on('timeSyncUTC', (request) => {
* console.log('address: ', device.address, ' - dateTime: ', device.dateTime);
* });
*/
this.emit('timeSyncUTC', {address: address, dateTime: result.dateTime});
} else if (service === baEnum.UnconfirmedServiceChoice.UNCONFIRMED_EVENT_NOTIFICATION) {
result = baServices.eventNotifyData.decode(buffer, offset, length);
if (!result) return debug('Received invalid EventNotify message');
this.emit('eventNotify', {address: address, eventData: result.eventData});
} else if (service === baEnum.UnconfirmedServiceChoice.I_HAVE) {
result = baServices.iHaveBroadcast.decode(buffer, offset, length);
if (!result) return debug('Received invalid ihaveBroadcast message');
this.emit('ihaveBroadcast', {address: address, eventData: result.eventData});
} else if (service === baEnum.UnconfirmedServiceChoice.UNCONFIRMED_PRIVATE_TRANSFER) {
result = baServices.privateTransfer.decode(buffer, offset, length);
if (!result) return debug('Received invalid privateTransfer message');
this.emit('privateTransfer', {address: address, eventData: result.eventData});
} else {
debug('Received unsupported unconfirmed service request');
}
}
_handlePdu(address, type, buffer, offset, length) {
let result;
// Handle different PDU types
switch (type & baEnum.PDU_TYPE_MASK) {
case baEnum.PduTypes.UNCONFIRMED_REQUEST:
result = baApdu.decodeUnconfirmedServiceRequest(buffer, offset);
this._processUnconfirmedServiceRequest(address, result.type, result.service, buffer, offset + result.len, length - result.len);
break;
case baEnum.PduTypes.SIMPLE_ACK:
result = baApdu.decodeSimpleAck(buffer, offset);
offset += result.len;
length -= result.len;
this._invokeCallback(result.invokeId, null, {result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
break;
case baEnum.PduTypes.COMPLEX_ACK:
result = baApdu.decodeComplexAck(buffer, offset);
if ((type & baEnum.PduConReqBits.SEGMENTED_MESSAGE) === 0) {
this._invokeCallback(result.invokeId, null, {result: result, buffer: buffer, offset: offset + result.len, length: length - result.len});
} else {
this._processSegment(address, result.type, result.service, result.invokeId, baEnum.MaxSegmentsAccepted.SEGMENTS_0, baEnum.MaxApduLengthAccepted.OCTETS_50, false, result.sequencenumber, result.proposedWindowNumber, buffer, offset + result.len, length - result.len);
}
break;
case baEnum.PduTypes.SEGMENT_ACK:
result = baApdu.decodeSegmentAck(buffer, offset);
//m_last_segment_ack.Set(address, result.originalInvokeId, result.sequencenumber, result.actualWindowSize);
//this._processSegmentAck(address, result.type, result.originalInvokeId, result.sequencenumber, result.actualWindowSize, buffer, offset + result.len, length - result.len);
break;
case baEnum.PduTypes.ERROR:
result = baApdu.decodeError(buffer, offset);
this._processError(result.invokeId, buffer, offset + result.len, length - result.len);
break;
case baEnum.PduTypes.REJECT:
case baEnum.PduTypes.ABORT:
result = baApdu.decodeAbort(buffer, offset);
this._processAbort(result.invokeId, result.reason);
break;
case baEnum.PduTypes.CONFIRMED_REQUEST:
result = baApdu.decodeConfirmedServiceRequest(buffer, offset);
if ((type & baEnum.PduConReqBits.SEGMENTED_MESSAGE) === 0) {
this._processConfirmedServiceRequest(address, result.type, result.service, result.maxSegments, result.maxApdu, result.invokeId, buffer, offset + result.len, length - result.len);
} else {
this._processSegment(address, result.type, result.service, result.invokeId, result.maxSegments, result.maxApdu, true, result.sequencenumber, result.proposedWindowNumber, buffer, offset + result.len, length - result.len);
}
break;
default:
debug('Received unknown PDU type -> Drop package');
break;
}
}
_handleNpdu(buffer, offset, msgLength, remoteAddress) {
// Check data length
if (msgLength <= 0) return debug('No NPDU data -> Drop package');
// Parse baNpdu header
const result = baNpdu.decode(buffer, offset);
if (!result) return debug('Received invalid NPDU header -> Drop package');
if (result.funct & baEnum.NpduControlBits.NETWORK_LAYER_MESSAGE) {
return debug('Received network layer message -> Drop package');
}
offset += result.len;
msgLength -= result.len;
if (msgLength <= 0) return debug('No APDU data -> Drop package');
const apduType = baApdu.getDecodedType(buffer, offset);
this._handlePdu(remoteAddress, apduType, buffer, offset, msgLength);
}
_receiveData(buffer, remoteAddress) {
// Check data length
if (buffer.length < baBvlc.BVLC_HEADER_LENGTH) return debug('Received invalid data -> Drop package');
// Parse BVLC header
const result = baBvlc.decode(buffer, 0);
if (!result) return debug('Received invalid BVLC header -> Drop package');
// Check BVLC function
if (result.func === baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU || result.func === baEnum.BvlcResultPurpose.ORIGINAL_BROADCAST_NPDU || result.func === baEnum.BvlcResultPurpose.FORWARDED_NPDU) {
this._handleNpdu(buffer, result.len, buffer.length - result.len, remoteAddress);
} else {
debug('Received unknown BVLC function -> Drop package');
}
}
_receiveError(err) {
/**
* @event bacstack.error
* @param {error} err - The error object thrown by the underlying transport layer.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.on('error', (err) => {
* console.log('Error occurred: ', err);
* client.close();
* });
*/
this.emit('error', err);
}
/**
* The whoIs command discovers all BACNET devices in a network.
* @function bacstack.whoIs
* @param {object=} options
* @param {number=} options.lowLimit - Minimal device instance number to search for.
* @param {number=} options.highLimit - Maximal device instance number to search for.
* @param {string=} options.address - Unicast address if command should address a device directly.
* @fires bacstack.iAm
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.whoIs();
*/
whoIs(options) {
options = options || {};
const settings = {
lowLimit: options.lowLimit,
highLimit: options.highLimit,
address: options.address || this._transport.getBroadcastAddress()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE, this._settings.address, null, DEFAULT_HOP_COUNT, baEnum.NetworkLayerMessageType.WHO_IS_ROUTER_TO_NETWORK, 0);
baApdu.encodeUnconfirmedServiceRequest(buffer, baEnum.PduTypes.UNCONFIRMED_REQUEST, baEnum.UnconfirmedServiceChoice.WHO_IS);
baServices.whoIs.encode(buffer, settings.lowLimit, settings.highLimit);
const npduType = (this._settings.address !== this._transport.getBroadcastAddress()) ? baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU : baEnum.BvlcResultPurpose.ORIGINAL_BROADCAST_NPDU;
baBvlc.encode(buffer.buffer, npduType, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, settings.address);
}
/**
* The timeSync command sets the time of a target device.
* @function bacstack.timeSync
* @param {string} address - IP address of the target device.
* @param {date} dateTime - The date and time to set on the target device.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.timeSync('192.168.1.43', new Date());
*/
timeSync(address, dateTime) {
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE, address);
baApdu.encodeUnconfirmedServiceRequest(buffer, baEnum.PduTypes.UNCONFIRMED_REQUEST, baEnum.UnconfirmedServiceChoice.TIME_SYNCHRONIZATION);
baServices.timeSync.encode(buffer, dateTime);
const npduType = (address !== this._transport.getBroadcastAddress()) ? baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU : baEnum.BvlcResultPurpose.ORIGINAL_BROADCAST_NPDU;
baBvlc.encode(buffer.buffer, npduType, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
}
/**
* The timeSyncUTC command sets the UTC time of a target device.
* @function bacstack.timeSyncUTC
* @param {string} address - IP address of the target device.
* @param {date} dateTime - The date and time to set on the target device.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.timeSyncUTC('192.168.1.43', new Date());
*/
timeSyncUTC(address, dateTime) {
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE, address);
baApdu.encodeUnconfirmedServiceRequest(buffer, baEnum.PduTypes.UNCONFIRMED_REQUEST, baEnum.UnconfirmedServiceChoice.UTC_TIME_SYNCHRONIZATION);
baServices.timeSync.encode(buffer, dateTime);
const npduType = (address !== this._transport.getBroadcastAddress()) ? baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU : baEnum.BvlcResultPurpose.ORIGINAL_BROADCAST_NPDU;
baBvlc.encode(buffer.buffer, npduType, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
}
/**
* The readProperty command reads a single property of an object from a device.
* @function bacstack.readProperty
* @param {string} address - IP address of the target device.
* @param {object} objectId - The BACNET object ID to read.
* @param {number} objectId.type - The BACNET object type to read.
* @param {number} objectId.instance - The BACNET object instance to read.
* @param {number} propertyId - The BACNET property id in the specified object to read.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {number=} options.arrayIndex - The array index of the property to be read.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.readProperty('192.168.1.43', {type: 8, instance: 44301}, 28, (err, value) => {
* console.log('value: ', value);
* });
*/
readProperty(address, objectId, propertyId, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId(),
arrayIndex: options.arrayIndex || baEnum.ASN1_ARRAY_ALL
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address, null, DEFAULT_HOP_COUNT, baEnum.NetworkLayerMessageType.WHO_IS_ROUTER_TO_NETWORK, 0);
const type = baEnum.PduTypes.CONFIRMED_REQUEST | (settings.maxSegments !== baEnum.MaxSegmentsAccepted.SEGMENTS_0 ? baEnum.PduConReqBits.SEGMENTED_RESPONSE_ACCEPTED : 0);
baApdu.encodeConfirmedServiceRequest(buffer, type, baEnum.ConfirmedServiceChoice.READ_PROPERTY, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.readProperty.encode(buffer, objectId.type, objectId.instance, propertyId, settings.arrayIndex);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
const result = baServices.readProperty.decodeAcknowledge(data.buffer, data.offset, data.length);
if (!result) return next(new Error('INVALID_DECODING'));
next(null, result);
});
}
/**
* The writeProperty command writes a single property of an object to a device.
* @function bacstack.writeProperty
* @param {string} address - IP address of the target device.
* @param {object} objectId - The BACNET object ID to write.
* @param {number} objectId.type - The BACNET object type to write.
* @param {number} objectId.instance - The BACNET object instance to write.
* @param {number} propertyId - The BACNET property id in the specified object to write.
* @param {object[]} values - A list of values to be written to the specified property.
* @param {ApplicationTags} values.tag - The data-type of the value to be written.
* @param {number} values.value - The actual value to be written.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {number=} options.arrayIndex - The array index of the property to be read.
* @param {number=} options.priority - The priority of the value to be written.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.writeProperty('192.168.1.43', {type: 8, instance: 44301}, 28, [
* {type: bacnet.enum.ApplicationTags.REAL, value: 100}
* ], (err, value) => {
* console.log('value: ', value);
* });
*/
writeProperty(address, objectId, propertyId, values, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId(),
arrayIndex: options.arrayIndex || baEnum.ASN1_ARRAY_ALL,
priority: options.priority
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address, null, DEFAULT_HOP_COUNT, baEnum.NetworkLayerMessageType.WHO_IS_ROUTER_TO_NETWORK, 0);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.WRITE_PROPERTY, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.writeProperty.encode(buffer, objectId.type, objectId.instance, propertyId, settings.arrayIndex, settings.priority, values);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
next(err);
});
}
/**
* The readPropertyMultiple command reads multiple properties in multiple objects from a device.
* @function bacstack.readPropertyMultiple
* @param {string} address - IP address of the target device.
* @param {object[]} requestArray - List of object and property specifications to be read.
* @param {object} requestArray.objectId - Specifies which object to read.
* @param {number} requestArray.objectId.type - The BACNET object type to read.
* @param {number} requestArray.objectId.instance - The BACNET object instance to read.
* @param {object[]} requestArray.properties - List of properties to be read.
* @param {number} requestArray.properties.id - The BACNET property id in the specified object to read. Also supports 8 for all properties.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* const requestArray = [
* {objectId: {type: 8, instance: 4194303}, properties: [{id: 8}]}
* ];
* client.readPropertyMultiple('192.168.1.43', requestArray, (err, value) => {
* console.log('value: ', value);
* });
*/
readPropertyMultiple(address, propertiesArray, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address, null, DEFAULT_HOP_COUNT, baEnum.NetworkLayerMessageType.WHO_IS_ROUTER_TO_NETWORK, 0);
const type = baEnum.PduTypes.CONFIRMED_REQUEST | (settings.maxSegments !== baEnum.MaxSegmentsAccepted.SEGMENTS_0 ? baEnum.PduConReqBits.SEGMENTED_RESPONSE_ACCEPTED : 0);
baApdu.encodeConfirmedServiceRequest(buffer, type, baEnum.ConfirmedServiceChoice.READ_PROPERTY_MULTIPLE, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.readPropertyMultiple.encode(buffer, propertiesArray);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
const result = baServices.readPropertyMultiple.decodeAcknowledge(data.buffer, data.offset, data.length);
if (!result) return next(new Error('INVALID_DECODING'));
next(null, result);
});
}
/**
* 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[]} values - List of object and property specifications to be written.
* @param {object} values.objectId - Specifies which object to read.
* @param {number} values.objectId.type - The BACNET object type to read.
* @param {number} values.objectId.instance - The BACNET object instance to read.
* @param {object[]} values.values - List of properties to be written.
* @param {object} values.values.property - Property specifications to be written.
* @param {number} values.values.property.id - The BACNET property id in the specified object to write.
* @param {number} values.values.property.index - The array index of the property to be written.
* @param {object[]} values.values.value - A list of values to be written to the specified property.
* @param {ApplicationTags} values.values.value.tag - The data-type of the value to be written.
* @param {object} values.values.value.value - The actual value to be written.
* @param {number} values.values.priority - The priority to be used for writing to the property.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* const values = [
* {objectId: {type: 8, instance: 44301}, values: [
* {property: {id: 28, index: 12}, value: [{type: bacnet.enum.ApplicationTags.BOOLEAN, value: true}], priority: 8}
* ]}
* ];
* client.writePropertyMultiple('192.168.1.43', values, (err, value) => {
* console.log('value: ', value);
* });
*/
writePropertyMultiple(address, values, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.WRITE_PROPERTY_MULTIPLE, settings.maxSegments, settings.maxApdu, settings.invokeId);
baServices.writePropertyMultiple.encodeObject(buffer, values);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
next(err);
});
}
/**
* The deviceCommunicationControl command enables or disables network communication of the target device.
* @function bacstack.deviceCommunicationControl
* @param {string} address - IP address of the target device.
* @param {number} timeDuration - The time to hold the network communication state in seconds. 0 for infinite.
* @param {EnableDisable} enableDisable - The network communication state to set.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {string=} options.password - The optional password used to set the network communication state.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.deviceCommunicationControl('192.168.1.43', 0, bacnet.enum.EnableDisable.DISABLE, (err) => {
* console.log('error: ', err);
* });
*/
deviceCommunicationControl(address, timeDuration, enableDisable, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId(),
password: options.password
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.DEVICE_COMMUNICATION_CONTROL, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.deviceCommunicationControl.encode(buffer, timeDuration, enableDisable, settings.password);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
next(err);
});
}
/**
* The reinitializeDevice command initiates a restart of the target device.
* @function bacstack.reinitializeDevice
* @param {string} address - IP address of the target device.
* @param {ReinitializedState} state - The type of restart to be initiated.
* @param {object=} options
* @param {MaxSegmentsAccepted=} options.maxSegments - The maximimal allowed number of segments.
* @param {MaxApduLengthAccepted=} options.maxApdu - The maximal allowed APDU size.
* @param {number=} options.invokeId - The invoke ID of the confirmed service telegram.
* @param {string=} options.password - The optional password used to restart the device.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.reinitializeDevice('192.168.1.43', bacnet.enum.ReinitializedState.COLDSTART, (err, value) => {
* console.log('value: ', value);
* });
*/
reinitializeDevice(address, state, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId(),
password: options.password
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.REINITIALIZE_DEVICE, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.reinitializeDevice.encode(buffer, state, settings.password);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
next(err);
});
}
writeFile(address, objectId, position, fileBuffer, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.ATOMIC_WRITE_FILE, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.atomicWriteFile.encode(buffer, false, objectId, position, fileBuffer);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
const result = baServices.atomicWriteFile.decodeAcknowledge(data.buffer, data.offset, data.length);
if (!result) return next(new Error('INVALID_DECODING'));
next(null, result);
});
}
readFile(address, objectId, position, count, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.ATOMIC_READ_FILE, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.atomicReadFile.encode(buffer, true, objectId, position, count);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
const result = baServices.atomicReadFile.decodeAcknowledge(data.buffer, data.offset, data.length);
if (!result) return next(new Error('INVALID_DECODING'));
next(null, result);
});
}
readRange(address, objectId, idxBegin, quantity, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.READ_RANGE, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.readRange.encode(buffer, objectId, baEnum.PropertyIdentifier.LOG_BUFFER, baEnum.ASN1_ARRAY_ALL, baEnum.ReadRangeType.BY_POSITION, idxBegin, new Date(), quantity);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
const result = baServices.readRange.decodeAcknowledge(data.buffer, data.offset, data.length);
if (!result) return next(new Error('INVALID_DECODING'));
next(null, result);
});
}
subscribeCOV(address, objectId, subscribeId, cancel, issueConfirmedNotifications, lifetime, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.SUBSCRIBE_COV, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.subscribeCov.encode(buffer, subscribeId, objectId, cancel, issueConfirmedNotifications, lifetime);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
next();
});
}
subscribeProperty(address, objectId, monitoredProperty, subscribeId, cancel, issueConfirmedNotifications, options, next) {
next = next || options;
const settings = {
maxSegments: options.maxSegments || baEnum.MaxSegmentsAccepted.SEGMENTS_65,
maxApdu: options.maxApdu || baEnum.MaxApduLengthAccepted.OCTETS_1476,
invokeId: options.invokeId || this._getInvokeId()
};
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE | baEnum.NpduControlBits.EXPECTING_REPLY, address);
baApdu.encodeConfirmedServiceRequest(buffer, baEnum.PduTypes.CONFIRMED_REQUEST, baEnum.ConfirmedServiceChoice.SUBSCRIBE_COV_PROPERTY, settings.maxSegments, settings.maxApdu, settings.invokeId, 0, 0);
baServices.subscribeProperty.encode(buffer, subscribeId, objectId, cancel, issueConfirmedNotifications, 0, monitoredProperty, false, 0x0f);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
this._addCallback(settings.invokeId, (err, data) => {
if (err) return next(err);
next();
});
}
/**
* The unconfirmedCOVNotification command sends an unconfirmed COV notification to a device
* @function bacstack.unconfirmedCOVNotification
* @param {string} address - IP address of the target device.
* @param {number} subscriberProcessId - The process id which was used by a target device in the subscription.
* @param {number} initiatingDeviceId - The id of this device.
* @param {object} monitoredObjectId - Specifies about which object the notification is.
* @param {number} monitoredObjectId.type - The BACNET object type of the notification.
* @param {number} monitoredObjectId.instance - The BACNET object instance of the notification.
* @param {number} timeRemaining - How long the subscription is still active in seconds.
* @param {object[]} values - List of properties with updated values.
* @param {object} values.property - Property specifications.
* @param {number} values.property.id - The updated BACNET property id.
* @param {number} values.property.index - The array index of the updated property.
* @param {object[]} values.value - A list of updated values.
* @param {ApplicationTags} values.value.type - The data-type of the updated value.
* @param {object} values.value.value - The actual updated value.
* @param {number} values.priority - The priority of the updated property.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.unconfirmedCOVNotification(
* '127.0.0.1',
* 3,
* 433,
* {type: 2, instance: 122},
* 120,
* [
* {
* property: {id: 85},
* value: [{type: baEnum.ApplicationTags.REAL, value: 12.3}]
* },
* {
* property: {id: 111},
* value: [{type: baEnum.ApplicationTags.BIT_STRING, value: 0xFFFF}]
* }
* ]);
*/
unconfirmedCOVNotification(address, subscriberProcessId, initiatingDeviceId, monitoredObjectId, timeRemaining, values) {
const buffer = this._getBuffer();
baNpdu.encode(buffer, baEnum.NpduControlPriority.NORMAL_MESSAGE, address);
baApdu.encodeUnconfirmedServiceRequest(buffer, baEnum.PduTypes.UNCONFIRMED_REQUEST, baEnum.UnconfirmedServiceChoice.UNCONFIRMED_COV_NOTIFICATION);
baServices.covNotify.encode(buffer, subscriberProcessId, initiatingDeviceId, monitoredObjectId, timeRemaining, values);
baBvlc.encode(buffer.buffer, baEnum.BvlcResultPurpose.ORIGINAL_UNICAST_NPDU, buffer.offset);
this._transport.send(buffer.buffer, buffer.offset, address);
}
/**
* The unconfirmedCOVNotification command sends an unconfirmed COV notification to a device
* @function bacstack.unconfirmedCOVNotification
* @param {string} address - IP address of the target device.
* @param {number} subscriberProcessId - The process id which was used by a target device in the subscription.
* @param {number} initiatingDeviceId - The id of this device.
* @param {object} monitoredObjectId - Specifies about which object the notification is.
* @param {number} monitoredObjectId.type - The BACNET object type of the notification.
* @param {number} monitoredObjectId.instance - The BACNET object instance of the notification.
* @param {number} timeRemaining - How long the subscription is still active in seconds.
* @param {object[]} values - List of properties with updated values.
* @param {object} values.property - Property specifications.
* @param {number} values.property.id - The updated BACNET property id.
* @param {number} values.property.index - The array index of the updated property.
* @param {object[]} values.value - A list of updated values.
* @param {ApplicationTags} values.value.type - The data-type of the updated value.
* @param {object} values.value.value - The actual updated value.
* @param {number} values.priority - The priority of the updated property.
* @param {function} next - The callback containing an error, in case of a failure and value object in case of success.
* @example
* const bacnet = require('bacstack');
* const client = new bacnet();
*
* client.confirmedCOVNotification(
* '127.0.0.1',
* 3,
* 433,
* {type: 2, instance: 122},
* 120,
* [
* {
* property: {id: 85},
* value: [{type: baEnum.ApplicationTags.REAL, value: 12.3}]
* },
* {
* property: {id: 111},
* value: [{type: baEnum.ApplicationTags.BIT_STRING, value: 0xFFFF}]
* }
* ],