RFID reading and authorization
This commit is contained in:
@@ -6,6 +6,7 @@ import base64
|
||||
import asyncio
|
||||
import fcntl
|
||||
import atexit
|
||||
import re
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from flask import Flask, Response, request, jsonify, current_app, abort, g
|
||||
from pymongo import MongoClient, ASCENDING, DESCENDING
|
||||
@@ -45,6 +46,7 @@ alert_collection = db["alert_data"]
|
||||
webex_tokens_collection = db["webex_tokens"]
|
||||
device_ip_collection = db["device_ips"]
|
||||
cve_audit_results = db["cve_audit_results"]
|
||||
rfid_cards_collection = db["rfid_cards"]
|
||||
|
||||
# Initialize Database Indexes
|
||||
def init_db_indexes():
|
||||
@@ -490,6 +492,52 @@ def run_cve_audit():
|
||||
"timestamp": datetime.now(timezone.utc).isoformat()
|
||||
}), status_code
|
||||
|
||||
@app.route("/rfid-card", methods=["GET"])
|
||||
def get_rfid_cards():
|
||||
"""OData compliant endpoint for technician RFID card validation."""
|
||||
filter_str = request.args.get("$filter", "")
|
||||
card_id = request.args.get("card_id")
|
||||
|
||||
mongo_query = {}
|
||||
|
||||
# Extract card_id from OData $filter string if not supplied directly
|
||||
if not card_id and filter_str:
|
||||
match = re.search(r"(?:card_id|cardId)\s+eq\s+['\"]([^'\"]+)['\"]", filter_str, re.IGNORECASE)
|
||||
if match:
|
||||
card_id = match.group(1)
|
||||
else:
|
||||
# Generic fallback to extract quoted strings
|
||||
generic_match = re.search(r"['\"]([a-zA-Z0-9]+)['\"]", filter_str)
|
||||
if generic_match:
|
||||
card_id = generic_match.group(1)
|
||||
|
||||
if card_id:
|
||||
mongo_query["card_id"] = card_id
|
||||
|
||||
# Filter for active/valid cards only
|
||||
mongo_query["valid"] = True
|
||||
|
||||
try:
|
||||
cards = list(rfid_cards_collection.find(mongo_query))
|
||||
|
||||
result_value = [
|
||||
{
|
||||
"id": str(card["_id"]),
|
||||
"card_id": card.get("card_id"),
|
||||
"valid": card.get("valid", False)
|
||||
}
|
||||
for card in cards
|
||||
]
|
||||
|
||||
return jsonify({
|
||||
"@odata.context": f"{request.host_url.rstrip('/')}/$metadata#RfidCards",
|
||||
"value": result_value
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Error querying rfid_cards collection")
|
||||
return jsonify({"@odata.error": {"code": "500", "message": "Database query error"}}), 500
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Debug Endpoints (Protected)
|
||||
# ---------------------------------------------------------
|
||||
@@ -570,11 +618,18 @@ def odata_metadata():
|
||||
<Property Name="webex_status" Type="Edm.String"/>
|
||||
<Property Name="sms_sent" Type="Edm.Boolean"/>
|
||||
</EntityType>
|
||||
<EntityType Name="RfidCard">
|
||||
<Key><PropertyRef Name="id"/></Key>
|
||||
<Property Name="id" Type="Edm.String" Nullable="false"/>
|
||||
<Property Name="card_id" Type="Edm.String" Nullable="false"/>
|
||||
<Property Name="valid" Type="Edm.Boolean" Nullable="false"/>
|
||||
</EntityType>
|
||||
<EntityContainer Name="Container">
|
||||
<EntitySet Name="Telemetry" EntityType="MicrowaveNetwork.Telemetry"/>
|
||||
<EntitySet Name="CookingParams" EntityType="MicrowaveNetwork.CookingParams"/>
|
||||
<EntitySet Name="CveAudit" EntityType="MicrowaveNetwork.CveAudit"/>
|
||||
<EntitySet Name="Alerts" EntityType="MicrowaveNetwork.Alert"/>
|
||||
<EntitySet Name="RfidCards" EntityType="MicrowaveNetwork.RfidCard"/>
|
||||
</EntityContainer>
|
||||
</Schema>
|
||||
</edmx:DataServices>
|
||||
|
||||
Reference in New Issue
Block a user