SHARE
TWEET

Untitled

a guest May 19th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // From https://opengarage.io/forums/topic/opengarage-integration-google-home-door-status/
  2. //
  3. // @author: Les
  4. // OpenGarage - Google Home integration for open garage including door status.  This should be run on firebase, see other example for running on own server.
  5. // Thanks to google for posting bitcoin example
  6. // @date: Mar 2018
  7. //
  8. //
  9. "use strict";
  10. const functions = require("firebase-functions");
  11. const { WebhookClient } = require("dialogflow-fulfillment");
  12. const { Card, Suggestion } = require("dialogflow-fulfillment");
  13. const request = require("request");
  14. process.env.DEBUG = "dialogflow:error";
  15.  
  16. // The end-points to our calls - adjust to garage though Blynk API
  17. const EXT_GARAGE_API_URL =
  18.   "http://blynk-cloud.com/<YOUR BLYNK KEY>";
  19. const EXT_STATUS = "/get/v0";
  20. const EXT_OPEN_START = "/update/v1?value=1";
  21. const EXT_OPEN_END = "/update/v1?value=0";
  22. const EXT_CLOSE_START = "/update/v2?value=1";
  23. const EXT_CLOSE_END = "/update/v2?value=0";
  24.  
  25. var newmsg = "";
  26.  
  27. function sleep(ms) {
  28.   return new Promise(resolve => setTimeout(resolve, ms));
  29. }
  30.  
  31. exports.dialogflowFirebaseFulfillment = functions.https.onRequest(
  32.   (req, resp) => {
  33.     const agent = new WebhookClient({ request: req, response: resp });
  34.  
  35.     // Get garage status notification
  36.     function statusHandler(agent) {
  37.       return new Promise((resolve, reject) => {
  38.         request.get(EXT_GARAGE_API_URL + EXT_STATUS, function(
  39.           error,
  40.           resp,
  41.           body
  42.         ) {
  43.           // blynk returns a list of a string of an integer: ["1"]
  44.           // Don't ask me why...
  45.           let json = JSON.parse(body);
  46.           if (json.length === 1 && json[0] === "1") {
  47.             agent.add("It's open.");
  48.           } else if (json.length === 1 && json[0] === "0") {
  49.             agent.add("It's closed.");
  50.           } else {
  51.             console.log(json);
  52.             agent.add("I don't know.");
  53.           }
  54.           resolve();
  55.         });
  56.       });
  57.     }
  58.  
  59.     // Open the garage door
  60.     // We need to set the pin up, wait a bit, then set the pin back down.
  61.     function openHandler(agent) {
  62.       return new Promise((resolve, reject) => {
  63.         request.get(EXT_GARAGE_API_URL + EXT_OPEN_START, function(
  64.           error,
  65.           resp,
  66.           body
  67.         ) {
  68.           const msg = "Opening the garage door.";
  69.           agent.add(msg);
  70.         });
  71.         sleep(2000).then(() => {
  72.           request.get(EXT_GARAGE_API_URL + EXT_OPEN_END, function(
  73.             error,
  74.             resp,
  75.             body
  76.           ) {
  77.             agent.add("Okay.");
  78.             resolve();
  79.           });
  80.         });
  81.       });
  82.     }
  83.  
  84.     // Fulfill block count action
  85.     function closeHandler(agent) {
  86.       return new Promise((resolve, reject) => {
  87.         request.get(EXT_GARAGE_API_URL + EXT_CLOSE_START, function(
  88.           error,
  89.           resp,
  90.           body
  91.         ) {
  92.           const msg = "Closing the garage door.";
  93.           agent.add(msg);
  94.         });
  95.         sleep(2000).then(() => {
  96.           request.get(EXT_GARAGE_API_URL + EXT_CLOSE_END, function(
  97.             error,
  98.             resp,
  99.             body
  100.           ) {
  101.             agent.add("Okay.");
  102.             resolve();
  103.           });
  104.         });
  105.       });
  106.     }
  107.  
  108.     const actionMap = new Map();
  109.     actionMap.set("GarageStatus", statusHandler);
  110.     actionMap.set("GarageOpen", openHandler);
  111.     actionMap.set("GarageClose", closeHandler);
  112.  
  113.     agent.handleRequest(actionMap);
  114.   }
  115. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top