]> www.average.org Git - loctrkd.git/blob - gps303/evstore.py
Work with cell location data; use opencellid
[loctrkd.git] / gps303 / evstore.py
1 from logging import getLogger
2 from sqlite3 import connect
3
4 __all__ = ("initdb", "stow")
5
6 log = getLogger("gps303")
7
8 DB = None
9
10 SCHEMA = """create table if not exists events (
11     timestamp real not null,
12     imei text,
13     clntaddr text not null,
14     length int,
15     proto int not null,
16     payload blob
17 )"""
18
19
20 def initdb(dbname):
21     global DB
22     log.info('Using Sqlite3 database "%s"', dbname)
23     DB = connect(dbname)
24     DB.execute(SCHEMA)
25
26
27 def stow(clntaddr, timestamp, imei, length, proto, payload):
28     assert DB is not None
29     parms = dict(
30         zip(
31             ("clntaddr", "timestamp", "imei", "length", "proto", "payload"),
32             (str(clntaddr), timestamp, imei, length, proto, payload),
33         )
34     )
35     DB.execute(
36         """insert or ignore into events
37                 (timestamp, imei, clntaddr, length, proto, payload)
38                 values
39                 (:timestamp, :imei, :clntaddr, :length, :proto, :payload)
40         """,
41         parms,
42     )
43     DB.commit()