|
1 | 1 | """tucache module""" |
2 | 2 |
|
3 | | -import sqlite3 |
4 | | -import os.path |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from math import ceil |
5 | 5 | from .webcache import WebCache |
| 6 | +from .tuiter import ReqIter, TuIter |
| 7 | + |
| 8 | +URL_BASE = r'https://api.trackunit.com/public/' |
6 | 9 |
|
7 | 10 | class TuCache: |
8 | 11 | """tucache class""" |
9 | | - def __init__(self,auth=None,_dir=None,use_sqlite=False,verbose=False): |
| 12 | + def __init__(self,auth=None,_dir=None,verbose=False): |
10 | 13 | self.cache = WebCache(auth=auth,_dir=_dir,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() |
| 14 | + self.req_period = 30 |
| 15 | + self.tdelta_end = None |
31 | 16 | def clean(self): |
32 | 17 | """deletes all cached data""" |
33 | 18 | self.cache.clean() |
34 | | - def get(self,url): |
| 19 | + async def get_url(self,url): |
35 | 20 | """takes the data from cache if possible. otherwise data is loaded from web""" |
36 | | - data = self.cache.get(url) |
| 21 | + data = await self.cache.get(URL_BASE+url) |
37 | 22 | if self.cache.return_only_cache_files: |
38 | 23 | return [data] |
39 | 24 | if self.cache.dont_return_data: |
40 | 25 | return [] |
41 | 26 | if self.cache.dont_read_files and len(data) == 0: |
42 | 27 | 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"]))) |
46 | 28 | return data.get('list') |
| 29 | + def general_daydiff_get(self,furl,tdelta,previter=None): |
| 30 | + """returns data for timedependant requests for a given daydelta""" |
| 31 | + if self.tdelta_end is None: |
| 32 | + end = datetime.now() |
| 33 | + else: |
| 34 | + end = self.tdelta_end |
| 35 | + end = end.replace(hour=0,minute=0,second=0,microsecond=0) |
| 36 | + if isinstance(tdelta,datetime): |
| 37 | + start = end+tdelta |
| 38 | + else: |
| 39 | + irange = int(tdelta) |
| 40 | + if irange <= 0: |
| 41 | + return [] |
| 42 | + start = end-timedelta(days=irange) |
| 43 | + return self.general_time_range_get(furl,start,end,previter) |
| 44 | + def general_time_range_get(self,furl,start=None,end=None,previter=None): |
| 45 | + """returns data for timedependant requests for a start and enddate""" |
| 46 | + days = (end-start).days |
| 47 | + requests = [] |
| 48 | + for week in range(ceil(days/self.req_period)): |
| 49 | + wstart = start+timedelta(days=week*self.req_period) |
| 50 | + wend = wstart+timedelta(days=min(self.req_period,(end-wstart).days)) |
| 51 | + requests.append(furl(\ |
| 52 | + wstart.strftime("%Y-%m-%dT%H:%M:%S"),\ |
| 53 | + wend.strftime("%Y-%m-%dT%H:%M:%S"))) |
| 54 | + internal_iter = ReqIter(self,iter(requests)) |
| 55 | + if previter is None: |
| 56 | + previter = TuIter() |
| 57 | + previter.add(internal_iter) |
| 58 | + return previter,len(requests) |
| 59 | + |
| 60 | + def get_history(self,veh_id,tdelta,previter=None): |
| 61 | + """getHistory method""" |
| 62 | + return self.general_daydiff_get(lambda t1,t2: \ |
| 63 | + 'Report/UnitHistory?unitId='+veh_id+'&from='+t1+'.0000001Z&to='+t2+'.0000000Z',\ |
| 64 | + tdelta,previter) |
| 65 | + def get_candata(self,veh_id,tdelta=None,previter=None): |
| 66 | + """getCanData method""" |
| 67 | + return self.general_daydiff_get(lambda t1,t2: \ |
| 68 | + 'Report/UnitExtendedInfo?Id='+veh_id+'&from='+t1+'.0000001Z&to='+t2+'.0000000Z',\ |
| 69 | + tdelta,previter) |
0 commit comments