--- title: "1. 创建设备绑定程序" date: 2019-09-05T23:06:51+08:00 draft: false chapter: false weight: 201 --- 在本章节中,您将创建一个应用程序,这个应用的主要目的是将设备绑定给当前用户。 我们将使用[AWS Amplify](https://aws-amplify.github.io/) 完成程序的构建, [Amazon Cognito User Pools](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html) 作为用户池。 ## 架构图 ![](/images/smart-home/arch.jpg) ## 流程概述 总体上,整个流程分为两步。一为用户设备的绑定,二为Device Cloud接受到[Alexa.Discovery](https://developer.amazon.com/docs/device-apis/alexa-discovery.html)指令时,获取用户当前设备并返回给Alexa。 #### 步骤1:用户绑定设备 首先,我们需要将设备绑定在某个特定用户下,实现方法是调用 API 在 DynamoDB 中建立一条记录。 对于一个物理设备,序列号通常是每个设备唯一的序号,可以用来区分特定设备。在这个方案中,我们通过将 **序列号+设备绑定网址** 组合的方案组成 一个二维码的内容。用户扫描二维码,即可访问该网站,建立用户和设备的绑定关系。在销售设备的过程中,这个二维码可以放在设备包装盒内一起交付给客户。 以下这个流程图,描述了一种如何将物理设备绑定到用户的方法: ![](/images/smart-home/device-bind-flow.png) 整个流程梳理如下: 1. 客户通过手机扫描二维码 2. 手机上打开二维码所指向的网站 3. 重定向到登陆页面 (如果之前登陆过,直接进入第六步) 4. 提交登陆信息来换取 **accessToken** 和 **idToken** 5. 用 **accessToken** 来换取用户信息 6. 调用设备绑定API 7. 在数据库中创建用户和设备的绑定关系 {{% notice info %}} Alexa 对于在您的系统中如何建立用户和设备的关系没有任何要求,您可以按照项目需求设计绑定流程。 {{% /notice %}} #### 步骤2:Alexa获取设备信息 当 Alexa 后端服务接收到 [Alexa.Discovery](https://developer.amazon.com/docs/device-apis/alexa-discovery.html) 指令的时候,Lambda 会负责从 DynamoDB(AWS NoSQL数据库) 中取出记录,并且返回给 Alexa. ## 如何运行 你可以选择 **通过 Amplify Console 部署** 或者 **本地运行**。 这是一个 [现代 Web 应用](https://docs.aws.amazon.com/amplify/latest/userguide/welcome.html#what-are-modern-web-applications), 因此最简单的部署方式是通过[AWS Amplify Console](https://docs.aws.amazon.com/zh_cn/amplify/latest/userguide/welcome.html). {{% notice tip %}} 点击访问[应用程序的代码](https://github.com/aws-samples/aws-alexa-workshop-smarthome-ui). **Amplify Console** 支持 GitHub, BitBucket, GitLab 或者 CodeCommit 作为源代码托管。最简单的方法是先 [Fork 代码仓库](https://help.github.com/en/articles/fork-a-repo#fork-an-example-repository)。 {{% /notice %}} {{% tabs %}} {{% tab name="选项一:通过 Amplify Console 部署" %}} 1. 在[AWS Console](https://console.aws.amazon.com/amplify/home?region=us-east-1#/)中打开 Amplify Console, 点击 **Get started** ![](/images/smart-home/amplify-console-get-started.png) 1. 选择对应的 GitHub 作为 Git 服务提供商,并且点击 **Continue** 1. GitHub 会自动请求授权,当授权完成后,选择对应的仓库和分支,点击 **Next** ![](/images/smart-home/amplify-console-repo.png) 1. 输入 **App name** 1. 创建一个新的环境,输入 **environment name** 或者保持默认 1. 选择 **create a new service role**, 并且点击 **Next** ![](/images/smart-home/amplify-console-settings.png) 1. 点击 **Save and deploy** 1. 当部署完成后,您将会获得一个 URL 来访问这个 web 应用程序。这个 URL 将在后续的步骤中使用,请先记住此 URL 。 ![](/images/smart-home/amplify-url.png) {{% /tab %}} {{% tab name="选项二:本地运行" %}} {{% notice warning %}} 只有当您希望进行 **本地开发** 时,才需要运行这个步骤。如果您已经完成[通过 Amplify Console 部署](#通过-Amplify-Console-部署), 请 **跳过这个步骤**。 {{% /notice %}} 在这个 Web 应用中,**Yarn** 和 **node.js** 被用来构建应用。 - 请根据文档安装 [Yarn](https://yarnpkg.com/en/) 和 [node.js](https://nodejs.org/en/)。安装 Nodejs 最简单的方法是通过 [NVM](https://github.com/nvm-sh/nvm)。 - Web 应用的后端服务是通过 [AWS Amplify CLI](https://github.com/aws-amplify/amplify-cli#install-the-cli) 来进行 部署的。最简单的安装方法是通过 **npm** 来安装。如果您是通过 NVM 来安装的 NodeJS, 那么 npm 已经自动安装了。 1. 初始化后端. 运行 `amplify init`, 在 environment name 输入 **dev** 1. 选择您使用的 **default editor** 已经 **AWS profile**. 等待初始化完成。 ![](/images/smart-home/amplify-init.png) 1. 运行 `amplify push`, 并且当需要确认的时候,输入 **YES** 来确认 1. 输入 **Enter(回车)** 按钮来让其他设置保持默认 ![](/images/smart-home/amplify-push.png) 1. 运行 `yarn install` 安装依赖 1. 运行 `yarn start` 启动 web 应用 1. 打开 [http://localhost:3000/?thingName=xxxxxxxx](http://localhost:3000/?thingName=xxxxxxxx), 在浏览器中查看应用。 1. 如果您是第一次运行这个 web 应用。您应该点击 **Create account** 按钮来创建一个用户账号。 {{% /tab %}} {{% tabs %}}