-
Notifications
You must be signed in to change notification settings - Fork 1
/
hue-callionica-components.html
323 lines (274 loc) · 15.1 KB
/
hue-callionica-components.html
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hue Components</title>
<link rel="stylesheet" href="hue-callionica.css">
<script type="module">
import { loadCurrentBridge, loadConnection } from "./hue-callionica-connect.js";
import { sortBy, getAll, setSensorValue, setRuleActions, createSchedule, getConnectedComponents, deleteComponent, deleteSchedule, displayLocalTime } from "./hue-callionica.js";
import { paramsSort } from "./hue-callionica-ui.js";
// Changes the action used by the rule from one scene to another
async function setSceneRule(connection, data, ruleID, sceneID) {
console.log(ruleID, sceneID);
// Get the rule
const rule = data.rules[ruleID];
// Copy the actions
const actions = rule.actions.map(a => ({ ...a }));
// Find the action to edit
const action = actions.find(action => action.body.scene !== undefined);
if (action) {
// Change the action
action.body.scene = sceneID;
// Update the rule
try {
await setRuleActions(connection, ruleID, actions);
} catch (e) {
console.log("XGXGXG", e);
}
}
}
// Creates the HTML for adding a scheduled change to a component property:
// select the time, select the property, select the value, hit Add
function createAddScheduleElement(connection, component) {
if (component.sensors.length <= 0) {
return;
}
const root = document.createElement("tr");
root.className = "add-schedule";
const basics = `<td><input type="time" data-role="time" value="08:00"></td><td><select data-role="property"></select></td><td><select data-role="value"></select></td><td><button data-action="add-schedule">Add</button></td>`;
root.innerHTML = basics;
const t = root.querySelector("*[data-role='time']");
const p = root.querySelector("*[data-role='property']");
const v = root.querySelector("*[data-role='value']");
const b = root.querySelector("*[data-action='add-schedule']");
function populateProperties() {
p.innerHTML = component.sensors.filter(sensor => sensor.metadata).map(sensor => `<option value="${sensor.id}">${sensor.metadata.property.replaceAll(">", "›")}</option>`).join("\n");
}
function populateValues() {
const sensorID = p.value;
const sensor = component.sensors.find(sensor => sensor.id === sensorID);
v.innerHTML = sensor.values.map(s => `<option value="${s.value}">${s.name.replaceAll(">", "›")}</option>`).join("\n");
}
function toHMS(value) {
const pieces = value.split(":");
while (pieces.length < 3) {
pieces.push("00");
}
return pieces.map(p => (p == "") ? "00" : p).join(":");
}
async function addSchedule() {
const sensorID = p.value;
const sensor = component.sensors.find(sensor => sensor.id === sensorID);
const value = parseInt(v.value, 10);
const time = toHMS(t.value);
const body = {
name: component.name.substring(0, 32),
description: sensor.metadata.property.substring(0, 32),
recycle: false,
localtime: `W127/T${time}`,
command: {
address: `/api/${connection.token}/sensors/${sensorID}/state`,
body: {
status: value
},
method: "PUT"
}
};
return createSchedule(connection, body);
}
populateProperties();
populateValues();
p.onchange = populateValues;
b.onclick = () => {
addSchedule().then(x => {
document.location = document.location;
});
};
return root;
}
function valuesHTML(sensor) {
return `<select data-sensor="${sensor.id}">\n` +
`<option value="${sensor.value}" "selected"}>${sensor.value?.name?.replaceAll?.(">", "›") || "(Unknown)"}</option>` +
sensor.values.map(s => `<option value="${s.value}" title="${s.description || ""}">${s.name.replaceAll(">", "›")}</option>`) +
`</select>\n`;
}
function sceneSelector(group, data, sceneName) {
return `<select>\n` +
Object.values(data.scenes).filter(s => s.group === group.id).map(s => `<option value="${s.id}" ${sceneName === s.name ? "selected" : ""}>${s.name}</option>`) +
`</select>\n`;
}
function propertyHTML(sensor, data) {
let v = sensor.values ? valuesHTML(sensor) : "";
let result = `<div id="sensors-${sensor.id}"><span class="property">${sensor.metadata.property.replaceAll(">", "›")}</span> = <span class="value" title="Last update: ${sensor.state.lastupdated}">${v}</span></div>`;
let list = "";
if (sensor.metadata.list) {
if (sensor.values.find(v => v.properties.find(p => p.kind === "scene"))) {
// Create a table of items where each column is a property
// and where scenes can be changed to any other scene in the same group
const name = sensor.metadata.property.replace(/^(?<name>.*)s > Current \k<name>$/, "$1");
list += `<div>`;
list += `<div style="font-weight: bold;">${name}s</div>`;
list += `<table class="property">`;
let first = true;
for (let value of sensor.values) {
if (first) {
list += `<tr>`;
for (let property of value.properties) {
list += `<td>${property.name}</td>`;
}
list += `</tr>`;
}
first = false;
list += `<tr ${(value === sensor.value) ? "class='highlight'" : ""}>`;
for (let property of value.properties) {
let done = false;
if (property.kind === "scene") {
const scene = data.scenes[property.value];
if (scene) {
const scenes = Object.values(data.scenes).filter(s => s.group === scene.group);
list += `<td><select data-rule="${property.rule.id}">\n` +
scenes.map(s => `<option value="${s.id}" ${scene.id === s.id ? "selected" : ""}>${s.name}</option>`) +
`</select></td>`;
done = true;
}
}
if (!done) {
list += `<td>${property.displayValue}</td>`;
}
}
list += `</tr>`;
}
list += `</table>`;
list += `</div>`;
}
}
return list + result;
}
function createComponentElement(connection, component, data) {
var tiedToHTML = component.connections.map(connection => {
let category = connection.category;
let item = connection.item;
let description = item.type;
if (category === "sensors") {
description = (item.productname || item.modelid);
} else if (category === "resourcelinks") {
description = item.description;
}
return `<div class="connected">${item.name} (${description})</div>\n`;
}).join("\n");
var properties = component.sensors.filter(sensor => sensor.metadata).map(sensor => propertyHTML(sensor, data)).join("\n");
var agenda = "";
if (properties.length > 0) {
agenda = `<div style="font-weight: bold; margin-top: 12pt;">Schedule</div>`;
agenda += `<table class="agenda">`;
if (component.agenda && component.agenda.length > 0) {
// Columns: time, property, value, controls
agenda += component.agenda.map(a => "<tr><td>" + [displayLocalTime(a.localTime), a.sensor.metadata.property.replaceAll(">", "›"), a.sensor.values.filter(v => v.value === a.value).map(p => p.name).join(", ").replaceAll(">", "›"), `<button data-action="delete-schedule" data-id="${a.schedule.id}">Delete</button>`].join("</td><td>") + "</td></tr>").join("\n");
}
agenda += "</table>";
}
var daylightAgenda = "";
if (component.daylightAgenda && component.daylightAgenda.length > 0) {
daylightAgenda = `<div style="font-weight: bold; margin-top: 12pt;">Daylight Schedule</div><table>` + component.daylightAgenda.map(a => "<tr><td>" + [displayLocalTime(a.localTime), a.sensor.metadata.property.replaceAll(">", "›"), a.sensor.values.filter(v => v.value === a.value).map(p => p.name).join(", ")].join("</td><td>") + "</td></tr>").join("\n") + "</table>";
}
const root = document.createElement("div");
root.id = `components-${component.id}`;
root.className = "component";
var html = `
<h1>${component.name}</h1>
<h2 class="metadata" title="${component.metadata.comment}"><a href="${component.metadata.url}">${component.metadata.name} by ${component.metadata.manufacturer}</a></h2>
${tiedToHTML}
${properties}
${agenda}
${daylightAgenda}
<div style="margin-top: 16pt;"><button data-component="${component.id}" class="delete">Delete</button></div>
`;
root.innerHTML = html;
const agendaElement = root.querySelector(".agenda");
if (agendaElement) {
const addToScheduleElement = createAddScheduleElement(connection, component);
if (addToScheduleElement) {
agendaElement.appendChild(addToScheduleElement);
}
}
return root;
}
async function main() {
const bridge = loadCurrentBridge();
const connection = await loadConnection("Callionica", bridge);
const data = await getAll(connection);
let components = Object.values(data.components);
const d = document.getElementById("data");
if (components.length === 0) {
d.innerHTML = `<p>There are currently no components installed on bridge ${bridge.name}.</p><p>If you install any components, this page will show your components and allow you to control them.</p><p>Why not start by creating a <a href="hue-callionica-power-managed-zone.html">Power Managed Zone</a>?</p>`;
}
components.sort(sortBy(g => g.name + "." + g.description));
const params = new URLSearchParams(document.location.search);
components = paramsSort(params, components);
const componentElements = components.map(component => createComponentElement(connection, component, data));
componentElements.map(componentElement => d.appendChild(componentElement));
function onSensorChange(evt) {
const sensor = evt.srcElement.getAttribute("data-sensor");
const value = evt.srcElement.value;
setSensorValue(connection, sensor, value).then(x => document.location = document.location);
}
[...document.querySelectorAll("select[data-sensor]")].forEach(s => s.onchange = onSensorChange);
function onSceneRuleChange(evt) {
const ruleID = evt.srcElement.getAttribute("data-rule");
const sceneID = evt.srcElement.value;
setSceneRule(connection, data, ruleID, sceneID).then(x => document.location = document.location);
}
[...document.querySelectorAll("select[data-rule]")].forEach(s => s.onchange = onSceneRuleChange);
function onDelete(evt) {
const componentID = evt.srcElement.getAttribute("data-component");
const component = data.components[componentID];
const connected = getConnectedComponents(component, data);
let doIt = true;
if (connected.length > 0) {
doIt = confirm(`Component in use by ${connected.map(c => `${c.name} (${c.description})`).join(", ")}.\n\nContinuing will delete ${component.name} (${component.description}) and all its resources, but will leave these other components in a broken state.\n\nOK to delete ${component.name} (${component.description}) anyway?`);
} else {
doIt = confirm(`Continuing will delete ${component.name} (${component.description}) and all its resources.\n\nOK to delete ${component.name} (${component.description})?`);
}
if (!doIt) {
return;
}
deleteComponent(connection, component).then(x => {
document.location = document.location;
});
}
[...document.querySelectorAll("button.delete")].forEach(s => s.onclick = onDelete);
document.onclick = (evt) => {
const src = evt.srcElement;
const action = src.getAttribute("data-action");
if (action) {
if (action === "delete-schedule") {
const id = src.getAttribute("data-id");
deleteSchedule(connection, id).then(x => {
document.location = document.location;
});
return;
}
}
};
}
// const oneSecond = 1.0 * 1000;
// window.setInterval(() => {
// main().then(x => console.log("Update"));
// }, oneSecond);
main().then(x => {
if (document.location.hash.length > 0) {
document.querySelector(document.location.hash)?.scrollIntoView();
}
});
</script>
</head>
<body>
<h1>Hue Components</h1>
<p>Here you can control the components installed on your Hue bridge.</p>
<hr/>
<div id="data"></div>
</body>
</html>