在sub端 googleHomeService要進行一些更動
加入pub/sub的subsciption
加入金鑰的json檔案
設定subscriberOptions
pubsub.subscription to google tts push message
設定ack
const googleHome = require('google-home-push');
const { PubSub } = require('@google-cloud/pubsub');
const config = require('./config')
const language = "zh-TW"
const accent = "zh-TW"
const options = {
language: language,
accent: accent
}
const subscriptionName = 'projects/utliot-3a89b/subscriptions/utl-smart-speakers'
const pubsub = new PubSub({
projectId: 'your-projectId',
keyFilename: '/home/pi/Desktop/googleHomeService/金曜.json'
});
const subscriberOptions = {
flowControl: {
maxMessages: 1
}
}
const subscription = pubsub.subscription(subscriptionName,subscriberOptions);
const messageHandler = message => {
console.log(message.data)
console.log(Buffer.from(message.data, 'base64').toString())
const notifyMessage = Buffer.from(message.data, 'base64').toString()
const googleHomeIp = config.moudule.ip
const myHome = new googleHome(googleHomeIp, options)
myHome.speak(`${notifyMessage}`).then((status)=>{
console.log(status)
})
}
message.ack();
}
// Listen for new messages until timeout is hit
subscription.on('message',messageHandler )
subscription.on('error', function (error) {
// Do something with the error
console.error(`ERROR: ${error}`);
throw error;
});
記得先到GCP該專案的IAM與管理,建立服務帳戶