Change samples to use get-stdin for brevity.

This commit is contained in:
Alex Ellis 2016-12-30 19:40:49 +00:00
parent 4573f14f65
commit d181fed5fd
6 changed files with 33 additions and 27 deletions

View File

@ -4,10 +4,9 @@ let sample = require("./sample.json");
let SendColor = require('./sendColor'); let SendColor = require('./sendColor');
let sendColor = new SendColor("alexellis.io/tree1") let sendColor = new SendColor("alexellis.io/tree1")
var content = ''; const getStdin = require('get-stdin');
process.stdin.resume();
process.stdin.on('data', function(buf) { content += buf.toString(); }); getStdin().then(content => {
process.stdin.on('end', function() {
let request = JSON.parse(content); let request = JSON.parse(content);
handle(request, request.request.intent); handle(request, request.request.intent);
}); });
@ -20,7 +19,7 @@ function tellWithCard(speechOutput) {
process.exit(0); process.exit(0);
} }
function handle(request,intent) { function handle(request, intent) {
let colorRequested = intent.slots.LedColor.value; let colorRequested = intent.slots.LedColor.value;
let req = {r:0,g:0,b:0}; let req = {r:0,g:0,b:0};
if(colorRequested == "red") { if(colorRequested == "red") {
@ -31,10 +30,10 @@ function handle(request,intent) {
req.g = 255; req.g = 255;
} }
else { 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, () => { sendColor.sendColor(req, () => {
tellWithCard(speechOutput); var speechOutput = "OK, " + colorRequested + ".";
return tellWithCard(speechOutput);
}); });
} }

View File

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"get-stdin": "^5.0.1",
"mqtt": "^2.0.1" "mqtt": "^2.0.1"
} }
} }

View File

@ -13,7 +13,7 @@ class Send {
var client = mqtt.connect(ops); var client = mqtt.connect(ops);
client.on('connect', () => { client.on('connect', () => {
console.log("Connected");
let payload = req; let payload = req;
let cb = () => { let cb = () => {
done(); done();
@ -30,7 +30,7 @@ class Send {
done(); done();
}; };
client.on('connect', () => { client.on('connect', () => {
console.log("Connected");
let payload = req; let payload = req;
client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb); client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
}); });

View File

@ -2,21 +2,24 @@
let fs = require('fs'); let fs = require('fs');
let sample = require("./sample.json"); let sample = require("./sample.json");
var content = ''; getStdin().then(content => {
process.stdin.resume(); let request = JSON.parse(content);
process.stdin.on('data', function(buf) { content += buf.toString(); }); handle(request, request.request.intent);
process.stdin.on('end', function() { });
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) => { fs.readFile("/etc/hostname", "utf8", (err, data) => {
if(err) { if(err) {
return console.log(err); return console.log(err);
} }
// console.log(content); tellWithCard("Your hostname is " + data);
});
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);
});
});

View File

@ -8,5 +8,8 @@
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC" "license": "ISC",
"dependencies": {
"get-stdin": "^5.0.1"
}
} }

View File

@ -13,8 +13,8 @@ import (
func main() { func main() {
s := &http.Server{ s := &http.Server{
Addr: ":8080", Addr: ":8080",
ReadTimeout: 500 * time.Millisecond, ReadTimeout: 5 * time.Second,
WriteTimeout: 1 * time.Second, WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }