diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-11-18 04:11:14 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-11-18 04:11:14 +0300 |
commit | 7d2f2a89ac7030dc3e13de735048b267c1b390b8 (patch) | |
tree | 20e2ca42d3b3f95f746e63c529a741eee6244aff | |
parent | bbf71ac894b3f155fbc0e74d1d17a121c68e4cc5 (diff) |
sound_sensor_server: persistent guard_enabled state
-rw-r--r-- | src/home/database/sqlite.py | 10 | ||||
-rw-r--r-- | src/home/soundsensor/server.py | 46 |
2 files changed, 46 insertions, 10 deletions
diff --git a/src/home/database/sqlite.py b/src/home/database/sqlite.py index 8f1763e..a3a1664 100644 --- a/src/home/database/sqlite.py +++ b/src/home/database/sqlite.py @@ -5,19 +5,21 @@ import logging from ..config import config, is_development_mode -def _get_database_path(name) -> str: - return os.path.join(os.environ['HOME'], '.config', name, 'bot.db') +def _get_database_path(name: str, dbname: str) -> str: + return os.path.join(os.environ['HOME'], '.config', name, f'{dbname}.db') class SQLiteBase: SCHEMA = 1 - def __init__(self, name=None, check_same_thread=False): + def __init__(self, name=None, dbname='bot', check_same_thread=False): if not name: name = config.app_name + if not dbname: + dbname = name self.logger = logging.getLogger(self.__class__.__name__) - self.sqlite = sqlite3.connect(_get_database_path(name), + self.sqlite = sqlite3.connect(_get_database_path(name, dbname), check_same_thread=check_same_thread) if is_development_mode(): diff --git a/src/home/soundsensor/server.py b/src/home/soundsensor/server.py index 0a53ae6..3e4fa03 100644 --- a/src/home/soundsensor/server.py +++ b/src/home/soundsensor/server.py @@ -3,6 +3,7 @@ import json import logging import threading +from ..database.sqlite import SQLiteBase from ..config import config from .. import http @@ -34,26 +35,60 @@ class SoundSensorHitHandler(asyncio.DatagramProtocol): pass +class Database(SQLiteBase): + SCHEMA = 1 + + def __init__(self): + super().__init__(dbname='sound_sensor_server') + + def schema_init(self, version: int) -> None: + cursor = self.cursor() + + if version < 1: + cursor.execute("CREATE TABLE IF NOT EXISTS status (guard_enabled) INTEGER NOT NULL") + cursor.execute("INSERT INTO status (guard_enabled) VALUES (-1)") + + self.commit() + + def get_guard_enabled(self) -> int: + cur = self.cursor() + cur.execute("SELECT guard_enabled FROM status LIMIT 1") + return int(cur.fetchone()[0]) + + def set_guard_enabled(self, enabled: bool) -> None: + cur = self.cursor() + cur.execute("UPDATE status SET guard_enabled=?", (int(enabled),)) + + class SoundSensorServer: def __init__(self, addr: Addr, handler_impl: Type[SoundSensorHitHandler]): self.addr = addr self.impl = handler_impl + self.db = Database() self._recording_lock = threading.Lock() self._recording_enabled = True if self.guard_control_enabled(): - if 'guard_recording_default' in config['server']: - self._recording_enabled = config['server']['guard_recording_default'] - - def guard_control_enabled(self) -> bool: + current_status = self.db.get_guard_enabled() + if current_status == -1: + self.set_recording(config['server']['guard_recording_default'] + if 'guard_recording_default' in config['server'] + else False) + else: + self.set_recording(bool(current_status), update=False) + + @staticmethod + def guard_control_enabled() -> bool: return 'guard_control' in config['server'] and config['server']['guard_control'] is True - def set_recording(self, enabled: bool): + def set_recording(self, enabled: bool, update=True): with self._recording_lock: self._recording_enabled = enabled + if update: + self.db.set_guard_enabled(enabled) def is_recording_enabled(self) -> bool: with self._recording_lock: @@ -89,4 +124,3 @@ class SoundSensorServer: asyncio.set_event_loop(asyncio.new_event_loop()) # need to create new event loop in new thread http.serve(self.addr, routes, handle_signals=False) # handle_signals=True doesn't work in separate thread - |