Created
March 2, 2017 22:39
-
-
Save aaroneiche/ffb492ef2425da68aa0b92bb09d189bf to your computer and use it in GitHub Desktop.
Oberon Smart Home Server - A very simply web server intended to send incoming requests from an Amazon Echo and fire off the associated tasks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var fs = require('fs'); | |
var mqtt = require('mqtt'); | |
var mqttOptions = { | |
"username": "oberon", | |
"password": "titania" | |
} | |
var options = { | |
ca: fs.readFileSync('certificates/fullchain.pem'), | |
key: fs.readFileSync('certificates/rsaprivkey.pem'), | |
cert: fs.readFileSync('certificates/cert.pem') | |
}; | |
var mqtt_client = mqtt.connect('tcp:192.168.1.105',mqttOptions); | |
var alexaResponse = { | |
"version": "0.2", | |
"sessionAttributes": {}, | |
"response": { | |
"outputSpeech": { | |
"type": "PlainText", | |
}, | |
"card": { | |
"type": "Simple", | |
"title": "Oberon", | |
"content": "Oberon will fulfill your command." | |
}, | |
"reprompt": { | |
"outputSpeech": { | |
"type": "PlainText", | |
"text": "No!" | |
} | |
}, | |
"shouldEndSession": true | |
} | |
} | |
function changeAlexaResponse(currentResponse,newPhrase){ | |
currentResponse.response.outputSpeech.text = newPhrase; | |
return currentResponse; | |
} | |
function handleResponse(req,res){ | |
var result; | |
if(req.method == "POST"){ | |
body =""; | |
req.on('data', function (chunk) { | |
body += chunk.toString(); | |
}); | |
req.on('end', function () { | |
var jsonObj = JSON.parse(body); | |
if(jsonObj.request.type == "IntentRequest"){ | |
if(jsonObj.request.intent.name == "Lights"){ | |
var message = jsonObj.request.intent.slots.to.value.toUpperCase(); | |
console.log("target: "+target+" message "+message); | |
if(jsonObj.request.intent.slots.channel.value == undefined){ | |
mqtt_client.publish("oberon/lights/2",message); | |
mqtt_client.publish("oberon/lights/3",message); | |
}else{ | |
var target = 'oberon/lights/'+jsonObj.request.intent.slots.channel.value; | |
mqtt_client.publish(target,message); | |
} | |
} | |
}else{ | |
console.log("nothing more than that"); | |
} | |
res.writeHead(200); | |
res.end(JSON.stringify(alexaResponse)); | |
}); | |
} | |
} | |
var a = https.createServer(options, handleResponse).listen(443); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment