Skip to content

Commit 05c01af

Browse files
committed
fixed logging, archiving of logs and added preliminary db support
1 parent 4314156 commit 05c01af

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

podcomm/definitions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
POD_FILE = "data/pod"
99
POD_FILE_SUFFIX = ".json"
1010
POD_LOG_SUFFIX = ".log"
11+
POD_DB_SUFFIX = ".db"
1112
LOG_PATH = "./data"
1213
OMNIPY_LOGGER = "OMNIPY"
1314
OMNIPY_LOGFILE = "data/omnipy.log"

podcomm/pod.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .definitions import *
22
import simplejson as json
3-
from datetime import datetime
3+
import time
44
import sqlite3
55

66
class Pod:
@@ -64,6 +64,7 @@ def __init__(self):
6464
self.var_insertion_date = None
6565

6666
self.path = None
67+
self.path_db = None
6768
self.log_file_path = None
6869

6970
self.last_command = None
@@ -76,27 +77,33 @@ def __init__(self):
7677

7778
def Save(self, save_as = None):
7879
if save_as is not None:
79-
self.path = save_as
80+
self.path = save_as + POD_FILE_SUFFIX
8081
self.log_file_path = save_as + POD_LOG_SUFFIX
82+
self.path_db = save_as + POD_DB_SUFFIX
8183

8284
if self.path is None:
83-
self.path = "./unnamed_pod.json"
84-
self.log_file_path = "./unnamed_pod.log"
85+
self.path = POD_FILE + POD_FILE_SUFFIX
86+
self.log_file_path = POD_FILE + POD_LOG_SUFFIX
87+
self.path_db = POD_FILE + POD_DB_SUFFIX
8588

8689
self.log()
8790

8891
with open(self.path, "w") as stream:
8992
json.dump(self.__dict__, stream, indent=4, sort_keys=True)
9093

9194
@staticmethod
92-
def Load(path, log_file_path=None):
95+
def Load(path, log_file_path=None, db_path=None):
9396
if log_file_path is None:
94-
log_file_path = path + POD_LOG_SUFFIX
97+
log_file_path = POD_FILE + POD_LOG_SUFFIX
98+
99+
if db_path is None:
100+
db_path = POD_FILE + POD_DB_SUFFIX
95101

96102
with open(path, "r") as stream:
97103
d = json.load(stream)
98104
p = Pod()
99105
p.path = path
106+
p.path_db = db_path
100107
p.log_file_path = log_file_path
101108

102109
p.id_lot = d.get("id_lot", None)
@@ -176,21 +183,31 @@ def __str__(self):
176183
return json.dumps(self.__dict__, indent=4, sort_keys=True)
177184

178185
def log(self):
179-
pass
180-
181-
def _get_conn(self):
182-
conn = sqlite3.connect(self.log_file_path)
183-
sql = """ CREATE TABLE IF NOT EXISTS pod_history (
184-
timestamp real,
185-
pod_state integer, pod_minutes integer, pod_last_command text,
186-
insulin_delivered real, insulin_canceled real, insulin_reservoir real
187-
) """
188-
189-
c = conn.cursor()
190-
c.execute(sql)
191-
return conn
192-
193-
def _sql(self, sql):
194-
conn = sqlite3.connect(self.log_file_path)
195-
c = conn.cursor()
196-
c.execute(sql)
186+
try:
187+
with open(self.log_file_path, "a") as stream:
188+
json.dump(self.__dict__, stream, sort_keys=True)
189+
except:
190+
getLogger().exception("Error while writing to log file")
191+
192+
try:
193+
conn = sqlite3.connect(self.path_db)
194+
with conn:
195+
sql = """ CREATE TABLE IF NOT EXISTS pod_history (
196+
timestamp real,
197+
pod_state integer, pod_minutes integer, pod_last_command text,
198+
insulin_delivered real, insulin_canceled real, insulin_reservoir real
199+
) """
200+
201+
c = conn.cursor()
202+
c.execute(sql)
203+
204+
sql = """ INSERT INTO pod_history (timestamp, pod_state, pod_minutes, pod_last_command,
205+
insulin_delivered, insulin_canceled, insulin_reservoir)
206+
VALUES(?,?,?,?,?,?,?) """
207+
208+
vals = (time.time(), self.state_progress, self.state_active_minutes,
209+
str(self.last_command), self.insulin_delivered, self.insulin_canceled, self.insulin_reservoir)
210+
211+
c.execute(sql, vals)
212+
except:
213+
getLogger().exception("Error while writing to database")

restapi.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from podcomm.pod import Pod
1515
from podcomm.pr_rileylink import RileyLink
1616
from podcomm.definitions import *
17+
from logging import FileHandler
1718

1819
g_key = None
1920
g_pod = None
@@ -40,10 +41,11 @@ def _get_pod():
4041
try:
4142
if g_pod is None:
4243
if os.path.exists(POD_FILE + POD_FILE_SUFFIX):
43-
g_pod = Pod.Load(POD_FILE + POD_FILE_SUFFIX, POD_FILE + POD_LOG_SUFFIX)
44+
g_pod = Pod.Load(POD_FILE + POD_FILE_SUFFIX, POD_FILE + POD_LOG_SUFFIX, POD_FILE + POD_DB_SUFFIX)
4445
else:
4546
g_pod = Pod()
4647
g_pod.path = POD_FILE + POD_FILE_SUFFIX
48+
g_pod.path_db = POD_FILE + POD_DB_SUFFIX
4749
g_pod.log_file_path = POD_FILE + POD_LOG_SUFFIX
4850
g_pod.Save()
4951
return g_pod
@@ -63,11 +65,13 @@ def _get_pdm():
6365
return None
6466

6567

66-
def _flush_memory_handler(logger):
68+
def _flush_handlers(logger):
6769
for handler in getLogger().handlers:
6870
if isinstance(handler, MemoryHandler):
6971
handler.flush()
70-
72+
if isinstance(handler, FileHandler):
73+
handler.flush()
74+
handler.close()
7175

7276
def _archive_pod():
7377
global g_pod
@@ -77,13 +81,16 @@ def _archive_pod():
7781
g_pdm = None
7882
archive_name = None
7983
archive_suffix = datetime.utcnow().strftime("_%Y%m%d_%H%M%S")
84+
8085
if os.path.isfile(POD_FILE + POD_FILE_SUFFIX):
8186
archive_name = os.rename(POD_FILE + POD_FILE_SUFFIX, POD_FILE + archive_suffix + POD_FILE_SUFFIX)
8287
if os.path.isfile(POD_FILE + POD_LOG_SUFFIX):
8388
os.rename(POD_FILE + POD_LOG_SUFFIX, POD_FILE + archive_suffix + POD_LOG_SUFFIX)
89+
if os.path.isfile(POD_FILE + POD_DB_SUFFIX):
90+
os.rename(POD_FILE + POD_DB_SUFFIX, POD_FILE + archive_suffix + POD_DB_SUFFIX)
8491

85-
_flush_memory_handler(getLogger())
86-
_flush_memory_handler(get_packet_logger())
92+
_flush_handlers(getLogger())
93+
_flush_handlers(get_packet_logger())
8794

8895
if os.path.isfile(OMNIPY_PACKET_LOGFILE + OMNIPY_LOGFILE_SUFFIX):
8996
os.rename(OMNIPY_PACKET_LOGFILE + OMNIPY_LOGFILE_SUFFIX, OMNIPY_PACKET_LOGFILE + archive_suffix + OMNIPY_LOGFILE_SUFFIX)
@@ -215,7 +222,7 @@ def archive_pod():
215222
_verify_auth(request)
216223
pod = Pod()
217224
_archive_pod()
218-
pod.Save(POD_FILE + POD_FILE_SUFFIX)
225+
pod.Save()
219226

220227
def ping():
221228
return {"pong": None}
@@ -262,7 +269,7 @@ def new_pod():
262269
pod.radio_address = _get_pdm_address(45000)
263270

264271
_archive_pod()
265-
pod.Save(POD_FILE + POD_FILE_SUFFIX)
272+
pod.Save()
266273

267274
def activate_pod():
268275
_verify_auth(request)
@@ -271,7 +278,7 @@ def activate_pod():
271278
if pod.state_progress >= PodProgress.Running:
272279
pod = Pod()
273280
_archive_pod()
274-
pod.Save(POD_FILE + POD_FILE_SUFFIX)
281+
pod.Save()
275282

276283
pdm = _get_pdm()
277284

0 commit comments

Comments
 (0)