-
Notifications
You must be signed in to change notification settings - Fork 1
/
thingy.js
98 lines (91 loc) · 2.65 KB
/
thingy.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
95
96
97
98
var Thingy = require('./index');
var enable=false;
var battery_enable=false;
var current_temp = null;
var current_hum = null;
var current_press = null;
var current_battery= null;
var current_eco2=null;
var current_tvoc=null;
var deviceName=null;
var duration;
function onTemperatureData(temperature,id) {
current_temp = temperature;
enable=true;
deviceName=id
}
function onPressureData(pressure) {
current_press= pressure;
}
function onHumidityData(humidity) {
current_hum=humidity;
}
function onBatteryLevelChange(level) {
current_battery= level;
battery_enable=true;
}
function onGasData(gas) {
current_eco2= gas.eco2;
current_tvoc= gas.tvoc;
}
function onDiscover(thingy) {
console.log('Discovered: ' + thingy);
thingy.on('temperatureNotif', onTemperatureData);
thingy.on('pressureNotif', onPressureData);
thingy.on('humidityNotif', onHumidityData);
thingy.on('gasNotif', onGasData);
thingy.connectAndSetUp(function(error) {
thingy.temperature_interval_set(duration, function(error) {
if (error) {
console.log('Temperature sensor configure! ' + error);
}
});
thingy.pressure_interval_set(duration, function(error) {
if (error) {
console.log('Pressure sensor configure! ' + error);
}
});
thingy.humidity_interval_set(duration, function(error) {
if (error) {
console.log('Humidity sensor configure! ' + error);
}
});
thingy.gas_mode_set(1, function(error) {
if (error) {
console.log('Gas sensor configure! ' + error);
}
});
thingy.temperature_enable();
thingy.pressure_enable();
thingy.humidity_enable();
thingy.gas_enable();
thingy.on('batteryLevelChange', onBatteryLevelChange);
thingy.notifyBatteryLevel();
});
}
module.exports = function(RED) {
function ThingyNode(config) {
RED.nodes.createNode(this,config);
var node = this;
duration=config.time*1000;
Thingy.discover(onDiscover);
setInterval(function(){
if(enable&&battery_enable){
var outMsg= {
payload: {
temperature: current_temp,
humidity: current_hum,
pressure: current_press,
eco2: current_eco2,
tvoc:current_tvoc,
battery: current_battery,
id: deviceName
}
};
node.send(outMsg);
enable=false;
}
}, duration);
}
RED.nodes.registerType("Thingy",ThingyNode);
}