X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Fevstore.py;fp=loctrkd%2Fevstore.py;h=b0265055dec85cd55785f626e6ae690aeb1599f5;hb=ea3dc3f2096472d502d376e13050a59f97efd4a8;hp=da34cb9fdd1bfca083a44f03a9b4ecc738810fab;hpb=f24ba18e34cbfe34bf648459f0e80bd874334fe4;p=loctrkd.git diff --git a/loctrkd/evstore.py b/loctrkd/evstore.py index da34cb9..b026505 100644 --- a/loctrkd/evstore.py +++ b/loctrkd/evstore.py @@ -1,20 +1,32 @@ """ sqlite event store """ +from datetime import datetime +from json import dumps from sqlite3 import connect, OperationalError -from typing import Any, List, Tuple +from typing import Any, Dict, List, Tuple -__all__ = "fetch", "initdb", "stow" +__all__ = "fetch", "initdb", "stow", "stowloc" DB = None -SCHEMA = """create table if not exists events ( +SCHEMA = ( + """create table if not exists events ( tstamp real not null, imei text, peeraddr text not null, is_incoming int not null default TRUE, proto text not null, packet blob -)""" +)""", + """create table if not exists reports ( + imei text, + devtime text not null, + accuracy real, + latitude real, + longitude real, + remainder text +)""", +) def initdb(dbname: str) -> None: @@ -26,7 +38,8 @@ def initdb(dbname: str) -> None: is_incoming int not null default TRUE""" ) except OperationalError: - DB.execute(SCHEMA) + for stmt in SCHEMA: + DB.execute(stmt) def stow(**kwargs: Any) -> None: @@ -54,6 +67,30 @@ def stow(**kwargs: Any) -> None: DB.commit() +def stowloc(**kwargs: Dict[str, Any]) -> None: + assert DB is not None + parms = { + k: kwargs.pop(k) if k in kwargs else v + for k, v in ( + ("imei", None), + ("devtime", str(datetime.now())), + ("accuracy", None), + ("latitude", None), + ("longitude", None), + ) + } + parms["remainder"] = dumps(kwargs) + DB.execute( + """insert or ignore into reports + (imei, devtime, accuracy, latitude, longitude, remainder) + values + (:imei, :devtime, :accuracy, :latitude, :longitude, :remainder) + """, + parms, + ) + DB.commit() + + def fetch( imei: str, matchlist: List[Tuple[bool, str]], backlog: int ) -> List[Tuple[bool, float, str, bytes]]: