iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 30
1

在昨天我們把資料寫入了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


上一篇
Day29 利用web發送訊息(上)
系列文
手拉手教自己用樹梅派、GoogleHome、GCP做一個AI智慧提醒機器人!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言