前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google Assistant SmartHome 入门指南

Google Assistant SmartHome 入门指南

作者头像
程序手艺人
发布2019-05-15 14:05:45
3K0
发布2019-05-15 14:05:45
举报
文章被收录于专栏:程序手艺人程序手艺人
需求

通过Google Assistant 控制一些从设备(Light, Washer等设备), Youtube上一个简短的视频介绍了Google Assistant控制SmartHome的流程。Integrating Smart Home Devices with the Google Assistant

Google Assistant控制第三方从设备,必须通过创建一个Action

官方文档中Smart Home Washer提供了一个通过Google Assistant控制Washer功能。

Actions console创建了一个简单的Demo.

1. Create a smart home Action

需要填写项目的名称

选择 Smart Home App

2. Install the Firebase Command Line Interface

Firebase命令行界面(CLI)允许您在本地提供Web应用程序,并将您的Web应用程序部署到Firebase托管。

代码语言:javascript
复制
// 安装firebase-tools
npm -g install firebase-tools
firebase --version
// 如果不是V**网络,会出现授权失败的问题
firebase login

  • 成功之后的提示

3. 下载smarthome-washer源码

代码语言:javascript
复制
//源码下载
git clone https://github.com/googlecodelabs/smarthome-washer.git
//
cd washer-start
//
firebase use --add
//
npm --prefix functions/ install
// 部署
firebase deploy --only functions:smarthome
//部署成功之后会提示
✔  Deploy complete!

Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/supple-tracker-237900/overview

4. Configure your project in the Actions on Google console

代码语言:javascript
复制
https://us-central1-<project-id>.cloudfunctions.net/smarthome
其中<project-id>就是 supple-tracker-237900

  • 链接账户

  • 配置相关信息

Client ID

Client secret

Authorization URL

Token URL

ABC123

DEF456

https://us-central1-.cloudfunctions.net/fakeauth

https://us-central1-.cloudfunctions.net/faketoken

5. Link to the Google Assistant

  • 找到 Google Assistant > Settings > Home Control
  • 会发现带有 [test] 前缀的 test app

6. Creating a washer

修改 washer-start/functions/index.js中的代码

代码语言:javascript
复制
app.onSync(body => {
  return {
    requestId: body.requestId,
    payload: {
      agentUserId: '123', 
      devices: [{
        id: 'washer',
        type: 'action.devices.types.WASHER',
        traits: [
          'action.devices.traits.OnOff',
          'action.devices.traits.StartStop',
          'action.devices.traits.RunCycle'
        ],
        name: {
          defaultNames: ['My Washer'],
          name: 'Washer',
          nicknames: ['Washer']
        },
        deviceInfo: {
          manufacturer: 'Acme Co',
          model: 'acme-washer',
          hwVersion: '1.0',
          swVersion: '1.0.1'
        },
        attributes: {
          pausable: true
        }
     }]
    }
  };
});

  • 重新进行部署
代码语言:javascript
复制
firebase deploy

  • 如果需要控制washer,需要修改washer-start/functions/index.js中
代码语言:javascript
复制
app.onExecute((body) => {
  const {requestId} = body;
  const payload = {
    commands: [{
      ids: [],
      status: 'SUCCESS',
      states: {
        online: true,
      },
    }],
  };
  for (const input of body.inputs) {
    for (const command of input.payload.commands) {
      for (const device of command.devices) {
        const deviceId = device.id;
        payload.commands[0].ids.push(deviceId);
        for (const execution of command.execution) {
          const execCommand = execution.command;
          const {params} = execution;
          switch (execCommand) {
            case 'action.devices.commands.OnOff':
              firebaseRef.child(deviceId).child('OnOff').update({
                on: params.on,
              });
              payload.commands[0].states.on = params.on;
              break;
            case 'action.devices.commands.StartStop':
              firebaseRef.child(deviceId).child('StartStop').update({
                isRunning: params.start,
              });
              payload.commands[0].states.isRunning = params.start;
              break;
            case 'action.devices.commands.PauseUnpause':
              firebaseRef.child(deviceId).child('StartStop').update({
                isPaused: params.pause,
              });
              payload.commands[0].states.isPaused = params.pause;
              break;
          }
        }
      }
    }
  }
  return {
    requestId: requestId,
    payload: payload,
  };
});

  • 重新进行部署firebase deploy,然后就可以通过Google Assistant 控制了
代码语言:javascript
复制
"Turn on my washer"

"Pause my washer"

"Stop my washer"

总结

按照文档中的示例,熟悉了通过Google Assistant 控制 washer。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档