Coder Social home page Coder Social logo

ebarahona / actionssdk-smart-home-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from google-home/smart-home-nodejs

0.0 3.0 0.0 97 KB

A sample of the Smart Home device control APIs in Actions on Google

License: Apache License 2.0

JavaScript 68.58% HTML 31.42%

actionssdk-smart-home-nodejs's Introduction

Actions on Google: Smart Home sample using Node.js

This sample contains a fully functioning example of a Smart Home provider cloud service designed to work with Actions on Google. This can be used with a Actions Console project to create an Action interface to your IoT devices. This sample includes everything you need to get started, including a mocked user authentication service with sample users and a user web portal to demonstrate the user experience when controlling their lights with your action.

An example of a Smart Home IoT cloud engine is stored in smart-home-provider/cloud. This consists of both the main smart-home-provider-cloud.js main web service, as well as the web portal used to interact with virtual devices in frontend/. smart-home-provider-cloud.js is the entry point to the Node.js Express app that runs the IoT cloud service, and handles authentication, and ultimately handles requests from the Google Assistant.

The primary AoG intent handlers are stored in smart-home-provider/smart-home-app.js. Here, you can find listeners for POST requests similar to the one your app will receive from the Google Assistant when SYNCing, QUERYing, or EXECuting smart home device control with your cloud. The path for requests to this app is '/ha'.

This sample also includes a mock-assistant-platform module that you can use to locally test your Smart Home app with mocked requests for the SYNC, QUERY, and EXEC intents that you will receive from the Google Assistant. See below for instructions on how to use. It is hard coded to use the account under username 'rick' given in smart-home-provider/cloud/datastore.js. However you swap the appropriate Bearer token in the makeReq method of the mock-assistant-platform.

Setup Instructions

See the developer guide and release notes at https://developers.google.com/actions/ for more details.

Steps for testing with Google Assistant

  1. Navigate to the Actions Console and click Add/Import Project.

  2. Create a new project or import an existing Google Cloud project.

  3. Navigate to the Google Cloud Console API Manager for your project.

  4. Click Credentials

  5. Click 'Create credentials'

  6. Click 'API key'

  7. Copy the API key shown and insert it in smart-home-provider/cloud/config-provider.js

  8. Set up the web portal

     cd smart-home-provider/frontend
     npm install -g bower
     bower install
     cd ..
    
  9. Run cloud/smart-home-provider-cloud.js, either locally or hosted

    • If running locally

      cd cloud/
      npm install
      node smart-home-provider-cloud.js isLocal
      
    • If running in a hosted env,

      node smart-home-provider-cloud.js smart-home-pub="https://your_domain.com"
      
  10. In the resulting output, note the config data. In particular the client ID and client Secret.

  11. In a browser, open the ngrok URL shown.

  12. Log in with one of the sample user accounts, for instance

    user: rick
    password: oldman
    
  13. This is a web portal to your Smart Home devices. Configure the smart lights shown as you please. Click the cloud icon shown above at least one of them to enable it for cloud control.

  14. Return to the Actions Console project. Click 'Use Actions SDK'.

  15. Update the Action Package (action.json) with the URL where your project is hosted (don't forget to include /ha).

  16. Use the gActions CLI to run the command given with the 'action.json' file as your Action Package.

  17. Click Okay.

  18. Click ADD under App information.

  19. Give your App some information like an invocation name, some description, and some policy and contact info.

  20. Click Save.

  21. Click Add under Account Linking.

  22. Select 'Authorization Code' for Grant Type.

  23. Under Client Information, enter the client ID and secret from earlier.

  24. The Authorization URL is the hosted URL of your app with '/oauth' as the path, e.g. https://xyz123.ngrok.io/oauth

  25. The Token URL is the hosted URL of your app with '/token' as the path, e.g. https://xyz123.ngrok.io/token

  26. Enter any remaining necessary information you might need for authentication your app. Then Save.

  27. On a device with the Google Assistant logged into the same account used to create the project in the Actions Console, enter your Assistant settings.

  28. Click Home Control.

  29. Click the '+' sign to add a device.

  30. Find your app in the list of providers.

  31. Log in to your service.

  32. Start using the Google Assistant or Simulator in the Actions Console to control your devices. Try saying 'turn my lights on'.

ℹī¸ Assistant will only provide you control over items that are registered, so if you visit your front end https://xyz123.ngrok.io and click the cloud icon to enable a device you will need to unlink and then re-link your account to see the new or removed devices.

Steps for testing with mock-assistant-platform

  1. Set up the web portal

    cd smart-home-provider/frontend
    npm install -g bower
    bower install
    cd ..
    
  2. Run cloud/smart-home-provider-cloud.js, either locally or hosted

    • If running locally

      cd cloud/
      npm install
      node smart-home-provider-cloud.js isLocal
      
    • If running in a hosted env,

      node smart-home-provider-cloud.js smart-home-pub="https://your_domain.com"
      
  3. In the resulting output, note the config data. In particular the client ID and client Secret.

  4. In a browser, open the ngrok URL shown.

  5. Log in with one of the sample user accounts, for instance

    user: rick
    password: oldman
    
  6. This is a web portal to your Smart Home devices. Configure the smart lights shown as you please. Click the cloud icon shown above at least one of them to enable it for cloud control.

  7. Use the mock-assistant platform to send requests to your Smart Home app.

    cd mock-assistant-platform
    node platform help
    node platform url="https://<NGROK_DOMAIN>.ngrok.io/ha" sync
    node platform url="https://<NGROK_DOMAIN>.ngrok.io/ha" query
    node platform url="https://<NGROK_DOMAIN>.ngrok.io/ha" ex
    

For more detailed information on deployment, see the documentation.

Examples of SYNC, QUERY, and EXEC requests

Your app will need to handle these 3 basic requests from the Google Assistant.

Sync

POST /ha HTTP/1.1
Host: <something>.ngrok.io
Accept: application/json
Content-Type: application/json
Authorization: Bearer psokmCxKjfhk7qHLeYd1
Cache-Control: no-cache

Body:
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
        "intent": "action.devices.SYNC"
    }]
 }

Query

POST /ha HTTP/1.1
Host: <something>.ngrok.io
Accept: application/json
Content-Type: application/json
Authorization: Bearer psokmCxKjfhk7qHLeYd1
Cache-Control: no-cache

Body:
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
        "intent": "action.devices.QUERY",
        "payload": {
            "devices": [{
                "id": "1",
                "customData": {
                    "smartHomeProviderId": "FkldJVJCmDNSaoLkoq0txiz8Byf2Hr"
                }
            },{
                "id": "2"
            },{
                "id": "3"
            },{
                "id": "4"
            }]
        }
    }]
}

Execute

POST /ha HTTP/1.1
Host: <something>.ngrok.io
Accept: application/json
Content-Type: application/json
Authorization: Bearer psokmCxKjfhk7qHLeYd1
Cache-Control: no-cache

Body:
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
        "intent": "action.devices.EXECUTE",
        "payload": {
            "commands": [{
                "devices": [{
                    "id": "1",
                    "customData": {
                        "smartHomeProviderId": "FkldJVJCmDNSaoLkoq0txiz8Byf2Hr"
                    }
                },{
                    "id": "3"
                }],
                "execution": [{
                    "command": "action.devices.commands.Brightness",
                    "params": {
                        "brightness": 60
                    }
                },{
                    "command": "action.devices.commands.ChangeColor",
                    "params": {
                        "color": {
                            "name": "cerulian",
                            "spectrumRGB": 523435
                        }
                    }
                },{
                    "command": "action.devices.commands.OnOff",
                    "params": {
                        "on": true
                    }
                }]
            }]
        }
    }]
}

References and How to report bugs

How to make contributions?

Please read and follow the steps in the CONTRIBUTING.md.

License

See LICENSE.md.

Terms

Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the Google APIs Terms of Service.

Google+

Actions on Google Developers Community on Google+ https://g.co/actionsdev.

actionssdk-smart-home-nodejs's People

Contributors

aleksrozman avatar atulep avatar smishra2 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.