-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.js
98 lines (85 loc) · 2.69 KB
/
helpers.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
let ieee = require('./ieee');
var helpers = {
calcRate: function (rate, rateUnit) {
let _rate;
switch (rateUnit) {
case "s":
_rate = rate * 1000; //seconds
break;
case "m":
_rate = rate * 60000; //minutes
break;
case "h":
_rate = rate * 3600000; //hours
break;
case "ms":
default:
_rate = rate; //milliseconds
break;
}
return _rate;
},
numfloat: function (nums, isBE) {
// console.log('Got a numfloat', nums);
var x = 0;
var data = [];
for (var i = 0; i < nums.length; i = i + 2) {
var num = [];
//currently setup for Big Endian (swap i and i+1 for Little Endian)
var z = (isBE) ? i : i + 1;
num[0] = nums[z] >> 8;
num[1] = (nums[z] & 0x00FF);
z = (isBE) ? i + 1 : i;
num[2] = nums[z] >> 8;
num[3] = (nums[z] & 0x00FF);
data[x] = ieee.unpackF32(num.reverse());
// console.log(data[x]);
x++;
}
return data;
},
numdouble: function (nums, isBE) {
//console.log('Got a numdouble:', isBE);
var x = 0;
var data = [];
var offset = [];
var z;
if (isBE != true) {
for (z = 3; z >= 0; z = z - 1) {
offset.push(z);
}
}
else {
for (z = 0; z <= 3; z = z + 1) {
offset.push(z);
}
}
for (var i = 0; i < nums.length; i = i + 4) {
var num = [];
//currently setup for Big Endian (swap i and i+1 for Little Endian)
num[0] = nums[i + offset[0]] >> 8;
num[1] = (nums[i + offset[0]] & 0x00FF);
num[2] = nums[i + offset[1]] >> 8;
num[3] = (nums[i + offset[1]] & 0x00FF);
num[4] = nums[i + offset[2]] >> 8;
num[5] = (nums[i + offset[2]] & 0x00FF);
num[6] = nums[i + offset[3]] >> 8;
num[7] = (nums[i + offset[3]] & 0x00FF);
data[x] = ieee.unpackF64(num.reverse());
x++;
}
return data;
},
timestamp: function () {
return new Date()
.toISOString()
.replace(/T/, " ") // replace T with a space
.replace(/\..+/, "");
},
log: function (msg, args) {
var ts = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
if (args) console.log(ts + ": " + msg, args);
else console.log( ts + ": " + msg);
}.bind(this)
}
module.exports = helpers;