-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
97 lines (97 loc) · 3.55 KB
/
bundle.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";
/// <reference types="@google/local-home-sdk" />
const app = new smarthome.App("1.0.0");
app.onIdentify((request) => {
console.debug("IDENTIFY request:", request);
const device = request.inputs[0].payload.device;
return new Promise((resolve, reject) => {
const response = {
intent: smarthome.Intents.IDENTIFY,
requestId: request.requestId,
payload: {
device: {
id: device.id || "",
verificationId: "local-elgato-device-id"
},
},
};
console.debug("IDENTIFY response", response);
resolve(response);
});
})
.onQuery((request) => {
console.debug("QUERY request", request);
const devices = {};
const response = {
requestId: request.requestId,
payload: { devices: devices }
};
return Promise.all(request.inputs[0].payload.devices.map((device) => {
const command = new smarthome.DataFlow.HttpRequestData();
command.requestId = request.requestId;
command.method = smarthome.Constants.HttpOperation.GET;
command.deviceId = device.id;
command.port = 9123;
command.path = '/elgato/lights';
return app.getDeviceManager().send(command)
.then((result) => {
console.debug("QUERY response", result);
const httpResult = result;
const responseBody = httpResult.httpResponse.body;
const deviceState = JSON.parse(responseBody);
const lightState = deviceState.lights[0];
devices[device.id] = {
"on": !!lightState['on'],
"color": {
"temperatureK": Math.floor(1e6 / lightState['temperature']),
},
"brightness": lightState['brightness'],
};
});
})).then(() => {
return response;
});
})
.onExecute((request) => {
console.debug("EXECUTE request", request);
const response = new smarthome.Execute.Response.Builder()
.setRequestId(request.requestId);
const command = request.inputs[0].payload.commands[0];
const lightState = {};
const newState = { "lights": [lightState] };
for (const exec of command.execution) {
switch (exec.command) {
case "action.devices.commands.OnOff":
lightState['on'] = exec.params.on * 1;
break;
case "action.devices.commands.ColorAbsolute":
lightState['temperature'] = Math.floor(1e6 / exec.params.color.temperature);
break;
case "action.devices.commands.BrightnessAbsolute":
lightState["brightness"] = exec.params.brightness;
break;
}
}
return Promise.all(command.devices.map((device) => {
const command = new smarthome.DataFlow.HttpRequestData();
command.requestId = request.requestId;
command.deviceId = device.id;
command.method = smarthome.Constants.HttpOperation.PUT;
command.port = 9123;
command.path = '/elgato/lights';
command.data = JSON.stringify(newState);
command.dataType = 'application/json';
return app.getDeviceManager().send(command).then(() => {
response.setSuccessState(device.id, "FOOBAR");
}).catch((e) => {
response.setErrorState(device.id, e);
});
})).then(() => {
console.debug("EXECUTE response", response);
return response.build();
});
})
.listen()
.then(() => {
console.log("Ready");
});