-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtame-w-comments.js
2363 lines (2165 loc) · 89.8 KB
/
tame-w-comments.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
/*!
* TAME [TwinCAT ADS Made Easy] V2.2.2
*
* Copyright (c) 2009-2012 Thomas Schmidt; t.schmidt.p1 at freenet.de
*
* Dual licensed under:
* MIT - http://www.opensource.org/licenses/mit-license
* GPLv3 - http://www.opensource.org/licenses/GPL-3.0
*
*/
/**
* This is the global TAME object. Used as a namespace to store values and functions.
*/
var TAME = {
//Names of days and months. This is for the formatted output of date values. You can
//simply add your own values if you need.
weekdShortNames: {
ge: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
en: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
},
weekdLongNames: {
ge: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
en: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
},
monthsShortNames: {
ge: ['Jan', 'Feb', 'Mrz', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
en: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dez']
},
monthsLongNames: {
ge: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
}
};
/**
* The constructor function for the Web Service Client.
*
* @param {Object} service Contains the paramters of the Web Service.
*/
TAME.WebServiceClient = function(service) {
//======================================================================================
// Properties
//======================================================================================
//Set language specific names of days and months, default is german.
var lang = (typeof service.language == 'string') ? service.language : 'ge';
this.dateNames = {
weekdShort: TAME.weekdShortNames[lang],
weekdLong: TAME.weekdLongNames[lang],
monthsShort: TAME.monthsShortNames[lang],
monthsLong: TAME.monthsLongNames[lang]
};
//Maximum string length.
this.maxStringLen = 255;
//Maximum count of dropped requests after a request
//was not acknowledged (in conjunction with a reqest ID).
this.maxDropReq = 10;
//Check limits of numeric variables before sending them to the PLC
this.useCheckBounds = true;
//======================================================================================
// Variables
//======================================================================================
var instance = this,
//Index-Group's
fields = {
M: '16416', //PLC memory range(%M field), READ_M - WRITE_M
MX: '16417', //PLC memory range(%MX field), READ_MX - WRITE_MX
I: '61472', //PLC process diagram of the physical inputs(%I field), READ_I - WRITE_I
IX: '61473', //PLC process diagram of the physical inputs(%IX field), READ_IX - WRITE_IX
Q: '61488', //PLC process diagram of the physical outputs(%Q field), READ_Q - WRITE_Q
QX: '61489' //PLC process diagram of the physical outputs(%QX field), READ_QX - WRITE_QX
},
//Lenght of PLC data types in byte.
plcTypeLen = {
BOOL: 1,
BYTE: 1,
USINT: 1,
SINT: 1,
WORD: 2,
UINT: 2,
INT: 2,
INT1DP: 2,
DWORD: 4,
UDINT: 4,
DINT: 4,
TIME: 4, //time base in PLC: milliseconds
TOD: 4, //time base in PLC: milliseconds
DATE: 4, //time base in PLC: seconds
DT: 4, //time base in PLC: seconds
REAL: 4,
LREAL: 8,
STRING: 80, //without termination
EndStruct: 0 //should be 0!
},
//Generate a Base64 alphabet for the encoder. Using an array or object to
//store the alphabet the en-/decoder runs faster than with the commonly
//used string. At least with the browsers of 2009. ;-)
b64Enc = function() {
var ret = {},
str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (var i = 0; i < str.length; i++) {
ret[i] = str.charAt(i);
}
return ret;
}(),
//Generate a Base64 alphabet for the decoder.
b64Dec = function() {
var ret = {},
str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
for (var i = 0; i < str.length; i++) {
ret[str.charAt(i)] = i;
}
return ret;
}(),
//4-byte data alignment, for a x86 set it to false, for a ARM to true
dataAlign4 = service.dataAlign4,
//Array for the request acknowledgement counter.
currReq = [0],
url,
netId,
port;
//URL of the TcAdsWebService.dll
if (typeof service.serviceUrl == 'string') {
url = service.serviceUrl;
} else {
try {
console.log('TAME library error: Service URL is not a string!');
} catch(e) {}
return;
}
//AMS NetID of the PLC
if (typeof service.amsNetId == 'string') {
netId = service.amsNetId;
} else {
try {
console.log('TAME library error: NetId is not a string!');
} catch(e) {}
return;
}
//AMS Port Number of the Runtime System
if (service.amsPort === undefined) {
port = '801';
} else if (typeof service.amsPort == 'string' && parseInt(service.amsPort, 10) >= 801 && parseInt(service.amsPort, 10) <= 804) {
port = service.amsPort;
} else {
try {
console.log('TAME library error: AMS Port Number (' + parseInt(service.amsPort, 10) + ') is no string or out of range!');
} catch(e) {}
return;
}
//======================================================================================
// Encoder Functions
//======================================================================================
/**
* Conversion of ASCII(0-9, a-f, A-F) charcodes to numbers 0-15
*
* @param {Number} charcode
*/
function charcodeToDual(charcode) {
if ((charcode >= 0x61) && (charcode <= 0x66)) {
return (charcode - 0x57); //a-f
}
if ((charcode >= 0x41) && (charcode <= 0x46)) {
return (charcode - 0x37); //A-F
}
if ((charcode >= 0x30) && (charcode <= 0x39)) {
return (charcode - 0x30); //0-9
}
return 0;
}
/**
* Convert a number into an array of bytes.
*
* @param {Number} value
* @param {Number} varlen
*/
function numToByteArr(value, varlen) {
var bytes = [],
hex = value.toString(16);
while (hex.length < varlen * 2) {
hex = '0' + hex;
}
for (var i = 0; i < varlen; i++) {
bytes[(varlen - 1) - i] =
((charcodeToDual(hex.charCodeAt(i * 2)) * 16) +
charcodeToDual(hex.charCodeAt((i * 2) + 1)));
}
return bytes;
}
/**
* Convert a JavaScript floating point number to a PLC REAL value.
*
* @param {Number} num
*/
function floatToReal(num) {
var mant = 0,
real = 0,
bas, abs, tmp, exp;
abs = Math.abs(num);
if (num !== 0) {
//Find exponent and base.
for (var i = 128; i > -127; i--) {
tmp = abs / Math.pow(2, i);
if (tmp >= 2) {
break;
}
exp = i;
bas = tmp;
}
exp += 127;
bas = bas.toString(2);
//Create the mantissa.
for (i = 2; i < 25; i++) {
mant <<= 1;
if (bas.charAt(i) == '1') {
mant += 1;
}
}
if (bas.charAt(25) == '1') {
mant += 1;
}
//Create the REAL value.
real = exp; //exponent
real <<= 23;
real += mant; //mantissa
if (num < 0) {
//Create negative sign.
real += 2147483648;
}
}
return real;
}
/**
* Convert a JavaScript floating point number to a PLC LREAL value.
* Cause it's a 64 bit value, we have to split it in two 32 bit integer.
*
* @param {Number} num
*/
function floatToLreal(num) {
var mant = 0,
mant2 = 0,
lreal = {
part1: 0,
part2: 0
},
abs, tmp, exp, firstbit, bas;
abs = Math.abs(num);
if (num !== 0) {
//Find exponent and base.
for (var i = 1024; i >= -1023; i--) {
tmp = abs / Math.pow(2, i);
if (tmp >= 2) {
break;
}
exp = i;
bas = tmp;
}
exp += 1023;
bas = bas.toString(2);
//Create the mantissa.
for (i = 2; i < 22; i++) {
mant <<= 1;
if (bas.charAt(i) == '1') {
mant += 1;
}
}
if (bas.charAt(i) == '1') {
firstbit = true;
}
i++;
for (i; i < 54; i++) {
mant2 <<= 1;
if (bas.charAt(i) == '1') {
mant2 += 1;
}
}
//Create the LREAL value.
lreal.part1 = exp; //exponent
lreal.part1 <<= 20;
lreal.part1 += mant; //mantissa
if (num < 0) {
//Create negative sign.
lreal.part1 += 2147483648;
}
lreal.part2 = mant2;
if (firstbit === true) {
lreal.part2 += 2147483648;
}
}
return lreal;
}
/**
* Convert a value to value in milliseconds, depending
* on the format string.
*
* @param {Number} time
* @param {String} format
*/
function toMillisec(time, format) {
var tmp;
switch (format) {
case '#d':
case '#dd':
tmp = time * 86400000;//days
break;
case '#h':
case '#hh':
tmp = time * 3600000; //hours
break;
case '#m':
case '#mm':
tmp = time * 60000; //minutes
break;
case '#s':
case '#ss':
tmp = time * 1000; //seconds
break;
case '#ms':
case '#msmsms': //milliseconds
tmp = time;
break;
default:
tmp = time;
break;
}
return tmp;
}
/**
* Base64 encoder
*
* @param {Array} data
*/
function encodeBase64(data) {
var $ = b64Enc,
i = 0,
out = '',
c1, c2, c3;
while (i < data.length) {
c1 = data[i++];
c2 = data[i++];
c3 = data[i++];
out = out +
$[c1 >> 2] +
$[((c1 & 3) << 4) | (c2 >> 4)] +
(isNaN(c2) ? '=' : $[(((c2 & 15) << 2) | (c3 >> 6))]) +
((isNaN(c2) || isNaN(c3)) ? '=' : $[c3 & 63]);
}
return out;
}
//======================================================================================
// Decoder Functions
//======================================================================================
/**
* Convert data string to USINT/BYTE.
*
* @param {String} string
*/
function parsePlcUsint(string) {
var hexs = numToHexString(string.charCodeAt(0));
return parseInt(hexs, 16);
}
/**
* Convert data string to SINT.
*
* @param {String} string
*/
function parsePlcSint(string) {
var dec = parsePlcUsint(string);
if (dec > 127) {
dec = dec - 256;
}
return dec;
}
/**
* Convert data string to UINT/WORD.
*
* @param {String} string
*/
function parsePlcUint(string) {
var hexs = numToHexString(string.charCodeAt(1));
hexs += numToHexString(string.charCodeAt(0));
return parseInt(hexs, 16);
}
/**
* Convert data string to INT.
*
* @param {String} string
*/
function parsePlcInt(string) {
var dec = parsePlcUint(string);
if (dec > 32767) {
dec = dec - 65536;
}
return dec;
}
/**
* Convert data string to UDINT/DWORD.
*
* @param {String} string
*/
function parsePlcUdint(string) {
var hexs = numToHexString(string.charCodeAt(3));
hexs += numToHexString(string.charCodeAt(2));
hexs += numToHexString(string.charCodeAt(1));
hexs += numToHexString(string.charCodeAt(0));
return parseInt(hexs, 16);
}
/**
* Convert data string to DINT.
*
* @param {String} string
*/
function parsePlcDint(string) {
var dec = parsePlcUdint(string);
if (dec > 2147483647) {
dec = dec - 4294967296;
}
return dec;
}
/**
* Convert data string to a formatted time string
*
* @param {String} string
* @param {String} format
*/
function parsePlcTime(string, format) {
var time = parsePlcUdint(string);
if (format === undefined) {
return time; //Unformatted: value in milliseconds.
} else {
return (timeToString(time, format));
}
}
/**
* Convert data string to a formatted time of day string.
*
* @param {String} string
* @param {String} format
*/
function parsePlcTod(string, format) {
//Create a date object (time base in the PLC are milliseconds)
var time = new Date(parsePlcUdint(string));
//Time zone correction.
time = new Date(time.getTime() + time.getTimezoneOffset() * 60000);
if (format === undefined) {
return time;
} else {
return (dateToString(time, format));
}
}
/**
* Convert data string to a formatted date/time of day string.
*
* @param {String} string
* @param {String} format
*/
function parsePlcDate(string, format) {
//Converte to milliseconds an create a date object
//(time base of DATE/DT variables in the PLC are seconds since 1.1.1970)
var date = new Date(parsePlcUdint(string) * 1000);
//Time zone correction.
date = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
if (format === undefined) {
return date;
} else {
return (dateToString(date, format));
}
}
/**
* Convert data string of a REAL variable
* to a JavaScript floating point number.
*
* @param {String} string
*/
function parsePlcReal(string) {
var mant = 1,
dual = 0.5,
num = parsePlcUdint(string),
sign, exp;
//Return if value is zero.
if (num === 0) {
return 0;
}
//Check the sign bit.
sign = ((num >>> 31) == 1) ? '-' : '+';
num <<= 1; //Delete the sign bit.
//Calculate the exponent.
exp = (num >>> 24) - 127;
//Calculate the 23 bit mantissa: Shift bits to left and evaluate them.
num <<= 8;
for (var i = 1; i <= 23; i++) {
mant += num < 0 ? dual : 0; //Add if left (sign bit) bit is true.
num <<= 1;
dual /= 2;
}
return parseFloat(sign + (mant * Math.pow(2, exp)));
}
/**
* Convert data string of a LREAL variable
* to a JavaScript floating point number.
*
* @param {String} string
*/
function parsePlcLreal(string) {
var num = parsePlcUdint(string.substring(4,8)),
num2 = parsePlcUdint(string.substring(0,4)),
i = 12,
mant = 1,
dual = 0.5,
sign, exp;
//Return if value is zero.
if (num === 0 && num2 === 0) {
return 0;
}
//Check the sign bit.
sign = ((num >>> 31) == 1) ? '-' : '+';
num <<= 1; //Delete the sign bit.
//Calculate the exponent.
exp = (num >>> 21) - 1023;
//Calculate the mantissa. Shift bits to left and evaluate them.
//Part 1.
num <<= 11;
while (i < 32) {
mant += num < 0 ? dual : 0; //Add if left (sign bit) bit is true.
num <<= 1;
dual /= 2;
i++;
}
//Part 2.
if ((num2 >>> 31) == 1) {
mant += dual;
num2 <<= 1;
dual /= 2;
}
while (i < 64) {
mant += num2 < 0 ? dual : 0; //Add if left (sign bit) bit is true.
num2 <<= 1;
dual /= 2;
i++;
}
return parseFloat(sign + (mant * Math.pow(2, exp)));
}
/**
* Convert data string to string by simply cutting of superfluous characters.
*
* @param {String} string
*/
function parsePlcString(string) {
var len = string.length;
for (var i = 0; i < len; i++) {
if (string.charCodeAt(i) === 0) {
break;
}
}
return string.substr(0, i);
}
/**
* Convert a number to a hex string.
*
* @param {Number} value
*/
function numToHexString(value) {
var ret = value.toString(16);
if ((ret.length % 2) !== 0) {
ret = '0' + ret;
}
return ret;
}
/**
* Set a fixed length of an integer by adding leading
* zeros(i.e. change 2 to 02).
*
* @param {Number} numb
* @param {Number} places
*/
function fixNumbLength(numb, places) {
places = (isNaN(places)) ? 0 : places;
var str = numb.toString(10);
while (str.length < places) {
str = '0' + str;
}
return str;
}
/**
* Convert a date object to a formatted string.
*
* @param {Date} time
* @param {String} format
*/
function dateToString(time, format) {
var arr = format.split('#'),
arrlen = arr.length,
tstring = '',
tmp;
for (var i = 1; i < arrlen; i++) {
switch (arr[i]) {
//Date formatting.
case 'D':
tmp = time.getDate();
break;
case 'DD':
tmp = time.getDate();
tmp = fixNumbLength(tmp, 2);
break;
case 'WD':
tmp = time.getDay();
break;
case 'WKD':
tmp = instance.dateNames.weekdShort[time.getDay()];
break;
case 'WEEKDAY':
tmp = instance.dateNames.weekdLong[time.getDay()];
break;
case 'M':
tmp = time.getMonth() + 1;
break;
case 'MM':
tmp = time.getMonth() + 1;
tmp = fixNumbLength(tmp, 2);
break;
case 'MON':
tmp = instance.dateNames.monthsShort[time.getMonth()];
break;
case 'MONTH':
tmp = instance.dateNames.monthsLong[time.getMonth()];
break;
case 'YY':
tmp = time.getYear();
while (tmp > 100) {
tmp -= 100;
}
break;
case 'YYYY':
tmp = time.getFullYear();
break;
//Time formatting.
case 'h':
tmp = time.getHours();
break;
case 'hh':
tmp = time.getHours();
tmp = fixNumbLength(tmp, 2);
break;
case 'm':
tmp = time.getMinutes();
break;
case 'mm':
tmp = time.getMinutes();
tmp = fixNumbLength(tmp, 2);
break;
case 's':
tmp = time.getSeconds();
break;
case 'ss':
tmp = time.getSeconds();
tmp = fixNumbLength(tmp, 2);
break;
case 'ms':
tmp = time.getMilliseconds();
break;
case 'msmsms':
tmp = time.getMilliseconds();
tmp = fixNumbLength(tmp, 3);
break;
default:
tmp = arr[i];
break;
}
tstring = tstring + tmp;
}
return tstring;
}
/**
* Convert a number with a value in milliseconds to a formatted string.
*
* @param {Number} time
* @param {String} format
*/
function timeToString(time, format) {
var arr = format.split('#'),
arrlen = arr.length,
tstring = '',
tmp;
for (var i = 1; i < arrlen; i++) {
switch (arr[i]) {
case 'd':
if (arrlen <= 2) {
tmp = time / 86400000;
} else {
tmp = Math.floor(time / 86400000);
time = time % 86400000;
}
break;
case 'dd':
if (arrlen <= 2) {
tmp = time / 86400000;
} else {
tmp = Math.floor(time / 86400000);
time = time % 86400000;
}
tmp = fixNumbLength(tmp, 2);
break;
case 'h':
if (arrlen <= 2) {
tmp = time / 3600000;
} else {
tmp = Math.floor(time / 3600000);
time = time % 3600000;
}
break;
case 'hh':
if (arrlen <= 2) {
tmp = time / 3600000;
} else {
tmp = Math.floor(time / 3600000);
time = time % 3600000;
}
tmp = fixNumbLength(tmp, 2);
break;
case 'm':
if (arrlen <= 2) {
tmp = time / 60000;
} else {
tmp = Math.floor(time / 60000);
time = time % 60000;
}
break;
case 'mm':
if (arrlen <= 2) {
tmp = time / 60000;
} else {
tmp = Math.floor(time / 60000);
time = time % 60000;
}
tmp = fixNumbLength(tmp, 2);
break;
case 's':
if (arrlen <= 2) {
tmp = time / 1000;
} else {
tmp = Math.floor(time / 1000);
time = time % 1000;
}
break;
case 'ss':
if (arrlen <= 2) {
tmp = time / 1000;
} else {
tmp = Math.floor(time / 1000);
time = time % 1000;
}
tmp = fixNumbLength(tmp, 2);
break;
case 'ms':
tmp = time;
break;
case 'msmsms':
tmp = time;
tmp = fixNumbLength(tmp, 3);
break;
default:
tmp = arr[i];
break;
}
tstring = tstring + tmp;
}
return tstring;
}
/**
* Base64 decoder
*
* @param {String} data
*/
function decodeBase64(data) {
var $ = b64Dec,
i = 0,
output = '',
c1, c2, c3,
e1, e2, e3, e4;
//Cut all characters but A-Z, a-z, 0-9, +, /, or =
data = data.replace(/[^A-Za-z0-9\+\/\=]/g, '');
do {
e1 = $[data.charAt(i++)];
e2 = $[data.charAt(i++)];
e3 = $[data.charAt(i++)];
e4 = $[data.charAt(i++)];
c1 = (e1 << 2) | (e2 >> 4);
c2 = ((e2 & 15) << 4) | (e3 >> 2);
c3 = ((e3 & 3) << 6) | e4;
output += String.fromCharCode(c1);
if (e3 != 64) {
output += String.fromCharCode(c2);
}
if (e4 != 64) {
output += String.fromCharCode(c3);
}
}
while (i < data.length);
return output;
}
//======================================================================================
// Helper Functions
//======================================================================================
/**
* Decode variable names passed as strings and return the object,
* store data values if they are passed too.
*
* @param {String} name The name of a JavaScript variable or a property.
* @param {Object} data Data values to store in the variable/property.
* @param {Object} obj The object containing the property to store the data in.
* Used with createArrayDescriptor and createStructDescriptor
* for better performance.
*/
function parseVarName(name, data, obj, prefix, suffix){
var arr = [],
last = 0,
a = [],
i = 0;
if (typeof name == 'number') {
arr[0] = name.toString(10);
} else if (typeof name == 'string') {
arr = name.split('.');
} else {
try {
console.log('TAME library error: Can\'t parse name of object/variable. Name is not a string or number!');
console.log(name);
} catch(e){}
return;
}
if (obj === undefined){
obj = window;
}
last = arr.length - 1;
//Walk through the tiers
while (i < last) {
//Check if the passed name points to an array.
if (arr[i].charAt(arr[i].length - 1) == ']') {
a = arr[i].substring(0,arr[i].length - 1).split('[');
obj = obj[a[0]][a[1]];
} else {
obj = obj[arr[i]];
}
i++;
}
//Last element
if (arr[i].charAt(arr[i].length - 1) == ']') {
//If last item of the name is an array
a = arr[i].substring(0, arr[i].length - 1).split('[');
obj = obj[a[0]];
//Store data if passed.
if (data !== undefined) {
if (typeof prefix == 'string') {
data = prefix + data;
}
if (typeof suffix == 'string') {
data = data + suffix;
}
obj[a[1]] = data;
}
return obj[a[1]];
} else {
//Store data if passed.
if (data !== undefined) {
if (typeof prefix == 'string') {
data = prefix + data;
}
if (typeof suffix == 'string') {
data = data + suffix;
}
obj[arr[i]] = data;
}
return obj[arr[i]];
}
}
/**
* Check if a passed string length is valid.
*
* @param {Number} len
*/
function isValidStringLen(len) {
if (len === undefined) {
return false;
} else if (! isNaN(len) && len > 0 && len <= instance.maxStringLen) {
return true;
} else {
try {
console.log('TAME library error: User defined string length not valid! length: ' + len);
console.log('Max. string length: ' + instance.maxStringLen);
} catch(e){}
return false;
}
}
/**
* Process the given address of the descriptor.
*
* @param {Object} descr A request descriptor.
*/
function processReqAddr(descr) {
var addr = '', mxaddr = [];
if (typeof descr.fld != 'string') {
//New addressing style with V2.0
if (typeof descr.addr == 'string' && descr.addr.charAt(0) == '%') {
if (descr.addr.charAt(2) == 'X') {
descr.fld = descr.addr.substr(1, 2);
addr = descr.addr.substr(3);
mxaddr = addr.split('.');
descr.addr = mxaddr[0] * 8 + mxaddr[1] * 1;
} else {
descr.fld = descr.addr.substr(1, 1);
descr.addr = parseInt(descr.addr.substr(3), 10);
}
} else {
try {