init commit

This commit is contained in:
matt committed 2026-10-12 10:51:12 -07:00
commit 8567e9998a
9 files changed
+544

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
from flask import Flask, jsonify
from flask_apscheduler import APScheduler
from datetime import datetime, timedelta
from scanner import update_cache, get_cache_snapshot, get_sensor_summary, get_sensor_details, scan_macs
from config import SCAN_INTERVAL_MINUTES
app = Flask(__name__)
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
scan_macs()
# --- Scheduler job ----------------------------------------------------
@scheduler.task(
'interval',
id='ble_scan',
minutes=SCAN_INTERVAL_MINUTES,
misfire_grace_time=30
)
def ble_scan_job():
"""Runs in the background, calls the async scanner."""
update_cache()
# --- API -------------------------------------------------------------
@app.route('/macs', methods=['GET'])
def list_macs():
"""Return the current set of MACs as JSON."""
macs = sorted(get_cache_snapshot())
return jsonify({"count": len(macs), "macs": macs})
@app.route('/details', methods=['GET'])
def sensor_detail():
"""Return the current set of MACs as JSON."""
sensors = get_sensor_details()
return jsonify(sensors)
@app.route('/summary', methods=['GET'])
def sensor_summary():
"""Return the current set of MACs as JSON."""
sensors = get_sensor_summary()
return jsonify(sensors)
# --- Convenience route -----------------------------------------------
@app.route('/', methods=['GET'])
def hello():
return "BLE Scanner API - see /macs for the list."
# --- Run --------------------------------------------------------------
if __name__ == '__main__':
# The scheduler runs in its own thread; the main thread serves Flask.
app.run(host='0.0.0.0', port=5000, debug=False)