mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Change samples to use get-stdin for brevity.
This commit is contained in:
parent
4573f14f65
commit
d181fed5fd
@ -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);
|
||||
});
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"get-stdin": "^5.0.1",
|
||||
"mqtt": "^2.0.1"
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
@ -8,5 +8,8 @@
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"get-stdin": "^5.0.1"
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user