google-cloud-platform - 智能家居 AOG。将设备状态与 Firestore 字段集成的最佳方式(2021 年)
问题描述
我有这个项目:https ://github.com/neuberfran/firebasefunction/blob/main/firebase/functions/smart-home/fulfillment.js 它运作良好。但是,例如,我想实现一个条件,如果我关闭了车库并且我说“关闭车库”,家庭助理会提醒我这件事。
如下图所示,我使用的是控制garagestate字段的rpi3/iot-device/back-end 。我需要知道实现此条件的最佳方法,即读取garagestate字段的值,并从中知道我是否可以打开车库:
解决方案
您可能需要在您的中添加一个中间条件以onExecute
根据Firestore 状态返回错误:
// ...
for (const target of command.devices) {
const configRef = firestore.doc(`device-configs/${target.id}`)
const targetDoc = await configRef.get()
const {garagestate} = targetDoc.data()
if (garagestate === false) {
// garagestate exists and is false
// return an error
return {
requestId,
payload: {
status: 'ERROR',
errorCode: 'alreadyClosed'
}
}
}
// ...
}
// ...