diff --git a/.gitignore b/.gitignore index 512d37b0..3d7624ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ fwatchdog +**/node_modules/ diff --git a/gateway/server.go b/gateway/server.go index 6b4aa013..d5cc543f 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -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.")) } } } diff --git a/sample-functions/ChangeColorIntent/Dockerfile b/sample-functions/ChangeColorIntent/Dockerfile new file mode 100644 index 00000000..cbc833c2 --- /dev/null +++ b/sample-functions/ChangeColorIntent/Dockerfile @@ -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"] diff --git a/sample-functions/ChangeColorIntent/handler.js b/sample-functions/ChangeColorIntent/handler.js new file mode 100644 index 00000000..4de388f9 --- /dev/null +++ b/sample-functions/ChangeColorIntent/handler.js @@ -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); + }); +} \ No newline at end of file diff --git a/sample-functions/ChangeColorIntent/oneshot.txt b/sample-functions/ChangeColorIntent/oneshot.txt new file mode 100644 index 00000000..f6e0746e --- /dev/null +++ b/sample-functions/ChangeColorIntent/oneshot.txt @@ -0,0 +1 @@ +docker build -t changecolorintent . ; docker service rm ChangeColorIntent ; docker service create --network=functions --name ChangeColorIntent changecolorintent diff --git a/sample-functions/ChangeColorIntent/package.json b/sample-functions/ChangeColorIntent/package.json new file mode 100644 index 00000000..4edbbdbc --- /dev/null +++ b/sample-functions/ChangeColorIntent/package.json @@ -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" + } +} diff --git a/sample-functions/ChangeColorIntent/sample.json b/sample-functions/ChangeColorIntent/sample.json new file mode 100644 index 00000000..27fc86ca --- /dev/null +++ b/sample-functions/ChangeColorIntent/sample.json @@ -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": {} +} diff --git a/sample-functions/ChangeColorIntent/sendColor.js b/sample-functions/ChangeColorIntent/sendColor.js new file mode 100644 index 00000000..8cf16880 --- /dev/null +++ b/sample-functions/ChangeColorIntent/sendColor.js @@ -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; \ No newline at end of file diff --git a/sample-functions/HostnameIntent/oneshot.txt b/sample-functions/HostnameIntent/oneshot.txt new file mode 100644 index 00000000..4f753271 --- /dev/null +++ b/sample-functions/HostnameIntent/oneshot.txt @@ -0,0 +1 @@ +docker build -t hostnameintent . ; docker service rm HostnameIntent ; docker service create --network=functions --name HostnameIntent hostnameintent diff --git a/watchdog/main.go b/watchdog/main.go index 0e603bcb..eed4fb05 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -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()) } -