Add Olimex ESP32-C6-EVB + PN532 NFC driver and web UI
- New driver: app/drivers/olimex_esp32_c6_evb_pn532/ - Full relay/input control (inherits base behaviour) - get_nfc_status(), get_nfc_config(), set_nfc_config() methods - manifest.json with NFC hardware metadata (UEXT1 pins, card standard) - NFC management routes (boards.py): - GET /boards/<id>/nfc — management page - GET /boards/<id>/nfc/status_json — live JSON (polled every 3 s) - POST /boards/<id>/nfc/config — save auth UID / relay / timeout - POST /boards/<id>/nfc/enroll — enrol last-seen card with one click - New template: templates/boards/nfc.html - Live reader status (PN532 ready, access state, last UID) - Quick Enroll: present card → Refresh → Enrol in one click - Manual Settings: type/paste UID, pick relay, set absence timeout - detail.html: NFC Access Control button shown for pn532 board type
This commit is contained in:
1
app/drivers/olimex_esp32_c6_evb_pn532/__init__.py
Normal file
1
app/drivers/olimex_esp32_c6_evb_pn532/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Olimex ESP32-C6-EVB with PN532 NFC reader driver package."""
|
||||||
184
app/drivers/olimex_esp32_c6_evb_pn532/driver.py
Normal file
184
app/drivers/olimex_esp32_c6_evb_pn532/driver.py
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
"""Olimex ESP32-C6-EVB + PN532 NFC reader board driver.
|
||||||
|
|
||||||
|
Hardware
|
||||||
|
--------
|
||||||
|
- 4 relays (outputs)
|
||||||
|
- 4 digital inputs
|
||||||
|
- PN532 NFC reader connected via UEXT1 (UART/HSU mode)
|
||||||
|
UEXT1 pin 3 = GPIO4 → PN532 RXD
|
||||||
|
UEXT1 pin 4 = GPIO5 → PN532 TXD
|
||||||
|
DIP1 = 0, DIP2 = 0 (HSU mode)
|
||||||
|
- HTTP REST API served directly by the board on port 80
|
||||||
|
|
||||||
|
API endpoints
|
||||||
|
-------------
|
||||||
|
POST /relay/on?relay=<n> → {"state": true}
|
||||||
|
POST /relay/off?relay=<n> → {"state": false}
|
||||||
|
POST /relay/toggle?relay=<n> → {"state": <new>}
|
||||||
|
GET /relay/status?relay=<n> → {"state": <bool>}
|
||||||
|
GET /input/status?input=<n> → {"state": <bool>}
|
||||||
|
POST /register?callback_url=<url> → {"status": "ok"}
|
||||||
|
GET /nfc/status → {"initialized": bool, "card_present": bool,
|
||||||
|
"last_uid": str, "access_state": str,
|
||||||
|
"auth_uid": str, "relay_num": int,
|
||||||
|
"pulse_ms": int}
|
||||||
|
GET /nfc/config → {"auth_uid": str, "relay_num": int, "pulse_ms": int}
|
||||||
|
POST /nfc/config?auth_uid=&relay=&pulse_ms= → {"status": "ok", ...}
|
||||||
|
|
||||||
|
Webhook (board → server)
|
||||||
|
------------------------
|
||||||
|
The board POSTs to the registered callback_url whenever an input changes:
|
||||||
|
POST <callback_url>
|
||||||
|
{"input": <n>, "state": <bool>}
|
||||||
|
|
||||||
|
The board also POSTs an NFC card event on every card detection:
|
||||||
|
POST <callback_url>
|
||||||
|
{"type": "nfc_card", "uid": "<UID>", "uptime": <seconds>}
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import urllib.parse
|
||||||
|
import requests
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from app.drivers.base import BoardDriver
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from app.models.board import Board
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
_TIMEOUT = 3
|
||||||
|
|
||||||
|
|
||||||
|
def _get(url: str) -> dict | None:
|
||||||
|
try:
|
||||||
|
r = requests.get(url, timeout=_TIMEOUT)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug("GET %s → %s", url, exc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _post(url: str) -> dict | None:
|
||||||
|
try:
|
||||||
|
r = requests.post(url, timeout=_TIMEOUT)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug("POST %s → %s", url, exc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class OlimexESP32C6EVBPn532Driver(BoardDriver):
|
||||||
|
"""Driver for the Olimex ESP32-C6-EVB board with PN532 NFC reader."""
|
||||||
|
|
||||||
|
DRIVER_ID = "olimex_esp32_c6_evb_pn532"
|
||||||
|
DISPLAY_NAME = "Olimex ESP32-C6-EVB + PN532 NFC"
|
||||||
|
DESCRIPTION = "4 relays · 4 digital inputs · PN532 NFC reader (UEXT1/HSU) · HTTP REST + webhook callbacks"
|
||||||
|
DEFAULT_NUM_RELAYS = 4
|
||||||
|
DEFAULT_NUM_INPUTS = 4
|
||||||
|
FIRMWARE_URL = "https://github.com/OLIMEX/ESP32-C6-EVB"
|
||||||
|
|
||||||
|
# ── relay control ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def get_relay_status(self, board: "Board", relay_num: int) -> bool | None:
|
||||||
|
data = _get(f"{board.base_url}/relay/status?relay={relay_num}")
|
||||||
|
return bool(data["state"]) if data is not None else None
|
||||||
|
|
||||||
|
def set_relay(self, board: "Board", relay_num: int, state: bool) -> bool:
|
||||||
|
action = "on" if state else "off"
|
||||||
|
return _post(f"{board.base_url}/relay/{action}?relay={relay_num}") is not None
|
||||||
|
|
||||||
|
def toggle_relay(self, board: "Board", relay_num: int) -> bool | None:
|
||||||
|
data = _post(f"{board.base_url}/relay/toggle?relay={relay_num}")
|
||||||
|
return bool(data["state"]) if data is not None else None
|
||||||
|
|
||||||
|
# ── poll ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def poll(self, board: "Board") -> dict:
|
||||||
|
_offline = {"relay_states": {}, "input_states": {}, "is_online": False}
|
||||||
|
|
||||||
|
relay_states: dict = {}
|
||||||
|
input_states: dict = {}
|
||||||
|
|
||||||
|
if board.num_relays > 0:
|
||||||
|
probe = _get(f"{board.base_url}/relay/status?relay=1")
|
||||||
|
if probe is None:
|
||||||
|
return _offline
|
||||||
|
relay_states["relay_1"] = bool(probe.get("state", False))
|
||||||
|
elif board.num_inputs > 0:
|
||||||
|
probe = _get(f"{board.base_url}/input/status?input=1")
|
||||||
|
if probe is None:
|
||||||
|
return _offline
|
||||||
|
input_states["input_1"] = bool(probe.get("state", False))
|
||||||
|
else:
|
||||||
|
return _offline
|
||||||
|
|
||||||
|
for n in range(2, board.num_relays + 1):
|
||||||
|
data = _get(f"{board.base_url}/relay/status?relay={n}")
|
||||||
|
if data is not None:
|
||||||
|
relay_states[f"relay_{n}"] = bool(data.get("state", False))
|
||||||
|
|
||||||
|
input_start = 2 if (board.num_relays == 0 and board.num_inputs > 0) else 1
|
||||||
|
for n in range(input_start, board.num_inputs + 1):
|
||||||
|
data = _get(f"{board.base_url}/input/status?input={n}")
|
||||||
|
if data is not None:
|
||||||
|
input_states[f"input_{n}"] = bool(data.get("state", True))
|
||||||
|
|
||||||
|
return {
|
||||||
|
"relay_states": relay_states,
|
||||||
|
"input_states": input_states,
|
||||||
|
"is_online": True,
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── webhook registration ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def register_webhook(self, board: "Board", callback_url: str) -> bool:
|
||||||
|
url = f"{board.base_url}/register?callback_url={callback_url}"
|
||||||
|
ok = _post(url) is not None
|
||||||
|
if ok:
|
||||||
|
logger.info("Webhook registered on board '%s' → %s", board.name, callback_url)
|
||||||
|
else:
|
||||||
|
logger.warning("Webhook registration failed for board '%s'", board.name)
|
||||||
|
return ok
|
||||||
|
|
||||||
|
# ── NFC access control ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def get_nfc_status(self, board: "Board") -> dict | None:
|
||||||
|
"""Return current NFC reader status (last UID, access_state, auth config)."""
|
||||||
|
return _get(f"{board.base_url}/nfc/status")
|
||||||
|
|
||||||
|
def get_nfc_config(self, board: "Board") -> dict | None:
|
||||||
|
"""Return current NFC access-control configuration from the board."""
|
||||||
|
return _get(f"{board.base_url}/nfc/config")
|
||||||
|
|
||||||
|
def set_nfc_config(
|
||||||
|
self,
|
||||||
|
board: "Board",
|
||||||
|
auth_uid: str = "",
|
||||||
|
relay_num: int = 1,
|
||||||
|
pulse_ms: int = 3000,
|
||||||
|
) -> bool:
|
||||||
|
"""Push NFC access-control config to the board.
|
||||||
|
|
||||||
|
auth_uid: authorized card UID (e.g. "04:AB:CD:EF"); empty = no card authorized.
|
||||||
|
relay_num: which relay to open on a matching card (1-4).
|
||||||
|
pulse_ms: absence timeout — relay closes this many ms after card is removed (100-60000).
|
||||||
|
"""
|
||||||
|
url = (
|
||||||
|
f"{board.base_url}/nfc/config"
|
||||||
|
f"?auth_uid={urllib.parse.quote(auth_uid.upper())}"
|
||||||
|
f"&relay={relay_num}"
|
||||||
|
f"&pulse_ms={pulse_ms}"
|
||||||
|
)
|
||||||
|
result = _post(url)
|
||||||
|
if result:
|
||||||
|
logger.info(
|
||||||
|
"NFC config pushed to board '%s': uid='%s' relay=%d pulse=%dms",
|
||||||
|
board.name, auth_uid, relay_num, pulse_ms,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.warning("NFC config push failed for board '%s'", board.name)
|
||||||
|
return result is not None
|
||||||
28
app/drivers/olimex_esp32_c6_evb_pn532/manifest.json
Normal file
28
app/drivers/olimex_esp32_c6_evb_pn532/manifest.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"driver_id": "olimex_esp32_c6_evb_pn532",
|
||||||
|
"display_name": "Olimex ESP32-C6-EVB + PN532 NFC",
|
||||||
|
"description": "Olimex ESP32-C6-EVB board with 4 relays, 4 digital inputs, and a PN532 NFC reader connected via UEXT1 (HSU/UART mode). Communicates via HTTP REST API with webhook callbacks. Supports NFC card access control.",
|
||||||
|
"manufacturer": "Olimex",
|
||||||
|
"chip": "ESP32-C6 WROOM-1",
|
||||||
|
"default_num_relays": 4,
|
||||||
|
"default_num_inputs": 4,
|
||||||
|
"firmware_url": "https://github.com/OLIMEX/ESP32-C6-EVB",
|
||||||
|
"nfc": {
|
||||||
|
"reader": "NXP PN532",
|
||||||
|
"interface": "UART/HSU via UEXT1",
|
||||||
|
"uext1_pin3_gpio": 4,
|
||||||
|
"uext1_pin4_gpio": 5,
|
||||||
|
"card_standard": "ISO14443A / Mifare"
|
||||||
|
},
|
||||||
|
"api": {
|
||||||
|
"relay_on": "POST /relay/on?relay={n}",
|
||||||
|
"relay_off": "POST /relay/off?relay={n}",
|
||||||
|
"relay_toggle": "POST /relay/toggle?relay={n}",
|
||||||
|
"relay_status": "GET /relay/status?relay={n}",
|
||||||
|
"input_status": "GET /input/status?input={n}",
|
||||||
|
"register": "POST /register?callback_url={url}",
|
||||||
|
"nfc_status": "GET /nfc/status",
|
||||||
|
"nfc_config": "GET /nfc/config",
|
||||||
|
"nfc_config_set": "POST /nfc/config?auth_uid={uid}&relay={n}&pulse_ms={ms}"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ from app.services.board_service import poll_board, register_webhook, set_relay
|
|||||||
from app.drivers.registry import registry
|
from app.drivers.registry import registry
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
|
_NFC_DRIVER_ID = "olimex_esp32_c6_evb_pn532"
|
||||||
|
|
||||||
boards_bp = Blueprint("boards", __name__)
|
boards_bp = Blueprint("boards", __name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -276,3 +278,101 @@ def edit_entities(board_id: int):
|
|||||||
@login_required
|
@login_required
|
||||||
def edit_labels(board_id: int):
|
def edit_labels(board_id: int):
|
||||||
return redirect(url_for("boards.edit_entities", board_id=board_id))
|
return redirect(url_for("boards.edit_entities", board_id=board_id))
|
||||||
|
|
||||||
|
|
||||||
|
# ── NFC management (PN532 boards only) ────────────────────────────────────────
|
||||||
|
|
||||||
|
@boards_bp.route("/<int:board_id>/nfc")
|
||||||
|
@login_required
|
||||||
|
def nfc_management(board_id: int):
|
||||||
|
board = db.get_or_404(Board, board_id)
|
||||||
|
if board.board_type != _NFC_DRIVER_ID:
|
||||||
|
abort(404)
|
||||||
|
driver = registry.get(_NFC_DRIVER_ID)
|
||||||
|
nfc_status = driver.get_nfc_status(board) if driver else None
|
||||||
|
return render_template(
|
||||||
|
"boards/nfc.html",
|
||||||
|
board=board,
|
||||||
|
nfc=nfc_status or {},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@boards_bp.route("/<int:board_id>/nfc/status_json")
|
||||||
|
@login_required
|
||||||
|
def nfc_status_json(board_id: int):
|
||||||
|
board = db.get_or_404(Board, board_id)
|
||||||
|
if board.board_type != _NFC_DRIVER_ID:
|
||||||
|
abort(404)
|
||||||
|
driver = registry.get(_NFC_DRIVER_ID)
|
||||||
|
data = driver.get_nfc_status(board) if driver else None
|
||||||
|
if data is None:
|
||||||
|
return jsonify({"error": "Board unreachable"}), 502
|
||||||
|
return jsonify(data)
|
||||||
|
|
||||||
|
|
||||||
|
@boards_bp.route("/<int:board_id>/nfc/config", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def nfc_config_save(board_id: int):
|
||||||
|
if not current_user.is_admin():
|
||||||
|
abort(403)
|
||||||
|
board = db.get_or_404(Board, board_id)
|
||||||
|
if board.board_type != _NFC_DRIVER_ID:
|
||||||
|
abort(404)
|
||||||
|
driver = registry.get(_NFC_DRIVER_ID)
|
||||||
|
if not driver:
|
||||||
|
flash("NFC driver not available.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
auth_uid = request.form.get("auth_uid", "").strip().upper()
|
||||||
|
relay_num = int(request.form.get("relay_num", 1))
|
||||||
|
pulse_ms = int(request.form.get("pulse_ms", 3000))
|
||||||
|
|
||||||
|
if relay_num < 1 or relay_num > 4:
|
||||||
|
flash("Relay number must be 1–4.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
if pulse_ms < 100 or pulse_ms > 60000:
|
||||||
|
flash("Absence timeout must be between 100 and 60 000 ms.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
ok = driver.set_nfc_config(board, auth_uid=auth_uid, relay_num=relay_num, pulse_ms=pulse_ms)
|
||||||
|
if ok:
|
||||||
|
uid_display = auth_uid if auth_uid else "(any card)"
|
||||||
|
flash(f"NFC config saved — authorized UID: {uid_display}, relay: {relay_num}, timeout: {pulse_ms} ms", "success")
|
||||||
|
else:
|
||||||
|
flash("Failed to push NFC config — board unreachable.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
|
||||||
|
@boards_bp.route("/<int:board_id>/nfc/enroll", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def nfc_enroll(board_id: int):
|
||||||
|
"""Read the last-seen UID from the board and immediately authorize it."""
|
||||||
|
if not current_user.is_admin():
|
||||||
|
abort(403)
|
||||||
|
board = db.get_or_404(Board, board_id)
|
||||||
|
if board.board_type != _NFC_DRIVER_ID:
|
||||||
|
abort(404)
|
||||||
|
driver = registry.get(_NFC_DRIVER_ID)
|
||||||
|
if not driver:
|
||||||
|
flash("NFC driver not available.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
status = driver.get_nfc_status(board)
|
||||||
|
if not status:
|
||||||
|
flash("Board unreachable — could not read card UID.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
uid = (status.get("last_uid") or "").strip().upper()
|
||||||
|
if not uid:
|
||||||
|
flash("No card has been presented to the reader yet.", "warning")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|
||||||
|
relay_num = int(request.form.get("relay_num", status.get("relay_num", 1)))
|
||||||
|
pulse_ms = int(request.form.get("pulse_ms", status.get("pulse_ms", 3000)))
|
||||||
|
|
||||||
|
ok = driver.set_nfc_config(board, auth_uid=uid, relay_num=relay_num, pulse_ms=pulse_ms)
|
||||||
|
if ok:
|
||||||
|
flash(f"Card enrolled — UID: {uid}, relay: {relay_num}, timeout: {pulse_ms} ms", "success")
|
||||||
|
else:
|
||||||
|
flash("Card read OK but failed to push config to board.", "danger")
|
||||||
|
return redirect(url_for("boards.nfc_management", board_id=board_id))
|
||||||
|
|||||||
@@ -20,6 +20,11 @@
|
|||||||
</div>
|
</div>
|
||||||
{% if current_user.is_admin() %}
|
{% if current_user.is_admin() %}
|
||||||
<div class="d-flex gap-2">
|
<div class="d-flex gap-2">
|
||||||
|
{% if board.board_type == 'olimex_esp32_c6_evb_pn532' %}
|
||||||
|
<a href="{{ url_for('boards.nfc_management', board_id=board.id) }}" class="btn btn-outline-primary">
|
||||||
|
<i class="bi bi-credit-card-2-front me-1"></i> NFC Access Control
|
||||||
|
</a>
|
||||||
|
{% endif %}
|
||||||
<a href="{{ url_for('boards.edit_entities', board_id=board.id) }}" class="btn btn-outline-info">
|
<a href="{{ url_for('boards.edit_entities', board_id=board.id) }}" class="btn btn-outline-info">
|
||||||
<i class="bi bi-palette me-1"></i> Configure Entities
|
<i class="bi bi-palette me-1"></i> Configure Entities
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
276
app/templates/boards/nfc.html
Normal file
276
app/templates/boards/nfc.html
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}NFC Access Control – {{ board.name }}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<nav aria-label="breadcrumb" class="mb-3">
|
||||||
|
<ol class="breadcrumb">
|
||||||
|
<li class="breadcrumb-item"><a href="{{ url_for('boards.list_boards') }}">Boards</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="{{ url_for('boards.board_detail', board_id=board.id) }}">{{ board.name }}</a></li>
|
||||||
|
<li class="breadcrumb-item active">NFC Access Control</li>
|
||||||
|
</ol>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<div class="d-flex align-items-center justify-content-between mb-4">
|
||||||
|
<div>
|
||||||
|
<h2 class="fw-bold mb-0"><i class="bi bi-credit-card-2-front me-2 text-primary"></i>NFC Access Control</h2>
|
||||||
|
<span class="text-secondary small font-monospace">{{ board.name }} — {{ board.host }}:{{ board.port }}</span>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-outline-secondary" id="refresh-btn" onclick="loadStatus()">
|
||||||
|
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row g-4">
|
||||||
|
|
||||||
|
<!-- ── Live Reader Status ──────────────────────────────────────────────── -->
|
||||||
|
<div class="col-lg-5">
|
||||||
|
<div class="card border-0 rounded-4 h-100">
|
||||||
|
<div class="card-header bg-transparent fw-semibold pt-3">
|
||||||
|
<i class="bi bi-broadcast me-1 text-info"></i> Reader Status
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
|
||||||
|
<!-- Hardware status -->
|
||||||
|
<div class="mb-3 d-flex align-items-center gap-3">
|
||||||
|
<span id="hw-badge" class="badge fs-6
|
||||||
|
{% if nfc.get('initialized') %}text-bg-success{% else %}text-bg-danger{% endif %}">
|
||||||
|
{% if nfc.get('initialized') %}
|
||||||
|
<i class="bi bi-check-circle me-1"></i>PN532 Ready
|
||||||
|
{% else %}
|
||||||
|
<i class="bi bi-x-circle me-1"></i>PN532 Not Detected
|
||||||
|
{% endif %}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Access state indicator -->
|
||||||
|
<div class="mb-4">
|
||||||
|
<div class="small text-secondary mb-1">Access state</div>
|
||||||
|
<div id="access-state-badge" class="badge fs-5
|
||||||
|
{% set as = nfc.get('access_state','idle') %}
|
||||||
|
{% if as == 'granted' %}text-bg-success
|
||||||
|
{% elif as == 'denied' %}text-bg-danger
|
||||||
|
{% else %}text-bg-secondary{% endif %}">
|
||||||
|
{% set as = nfc.get('access_state','idle') %}
|
||||||
|
{% if as == 'granted' %}<i class="bi bi-unlock me-1"></i>ACCESS GRANTED
|
||||||
|
{% elif as == 'denied' %}<i class="bi bi-lock me-1"></i>ACCESS DENIED
|
||||||
|
{% else %}<i class="bi bi-hourglass-split me-1"></i>Idle — waiting for card
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Last detected UID -->
|
||||||
|
<div class="mb-3">
|
||||||
|
<div class="small text-secondary mb-1">Last detected card UID</div>
|
||||||
|
<div class="d-flex align-items-center gap-2">
|
||||||
|
<code id="last-uid" class="fs-5 fw-bold text-primary">
|
||||||
|
{{ nfc.get('last_uid') or '—' }}
|
||||||
|
</code>
|
||||||
|
{% if current_user.is_admin() %}
|
||||||
|
<button id="use-uid-btn" class="btn btn-sm btn-outline-primary"
|
||||||
|
onclick="useLastUID()"
|
||||||
|
{% if not nfc.get('last_uid') %}disabled{% endif %}>
|
||||||
|
<i class="bi bi-arrow-down-circle me-1"></i>Use as authorized
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Current authorized UID -->
|
||||||
|
<div class="mb-3">
|
||||||
|
<div class="small text-secondary mb-1">Authorized card UID</div>
|
||||||
|
{% if nfc.get('auth_uid') %}
|
||||||
|
<code id="auth-uid-display" class="fs-6 fw-bold text-success">{{ nfc.get('auth_uid') }}</code>
|
||||||
|
{% else %}
|
||||||
|
<span id="auth-uid-display" class="text-danger small"><i class="bi bi-exclamation-triangle me-1"></i>None — no card enrolled yet</span>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Current relay & timeout -->
|
||||||
|
<dl class="row mb-0 small">
|
||||||
|
<dt class="col-5 text-secondary">Trigger relay</dt>
|
||||||
|
<dd class="col-7 fw-semibold" id="relay-display">Relay {{ nfc.get('relay_num', 1) }}</dd>
|
||||||
|
<dt class="col-5 text-secondary">Absence timeout</dt>
|
||||||
|
<dd class="col-7 fw-semibold" id="pulse-display">{{ nfc.get('pulse_ms', 3000) }} ms</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Right column: enroll + config ──────────────────────────────────── -->
|
||||||
|
<div class="col-lg-7 d-flex flex-column gap-4">
|
||||||
|
|
||||||
|
{% if current_user.is_admin() %}
|
||||||
|
<!-- ── Quick Enroll ──────────────────────────────────────────────────── -->
|
||||||
|
<div class="card border-0 rounded-4">
|
||||||
|
<div class="card-header bg-transparent fw-semibold pt-3">
|
||||||
|
<i class="bi bi-person-badge me-1 text-success"></i> Quick Enroll
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<p class="text-secondary small mb-3">
|
||||||
|
Present a card to the reader, click <strong>Refresh</strong> until the UID appears, then click <strong>Enroll card</strong>.
|
||||||
|
The UID will be set as the authorized card with the relay and timeout below.
|
||||||
|
</p>
|
||||||
|
<form method="POST" action="{{ url_for('boards.nfc_enroll', board_id=board.id) }}">
|
||||||
|
<div class="row g-3 align-items-end">
|
||||||
|
<div class="col-sm-4">
|
||||||
|
<label class="form-label small text-secondary mb-1">Trigger relay</label>
|
||||||
|
<select name="relay_num" id="enroll-relay" class="form-select">
|
||||||
|
{% for r in range(1, 5) %}
|
||||||
|
<option value="{{ r }}" {% if nfc.get('relay_num', 1) == r %}selected{% endif %}>Relay {{ r }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
<label class="form-label small text-secondary mb-1">Absence timeout (ms)</label>
|
||||||
|
<input type="number" name="pulse_ms" id="enroll-pulse"
|
||||||
|
class="form-control" min="100" max="60000"
|
||||||
|
value="{{ nfc.get('pulse_ms', 3000) }}">
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-4">
|
||||||
|
<button type="submit" class="btn btn-success w-100"
|
||||||
|
{% if not nfc.get('last_uid') %}disabled{% endif %}
|
||||||
|
id="enroll-btn">
|
||||||
|
<i class="bi bi-person-plus me-1"></i>Enroll card
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% if nfc.get('last_uid') %}
|
||||||
|
<div class="mt-2 small text-secondary">
|
||||||
|
Will enroll UID: <code class="text-primary">{{ nfc.get('last_uid') }}</code>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="mt-2 small text-warning">
|
||||||
|
<i class="bi bi-exclamation-triangle me-1"></i>No card detected yet — present a card to the reader and refresh.
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Manual Config ────────────────────────────────────────────────── -->
|
||||||
|
<div class="card border-0 rounded-4">
|
||||||
|
<div class="card-header bg-transparent fw-semibold pt-3">
|
||||||
|
<i class="bi bi-sliders me-1 text-warning"></i> Manual Settings
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="POST" action="{{ url_for('boards.nfc_config_save', board_id=board.id) }}">
|
||||||
|
<div class="row g-3">
|
||||||
|
<div class="col-12">
|
||||||
|
<label class="form-label small text-secondary mb-1">Authorized card UID <span class="text-muted">(leave empty to clear authorization)</span></label>
|
||||||
|
<input type="text" name="auth_uid" id="manual-uid"
|
||||||
|
class="form-control font-monospace text-uppercase"
|
||||||
|
placeholder="e.g. 04:AB:CD:EF"
|
||||||
|
value="{{ nfc.get('auth_uid', '') }}"
|
||||||
|
maxlength="31"
|
||||||
|
oninput="this.value=this.value.toUpperCase()">
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<label class="form-label small text-secondary mb-1">Trigger relay</label>
|
||||||
|
<select name="relay_num" class="form-select">
|
||||||
|
{% for r in range(1, 5) %}
|
||||||
|
<option value="{{ r }}" {% if nfc.get('relay_num', 1) == r %}selected{% endif %}>Relay {{ r }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<label class="form-label small text-secondary mb-1">Absence timeout (ms)</label>
|
||||||
|
<input type="number" name="pulse_ms" class="form-control"
|
||||||
|
min="100" max="60000" value="{{ nfc.get('pulse_ms', 3000) }}">
|
||||||
|
<div class="form-text">Relay closes this many ms after the card is removed (100 – 60 000).</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-12 d-flex gap-2">
|
||||||
|
<button type="submit" class="btn btn-primary">
|
||||||
|
<i class="bi bi-floppy me-1"></i>Save config
|
||||||
|
</button>
|
||||||
|
{% if nfc.get('auth_uid') %}
|
||||||
|
<button type="submit" class="btn btn-outline-danger"
|
||||||
|
onclick="document.getElementById('manual-uid').value=''">
|
||||||
|
<i class="bi bi-x-circle me-1"></i>Clear authorization
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}{# is_admin #}
|
||||||
|
|
||||||
|
</div>{# right col #}
|
||||||
|
</div>{# row #}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block scripts %}
|
||||||
|
<script>
|
||||||
|
const STATUS_URL = "{{ url_for('boards.nfc_status_json', board_id=board.id) }}";
|
||||||
|
|
||||||
|
function loadStatus() {
|
||||||
|
const btn = document.getElementById('refresh-btn');
|
||||||
|
if (btn) { btn.disabled = true; btn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span>Loading…'; }
|
||||||
|
|
||||||
|
fetch(STATUS_URL)
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(d => {
|
||||||
|
// hardware badge
|
||||||
|
const hwBadge = document.getElementById('hw-badge');
|
||||||
|
if (hwBadge) {
|
||||||
|
hwBadge.className = 'badge fs-6 ' + (d.initialized ? 'text-bg-success' : 'text-bg-danger');
|
||||||
|
hwBadge.innerHTML = d.initialized
|
||||||
|
? '<i class="bi bi-check-circle me-1"></i>PN532 Ready'
|
||||||
|
: '<i class="bi bi-x-circle me-1"></i>PN532 Not Detected';
|
||||||
|
}
|
||||||
|
// access state
|
||||||
|
const asBadge = document.getElementById('access-state-badge');
|
||||||
|
if (asBadge) {
|
||||||
|
const as = d.access_state || 'idle';
|
||||||
|
const cls = as === 'granted' ? 'text-bg-success' : as === 'denied' ? 'text-bg-danger' : 'text-bg-secondary';
|
||||||
|
const icon = as === 'granted' ? 'bi-unlock' : as === 'denied' ? 'bi-lock' : 'bi-hourglass-split';
|
||||||
|
const txt = as === 'granted' ? 'ACCESS GRANTED' : as === 'denied' ? 'ACCESS DENIED' : 'Idle — waiting for card';
|
||||||
|
asBadge.className = 'badge fs-5 ' + cls;
|
||||||
|
asBadge.innerHTML = `<i class="bi ${icon} me-1"></i>${txt}`;
|
||||||
|
}
|
||||||
|
// last UID
|
||||||
|
const uidEl = document.getElementById('last-uid');
|
||||||
|
if (uidEl) uidEl.textContent = d.last_uid || '—';
|
||||||
|
const useBtn = document.getElementById('use-uid-btn');
|
||||||
|
if (useBtn) useBtn.disabled = !d.last_uid;
|
||||||
|
const enrollBtn = document.getElementById('enroll-btn');
|
||||||
|
if (enrollBtn) enrollBtn.disabled = !d.last_uid;
|
||||||
|
// authorized UID display
|
||||||
|
const authEl = document.getElementById('auth-uid-display');
|
||||||
|
if (authEl) {
|
||||||
|
if (d.auth_uid) {
|
||||||
|
authEl.className = 'fs-6 fw-bold text-success';
|
||||||
|
authEl.textContent = d.auth_uid;
|
||||||
|
} else {
|
||||||
|
authEl.className = 'text-danger small';
|
||||||
|
authEl.innerHTML = '<i class="bi bi-exclamation-triangle me-1"></i>None — no card enrolled yet';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// relay & timeout summary
|
||||||
|
const relayDisp = document.getElementById('relay-display');
|
||||||
|
if (relayDisp) relayDisp.textContent = 'Relay ' + (d.relay_num || 1);
|
||||||
|
const pulseDisp = document.getElementById('pulse-display');
|
||||||
|
if (pulseDisp) pulseDisp.textContent = (d.pulse_ms || 3000) + ' ms';
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
.finally(() => {
|
||||||
|
if (btn) { btn.disabled = false; btn.innerHTML = '<i class="bi bi-arrow-clockwise me-1"></i>Refresh'; }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function useLastUID() {
|
||||||
|
const uid = document.getElementById('last-uid').textContent.trim();
|
||||||
|
if (uid && uid !== '—') {
|
||||||
|
const manualUid = document.getElementById('manual-uid');
|
||||||
|
if (manualUid) {
|
||||||
|
manualUid.value = uid;
|
||||||
|
manualUid.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-refresh every 3 s while the page is open
|
||||||
|
setInterval(loadStatus, 3000);
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user