-
Notifications
You must be signed in to change notification settings - Fork 1
/
dht.js
59 lines (49 loc) · 1.39 KB
/
dht.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
var webduino = require('webduino-js'),
utils = require('./utils');
module.exports = function (RED) {
'use strict';
function Dht(n) {
var node = this,
boardNode, dht;
RED.nodes.createNode(this, n);
node.pin = n.pin;
node.autostartInterval = parseInt(n.autostartInterval);
boardNode = RED.nodes.getNode(n.board);
boardNode.mount(node, function (board) {
dht = new webduino.module.Dht(board, board.getDigitalPin(node.pin));
if (node.autostartInterval && !isNaN(node.autostartInterval)) {
board.autostartInterval = node.autostartInterval;
if (board.autostartInterval > 0) {
dht.read(function (evt) {
if (evt) {
node.send({
payload: evt
});
}
}, board.autostartInterval * 1000);
}
}
});
node.on('input', function (msg) {
var payload = utils.getPayload(msg.payload);
payload.params = payload.params.map(function (val) {
return parseInt(val)
});
if (dht && payload) {
dht[payload.method].apply(dht, [function (evt) {
if (evt) {
node.send({
payload: evt
});
}
}].concat(payload.params));
}
});
node.on('close', function () {
if (dht) {
dht.stopRead();
}
});
}
RED.nodes.registerType("dht", Dht);
};