-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeS7CommPlus.js
More file actions
1156 lines (1014 loc) · 48.6 KB
/
nodeS7CommPlus.js
File metadata and controls
1156 lines (1014 loc) · 48.6 KB
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
const path = require('path');
const fs = require('fs');
const os = require('os');
const crypto = require('crypto');
// IMPORT EDGE-JS ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const nodeVersion = process.versions.node.split(".")[0];
const platform = process.platform;
const arch = process.arch;
const pathSep = path.sep;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const ns7cpLib_folder = 'ns7cpLib';
const EdgeJsLib_folder = 'EdgeJSLib';
const EdgeJsNative_folder = 'EdgeJsNative';
const S7CommPlus_folder = 'S7CommPlus';
const S7CommPlusWrapper_lib = 'S7CommPlusWrapper.dll';
const NS7CPlib_path = path.join(__dirname, ns7cpLib_folder);
const NS7CPlib_tempPath = path.resolve(os.tmpdir(), `${ns7cpLib_folder}-${hashString(NS7CPlib_path)}`);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var edgeJsNativePath = null;
var edgeJsLibPath = null;
var s7commplusLibPath = null;
const isPkg = typeof process.pkg !== 'undefined';
if (isPkg) {
// check to make sure ns7cp_lib folder was bundled
if (!fs.existsSync(NS7CPlib_path)) {
throw new Error(
`Missing bundled asset: ${NS7CPlib_path}\n` +
`This usually means the asset was not included in your yao-pkg bundle.\n\n` +
`To fix this, add the following to your yao-pkg.config.js:\n\n` +
` assets: [\n "${NS7CPlib_path}"\n ]\n\n` +
`Make sure this path is correct relative to your project root.`
);
}
// extract bundled ns7cp_lib folder to a temporary directory (if not already done)
if (!fs.existsSync(NS7CPlib_tempPath)) {
copyDirectorySync(NS7CPlib_path, NS7CPlib_tempPath);
}
// initialize ns7cp_lib paths
edgeJsNativePath = path.join(NS7CPlib_tempPath, EdgeJsNative_folder);
edgeJsLibPath = path.join(NS7CPlib_tempPath, EdgeJsLib_folder);
s7commplusLibPath = path.join(NS7CPlib_tempPath, S7CommPlus_folder, S7CommPlusWrapper_lib);
} else {
// initialize ns7cp_lib paths
edgeJsNativePath = path.join(NS7CPlib_path, EdgeJsNative_folder);
edgeJsLibPath = path.join(NS7CPlib_path, EdgeJsLib_folder);
s7commplusLibPath = path.join(NS7CPlib_path, S7CommPlus_folder, S7CommPlusWrapper_lib);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// resolve remainder of edge-js native files path based on platfrom, architecure, and node verison
// (user will have to make sure the node files for their target platfrom/architecture/nodeversion are built and stored
// in the appropriate, corresponding subdirectory structure within the ns7cp_lib/EdgeJsNative folder)
if (!hasSubdirectory(edgeJsNativePath, platform)) {
throw new Error(
`No ${EdgeJsNative_folder}${pathSep}${platform} subdirectory found in ${NS7CPlib_tempPath}`+
`edge_nativeclr and edge_coreclr native .node files for the ${platform} platform could not be found`
);
}
edgeJsNativePath = path.join(edgeJsNativePath, platform);
if (!hasSubdirectory(edgeJsNativePath, arch)) {
throw new Error(
`No ${EdgeJsNative_folder}${pathSep}${platform}${pathSep}${arch} subdirectory found in ${NS7CPlib_tempPath}`+
`edge_nativeclr and edge_coreclr native .node files for the ${arch} architecture could not be found`
);
}
edgeJsNativePath = path.join(edgeJsNativePath, arch);
if (!hasSubdirectory(edgeJsNativePath, nodeVersion)) {
throw new Error(
`No ${EdgeJsNative_folder}${pathSep}${platform}${pathSep}${arch}${pathSep}${nodeVersion} subdirectory found in ${NS7CPlib_tempPath}`+
`edge_nativeclr and edge_coreclr native .node files for the ${nodeVersion} architecture could not be found`
);
}
edgeJsNativePath = path.join(edgeJsNativePath, nodeVersion, 'edge_coreclr.node');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set environemnt variables
process.env.EDGE_NATIVE = edgeJsNativePath;
process.env.EDGE_APP_ROOT = edgeJsLibPath;
process.env.S7COMMPLUS = s7commplusLibPath;
process.env.EDGE_USE_CORECLR=1;
const edge = require('edge-js');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function copyDirectorySync(sourceDir, targetDir) {
// if target directory does not exist create it
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, {recursive: true});
}
// iterate through and copy source directory contents while preserving folder structure
const entries = fs.readdirSync(sourceDir, {withFileTypes: true});
for (const entry of entries) {
const sourcePath = path.join(sourceDir, entry.name);
const targetPath = path.join(targetDir, entry.name);
if (entry.isDirectory()) {
copyDirectorySync(sourcePath, targetPath);
} else {
const data = fs.readFileSync(sourcePath);
fs.writeFileSync(targetPath, data);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function hasSubdirectory(parentDir, subDirName) {
const entries = fs.readdirSync(parentDir, {withFileTypes: true});
return entries.some(entry => entry.isDirectory() && entry.name === subDirName);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function hashString(input) {
return crypto.createHash('sha256').update(input).digest('hex').slice(0, 8);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NODES7COMMPLUS CLASS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class NodeS7CommPlus {
constructor() {
this.host = "";
this.timeout = 5000;
this.connectionSessionID = 0;
this.connSessIdObtained = false;
this.isConnected = false;
this.tagMetaDataDict = new Map();
this.addRemoveArray = [];
this.readList = [];
this.writeList = []
// DEV NOTE:
// Choosing not to store password when connection parameters
// recieved to avoid potential security vulnerabilities
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS DICTIONARIES
//////////////////////////////////////////////////////////////////////////////////////////////////////
static TypeConstant = {
UNDEFINED: "[object Undefined]",
NULL: "[object Null]",
BOOLEAN: "[object Boolean]",
NUMBER: "[object Number]",
BIGINT: "[object BigInt]",
STRING: "[object String]",
SYMBOL: "[object Symbol]",
FUNCTION: "[object Function]",
OBJECT: "[object Object]",
ARRAY: "[object Array]",
DATE: "[object Date]",
REGEXP: "[object RegExp]",
ERROR: "[object Error]",
MAP: "[object Map]",
SET: "[object Set]",
WEAKMAP: "[object WeakMap]",
WEAKSET: "[object WeakSet]",
ARRAYBUFFER: "[object ArrayBuffer]",
DATAVIEW: "[object DataView]",
INT8ARRAY: "[object Int8Array]",
UINT8ARRAY: "[object Uint8Array]",
INT16ARRAY: "[object Int16Array]",
UINT16ARRAY: "[object Uint16Array]",
INT32ARRAY: "[object Int32Array]",
UINT32ARRAY: "[object Uint32Array]",
FLOAT32ARRAY: "[object Float32Array]",
FLOAT64ARRAY: "[object Float64Array]",
JSON: "[object JSON]",
PROMISE: "[object Promise]",
WINDOW: "[object Window]",
HTMLDOCUMENT: "[object HTMLDocument]",
NODELIST: "[object NodeList]",
HTMLHELEMENT: "[object HTMLElement]"
};
static GetType(property) {
return Object.prototype.toString.call(property);
};
static IsSingleton(property) {
return (
property !== null &&
typeof property !== "object" &&
!Array.isArray(property)
);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
static Property = {
HOST: "host",
PASSWORD: "password",
TIMEOUT: "timeout",
TAGFILEDIR: "tagFileDir",
TAGFILENAME: "tagFileName",
CONNRES: "connRes",
CONNSESSID: "connSessID",
CONNSTAT: "connStatOK",
GETTAGINFORES: "getTagInfoRes",
TAGINFO: "tagInfo",
ITEMSADDED: "itemsAdded",
S7COMMPLUSITEMS: "s7CommPlusItems",
READRES: "readRes",
READVALS: "readVals",
READERRORS: "readErrs",
WRITERES: "writeRes",
WRITEVALS: "writeVals",
WRITEERRORS: "writeErrs",
CONNDISCONNECTED: "connDisconnected"
};
static PropertyType = {
[this.Property.HOST]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.PASSWORD]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.TIMEOUT]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.TAGFILEDIR]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.TAGFILENAME]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.CONNRES]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.CONNSESSID]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.CONNSTAT]: NodeS7CommPlus.TypeConstant.BOOLEAN,
[this.Property.GETTAGINFORES]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.TAGINFO]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.ITEMSADDED]: NodeS7CommPlus.TypeConstant.BOOLEAN,
[this.Property.S7COMMPLUSITEMS]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.READRES]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.READVALS]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.READERRORS]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.WRITERES]: NodeS7CommPlus.TypeConstant.NUMBER,
[this.Property.WRITEVALS]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.WRITEERRORS]: NodeS7CommPlus.TypeConstant.ARRAY,
[this.Property.CONNDISCONNECTED]: NodeS7CommPlus.TypeConstant.BOOLEAN,
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
static QualityValue = {
BAD: "BAD",
OK: "OK"
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
static AddRemoveArrayAction = {
ADD: "add",
REMOVE: "remove"
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// ERROR HANDLING
//////////////////////////////////////////////////////////////////////////////////////////////////////
static ErrorOutput = class {
static Property = {
ERRORCODE: "code",
ERRORMSSG: "message",
ERRORSTACKTRACE: "stack"
};
static PropertyType = {
[this.Property.ERRORCODE]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.ERRORMSSG]: NodeS7CommPlus.TypeConstant.STRING,
[this.Property.ERRORSTACKTRACE]: NodeS7CommPlus.TypeConstant.STRING
}
static ErrorCode = {
NULL: "ENULLCODE",
INPUTPARSEERROR: "EINPARSE",
NOCONNERROR: "ENOCONN",
DRIVERCONNECTERROR: "EDRIVERCONN",
DRIVERCHECKCONNERROR: "EDRIVERCHECKCONN",
DRIVERGETTAGINFOERROR: "EDRIVERGETTAGINFO",
DRIVERREADTAGSERROR: "EDRIVERREADTAGS",
DRIVERWRITETAGSERROR: "EDRIVERWRITETAGS",
DRIVERDISCONNERROR: "EDRIVERDISCONN",
UNEXPECTEDERROR: "EUNEXPECTED"
};
constructor(errorCode = "ENULLCODE", errorInfo = "", errorStacktrace = "") {
this[NodeS7CommPlus.ErrorOutput.Property.ERRORMSSG] = errorInfo;
this[NodeS7CommPlus.ErrorOutput.Property.ERRORSTACKTRACE] = errorStacktrace;
this[NodeS7CommPlus.ErrorOutput.Property.ERRORCODE] = errorCode;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// S7COMMPLUS DLL LIBRARY FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////////////
static CONNECT = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'Connect'
});
static CHECK_CONNECTION = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'CheckConnection'
});
static GET_TAGINFO = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'GetTagInfo'
});
static READTAGS = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'ReadTags'
});
static WRITETAGS = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'WriteTags'
});
static DISCONNECT = edge.func({
assemblyFile: process.env.S7COMMPLUS,
typeName: 'S7CommPlusWrapper.S7CP',
methodName: 'Disconnect'
});
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CONNECT
//////////////////////////////////////////////////////////////////////////////////////////////////////
// DEV NOTE: For Successful connection we need to call GetTagInfo and write the retreived tag
// information to a text file as part of the honcho integration
//////////////////////////////////////////////////////////////////////////////////////////////////////
initiateConnection(connInput = {
[NodeS7CommPlus.Property.HOST]: "192.168.8.106",
[NodeS7CommPlus.Property.PASSWORD]: "",
[NodeS7CommPlus.Property.TIMEOUT]: 5000
}, callback) {
// Provide console Feedback
console.log(`Connecting to PLC with parameters:\n${JSON.stringify(connInput, null, 2)}`);
// Parse Connection Input
try { this.ParseConnectionInput(connInput); }
catch (error) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.INPUTPARSEERROR,
error.message,
error.stack
);
callback(errOutput, undefined);
return;
}
// Connect to PLC
NodeS7CommPlus.CONNECT(connInput, (error, output) => {
if (error) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERCONNECTERROR,
error.message,
error.stack
);
callback(errOutput, undefined);
return;
} else {
// check S7CommPlus driver connetion result
if (output[NodeS7CommPlus.Property.CONNRES] !== 0) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERCONNECTERROR,
`Error: Failed to connect to target PLC @ IP ${connInput[NodeS7CommPlus.Property.HOST]}, Driver Connect Result Code: ${output[NodeS7CommPlus.Property.CONNRES]}`,
new Error().stack
);
callback(errOutput, undefined);
return;
}
// Save Host IP address, timeout and Connection Session ID information
this.host = connInput[NodeS7CommPlus.Property.HOST];
this.timeout = connInput[NodeS7CommPlus.Property.TIMEOUT];
this.connectionSessionID = output[NodeS7CommPlus.Property.CONNSESSID];
// Flag connection establishment and connection status
this.connSessIdObtained = true;
this.isConnected = true;
// Build Tag Meta Data Dictionary
this.buildAndWriteTagMetaData(connInput, callback, output);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
ParseConnectionInput(connInput) {
if (NodeS7CommPlus.GetType(connInput) !== NodeS7CommPlus.TypeConstant.OBJECT) {
throw new InvalidInputError(
`Invalid Input:
Input must be an object of form:
{
${NodeS7CommPlus.Property.HOST}: ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.HOST]},
${NodeS7CommPlus.Property.PASSWORD}: ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.PASSWORD]},
${NodeS7CommPlus.Property.TIMEOUT}: ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TIMEOUT]}
}`
);
}
if (!(NodeS7CommPlus.Property.HOST in connInput)) {
throw new InvalidInputError(
`Invalid Input:
Input connection object is missing property${NodeS7CommPlus.Property.HOST}`
);
}
if (NodeS7CommPlus.GetType(connInput[NodeS7CommPlus.Property.HOST])
!== NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.HOST]) {
throw new InvalidInputError(
`Invalid Input:
Input connection object property ${NodeS7CommPlus.Property.HOST}
must be of type ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.HOST]}`
);
}
if (!(NodeS7CommPlus.Property.PASSWORD in connInput)) {
throw new InvalidInputError(
`Invalid Input:
Input connection object is missing property${NodeS7CommPlus.Property.PASSWORD}`
);
}
if (NodeS7CommPlus.GetType(connInput[NodeS7CommPlus.Property.PASSWORD])
!== NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.PASSWORD]) {
throw new InvalidInputError(
`Invalid Input:
Input connection object property ${NodeS7CommPlus.Property.PASSWORD}
must be of type ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.PASSWORD]}`
);
}
if (!(NodeS7CommPlus.Property.TIMEOUT in connInput)) {
throw new InvalidInputError(
`Invalid Input:
Input connection object is missing property${NodeS7CommPlus.Property.TIMEOUT}`
);
}
if (NodeS7CommPlus.GetType(connInput[NodeS7CommPlus.Property.TIMEOUT])
!== NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TIMEOUT]) {
throw new InvalidInputError(
`Invalid Input:
Input connection object property ${NodeS7CommPlus.Property.TIMEOUT}
must be of type ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TIMEOUT]}`
);
}
if (!(NodeS7CommPlus.Property.TAGFILEDIR in connInput)) {
throw new InvalidInputError(
`Invalid Input:
Input connection object is missing property${NodeS7CommPlus.Property.TAGFILEDIR}`
);
}
if (NodeS7CommPlus.GetType(connInput[NodeS7CommPlus.Property.TAGFILEDIR])
!== NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TAGFILEDIR]) {
throw new InvalidInputError(
`Invalid Input:
Input connection object property ${NodeS7CommPlus.Property.TAGFILEDIR}
must be of type ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TAGFILEDIR]}`
);
}
if (!(NodeS7CommPlus.Property.TAGFILENAME in connInput)) {
throw new InvalidInputError(
`Invalid Input:
Input connection object is missing property${NodeS7CommPlus.Property.TAGFILENAME}`
);
}
if (NodeS7CommPlus.GetType(connInput[NodeS7CommPlus.Property.TAGFILENAME])
!== NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TAGFILENAME]) {
throw new InvalidInputError(
`Invalid Input:
Input connection object property ${NodeS7CommPlus.Property.TAGFILENAME}
must be of type ${NodeS7CommPlus.PropertyType[NodeS7CommPlus.Property.TAGFILENAME]}`
);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
buildAndWriteTagMetaData(cParam, conn_callback, conn_output) {
let getTagInfoInput = {
[NodeS7CommPlus.Property.CONNSESSID]: this.connectionSessionID
}
NodeS7CommPlus.GET_TAGINFO(getTagInfoInput, (error, output) => {
if (error) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERGETTAGINFOERROR,
`Error: could not initialize internal tag metadata dictionary,
could not retrieve Tag Information from PLC @ IP ${this.host}, an error occured within the Driver: ${error.message}`,
error.stack
);
conn_callback(errOutput, undefined);
return;
} else {
if (output[NodeS7CommPlus.Property.GETTAGINFORES] !== 0) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERGETTAGINFOERROR,
`Error: could not initialize internal tag metadata dictionary,
could not retrieve Tag Information from PLC @ IP ${this.host}, Driver Get Tag Info Result Code: ${output[NodeS7CommPlus.Property.GETTAGINFORES]}`,
new Error().stack
);
conn_callback(errOutput, undefined);
return;
}
// build internal tag meta data dictionary (i.e. tagAccessSequence-to-{tagName, tagDatatType} mapping)
var tagInfoList = output[NodeS7CommPlus.Property.TAGINFO];
var tagInfoLen = 0;
const { tagNames, tagAccSeqs, tagDatatypes } = tagInfoList.reduce((acc, item) => {
acc.tagNames.push(item.Name);
acc.tagAccSeqs.push(item.AccessSequence);
acc.tagDatatypes.push(item.Softdatatype);
tagInfoLen++;
return acc;
}, {tagNames: [], tagAccSeqs: [], tagDatatypes: [] });
this.tagMetaDataDict = new Map(
tagAccSeqs.map((accseq, index) =>
[accseq, {tagName: tagNames[index], tagDatatype: tagDatatypes[index]}]
)
);
// write tag file
const tagFile = path.join(cParam[NodeS7CommPlus.Property.TAGFILEDIR], cParam[NodeS7CommPlus.Property.TAGFILENAME]);
const fileWriteStream = fs.createWriteStream(tagFile, {flags: 'w'});
for (let i = 0; i < tagInfoLen; i++) {
fileWriteStream.write(`${tagNames[i]}=${tagAccSeqs[i]}\n`);
}
fileWriteStream.end();
// return
conn_callback(undefined, conn_output);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CHECK CONNECTION
//////////////////////////////////////////////////////////////////////////////////////////////////////
checkConnection(callback) {
// Provide console feedback
console.log(`Checking connection to PLC @ IP: ${this.host}`);
// Check if a connection has at least been established to PLC (connection session ID will have been obtained)
if (!(this.connSessIdObtained)) {
let checkConnOutput = { [NodeS7CommPlus.Property.CONNSTAT]: false };
callback(undefined, checkConnOutput);
return;
}
// Initialize Check Connection Input
let checkConnInput = {
[NodeS7CommPlus.Property.CONNSESSID]: this.connectionSessionID
};
// Check Connection to PLC
NodeS7CommPlus.CHECK_CONNECTION(checkConnInput, (error, output) => {
if (error) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERCHECKCONNERROR,
error.message,
error.stack
);
callback(errOutput, undefined);
return;
} else {
// Update Connection Status
if (output[NodeS7CommPlus.Property.CONNSTAT] === true) {
this.isConnected = true;
} else {
this.isConnected = false;
}
callback(undefined, output);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// GET TAG INFORMATION
//////////////////////////////////////////////////////////////////////////////////////////////////////
getTagInfo(callback) {
// Provide console feedback
console.log(`Getting Tag Information from PLC @ IP: ${this.host}`);
// Check if a connection to PLC currently exists
if (!(this.isConnected)) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.NOCONNERROR,
`Cannot Retrieve Tag Information, No Connection to PLC @ IP: ${this.host}`,
new Error().stack
);
callback(errOutput, undefined);
return;
}
// Initialize GetTagInfo Input
let getTagInfoInput = {
[NodeS7CommPlus.Property.CONNSESSID]: this.connectionSessionID
}
// Get Tag Information from PLC
NodeS7CommPlus.GET_TAGINFO(getTagInfoInput, (error, output) => {
if (error) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERGETTAGINFOERROR,
error.message,
error.stack
);
callback(errOutput, undefined);
return;
} else {
if (output[NodeS7CommPlus.Property.GETTAGINFORES] !== 0) {
let errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERGETTAGINFOERROR,
`Error: Failed to retrieve Tag Information from PLC @ IP ${this.host}, Driver Get Tag Info Result Code: ${output[NodeS7CommPlus.Property.GETTAGINFORES]}`,
new Error().stack
);
callback(errOutput, undefined);
return;
}
// return GetTagInfo Output
callback(undefined, output);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// READ TAGS
//////////////////////////////////////////////////////////////////////////////////////////////////////
readAllItems(callback) {
var anyReadErrors = false;
var readOutputObject = {};
var errOutput = undefined;
// Provide console feedback
console.log(`Reading from Tags in PLC @ IP: ${this.host}`);
// Check if a connection to PLC currently exists
if (!(this.isConnected)) {
anyReadErrors = true;
readOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.NOCONNERROR,
`Cannot Read Tags, No Connection to PLC @ IP: ${this.host}`,
new Error().stack
);
callback(anyReadErrors, readOutputObject, errOutput);
return;
}
// Process Add/Remove array and add or remove items from internal read list
this.addRemoveArray.forEach((elem) => {
if (elem.action === NodeS7CommPlus.AddRemoveArrayAction.ADD) {
this.addItemsNow(elem.arg);
} else if (elem.action === NodeS7CommPlus.AddRemoveArrayAction.REMOVE) {
this.removeItemsNow(elem.arg);
}
})
this.addRemoveArray.length = 0;
// Initialize Read Tag Input
let readTagsInput = {
[NodeS7CommPlus.Property.CONNSESSID]: this.connectionSessionID,
[NodeS7CommPlus.Property.S7COMMPLUSITEMS]: this.readList
};
// Read from Tags in PLC
NodeS7CommPlus.READTAGS(readTagsInput, (error, output) => {
if (error) {
anyReadErrors = true;
readOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERREADTAGSERROR,
error.message,
error.stack
);
callback(anyReadErrors, readOutputObject, errOutput);
return;
} else {
if (output[NodeS7CommPlus.Property.READRES] !== 0) {
anyReadErrors = true;
readOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERREADTAGSERROR,
`Error: Failed to read from Tags in PLC @ IP ${this.host}, Driver Read Result Code: ${output[NodeS7CommPlus.Property.READRES]}`,
new Error().stack
);
callback(anyReadErrors, readOutputObject, errOutput);
return;
}
// overall successfull read operation but check for any single failed reads
var readValArrLen = output[NodeS7CommPlus.Property.READVALS].length;
for (let i = 0; i < readValArrLen; i++) {
if (output[NodeS7CommPlus.Property.READERRORS][i] === 0) {
readOutputObject[this.readList[i].tagAccSeq] = output[NodeS7CommPlus.Property.READVALS][i];
} else {
anyReadErrors = true;
readOutputObject[this.readList[i].tagAccSeq] = NodeS7CommPlus.QualityValue.BAD
}
}
// return
callback(anyReadErrors, readOutputObject, undefined);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
addItems(items) {
this.addRemoveArray.push({
arg: items,
action: NodeS7CommPlus.AddRemoveArrayAction.ADD
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
removeItems(items) {
this.addRemoveArray.push({
arg: items,
action: NodeS7CommPlus.AddRemoveArrayAction.REMOVE
})
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
addItemsNow(arg) {
if (Array.isArray(arg)) {
arg.forEach((tagAccSeq, index) => {
if (NodeS7CommPlus.GetType(tagAccSeq) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Element ${index} of Input Tag Access Sequence Array
must be of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
var tagMetaData = this.tagMetaDataDict.get(tagAccSeq);
if (!tagMetaData) {
throw new MissingTagMetaDataError(
`Tag Meta Data for Access Sequence ${tagAccSeq} could not be found`
);
}
this.readList.push(
new S7CommPlusItem(
tagMetaData.tagName,
tagAccSeq,
tagMetaData.tagDatatype,
)
);
});
} else {
let tagAccSeq = arg;
if (NodeS7CommPlus.GetType(tagAccSeq) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Input must be an Access Sequence of type ${NodeS7CommPlus.TypeConstant.STRING}
or an array of Access Sequences of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
var tagMetaData = this.tagMetaDataDict.get(tagAccSeq);
if (!tagMetaData) {
throw new MissingTagMetaDataError(
`Tag Meta Data for Access Sequence ${tagAccSeq} could not be found`
);
}
this.readList.push(
new S7CommPlusItem(
tagMetaData.tagName,
tagAccSeq,
tagMetaData.tagDatatype,
)
);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
removeItemsNow(arg) {
if (NodeS7CommPlus.GetType(arg) === NodeS7CommPlus.TypeConstant.UNDEFINED) {
// clear read list
this.readList.length = 0;
} else if (Array.isArray(arg)) {
// handle array case
arg.forEach((tagAccSeq, index) => {
if (NodeS7CommPlus.GetType(tagAccSeq) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Element ${index} of Input Tag Access Sequence Array
must be of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
const remIndex = this.readList.findIndex(s7commplusitem => s7commplusitem.tagAccSeq === tagAccSeq);
if (remIndex !== -1) {
this.readList.splice(remIndex, 1);
}
});
} else {
//handle singleton case
if (NodeS7CommPlus.GetType(arg) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Input must be an Access Sequence of type ${NodeS7CommPlus.TypeConstant.STRING}
or an array of Access Sequences of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
const remIndex = this.readList.findIndex(s7commplusitem => s7commplusitem.tagAccSeq === arg);
if (remIndex !== -1) {
this.readList.splice(remIndex, 1);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
findItem(items) {
if (NodeS7CommPlus.GetType(items) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Input must be an Access Sequence of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
const index = this.readList.findIndex(s7commplusitem => s7commplusitem.tagAccSeq === items);
if (index === -1) {
return undefined;
}
return this.readList[index];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// WRITE TAGS
//////////////////////////////////////////////////////////////////////////////////////////////////////
writeItems(tagAccSeqs = [], tagWriteVals = [], callback) {
var anyWriteErrors = false;
var writeOutputObject = {};
var errOutput = undefined;
// Provide console feedback
console.log(`Writing to Tags in PLC @ IP: ${this.host}`);
// Check if a connection to PLC currently exists
if (!(this.isConnected)) {
anyWriteErrors = true;
writeOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.NOCONNERROR,
`Cannot Write Tags, No Connection to PLC @ IP: ${this.host}`,
new Error().stack
);
callback(anyWriteErrors, writeOutputObject, errOutput);
return;
}
// Parse Input
try { this.ParseWriteItemsInput(tagAccSeqs, tagWriteVals) }
catch (error) {
anyWriteErrors = true;
writeOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.INPUTPARSEERROR,
error.message,
error.stack
);
this.clearWriteLists();
callback(anyWriteErrors, writeOutputObject, errOutput);
return;
}
// Initialize Write Tag Input
let writeTagsInput = {
[NodeS7CommPlus.Property.CONNSESSID]: this.connectionSessionID,
[NodeS7CommPlus.Property.S7COMMPLUSITEMS]: this.writeList
};
// Write to tags in PLC
NodeS7CommPlus.WRITETAGS(writeTagsInput, (error, output) => {
if (error) {
anyWriteErrors = true;
writeOutputObject = {};
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERWRITETAGSERROR,
error.message,
error.stack
);
this.clearWriteLists();
callback(anyWriteErrors, writeOutputObject, errOutput);
return;
} else {
if (output[NodeS7CommPlus.Property.WRITERES] !== 0) {
anyWriteErrors = true;
writeOutputObject = {}
errOutput = new NodeS7CommPlus.ErrorOutput(
NodeS7CommPlus.ErrorOutput.ErrorCode.DRIVERWRITETAGSERROR,
`Error: Failed to write to Tags in PLC @ IP ${this.host}, Driver Write Result Code: ${output[NodeS7CommPlus.Property.READRES]}`,
new Error().stack
);
callback(anyWriteErrors, writeOutputObject, errOutput);
return;
}
// overall successful write operation but check for any single failed writes
var writeValArrLen = output[NodeS7CommPlus.Property.WRITEVALS].length;
for (let i = 0; i < writeValArrLen; i++) {
if (output[NodeS7CommPlus.Property.WRITEERRORS][i] === "0") {
writeOutputObject[this.writeList[i].tagAccSeq] = NodeS7CommPlus.QualityValue.OK;
} else {
anyWriteErrors = true;
writeOutputObject[this.writeList[i].tagAccSeq] = NodeS7CommPlus.QualityValue.BAD;
}
}
this.clearWriteLists();
// return
callback(anyWriteErrors, writeOutputObject, errOutput);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
ParseWriteItemsInput(arg, value) {
if (Array.isArray(arg) && Array.isArray(value)) {
// Check if input arrays of equal length
let arrLen = arg.length;
if (value.length !== arrLen) {
throw new InvalidInputError(
`Invalid Input:
Arrays of Tag Access Sequences and Write Values must be of equal length`
);
}
// check if each element of input tag access sequence array is of correct type and if so
// retrieve its corresponding tag Name and tag Data Type and push them along with the write value
// onto the internal write list
for (let index = 0; index < arrLen; index++) {
var tagAccSeq = arg[index];
var tagWriteVal = value[index];
if (NodeS7CommPlus.GetType(tagAccSeq) !== NodeS7CommPlus.TypeConstant.STRING) {
throw new InvalidInputError(
`Invalid Input Type:
Element ${index} of Input Tag Access Sequence Array
must be of type ${NodeS7CommPlus.TypeConstant.STRING}`
);
}
var tagMetaData = this.tagMetaDataDict.get(tagAccSeq);
if (!tagMetaData) {
throw new MissingTagMetaDataError(
`Tag Meta Data for Access Sequence ${tagAccSeq} could not be found`
);
}
this.writeList.push(