-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (57 loc) · 1.45 KB
/
index.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
const owfs = require("owfs");
exports.init = function(node, app_config, main) {
var timer = [];
var get_temperature_interval = function(node, sid) {
var file = "/" + sid + "/temperature";
var still_active = null;
var t = setInterval(function() {
if (still_active) {
console.log("owfs:", sid,
"interval still active since",
still_active);
return;
}
still_active = new Date();
connection.read(file, function(err, temp) {
still_active = null;
if (err) {
console.error("OWFS", err.stack || err);
}
//console.log(sid, "temperature", +temp);
if (typeof temp !== "undefined") {
node.publish(undefined, +temp);
} else {
node.publish(undefined, null);
}
});
}, (app_config.interval || 5) * 1000);
timer.push(t);
};
var connection = new owfs.Client(
app_config.host || 'localhost',
app_config.port || 4304
);
var map = node.map(app_config.map, null, true, undefined,
function(n, metadata, c) {
var sid = c.map;
get_temperature_interval(n, sid);
n.announce([{
"type": "temperature.data",
"unit": "C",
"unit_long": "Celsius"
}, metadata]);
});
connection.dirall("/", function(err, directory) {
if (err) {
console.error("owfs Error", err.stack || err);
return;
}
//console.log(directory);
directory.forEach(function(file) {
let sid = file.replace(/^\//, "");
var n = map.node(sid);
if (!n) return;
});
});
return [map, timer, connection];
};