我正在尝试将智能家居设备
(Set-top Box)
与Google Assistant
集成。所以我参考这个链接智能家居机顶盒指南。
这里我计划用基本的
cloud function
来创建intent
,例如action.devices.traits.OnOff
,action.devices.traits.Volume
等
但是我的示例代码不起作用。我在我的移动设备中进行了测试
Google Home
设备显示离线
我的示例代码在这里:
const functions = require('firebase-functions');
const {smarthome} = require('actions-on-google');
const {google} = require('googleapis');
const util = require('util');
const admin = require('firebase-admin');
// Initialize Firebase
admin.initializeApp();
const firebaseRef = admin.database().ref('/');
// Initialize Homegraph
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/homegraph'],
});
const homegraph = google.homegraph({
version: 'v1',
auth: auth,
});
// Hardcoded user ID
const USER_ID = '123';
exports.login = functions.https.onRequest((request, response) => {
if (request.method === 'GET') {
functions.logger.log('Requesting login page');
response.send(`
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<form action="/login" method="post">
<input type="hidden"
name="responseurl" value="${request.query.responseurl}" />
<button type="submit" style="font-size:14pt">
Link this service to Google
</button>
</form>
</body>
</html>
`);
} else if (request.method === 'POST') {
// Here, you should validate the user account.
// In this sample, we do not do that.
const responseurl = decodeURIComponent(request.body.responseurl);
functions.logger.log(`Redirect to ${responseurl}`);
return response.redirect(responseurl);
} else {
// Unsupported method
response.send(405, 'Method Not Allowed');
}
})
exports.fakeauth = functions.https.onRequest((request, response) => {
const responseurl = util.format('%s?code=%s&state=%s',
decodeURIComponent(request.query.redirect_uri), 'xxxxxx',
request.query.state);
functions.logger.log(`Set redirect as ${responseurl}`);
return response.redirect(
`/login?responseurl=${encodeURIComponent(responseurl)}`);
});
exports.faketoken = functions.https.onRequest((request, response) => {
const grantType = request.query.grant_type ?
request.query.grant_type : request.body.grant_type;
const secondsInDay = 86400; // 60 * 60 * 24
const HTTP_STATUS_OK = 200;
functions.logger.log(`Grant type ${grantType}`);
let obj;
if (grantType === 'authorization_code') {
obj = {
token_type: 'bearer',
access_token: '123access',
refresh_token: '123refresh',
expires_in: secondsInDay,
};
} else if (grantType === 'refresh_token') {
obj = {
token_type: 'bearer',
access_token: '123access',
expires_in: secondsInDay,
};
}
response.status(HTTP_STATUS_OK)
.json(obj);
});
const app = smarthome();
app.onSync((body) => {
return {
requestId: body.requestId,
payload: {
agentUserId: USER_ID,
devices: [{
id: 'settop',
type: 'action.devices.types.SETTOP',
traits: [
'action.devices.traits.OnOff',
'action.devices.traits.Volume',
],
name: {
defaultNames: ['AMX TV'],
name: 'AMX TV',
nicknames: ['AMX TV'],
},
deviceInfo: {
manufacturer: 'ZTE',
model: 'Claro',
hwVersion: '1.0',
swVersion: '1.0.1',
},
willReportState: true,
attributes: {
pausable: true,
},
// TODO: Add otherDeviceIds for local execution
}],
},
};
});
exports.smarthome = functions.https.onRequest(app);
/*const {onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
exports.helloWorld = onRequest((request, response) => {
logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
*/
请给我推荐一些示例代码
action.devices.types.SETTOP
您可以使用以下 codelab 开始构建您的设置,并将 Firebase 实现从洗衣机一一更改为设置顶部设备类型吗?让我们知道问题是否仍然存在,如果是,请发送完整的同步和查询响应。