forked from alikh31/node-red-contrib-eq3-bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheq3.js
102 lines (87 loc) · 2.66 KB
/
eq3.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
99
100
101
102
'use strict'
const eq3device = require('./lib/eq3device')
module.exports = function(RED) {
function eq3(config) {
var node = this;
RED.nodes.createNode(this, config);
this.serverConfig = RED.nodes.getNode(config.server);
node.device = global[config.eq3device]
if (!node.device) {
eq3device.discoverByAddress(config.eq3device ,function(device) {
node.device = device
global[config.eq3device] = device
})
}
node.intervalId = setInterval(() => {
if(node.device) {
node.status({fill:"green",shape:"ring",text:"connected"});
} else {
node.status({fill:"red",shape:"ring",text:"disconnected"});
}
}, 10000)
node.on('close', function(done) {
clearInterval(node.intervalId)
done()
})
node.on('input', function(msg) {
node.setCommand = function() {
setTimeout(() => {
node.device.getInfo()
.then(a => {
msg.payload = a
node.send(msg)
})
}, 2000)
if (typeof msg.payload !== 'object') return
switch (msg.payload.setState) {
case 'on':
node.device.turnOn()
break;
case 'off':
node.device.turnOff()
break;
case 'manual':
node.device.manualMode()
break;
case 'auto':
node.device.automaticMode()
break;
default:
break;
}
switch (msg.payload.boost) {
case '0':
node.device.setBoost(false)
break;
case '1':
node.device.setBoost(true)
break;
default:
break;
}
if (msg.payload.setTemperature)
node.device.setTemperature(msg.payload.setTemperature)
}
if(!node.device) {
RED.log.error('the specified device at ' + config.eq3device
+ ' has not been found yet')
RED.log.warn('list of all available addressess will be retrieved...')
eq3device.discoverAll((device) => {
if(!node.device || config.eq3device !== device.address)
RED.log.warn('found device at address ' + device.address)
if(!node.device && config.eq3device === device.address) {
RED.log.info('device has found and configured!')
global[config.eq3device] = device
node.device = global[config.eq3device]
}
})
}
else if(!node.device.connectedAndSetUp)
node.device.connectAndSetup()
.then(() => node.setCommand())
else
node.setCommand()
});
}
RED.nodes.registerType("eq3-bluetooth", eq3);
}