-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
79 lines (66 loc) · 2.16 KB
/
utils.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
function rpmFromPercent(maxSpeed, percent) {
return Math.ceil((maxSpeed * percent) / 100);
}
function rpmToPercent(maxSpeed, speed) {
return Math.ceil((speed / maxSpeed) * 100);
}
function dec2hex(num, padding) {
if (!padding)
padding = 2;
return "0x" + num.toString(16).padStart(padding, '0')
}
function serialWrite(data, device, interval) {
if (device && device.serial) {
device.serial.write(data, function(err) {
console.log("[HaywardVSP] sent: ", data);
});
} else {
console.log("[HaywardVSP-test] sent: ", data);
}
if (interval && device) {
device.timer = setTimeout(function() {
serialWrite(data, device, interval);
}, interval);
}
}
function startWatchdog (device, cb) {
const WATCHDOG_MAX_WAIT = 8;
const WATCHDOG_CHECK_INTERVAL = 4;
if (!device)
return;
var delta = now() - device.lastReply;
if (delta > WATCHDOG_MAX_WAIT) {
cb(delta);
}
device.watchdogTimer = setTimeout(function() {
startWatchdog(device, cb);
}, WATCHDOG_CHECK_INTERVAL * 1000);
}
function now() {
return Math.floor(new Date().getTime() / 1000);
}
var log = console.log;
console.log = function () {
var first_parameter = arguments[0];
var other_parameters = Array.prototype.slice.call(arguments, 1);
function formatConsoleDate (date) {
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var milliseconds = date.getMilliseconds();
return ((hour < 10) ? '0' + hour: hour) +
':' +
((minutes < 10) ? '0' + minutes: minutes) +
':' +
((seconds < 10) ? '0' + seconds: seconds) +
'.' +
('00' + milliseconds).slice(-3) + ' ';
}
log.apply(console, [formatConsoleDate(new Date()) + first_parameter].concat(other_parameters));
};
module.exports.rpmFromPercent = rpmFromPercent;
module.exports.rpmToPercent = rpmToPercent;
module.exports.dec2hex = dec2hex;
module.exports.serialWrite = serialWrite;
module.exports.startWatchdog = startWatchdog;
module.exports.now = now;