pickle test 2 from intern
This commit is contained in:
1 parent
d809a109e4
commit
0e58f6b73a
4 files changed
+58
-11
No files matched your search
+50
-3
@@ -2,6 +2,8 @@ import asyncio
|
||||
import threading
|
||||
import time
|
||||
import redis
|
||||
import pickle
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from zoneinfo import ZoneInfo # Python 3.9+
|
||||
from bleak import BleakScanner, BleakClient
|
||||
@@ -16,9 +18,53 @@ _mac_cache: Set[str] = set()
|
||||
_lock = threading.Lock()
|
||||
_sensors_cache = []
|
||||
|
||||
# other vars
|
||||
MI_SERVICE_UUID = "ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"
|
||||
TEMP_CHAR_UUID = "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6"
|
||||
# ----------------------------------------------------------------------
|
||||
# Pickle persistence helpers
|
||||
# ----------------------------------------------------------------------
|
||||
STATE_FILE = "scanner_state.pkl"
|
||||
|
||||
def save_state() -> None:
|
||||
"""
|
||||
Persist the MAC cache and sensor objects to disk.
|
||||
Called automatically after each cache refresh and at program exit.
|
||||
"""
|
||||
with _lock:
|
||||
state = {
|
||||
"mac_cache": _mac_cache,
|
||||
"sensors": _sensors_cache,
|
||||
}
|
||||
try:
|
||||
with open(STATE_FILE, "wb") as f:
|
||||
pickle.dump(state, f, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
if debug_output:
|
||||
print("[Scanner] State persisted to", STATE_FILE)
|
||||
except Exception as exc:
|
||||
# Don't crash the app – just log the error
|
||||
print("[Scanner] Warning: could not persist state:", exc)
|
||||
|
||||
def load_state() -> None:
|
||||
"""
|
||||
Load the MAC cache and sensor objects from disk if the file exists.
|
||||
Called automatically on import.
|
||||
"""
|
||||
if not os.path.exists(STATE_FILE):
|
||||
return
|
||||
try:
|
||||
with open(STATE_FILE, "rb") as f:
|
||||
state = pickle.load(f)
|
||||
with _lock:
|
||||
# NOTE: we replace the lists/sets to avoid mutating old references
|
||||
_mac_cache.clear()
|
||||
_mac_cache.update(state.get("mac_cache", []))
|
||||
global _sensors_cache
|
||||
_sensors_cache = state.get("sensors", [])
|
||||
if debug_output:
|
||||
print("[Scanner] Loaded state from", STATE_FILE)
|
||||
except Exception as exc:
|
||||
print("[Scanner] Warning: could not load state:", exc)
|
||||
|
||||
# Load any persisted state on module import
|
||||
load_state()
|
||||
|
||||
# Redis Functions
|
||||
r = redis.Redis(host=bind_ip, port=redis_port)
|
||||
@@ -72,6 +118,7 @@ def update_cache() -> None:
|
||||
# if redis is enabled, push full details to the redis server
|
||||
if enable_redis:
|
||||
update_redis(get_sensor_details())
|
||||
save_state()
|
||||
|
||||
# this helper function scans for MACs
|
||||
def scan_macs():
|
||||
|
||||
Reference in new issue
Block a user