From d181fed5fd666fa881176e21a6ae6d01c38de157 Mon Sep 17 00:00:00 2001
From: Alex Ellis <alexellis2@gmail.com>
Date: Fri, 30 Dec 2016 19:40:49 +0000
Subject: [PATCH] Change samples to use get-stdin for brevity.

---
 sample-functions/ChangeColorIntent/handler.js | 15 +++++----
 .../ChangeColorIntent/package.json            |  1 +
 .../ChangeColorIntent/sendColor.js            |  4 +--
 sample-functions/HostnameIntent/handler.js    | 31 ++++++++++---------
 sample-functions/HostnameIntent/package.json  |  5 ++-
 watchdog/main.go                              |  4 +--
 6 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/sample-functions/ChangeColorIntent/handler.js b/sample-functions/ChangeColorIntent/handler.js
index 4de388f9..1439055f 100644
--- a/sample-functions/ChangeColorIntent/handler.js
+++ b/sample-functions/ChangeColorIntent/handler.js
@@ -4,10 +4,9 @@ let sample = require("./sample.json");
 let SendColor = require('./sendColor');
 let sendColor = new SendColor("alexellis.io/tree1")
 
-var content = '';
-process.stdin.resume();
-process.stdin.on('data', function(buf) { content += buf.toString(); });
-process.stdin.on('end', function() {
+const getStdin = require('get-stdin');
+ 
+getStdin().then(content => {
   let request = JSON.parse(content);
   handle(request, request.request.intent);
 });
@@ -20,7 +19,7 @@ function tellWithCard(speechOutput) {
   process.exit(0);
 }
 
-function handle(request,intent) {
+function handle(request, intent) {
   let colorRequested = intent.slots.LedColor.value;
   let req = {r:0,g:0,b:0};
   if(colorRequested == "red") { 
@@ -31,10 +30,10 @@ function handle(request,intent) {
       req.g = 255;
   }
   else {
-      tellWithCard("I heard "+colorRequested+ " but can only do: red, green, blue.", "I heard "+colorRequested+ " but can only do: red, green, blue.");
+      return tellWithCard("I heard "+colorRequested+ " but can only do: red, green, blue.", "I heard "+colorRequested+ " but can only do: red, green, blue.");
   }
-  var speechOutput = "OK, "+colorRequested+".";
   sendColor.sendColor(req, () => {
-    tellWithCard(speechOutput);
+    var speechOutput = "OK, " + colorRequested + ".";
+    return tellWithCard(speechOutput);
   });
 }
\ No newline at end of file
diff --git a/sample-functions/ChangeColorIntent/package.json b/sample-functions/ChangeColorIntent/package.json
index 4edbbdbc..141a2b89 100644
--- a/sample-functions/ChangeColorIntent/package.json
+++ b/sample-functions/ChangeColorIntent/package.json
@@ -10,6 +10,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "get-stdin": "^5.0.1",
     "mqtt": "^2.0.1"
   }
 }
diff --git a/sample-functions/ChangeColorIntent/sendColor.js b/sample-functions/ChangeColorIntent/sendColor.js
index 8cf16880..ecca6d97 100644
--- a/sample-functions/ChangeColorIntent/sendColor.js
+++ b/sample-functions/ChangeColorIntent/sendColor.js
@@ -13,7 +13,7 @@ class Send {
     var client = mqtt.connect(ops);
 
     client.on('connect', () => {
-      console.log("Connected");
+
       let payload = req;
       let cb = () => {
         done();
@@ -30,7 +30,7 @@ class Send {
       done();
     };
     client.on('connect', () => {
-      console.log("Connected");
+
       let payload = req;
       client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
     });
diff --git a/sample-functions/HostnameIntent/handler.js b/sample-functions/HostnameIntent/handler.js
index ac58f2ea..5c37fb78 100644
--- a/sample-functions/HostnameIntent/handler.js
+++ b/sample-functions/HostnameIntent/handler.js
@@ -2,21 +2,24 @@
 let fs = require('fs');
 let sample = require("./sample.json");
 
-var content = '';
-process.stdin.resume();
-process.stdin.on('data', function(buf) { content += buf.toString(); });
-process.stdin.on('end', function() {
+getStdin().then(content => {
+  let request = JSON.parse(content);
+  handle(request, request.request.intent);
+});
+
+function tellWithCard(speechOutput) {
+  sample.response.outputSpeech.text = speechOutput
+  sample.response.card.content = speechOutput
+  sample.response.card.title = "Hostname";
+  console.log(JSON.stringify(sample));
+  process.exit(0);
+}
+
+function handle(request, intent) {
     fs.readFile("/etc/hostname", "utf8", (err, data) => {
       if(err) {
         return console.log(err);
       }
-//      console.log(content);
-
-      sample.response.outputSpeech.text = "Your hostname is: " + data;
-      sample.response.card.content = "Your hostname is: "+ data
-      sample.response.card.title = "Your hostname";
-      console.log(JSON.stringify(sample));
-      process.exit(0);
-   });
-});
-
+      tellWithCard("Your hostname is " + data);
+  });
+};
diff --git a/sample-functions/HostnameIntent/package.json b/sample-functions/HostnameIntent/package.json
index 3bfc0919..c02111d6 100644
--- a/sample-functions/HostnameIntent/package.json
+++ b/sample-functions/HostnameIntent/package.json
@@ -8,5 +8,8 @@
   },
   "keywords": [],
   "author": "",
-  "license": "ISC"
+  "license": "ISC",
+  "dependencies": {
+    "get-stdin": "^5.0.1"
+  }
 }
diff --git a/watchdog/main.go b/watchdog/main.go
index eed4fb05..d22c7a52 100644
--- a/watchdog/main.go
+++ b/watchdog/main.go
@@ -13,8 +13,8 @@ import (
 func main() {
 	s := &http.Server{
 		Addr:           ":8080",
-		ReadTimeout:    500 * time.Millisecond,
-		WriteTimeout:   1 * time.Second,
+		ReadTimeout:    5 * time.Second,
+		WriteTimeout:   5 * time.Second,
 		MaxHeaderBytes: 1 << 20,
 	}
 
-- 
GitLab