Files
web/app/templates/device.html
T
2026-11-06 11:42:34 -05:00

120 lines
6.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% extends "base.html" %}
{% import "macros.html" as ui %}
{% block title %}Device{% endblock %}
{% block content %}
{% if new_token %}
<section class="card token-card" aria-live="polite">
<h2>Token for {{ new_device.name }}</h2>
<p>Copy it now: it is shown <strong>only once</strong>. If you lose it, rotate it to get a new one.</p>
<div class="copy-row">
<code id="token">{{ new_token }}</code>
<button class="btn btn-small" data-copy="#token">Copy</button>
</div>
<p class="small">Put it in the firmware's <code>secrets.h</code>, next to the Wi-Fi credentials:</p>
<pre class="code" id="secrets">#define MAMADACHI_SYNC_URL "{{ base_url }}/api/v1/device/sync"
#define MAMADACHI_TOKEN "{{ new_token }}"</pre>
<p class="small">Or test it from your computer, pretending to be the device:</p>
<pre class="code">python tools/fake_device.py --url {{ base_url }} --token {{ new_token }} press 6
python tools/fake_device.py --url {{ base_url }} --token {{ new_token }} sync</pre>
</section>
{% endif %}
{% for c in cards %}
{% set dev = c.dev %}
<section class="card device{% if not dev.active %} is-revoked{% endif %}">
<div class="card-head">
<h2>{{ dev.name }}</h2>
<span class="pill {{ 'pill-ok' if dev.active else 'pill-off' }}">{{ 'Active' if dev.active else 'Revoked' }}</span>
</div>
<dl class="facts">
<div><dt>Last sync</dt><dd>{% if dev.last_seen_at %}<span data-ago="{{ f.unix(dev.last_seen_at) }}">{{ f.ago(dev.last_seen_at, now) }}</span>{% else %}never{% endif %}</dd></div>
<div><dt>Battery</dt><dd>{% if dev.battery_mv %}{{ c.battery_pct }}% <span class="muted">{{ '%.2f' % (dev.battery_mv / 1000) }} V{% if dev.charging %} · charging{% endif %}</span>{% else %}–{% endif %}</dd></div>
<div><dt>Wi-Fi signal</dt><dd>{% if dev.rssi is not none %}{{ c.signal }} <span class="muted">{{ dev.rssi }} dBm</span>{% else %}–{% endif %}</dd></div>
<div><dt>Firmware</dt><dd>{{ dev.firmware or '–' }}</dd></div>
<div><dt>Clock</dt><dd>{% if dev.clock_skew_s is none %}–{% elif dev.clock_skew_s|abs <= 120 %}in sync <span class="muted">{{ dev.clock_skew_s }} s</span>{% else %}off by {{ f.duration(dev.clock_skew_s|abs) }}{% endif %}</dd></div>
<div><dt>Presses received</dt><dd>{{ c.presses }}</dd></div>
<div><dt>Boots</dt><dd>{{ dev.boot_count if dev.boot_count is not none else '–' }}</dd></div>
<div><dt>Token</dt><dd><code>mmd_…{{ dev.token_hint }}</code></dd></div>
</dl>
<h3>Battery, last {{ battery_days }} days</h3>
{{ ui.battery_chart(c.chart) }}
{% if c.log %}
<details>
<summary><h3>Recent syncs</h3></summary>
<div class="table-scroll">
<table class="num-table">
<thead><tr><th scope="col">When</th><th scope="col">Presses</th><th scope="col">Battery</th><th scope="col">Signal</th><th scope="col">Clock</th><th scope="col">Server time</th></tr></thead>
<tbody>
{% for r in c.log %}
<tr>
<th scope="row">{{ f.when(r.received_at) }}</th>
<td>{{ r.events_new }}{% if r.events_received != r.events_new %} <span class="muted">({{ r.events_received - r.events_new }} repeat)</span>{% endif %}</td>
<td>{{ '%.2f V' % (r.battery_mv / 1000) if r.battery_mv else '–' }}</td>
<td>{{ r.rssi if r.rssi is not none else '–' }}</td>
<td>{{ '%+d s' % r.clock_skew_s if r.clock_skew_s is not none else '–' }}</td>
<td>{{ r.duration_ms if r.duration_ms is not none else '–' }} ms</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</details>
{% endif %}
<div class="actions">
<form method="post" action="/device/{{ dev.id }}/rename" class="inline-form">
<input type="text" name="name" value="{{ dev.name }}" aria-label="Device name" maxlength="80">
<button class="btn btn-ghost btn-small">Rename</button>
</form>
<form method="post" action="/device/{{ dev.id }}/rotate" data-confirm="Make a new token? The old one stops working, so the device needs reflashing with the new one.">
<button class="btn btn-ghost btn-small">{{ 'New token' if dev.active else 'Re-enable with new token' }}</button>
</form>
{% if dev.active %}
<form method="post" action="/device/{{ dev.id }}/revoke" data-confirm="Stop {{ dev.name }} from syncing? Its past events stay.">
<button class="btn btn-danger btn-small">Revoke</button>
</form>
{% endif %}
</div>
</section>
{% endfor %}
<section class="card">
<h2>{{ 'Add a device' if cards else 'Connect your Mamadachi' }}</h2>
<p class="muted small">Each device gets its own secret token. It sends it with every sync, so the server knows the presses are really from your Mamadachi.</p>
<form method="post" action="/device/new" class="inline-form">
<input type="text" name="name" value="Mamadachi{% if cards %} {{ cards | length + 1 }}{% endif %}" aria-label="Device name" maxlength="80">
<button class="btn">Create token</button>
</form>
</section>
<section class="card">
<details>
<summary><h2>How the device syncs</h2><span class="muted small">For firmware work</span></summary>
<ol class="steps small">
<li>Every press is queued on the device with a <strong>seq</strong> (a counter kept in NVS, +1 per press), the <strong>time</strong> from the RV-3028 (UNIX seconds, UTC) and the <strong>button</strong> number (1–8).</li>
<li>When it's time to sync (every {{ profile.sync_interval_min }} min, or after a press if you like), it joins Wi-Fi and makes <strong>one</strong> HTTPS request:</li>
</ol>
<pre class="code">POST {{ base_url }}/api/v1/device/sync
Authorization: Bearer mmd_…
Content-Type: application/json
{"device_time": 1760630400, "firmware": "0.1.0", "battery_mv": 3987,
"charging": false, "rssi": -61, "boot_count": 42, "queue_len": 0,
"events": [{"seq": 101, "time": 1760620000, "button": 6},
{"seq": 102, "time": 1760627000, "button": 6}]}</pre>
<ol class="steps small" start="3">
<li><strong>HTTP 200</strong>: every event sent is stored. Delete them from the queue. Anything else (no Wi-Fi, timeout, 5xx): keep them and try again later. Resending is safe: the server skips presses it already has.</li>
<li>The response carries <code>server_time</code> (set the RTC if it drifted), <code>tz_offset_s</code> (to show local time), the <code>buttons</code> layout with short labels for the e-paper, and a <code>status</code> block: last feed, next side, ongoing sleep, today's counts.</li>
<li>Send at most 500 events per request. If more are queued, sync again right away.</li>
</ol>
<p class="small">Current layout:
{% for n, k in buttons.items() %}<span class="pill">{{ n }}: {{ k.short if k else '–' }}</span> {% endfor %}
</p>
<p class="small">Full field reference: <a href="/docs#/device">API docs</a>.</p>
</details>
</section>
{% endblock %}