在昨天我們把資料寫入了firebase,接下來我們再利用googleHomeTrigger 藉由onUpdate進行傳遞訊息
exports.googleHomeTrigger = functions.firestore.document('personal-accounts/{firebaseId}/peoples/{tagMac}')
.onUpdate(async (change, context) => {
const beforeData = change.before.data() as peopleInterface
const afterData = change.after.data() as peopleInterface
// const firebaseId = context.params.firebaseId
// const tagMac = context.params.tagMac
const physiologicalState = verifyPhysiologicalState(beforeData, afterData)
// const moudule = getLocation(afterData)
await notifyPhysiological(physiologicalState, afterData)
.catch(err =>console.log(err))
});
接著新增:
beforeCoachMessage
afterCoachMessage
(因並非與其他生理狀態寫在同一個map裡)
當coachMessage update 讓physiologicalState = 8
const verifyPhysiologicalState = (beforeData: peopleInterface, afterData: peopleInterface): number => {
let physiologicalState
const beforePhysiological = beforeData.physiological
const afterPhysiological = afterData.physiological
const beforeCoachMessage = beforeData.coachMessage
const afterCoachMessage = afterData.coachMessage
if (afterPhysiological.HR != beforePhysiological.HR) {
//心律
if(afterPhysiological.HR >= 90 || afterPhysiological.HR <= 60){
physiologicalState = 1
}
}
.
.
.
else if (beforeData.coachMessage != afterData.coachMessage) {
physiologicalState = 8
} else {
//生理數據不變
physiologicalState = 0
}
return physiologicalState
}
再新增switch case=>當physiological state=8時,產生如下的message
case 8:
// notifyMessage = `${afterData.name}血糖為${afterData.physiological?.BS}`
notifyMessage = {
tagMAC:afterData.tagMAC,
name:afterData.name,
result:`${afterData.coachMessage}`,
message:`${afterData.coachMessage}`,
moudule:arr[0]
}
break;
即可完成傳送coach message