Add changecolorintent

This commit is contained in:
Alex Ellis 2016-12-30 19:24:25 +00:00
parent 8734a3a5ac
commit 4573f14f65
10 changed files with 129 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
fwatchdog
**/node_modules/

View File

@ -116,10 +116,9 @@ func makeProxy(metrics metrics.MetricOptions) http.HandlerFunc {
len(alexaService.Request.Intent.Name) > 0 {
fmt.Println("Alexa skill detected")
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Provide an x-function header."))
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
}
}
}

View File

@ -0,0 +1,12 @@
FROM alpine:latest
RUN apk --update add nodejs
COPY ./fwatchdog /usr/bin/
COPY package.json .
COPY handler.js .
COPY sendColor.js .
COPY sample.json .
RUN npm i
ENV fprocess="node handler.js"
CMD ["fwatchdog"]

View File

@ -0,0 +1,40 @@
"use strict"
let fs = require('fs');
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() {
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 = "Christmas Lights";
console.log(JSON.stringify(sample));
process.exit(0);
}
function handle(request,intent) {
let colorRequested = intent.slots.LedColor.value;
let req = {r:0,g:0,b:0};
if(colorRequested == "red") {
req.r = 255;
} else if(colorRequested== "blue") {
req.b = 255;
} else if (colorRequested == "green") {
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.");
}
var speechOutput = "OK, "+colorRequested+".";
sendColor.sendColor(req, () => {
tellWithCard(speechOutput);
});
}

View File

@ -0,0 +1 @@
docker build -t changecolorintent . ; docker service rm ChangeColorIntent ; docker service create --network=functions --name ChangeColorIntent changecolorintent

View File

@ -0,0 +1,15 @@
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mqtt": "^2.0.1"
}
}

View File

@ -0,0 +1,16 @@
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "There's currently 6 people in space"
},
"card": {
"content": "There's currently 6 people in space",
"title": "People in space",
"type": "Simple"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}

View File

@ -0,0 +1,40 @@
"use strict"
var mqtt = require('mqtt');
class Send {
constructor(topic) {
this.topic = topic;
}
sendIntensity(req, done) {
var ops = { port: 1883, host: "iot.eclipse.org" };
var client = mqtt.connect(ops);
client.on('connect', () => {
console.log("Connected");
let payload = req;
let cb = () => {
done();
};
client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
});
}
sendColor(req, done) {
var ops = { port: 1883, host: "iot.eclipse.org" };
var client = mqtt.connect(ops);
let cb = () => {
done();
};
client.on('connect', () => {
console.log("Connected");
let payload = req;
client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
});
}
}
module.exports = Send;

View File

@ -0,0 +1 @@
docker build -t hostnameintent . ; docker service rm HostnameIntent ; docker service create --network=functions --name HostnameIntent hostnameintent

View File

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