diff --git a/README.md b/README.md index 45c7d8d..8391c64 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ ble-scanner/ ├─ ble.py # BLE helper functions to read temp+humidity data ├─ scanner.py # Async BLE scan + cache logic ├─ config.py # Shared constants (prefix, interval, etc.) +├─ redis.py # Redis functions for Websocket support ├─ requirements.txt # Python VENV requirements ├─ run.sh # Run API from shell ├─ scanner.py # Handles scanning for Mii sensors, other helper functions diff --git a/app.py b/app.py index 31365c9..267b686 100644 --- a/app.py +++ b/app.py @@ -50,4 +50,4 @@ def hello(): # --- 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) \ No newline at end of file + app.run(host=bind_ip, port=api_port, debug=False) \ No newline at end of file diff --git a/config.py b/config.py index 9816ea3..b7a8098 100644 --- a/config.py +++ b/config.py @@ -1,3 +1,22 @@ # --- BLE & app config ------------------------------------------------- -MAC_PREFIX = "A4:C1:38" # 3 octets (change to what you need) -SCAN_INTERVAL_MINUTES = 1 # how often to run the scan \ No newline at end of file + +MAC_PREFIX = "A4:C1:38" +SCAN_INTERVAL_MINUTES = 1 +debug_output = False +bind_ip = "0.0.0.0" +api_port = 5000 +redis_port = 6379 +enable_redis = True + +# --- This will store friendly names for MAC addresses ----------------- +comment_lookup = +[ + { + "mac": "A4:C1:38:00:11:10", + "comment": "Living Room" + }, + { + "mac": "A4:C1:38:00:11:11", + "comment": "Main Bedroom" + } +] \ No newline at end of file diff --git a/redis.py b/redis.py new file mode 100644 index 0000000..f7e6a7f --- /dev/null +++ b/redis.py @@ -0,0 +1,20 @@ + +import redis +import json +from config.py import * + +#################################################### +### Redis Functions +#################################################### +if enable_redis: + r = redis.Redis(host=bind_ip, port=redis_port) + +def update_redis(data): + # push data to redis + # Publish to the Redis channel that the WS server is listening on + r.publish('temp_hum', json.dumps(data)) + if debug_output: + print("=== Stats Redis Update ===") + print(json.dumps(data, indent=2)) + return True + diff --git a/scanner.py b/scanner.py index 9fcf618..77be153 100644 --- a/scanner.py +++ b/scanner.py @@ -52,6 +52,9 @@ def update_cache() -> None: if sensor.name == sensor_datum['name']: sensor.add_or_update(key="temp", value=sensor_datum['temp']) sensor.add_or_update(key="humidity", value=sensor_datum['humidity']) + + if enable_redis: + update_redis(get_sensor_details()) def scan_macs(): new_macs = asyncio.run(_scan_once())