updated to multiple cared on nfc

This commit is contained in:
2026-06-13 10:30:53 +03:00
parent 916e35b22b
commit 61ae9b2431
3 changed files with 136 additions and 48 deletions
+67 -7
View File
@@ -318,6 +318,7 @@ def nfc_status_json(board_id: int):
@boards_bp.route("/<int:board_id>/nfc/config", methods=["POST"])
@login_required
def nfc_config_save(board_id: int):
"""Save relay/timeout settings and optionally add a new authorized UID."""
if not current_user.is_admin():
abort(403)
board = db.get_or_404(Board, board_id)
@@ -328,9 +329,9 @@ def nfc_config_save(board_id: int):
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", 0))
new_uid = request.form.get("new_uid", "").strip().upper()
if relay_num < 1 or relay_num > board.num_relays:
flash(f"Relay number must be 1{board.num_relays}.", "danger")
@@ -339,10 +340,21 @@ def nfc_config_save(board_id: int):
flash("Release delay must be between 0 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)
# Fetch current list from board so we can append
current_cfg = driver.get_nfc_config(board) or {}
current_uids: list[str] = current_cfg.get("auth_uids") or (
[current_cfg["auth_uid"]] if current_cfg.get("auth_uid") else []
)
if new_uid:
if new_uid not in current_uids:
if len(current_uids) >= 10:
flash("Maximum 10 authorized cards reached. Remove one before adding.", "danger")
return redirect(url_for("boards.nfc_management", board_id=board_id))
current_uids.append(new_uid)
ok = driver.set_nfc_config(board, auth_uids=current_uids, 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")
flash(f"NFC config saved — {len(current_uids)} card(s) authorized, 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))
@@ -351,7 +363,7 @@ def nfc_config_save(board_id: int):
@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."""
"""Read the last-seen UID from the board and add it to the authorized list."""
if not current_user.is_admin():
abort(403)
board = db.get_or_404(Board, board_id)
@@ -375,14 +387,62 @@ def nfc_enroll(board_id: int):
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", 0)))
ok = driver.set_nfc_config(board, auth_uid=uid, relay_num=relay_num, pulse_ms=pulse_ms)
# Fetch current list and append
current_cfg = driver.get_nfc_config(board) or {}
current_uids: list[str] = current_cfg.get("auth_uids") or (
[current_cfg["auth_uid"]] if current_cfg.get("auth_uid") else []
)
if uid in current_uids:
flash(f"Card already enrolled — UID: {uid}", "info")
return redirect(url_for("boards.nfc_management", board_id=board_id))
if len(current_uids) >= 10:
flash("Maximum 10 authorized cards reached. Remove one before enrolling.", "danger")
return redirect(url_for("boards.nfc_management", board_id=board_id))
current_uids.append(uid)
ok = driver.set_nfc_config(board, auth_uids=current_uids, relay_num=relay_num, pulse_ms=pulse_ms)
if ok:
flash(f"Card enrolled — UID: {uid}, relay: {relay_num}, timeout: {pulse_ms} ms", "success")
flash(f"Card enrolled — UID: {uid} ({len(current_uids)} card(s) total), 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))
@boards_bp.route("/<int:board_id>/nfc/remove_card", methods=["POST"])
@login_required
def nfc_remove_card(board_id: int):
"""Remove one UID from the authorized list."""
if not current_user.is_admin():
abort(403)
board = db.get_or_404(Board, board_id)
if board.board_type not in _NFC_DRIVER_IDS:
abort(404)
driver = registry.get(board.board_type)
if not driver:
flash("NFC driver not available.", "danger")
return redirect(url_for("boards.nfc_management", board_id=board_id))
uid_to_remove = request.form.get("uid", "").strip().upper()
if not uid_to_remove:
flash("No UID specified.", "danger")
return redirect(url_for("boards.nfc_management", board_id=board_id))
current_cfg = driver.get_nfc_config(board) or {}
current_uids: list[str] = current_cfg.get("auth_uids") or (
[current_cfg["auth_uid"]] if current_cfg.get("auth_uid") else []
)
relay_num = int(current_cfg.get("relay_num", 1))
pulse_ms = int(current_cfg.get("pulse_ms", 0))
new_uids = [u for u in current_uids if u != uid_to_remove]
ok = driver.set_nfc_config(board, auth_uids=new_uids, relay_num=relay_num, pulse_ms=pulse_ms)
if ok:
flash(f"Card removed — UID: {uid_to_remove} ({len(new_uids)} card(s) remaining)", "success")
else:
flash("Failed to push updated config — board unreachable.", "danger")
return redirect(url_for("boards.nfc_management", board_id=board_id))
@boards_bp.route("/<int:board_id>/nfc/enable", methods=["POST"])
@login_required
def nfc_enable(board_id: int):