aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2022-10-31 20:43:40 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2022-10-31 20:43:40 +0100
commitf5932782310806150aa3dceeadacaeae448ca702 (patch)
tree4352016a0ede1f78ffdffd938b2326d97bbdcb6a
parent1546176881719a4fa7e63109f691eba32dd7bf85 (diff)
downloadglutte-serial-web-f5932782310806150aa3dceeadacaeae448ca702.tar.gz
glutte-serial-web-f5932782310806150aa3dceeadacaeae448ca702.tar.bz2
glutte-serial-web-f5932782310806150aa3dceeadacaeae448ca702.zip
Fix file not found
-rw-r--r--config.py.dist4
-rw-r--r--serialrx.py20
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: