-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
197 lines (168 loc) · 5.97 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var noble = require('@abandonware/noble')
var constHelper = require('./constHelper')
var tempHelper = require('./tempHelper')
var mqtt = require('mqtt')
var config = require('config')
var { Command } = require('commander');
var path = require('path')
var GoogleAssistant = require('google-assistant')
var mqttConfig = config.get('mqtt')
var notificationConfig = config.get('notifications')
var deviceConfig = config.get('device')
var localizationConfig = config.get('localization')
var mqttConnected = false
var msgCount = 0
var program = new Command();
program.version('1.1.0')
for (var i = 1; i < ++deviceConfig.probes; i++) {
program.option(`-p${i}, --probe${i} <n>`, `Probe ${i} Target Temperature(${localizationConfig.units})`, parseInt)
}
program.parse(process.argv)
notificationConfig.targets = [
program.probe1,
program.probe2,
program.probe3,
program.probe4,
program.probe5,
program.probe6]
notificationConfig.set = [true,true,true,true,true,true]
const googleConfig = {
auth: {
keyFilePath: path.resolve(__dirname,notificationConfig.googleAssistant.oAuthSecretsFile),
// where you want the tokens to be saved
// will create the directory if not already there
savedTokensPath: path.resolve(__dirname, 'config/tokens.json'),
}
}
const assistant = notificationConfig.googleAssistant.enabled ? new GoogleAssistant(googleConfig.auth) : null
var mqttConnString = `${mqttConfig.protocol}://${mqttConfig.username}:${mqttConfig.key}@${mqttConfig.url}`
var client = mqtt.connect(mqttConnString)
client.on('connect',()=>{
mqttConnected = true
})
noble.startScanning()
var pairCharacteristic, tempCharacteristic, commandCharacteristic
noble.on('discover',(peripheral)=>{
// Check out this sample code: https://github.com/noble/noble/issues/179
if (peripheral.advertisement.localName === 'iBBQ' ||
(peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.includes('fff0'))){
console.log('iBBQ Discovered')
noble.stopScanning()
peripheral.on('disconnect', () => {
console.log('Lost connection to device.')
noble.startScanning()
})
peripheral.connect((error)=>{
if (error){
console.error(error)
}
else {
connectToIBBQ(peripheral)
}
})
}
})
function connectToIBBQ(peripheral) {
peripheral.discoverAllServicesAndCharacteristics((error,services,characteristics)=>{
if(error){
console.error(error)
}
else{
for (let characteristic of characteristics){
switch(characteristic.uuid){
case 'fff2':
pairCharacteristic = characteristic
break
case 'fff4':
tempCharacteristic = characteristic
break
case 'fff5':
commandCharacteristic = characteristic
break
}
}
pairToDevice()
}
})
}
function pairToDevice() {
pairCharacteristic.write(constHelper.autoPairKey(),true, (error)=>{
if (error){
console.error(error)
}
else {
console.log('paired')
subscribeToEvents()
}
})
}
function subscribeToEvents() {
tempCharacteristic.subscribe((error)=>{
if (error) {
console.error(error)
}
})
tempCharacteristic.on('data',(data) => handleTempEvent(data))
if (localizationConfig.units === 'F') {
console.log('setting units to F')
commandCharacteristic.write(constHelper.setUnitsFKey(),false)
}
else {
console.log('setting units to C')
commandCharacteristic.write(constHelper.setUnitsCKey(),false)
}
console.log('sending start temp events')
commandCharacteristic.write(constHelper.startTempUpdates(),false)
}
function handleTempEvent(data) {
if (data && data.length == (deviceConfig.probes*2)){
var probeTemps = []
var logMsg = 'Probes '
for (var i = 0; i< deviceConfig.probes; i++) {
var rawTemp = data.readInt16LE(i*2)
if (rawTemp != -10) {
if (localizationConfig.units === 'F') {
probeTemps.push(tempHelper.cToF(rawTemp/10))
}
else {
probeTemps.push(rawTemp/10)
}
}
else {
probeTemps.push(null)
}
logMsg+=`${i+1}: ${probeTemps[i]||'--'}${localizationConfig.units} `
}
console.log(logMsg)
if (mqttConnected){
msgCount++
for (var j = 0; j < deviceConfig.probes; j++){
if(msgCount % mqttConfig.probeMessagePerPublish == 0 && probeTemps[j] != null) {
client.publish(mqttConfig.topics[j],JSON.stringify({
value:probeTemps[j]
}))
}
}
}
if (notificationConfig.googleAssistant.enabled){
for (var k = 0; k < deviceConfig.probes; k++){
if (notificationConfig.set[k] && notificationConfig.targets[k]) {
if(probeTemps[k]>notificationConfig.targets[k]) {
notificationConfig.set[k] = false
sendGoogleNotification(k+1,probeTemps[k])
}
}
}
}
}
else if (!data){
console.error('Probe data empty')
}
else {
console.error(`Probe data length ${data.length} but expected ${deviceConfig.probes*2} for a ${deviceConfig.probes} probe device. Check your device config json.`)
}
}
function sendGoogleNotification(probe,temp) {
assistant.start({'textQuery':`broadcast Probe ${probe} has reached ${temp} degrees ${localizationConfig.units === 'F' ? 'fahrenheit':'celsius'}.`})
}