-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhue-callionica-light-levels.html
118 lines (100 loc) · 4.1 KB
/
hue-callionica-light-levels.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
<!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 Light Levels</title>
<link rel="stylesheet" href="hue-callionica.css">
<style>
.limit {
color: lightgray;
}
.numeric {
text-align: right;
}
.celsius::after {
content: '℃';
color: gray;
}
.fahrenheit::after {
content: '℉';
color: gray;
}
td:first-of-type {
padding: 0;
}
tr[data-on='false'] {
color: gray;
}
tr[data-reachable='false'] {
color: gray;
}
a {
background-color: inherit;
}
</style>
<script type="module">
import { loadCurrentBridge, loadConnection } from "./hue-callionica-connect.js";
import { sortBy, getAll, getCapabilities, getSensorTriggeredRules } from "./hue-callionica.js";
import { formatHumanDateTime } from "./hue-callionica-ui.js";
let bridge;
let connection;
function lightLevelHTML(item, type) {
const isOn = (sensor) => {
const on = sensor.config?.on;
return on || (on === undefined);
};
const isReachable = (sensor) => {
const reachable = sensor.config?.reachable;
return reachable || (reachable === undefined);
};
const on = isOn(item);
const reachable = isReachable(item);
const lastUpdated = formatHumanDateTime(new Date(item.state.lastupdated + "Z"));
const lightLevel = (item.state.lightlevel == undefined) ? "Unknown" : item.state.lightlevel;
return `<tr id="${type}-${item.id}" title="ID: ${item.id} (${type})" data-on="${on}" data-reachable="${reachable}"><td><a href="hue-callionica-sensor-rename.html?bridge=${bridge.id}&sensor=${item.id}">${item.name}</a></td><td class="numeric lightlevel">${lightLevel}</td><td><time datetime="${item.state.lastupdated + "Z"}">${lastUpdated}</time></td></tr>`;
}
async function main() {
bridge = loadCurrentBridge();
connection = await loadConnection("Callionica", bridge);
const data = await getAll(connection);
const capabilities = await getCapabilities(connection);
const sensors = Object.values(data.sensors).filter(sensor => sensor?.state?.lightlevel !== undefined).sort((a, b) => a.name.localeCompare(b.name));
const msg = (sensors.length === 0) ? `<p>No light level sensors found.</p>` : "";
const html = `<h1>${connection.bridge.name}</h1>
<h2>Light Levels</h2>
<table>`
+ sensors.map(item => lightLevelHTML(item, "sensor")).join("\n")
+ `</table>`
+ msg
;
document.getElementById("light-levels").innerHTML = html;
}
async function _main() {
try {
await main();
delete document.body.dataset.showConnection;
// setTimeout(()=> _main(), 60*1000);
} catch (error) {
document.body.dataset.showConnection = true;
const e = document.querySelector("#connection");
if ((bridge === undefined) || (connection === undefined)) {
e.innerHTML = `<a href="hue-callionica-connect.html">Connect to your Hue bridge</a>`;
} else {
e.innerHTML = `<a href="https://${connection.bridge.ip}/api/unauthenticated/config">Refresh your Hue bridge connection</a>`;
}
setTimeout(()=> _main(), 3*1000);
}
}
_main().then(x => console.log("Done"));
</script>
</head>
<body>
<h1>Hue Light Levels</h1>
<p>Here you can see light levels reported by devices attached to the current bridge.</p>
<hr/>
<div id="connection"></div>
<div id="light-levels"></div>
</body>
</html>