-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixsensor.js
62 lines (57 loc) · 1.25 KB
/
fixsensor.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
const config = require("./config.json");
const nano = require("nano")(config.writer);
const weatherdb = nano.use("weatherdb");
const bulk = {
docs: [],
};
var sensors = {};
// var sensors={
// 'F8':{ name:'Buiten' },
// '6C':{ name:'Kas' }
// }
function bulkUpdate() {
weatherdb.bulk(bulk, function (err, body) {
if (!err) {
console.log("update complete");
} else {
console.log(err, body);
}
});
}
function processRecords(err, body) {
if (!err) {
console.log("rows to analyse:", body.total_rows);
body.rows.forEach(function (row) {
if (sensors[row.doc.sensorid]) {
row.doc.id = sensors[row.doc.sensorid].name;
bulk.docs.push(row.doc);
}
});
console.log("rows to fix:", bulk.docs.length);
if (process.argv[2] == "-force") {
bulkUpdate();
} else {
console.log("use: -force to update records");
// console.log(JSON.stringify(bulk.docs,null," "))
}
} else {
console.log("error getting view:", err.reason);
}
}
weatherdb.get("config/sensorIDs", function (err, body) {
if (!err) {
sensors = body.sensorIDs;
console.log("fetched sensorID's");
weatherdb.view(
"data",
"unknownSensors",
{
reduce: false,
include_docs: true,
},
processRecords,
);
} else {
console.log("err:", err.reason);
}
});