Add temp sensor.

This commit is contained in:
2026-06-28 13:25:14 -05:00
parent 0f2a03044c
commit e188930755
6 changed files with 35 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
class UptimeCard extends HTMLElement {
class TemperatureCard extends HTMLElement {
constructor() {
super();
this.source = null;
@@ -7,12 +7,13 @@ class UptimeCard extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<style>
:host{display:block}section{height:100%;box-sizing:border-box}.liveValue{font-size:34px;font-weight:700;line-height:1.15;margin-top:10px}.state{display:inline-flex;align-items:center;gap:6px;margin-top:6px}.dot{width:9px;height:9px;border-radius:50%;background:#b3261e}.dot.on{background:#188038}
:host{display:block}section{height:100%;box-sizing:border-box}.liveValue{font-size:34px;font-weight:700;line-height:1.15;margin-top:10px}.secondaryValue{font-size:18px;font-weight:600;margin-top:2px}.state{display:inline-flex;align-items:center;gap:6px;margin-top:6px}.dot{width:9px;height:9px;border-radius:50%;background:#b3261e}.dot.on{background:#188038}
</style>
<section>
<h3>Live temperature</h3>
<div class="muted">Server-Sent Events</div>
<div class="liveValue"><span id="temperature">--</span>&deg;C</div>
<div class="liveValue"><span id="temperatureC">--</span>&deg;C</div>
<div class="secondaryValue"><span id="temperatureF">--</span>&deg;F</div>
<div class="muted state"><span id="dot" class="dot"></span><span id="state">Disconnected</span></div>
</section>`;
window.addEventListener('app:login', event => this.start(event.detail.token));
@@ -36,13 +37,14 @@ class UptimeCard extends HTMLElement {
return;
}
this.setState('Connecting', false);
this.source = new EventSource('/api/uptime/events?token=' + encodeURIComponent(token));
this.source = new EventSource('/api/temperature/events?token=' + encodeURIComponent(token));
this.source.addEventListener('open', () => this.setState('Connected', true));
this.source.addEventListener('temperature', event => {
try {
const json = JSON.parse(event.data);
const connected = !!json.sensorConnected && json.temperatureC !== null;
this.querySelector('#temperature').textContent = connected ? Number(json.temperatureC).toFixed(2) : '--';
const connected = !!json.sensorConnected && json.temperatureC !== null && json.temperatureF !== null;
this.querySelector('#temperatureC').textContent = connected ? Number(json.temperatureC).toFixed(2) : '--';
this.querySelector('#temperatureF').textContent = connected ? Number(json.temperatureF).toFixed(2) : '--';
this.setState(connected ? 'Connected' : 'Sensor unavailable', connected);
} catch (error) {
this.setState('Invalid event', false);
@@ -57,4 +59,4 @@ class UptimeCard extends HTMLElement {
}
}
customElements.define('uptime-card', UptimeCard);
customElements.define('temperature-card', TemperatureCard);