-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (189 loc) · 5.75 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import delay from 'delay';
import RoonApi from "node-roon-api"
import RoonApiStatus from "node-roon-api-status"
import RoonApiTransport from "node-roon-api-transport"
import RoonApiSettings from 'node-roon-api-settings'
import IkeaConnection from './connection.js'
import IkeaDevices from './devices.js'
import fs from 'fs'
var _output_id = "";
var ikea_devices = new Array;
var tradfri
var first_run = false
var roon = new RoonApi({
extension_id: 'dk.hagenjohansen.roontradfri',
display_name: "Roon Tradfri",
display_version: "0.0.14",
publisher: 'Hasse Hagen Johansen',
email: '[email protected]',
website: 'https://github.com/HasseJohansen/roon-extension-ikea-tradfri',
core_paired: function(core) {
let transport = core.services.RoonApiTransport;
transport.subscribe_zones(function(cmd, data) {
if (cmd == "Changed") {
if (data.zones_changed) {
data.zones_changed.forEach(zone => {
zone.outputs.forEach(output => {
if (output.output_id == _mysettings.outputid.output_id) {
if ((zone.state == "playing") || (zone.state == "loading")) {
console.log('Turning ON IKEA device');
turn_ikea_device("ON", _mysettings.ikeaplug)
}
else {
console.log('Turning OFF IKEA device');
turn_ikea_device("OFF", _mysettings.ikeaplug)
}
}
});
});
}
}
});
},
core_unpaired: function(core) {
console.log(core.core_id,
core.display_name,
core.display_version,
"-",
"LOST");
}
});
var _mysettings = Object.assign({
zone: null,
ikeaplug: null
}, roon.load_config("settings") || {});
function makelayout(settings) {
var l = {
values: settings,
layout: [],
has_error: false
};
if( first_run == true ) {
do_not_log()
l.layout.push({
type: "string",
title: "Ikea gateway secret key",
setting: "ikeagwkey",
})
}
else {
delete l.values.ikeagwkey;
log()
l.layout.push({
type: "zone",
title: "Zone",
setting: "outputid",
});
l.layout.push({
type: "dropdown",
title: "IkeaPlug",
values: ikea_devices,
setting: "ikeaplug",
});
}
return l;
}
var svc_settings = new RoonApiSettings(roon, {
get_settings: function(cb) {
cb(makelayout(_mysettings));
},
save_settings: function(req, isdryrun, settings) {
if (req.body.settings) {
if ( (req.body.settings.values["outputid"]) && (first_run == false) ) {
_output_id = req.body.settings.values["outputid"]["output_id"];
}
}
let l = makelayout(settings.values);
req.send_complete(l.has_error ? "NotValid" : "Success", { settings: l });
if (!l.has_error && first_run == false) {
delete l.values.ikeagwkey;
_mysettings = l.values;
svc_settings.update_settings(l);
roon.save_config("settings", _mysettings);
update_status();
}
else {
first_run = false;
get_ikea_devices(l.values['ikeagwkey']).then( () => {
svc_status.set_status("Not Configured");
})
}
}
});
var svc_status = new RoonApiStatus(roon);
roon.init_services({
required_services: [ RoonApiTransport ],
provided_services: [ svc_settings, svc_status ]
});
function update_status() {
if ( (typeof(_mysettings.outputid) != "undefined") && (_mysettings.ikeaplug != null) ) {
var device_name = ikea_devices.filter(device => {
return device.value === _mysettings.ikeaplug
})[0]
svc_status.set_status(_mysettings.outputid.name + " set to: " + device_name.title, false);
}
else {
svc_status.set_status("First run. Please update settings");
}
}
const get_ikea_devices = async (gwkey="undefined") => {
tradfri = await IkeaConnection.getConnection()
if (typeof(tradfri) != "object" && gwkey != "undefined") {
tradfri = await IkeaConnection.getConnection(gwkey)
}
if (typeof(tradfri) != "object") {
first_run = true;
}
else {
tradfri = await IkeaConnection.getConnection(gwkey)
tradfri.observeDevices();
await delay(5000)
for (const deviceId in tradfri.devices) {
const device = tradfri.devices[deviceId];
var DeviceObj = new Object()
DeviceObj.title = device.name
DeviceObj.value = deviceId
if (typeof(device.plugList) != "undefined") {
ikea_devices.push(DeviceObj)
}
}
tradfri = tradfri;
}
}
const turn_ikea_device = async (cmd,deviceid) => {
for(const deviceId in tradfri.devices) {
if(deviceId === deviceid) {
var device = tradfri.devices[deviceId];
var accessory = device.plugList[0]
accessory.client = tradfri
if (cmd == "ON") {
accessory.turnOn()
}
if (cmd == "OFF") {
accessory.turnOff()
}
}
}
}
function do_not_log() {
var logger = process.stdout.write
var dev_null = fs.createWriteStream('/dev/null');
process.stdout.write = dev_null.write.bind(dev_null);
}
function log() {
var dev_stdout = fs.createWriteStream('/dev/stdout');
process.stdout.write = dev_stdout.write.bind(dev_stdout);
}
init_signal_handlers()
get_ikea_devices().then( () => {
roon.start_discovery();
update_status();
})
function init_signal_handlers() {
const handle = function(signal) {
process.exit(0);
};
// Register signal handlers to enable a graceful stop of the container
process.on('SIGTERM', handle);
process.on('SIGINT', handle);
}