forked from gleeds/cloudbbq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (152 loc) · 5.34 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
var noble = require('noble')
var constHelper = require('./constHelper')
var tempHelper = require('./tempHelper')
var mqtt = require('mqtt')
var config = require('config')
var program = require('commander')
var path = require('path')
var GoogleAssistant = require('google-assistant')
var mqttConfig = config.get('mqtt')
var notificationConfig = config.get('notifications')
var mqttConnected = false
var msgCount = 0
program
.version('1.1.0')
.option('-p1, --probe1 <n>','Probe 1 Target Temperature(F)',parseInt)
.option('-p2, --probe2 <n>','Probe 2 Target Temperature(F)',parseInt)
.option('-p3, --probe3 <n>','Probe 3 Target Temperature(F)',parseInt)
.option('-p4, --probe4 <n>','Probe 4 Target Temperature(F)',parseInt)
.option('-p6, --probe5 <n>','Probe 5 Target Temperature(F)',parseInt)
.option('-p7, --probe6 <n>','Probe 6 Target Temperature(F)',parseInt)
.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'){
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))
console.log('setting units')
commandCharacteristic.write(constHelper.setUnitsFKey(),false)
console.log('sending start temp events')
commandCharacteristic.write(constHelper.startTempUpdates(),false)
}
function handleTempEvent(data) {
if (data && data.length == 12){
var probeTemps = []
for (var i = 0; i< 6; i++) {
var rawTemp = data.readInt16LE(i*2)
if (rawTemp != -10) {
probeTemps.push(tempHelper.cToF(rawTemp/10))
}
else {
probeTemps.push(null)
}
}
console.log(`Probes 1: ${probeTemps[0]}F 2: ${probeTemps[1]}F 3: ${probeTemps[2]}F `+
`4: ${probeTemps[3]}F 5: ${probeTemps[4]}F 5: ${probeTemps[5]}F`)
if (mqttConnected){
msgCount++
for (var j = 0; j < 6; 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 < 6; k++){
if (notificationConfig.set[k] && notificationConfig.targets[k]) {
if(probeTemps[k]>notificationConfig.targets[k]) {
notificationConfig.set[k] = false
sendGoogleNotification(k+1,probeTemps[k])
}
}
}
}
}
else {
console.error('wierd empty or wrong size buffer')
}
}
function sendGoogleNotification(probe,temp) {
assistant.start({'textQuery':`broadcast Probe ${probe} has reached ${temp} degrees fahrenheit.`})
}