11from .definitions import *
22import simplejson as json
3- from datetime import datetime
3+ import time
44import sqlite3
55
66class 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" )
0 commit comments