diff options
-rw-r--r-- | config.py.dist | 4 | ||||
-rw-r--r-- | serialrx.py | 20 |
2 files changed, 17 insertions, 7 deletions
diff --git a/config.py.dist b/config.py.dist index 0e27155..f958a6c 100644 --- a/config.py.dist +++ b/config.py.dist @@ -3,6 +3,10 @@ BAUDRATE = 9600 LINES_TO_KEEP = 200 LAST_LINE_TO_KEEP = 1000 +# cache contains [ { 'ts': 'TIMESTMAP', 'line': 'LINE' } ] +CACHE_FILE = "/tmp/glutte_serial_web.json" +CACHE_MAX_AGE = 3600 * 24 * 7 + TELEGRAM_API_TOKEN = '' TELEGRAM_GROUP = '' TELEGRAM_REBOOT_COMMAND = './reboot.sh' diff --git a/serialrx.py b/serialrx.py index a10db0a..6d4d7a5 100644 --- a/serialrx.py +++ b/serialrx.py @@ -141,13 +141,19 @@ class SerialRX(threading.Thread): self._parser.parse_message(line) now = time.time() - if config.CACHE_FILE: - with open(config.CACHE_FILE) as fd: - hist = json.load(fd) - hist = [h for h in hist if h['ts'] + config.CACHE_MAX_AGE > now] - host.append({'ts': now, 'line': line}) - with open(config.CACHE_FILE, 'w') as fd: - json.dump(hist, fd) + try: + if config.CACHE_FILE: + try: + with open(config.CACHE_FILE) as fd: + hist = json.load(fd) + except FileNotFoundError: + hist = [] + hist = [h for h in hist if h['ts'] + config.CACHE_MAX_AGE > now] + hist.append({'ts': now, 'line': line}) + with open(config.CACHE_FILE, 'w') as fd: + json.dump(hist, fd) + except Exception as e: + print(e) self.data_lock.acquire() try: |