/
fulfillment.js
269 lines (239 loc) · 8.33 KB
/
fulfillment.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const functions = require('firebase-functions')
const { firestore } = require('../admin')
const { smarthome } = require('actions-on-google')
const Device = require('./device-model')
const jwt = require('jsonwebtoken')
const fulfillment = smarthome()
/**
* SYNC Intent Handler
*/
fulfillment.onSync(async (body, headers) => {
try {
const userId = validateCredentials(headers)
// Return all devices registered to the requested user
const result = await firestore
.collection('devices')
.where('owner', '==', userId)
.get()
const deviceList = result.docs.map((doc) => {
const device = Device.createDevice(doc.id, doc.data())
return device.metadata
})
console.log('SYNC Response', deviceList)
return {
requestId: body.requestId,
payload: {
agentUserId: userId,
devices: deviceList,
},
}
} catch (error) {
console.error('Unable to authenticate SYNC request', error)
return {
requestId: body.requestId,
payload: {
errorCode: 'authFailure',
debugString: error.toString(),
},
}
}
})
/**
* QUERY Intent Handler
*/
fulfillment.onQuery(async (body, headers) => {
try {
validateCredentials(headers)
const deviceSet = {}
// Return device state for the requested device ids
for (const target of body.inputs[0].payload.devices) {
const doc = await firestore.doc(`devices/${target.id}`).get()
const device = Device.createDevice(doc.id, doc.data())
deviceSet[device.id] = device.reportState
}
console.log('QUERY Response', deviceSet)
return {
requestId: body.requestId,
payload: {
devices: deviceSet,
},
}
} catch (error) {
console.error('Unable to authenticate QUERY request', error)
return {
requestId: body.requestId,
payload: {
errorCode: 'authFailure',
debugString: error.toString(),
},
}
}
})
/**
* EXECUTE Intent Handler
*/
fulfillment.onExecute(async (body, headers) => {
try {
validateCredentials(headers)
// Update the device configs for each requested id
let reqCommand = body.inputs[0].payload.commands[0]
const command = body.inputs[0].payload.commands[0]
console.log('EXECUTE Request', command)
// Apply the state update to each device
const update = Device.stateFromExecution(command.execution)
const batch = firestore.batch()
for (const target of command.devices) {
const configRef = firestore.doc(`device-configs/garagem2`)
const configRef5 = firestore.doc(`device-configs/cerca2`)
const targetDoc = await configRef.get()
const targetDoc5 = await configRef5.get()
const { cercastate } = targetDoc5.data()
const { garagestate } = targetDoc.data()
// const configRef2 = firestore.doc(`device-configs/${target.id}`)
const configRef7 = firestore.doc(`device-configs/${target.id}`)
// const targetDoc2 = await configRef2.get()
const targetDoc7 = await configRef7.get()
const { gpiogaragestate } = targetDoc7.data()
const { gpiocercastate } = targetDoc7.data()
let params = reqCommand.execution[0].params
let command = reqCommand.execution[0].command
console.log('COMMAND.EXECUTION', command)
console.log(
'reqCommand.execution - cerca2',
reqCommand.execution[0].params.on
)
console.log(
'reqCommand.execution - Garagem',
reqCommand.execution[0].params.openPercent
)
console.log('Params variavel=', params)
console.log('gpiocercastate - 927 = ', gpiocercastate)
console.log('gpiogaragestate - 927 = ', gpiogaragestate)
// Cerca abaixo
if (
cercastate === false &&
command === 'action.devices.commands.OnOff' &&
reqCommand.execution[0].params.on === false
) {
return {
requestId,
payload: {
status: 'ERROR',
errorCode: 'alreadyOff',
},
}
} else if (
cercastate === true &&
command === 'action.devices.commands.OnOff' &&
reqCommand.execution[0].params.on === true
) {
return {
requestId,
payload: {
status: 'ERROR',
errorCode: 'alreadyOn',
},
}
}
// Garagem abaixo
if (
garagestate === true &&
command === 'action.devices.commands.OpenClose' &&
reqCommand.execution[0].params.openPercent === 0
) {
return {
requestId,
payload: {
status: 'ERROR',
errorCode: 'alreadyClosed',
},
}
} else if (
garagestate === false &&
command === 'action.devices.commands.OpenClose' &&
reqCommand.execution[0].params.openPercent === 100
) {
return {
requestId,
payload: {
status: 'ERROR',
errorCode: 'alreadyOpen',
},
}
}
if(
cercastate === true &&
command === 'action.devices.commands.OpenClose' &&
reqCommand.execution[0].params.openPercent === 100)
{ await firestore.doc(`device-configs/cerca`).update({ 'gpiocercastate':true, 'value.on':true })}
await firestore.doc(`device-configs/${target.id}`).update({ 'gpiogaragestate':true })
await firestore.doc(`device-configs/${target.id}`).update({ 'gpiocercastate':true })
// /${target.id}
batch.update(configRef7, update)
}
await batch.commit()
return {
requestId: body.requestId,
payload: {
commands: [
{
ids: command.devices.map((device) => device.id),
status: 'PENDING',
},
],
},
}
} catch (error) {
console.error('Unable to authenticate EXECUTE request', error)
return {
requestId: body.requestId,
payload: {
errorCode: 'authFailure',
debugString: error.toString(),
},
}
}
})
/**
* DISCONNECT Intent Handler
*/
fulfillment.onDisconnect(async (body, headers) => {
try {
const userId = validateCredentials(headers)
// Clear the user's current refresh token
const userRef = firestore.doc(`users/${userId}`)
await userRef.delete()
console.log(`Account unlinked: ${userId}`)
// Return empty body
return {}
} catch (error) {
console.error('Unable to authenticate DISCONNECT request', error)
return {
requestId: body.requestId,
payload: {
errorCode: 'authFailure',
debugString: error.toString(),
},
}
}
})
/**
* Verify the request credentials provided by the caller.
* If successful, return UID encoded in the token.
*/
function validateCredentials(headers) {
if (
!headers.authorization ||
!headers.authorization.startsWith('Bearer ')
) {
throw new Error('Request missing valid authorization')
}
const jwt_secret = functions.config().smarthome.key
const token = headers.authorization.split('Bearer ')[1]
const decoded = jwt.verify(token, jwt_secret)
return decoded.sub
}
/**
* Cloud Function: Handler for Smart Home intents
*/
module.exports = functions.https.onRequest(fulfillment)