-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (53 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const WebSocket = require("ws");
const matrix = require("@matrix-io/matrix-lite");
let relay = require('./relay.js');
const https = require("http");
let fs = require('fs');
const ws = new WebSocket("ws://localhost:12101/api/events/intent");
console.log("**Started Web Socket Client**");
ws.on("open", function open() {
console.log("\n**Connected**\n");
});
ws.on("close", function close() {
console.log("\n**Disconnected**\n");
});
// Intents are passed through here
ws.on("message", function incoming(data) {
data = JSON.parse(data);
console.log("**Captured New Intent**");
console.log(data);
if ("SetLed" === data.intent.name) {
matrix.led.set(data.slots["color"]);
say("Device changed to: " + data.slots["color"]);
}
if ("ReadTemp" === data.intent.name)
{
relay.startWaiting();
relay.tellTemp();
//respond with the temperature
say("The current temperature is "+ Math.floor(relay.currentTemperature()) + " degrees Celsius");
relay.sleep(3000);
relay.stopWaiting();
}
if ("SetTemp" === data.intent.name)
{
relay.startWaiting();
relay.makeTemp(data.slots.temp-1,data.slots.temp+1);
relay.stopWaiting();
}
});
// Text to speech for string argument
function say(text) {
const options = {
hostname: "localhost",
port: 12101,
path: "/api/text-to-speech",
method: "POST"
};
const req = https.request(options);
req.on("error", error => {
console.error(error);
});
req.write(text);
req.end();
}