11"""tucache module"""
22
3+ import sqlite3
4+ import os .path
35from .webcache import WebCache
46
57class TuCache :
68 """tucache class"""
7- def __init__ (self ,auth = None ,_dir = None ,verbose = False ):
9+ def __init__ (self ,auth = None ,_dir = None ,use_sqlite = False , verbose = False ):
810 self .cache = WebCache (auth = auth ,_dir = _dir ,verbose = verbose )
9- self ._verbose = verbose
11+ self .use_sqlite = use_sqlite
12+ if self .use_sqlite :
13+ self .web_db_path = os .path .join (_dir ,"webdb.db" )
14+ self ._db = sqlite3 .connect (self .web_db_path )
15+ cur = self ._db .cursor ()
16+ _res = list (cur .execute (
17+ '''
18+ SELECT name FROM sqlite_master WHERE type="table" AND name="history"
19+ ''' ))
20+ if len (_res ) == 0 :
21+ cur .execute ('''CREATE TABLE history
22+ (unit text not null, time text not null, event int, keyId text, location text, address text, heading int, speed real,
23+ km real, run1 real,run2 real,run3 real,run4 real,runOdo real,temperature1 real,temperature2 real,
24+ input1 int,input2 int,input3 int,input4 int,input5 int,input6 int,input7 int,input8 int,input9 int,input10 int,
25+ output1 int,output2 int,output3 int,output4 int,output5 int,
26+ analogInput1 real,analogInput2 real,analogInput4 real,
27+ Input1ChangeCounter INT,Input2ChangeCounter INT,Input3ChangeCounter INT,Input4ChangeCounter INT,
28+ batteryLevel real,externalPower real,
29+ primary key (unit,time))''' )
30+ self ._db .commit ()
1031 def clean (self ):
1132 """deletes all cached data"""
1233 self .cache .clean ()
@@ -19,4 +40,7 @@ def get(self,url):
1940 return []
2041 if self .cache .dont_read_files and len (data ) == 0 :
2142 return []
43+ # if self.use_sqlite and "api.trackunit.com/public/Report/UnitHistory" in url:
44+ # cur = self.db.cursor()
45+ # _data = list(map(lambda x: (x["time"])))
2246 return data .get ('list' )
0 commit comments