-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
94 lines (74 loc) · 2.55 KB
/
tools.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
"use strict";
var Baby = require('babyparse')
var Promise = require('bluebird')
var fs = Promise.promisifyAll(require('fs'))
const BatteryLogFilename = "battery.csv"
module.exports = {
promiseWhile: function (predicate, action) {
function loop() {
if (predicate) return;
return Promise.resolve(action()).then(loop);
}
return Promise.resolve().then(loop);
},
sortCandlesDict: function (candles) {
var sortedCandles = []
for (var key in candles) {
let candle = candles[key];
sortedCandles.push(candle)
}
sortedCandles = sortedCandles.sort(function (a, b) {
var nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase()
if (nameA < nameB)
return -1
if (nameA > nameB)
return 1
return 0
})
return sortedCandles
},
hexToBytes: function (hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
},
bytesToHex: function (bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
},
arrayObjectIndexOf: function (myArray, searchTerm) {
for (var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i] === searchTerm) return i;
}
return -1;
},
exportBatteryStatesToCSV: function (candles) {
var parsed = Baby.parseFiles(BatteryLogFilename, { dynamicTyping: true, skipEmptyLines: true })
var rows = parsed.data;
if (rows.length == 0) {
rows.push(["Date"])
}
for (var candle of candles) {
var indexOfCandle = this.arrayObjectIndexOf(rows[0], candle.name)
if (indexOfCandle == -1) {
for (var row of rows) {
row.push("")
}
indexOfCandle = rows[0].length - 1
rows[0][indexOfCandle] = candle.name
}
var lastRow = []
for (var i = 0, len = rows[0].length; i < len; i++) {
lastRow.push("")
}
lastRow[0] = candle.updateDate
lastRow[indexOfCandle] = candle.battery
rows.push(lastRow)
}
var output = Baby.unparse(rows, { delimiter: ";" })
return fs.writeFileAsync(BatteryLogFilename, output)
}
};