diff --git a/lib/codecs.js b/lib/codecs.js index 3cec5c3..6e48cc3 100644 --- a/lib/codecs.js +++ b/lib/codecs.js @@ -1,11 +1,11 @@ -let flag_rawmode = false; +const flag_rawmode = false; const enums = require('./enums'); function arr2Hex(arr) { // Convert byte array to hex string - var hs = ''; - for (var v in arr) { hs += toHex(arr[v],2) } + let hs = ''; + for (const v in arr) { hs += toHex(arr[v],2); } return hs; } @@ -16,8 +16,8 @@ function toHex(d, len) { function toByteArray(hs) { // Convert hex string, e.g. '21A8' to byte array: [33,168] - var ba = []; - for (var i=0; i= Math.pow(2,15)) { v -= Math.pow(2,16); } return v; } @@ -46,7 +46,7 @@ function uint32toVal(j, ofs=0) { } function sint32toVal(j, ofs=0) { - var v = Math.pow(2,24)*j[ofs+3]+Math.pow(2,16)*j[ofs+2]+Math.pow(2,8)*j[ofs+1]+j[ofs]; + let v = Math.pow(2,24)*j[ofs+3]+Math.pow(2,16)*j[ofs+2]+Math.pow(2,8)*j[ofs+1]+j[ofs]; if (v >= Math.pow(2,31)) { v -= Math.pow(2,32); } return v; } @@ -56,20 +56,17 @@ function int2val(j, ofs = 0, signed = false) { switch (j.length) { case 1 : return (signed ? sint08toVal(j,ofs) : uint08toVal(j,ofs) ); - break; case 2 : return (signed ? sint16toVal(j,ofs) : uint16toVal(j,ofs) ); - break; case 4 : return (signed ? sint32toVal(j,ofs) : uint32toVal(j,ofs) ); - break; default: return null; } } function RawEncode(string_ascii, len) { - let string_bin = toByteArray(string_ascii); + const string_bin = toByteArray(string_ascii); if (string_bin.length !== len) { throw new Error(`String must be ${len} long`); } @@ -106,21 +103,21 @@ class O3EInt { this.signed = signed; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } else { if (this.offset != 0) { - throw new Error("O3EInt.encode(): offset!=0 not implemented yet"); + throw new Error('O3EInt.encode(): offset!=0 not implemented yet'); } let val = Math.round(eval(string_ascii) * this.scale); if ( (this.signed) && (val < 0) ) { val += Math.pow(2,8*this.byte_width); } - let string_bin = toByteArray(toHex(val, this.byte_width*2)); + const string_bin = toByteArray(toHex(val, this.byte_width*2)); return string_bin.reverse(); } } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } return int2val(string_bin.slice(this.offset,this.offset+this.byte_width), 0, this.signed) / this.scale; @@ -159,21 +156,21 @@ class O3EByteVal { this.offset = offset; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } else { if (this.offset != 0) { - throw new Error("O3EByteVal.encode(): offset!=0 not implemented yet"); + throw new Error('O3EByteVal.encode(): offset!=0 not implemented yet'); } - let val = Math.round(eval(string_ascii)); - let string_bin = toHex(val, this.string_len*2); - let bytes = toByteArray(string_bin); + const val = Math.round(eval(string_ascii)); + const string_bin = toHex(val, this.string_len*2); + const bytes = toByteArray(string_bin); return bytes.reverse(); } } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } let val = 0; @@ -195,18 +192,18 @@ class O3EBool { this.offset = offset; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("O3EBool.encode(): not implemented yet"); + throw new Error('O3EBool.encode(): not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - let val = string_bin[this.offset]; - return (val == 0 ? "off" : "on" ); + const val = string_bin[this.offset]; + return (val == 0 ? 'off' : 'on' ); } __len__() { @@ -221,17 +218,18 @@ class O3EUtf8 { this.offset = offset; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("not implemented yet"); + throw new Error('not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - var mystr = ''; - for (var i=this.offset; i= 2) ? string_bin[1] : 0), // minute - ((this.string_len >= 3) ? string_bin[2] : 0) // second - ); + const now = new Date(); + const dt = new Date(now.getFullYear(), now.getMonth(), now.getDate(), + string_bin[0], // hour + ((this.string_len >= 2) ? string_bin[1] : 0), // minute + ((this.string_len >= 3) ? string_bin[2] : 0) // second + ); return dt.toLocaleTimeString(); - } + } __len__() { return this.string_len; } @@ -380,16 +378,16 @@ class O3EDateTime { this.timeformat = timeformat; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("not implemented yet"); + throw new Error('not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - var dt = new Date(); + let dt = new Date(); if (this.timeformat == 'VM') { dt = new Date( string_bin[0]*100+string_bin[1], // year @@ -398,14 +396,14 @@ class O3EDateTime { string_bin[5], // hour string_bin[6], // minute string_bin[7] // second - ); - } + ); + } if (this.timeformat == 'ts') { dt = new Date(uint32toVal(string_bin.slice(0,4),0)*1000); } - return { "DateTime": dt.toLocaleString(), - "Timestamp": Math.round(dt.getTime()) - } + return { 'DateTime': dt.toLocaleString(), + 'Timestamp': Math.round(dt.getTime()) + }; } __len__() { return this.string_len; @@ -418,16 +416,16 @@ class O3EUtc { this.id = idStr; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("not implemented yet"); + throw new Error('not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - var dt = new Date(uint32toVal(string_bin.slice(0,4),0)*1000); + const dt = new Date(uint32toVal(string_bin.slice(0,4),0)*1000); return dt.toUTCString(); } __len__() { @@ -442,24 +440,24 @@ class O3EEnum { this.listStr = listStr; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("not implemented yet"); + throw new Error('not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - var val = int2val(string_bin); - var txt = ''; + const val = int2val(string_bin); + let txt = ''; if ( (this.listStr in enums.enums) && (String(val) in enums.enums[this.listStr]) ) { txt = enums.enums[this.listStr][String(val)]; } else { - txt = "Enum not found in " + this.listStr; + txt = 'Enum not found in ' + this.listStr; } - return {"ID": val, - "Text": txt } + return {'ID': val, + 'Text': txt }; } __len__() { return this.string_len; @@ -473,29 +471,29 @@ class O3EList { this.subTypes = subTypes; } encode(string_ascii) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawEncode(string_ascii, this.string_len); } - throw new Error("not implemented yet"); + throw new Error('not implemented yet'); } decode(string_bin) { - if (flag_rawmode == true) { + if (flag_rawmode) { return RawDecode(string_bin); } - var result = {}; - var index = 0; - var count = 0; - for (var subType in this.subTypes) { - var subT = this.subTypes[subType]; + const result = {}; + let index = 0; + let count = 0; + for (const subType in this.subTypes) { + const subT = this.subTypes[subType]; if (subT.id.toLowerCase() == 'count') { count = subT.decode(string_bin.slice(index,index+subT.string_len)); result[subT.id]=count; index += subT.string_len; } else { - if (("subTypes" in subT)) { + if (('subTypes' in subT)) { // O3EComplexType result[subT.id] = []; - for (var i=0; i