-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog_sqlite3.py
executable file
·63 lines (54 loc) · 1.36 KB
/
log_sqlite3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
import numbers
import sqlite3
import sys
import os.path
import sage_boiler
boiler = None
if os.path.exists(sys.argv[1]):
import serial
serial_port=serial.Serial(port=sys.argv[1], baudrate=38400)
boiler = sage_boiler.Sage2Boiler(1, serial=serial_port)
else:
boiler = sage_boiler.Sage2Boiler(1, sys.argv[1])
readings = []
for key in dir(boiler):
this = getattr(boiler, key)
if isinstance(this, sage_boiler.Sage2Reading):
readings.append({
'boiler': boiler.boiler,
'register': this.register,
'raw_value': this.raw_value,
'numeric_value': this.value \
if isinstance(this.value, numbers.Number) \
else this.raw_value,
'title': this.title,
'value': this.value
})
db_con = sqlite3.connect('sage_boiler.sqlite3')
db_cur = db_con.cursor()
db_cur.execute('''
CREATE TABLE IF NOT EXISTS sage2_reading (
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
boiler INTEGER NOT NULL,
register INTEGER NOT NULL,
raw_value INTEGER NOT NULL,
value INTEGER NULL,
title TEXT NOT NULL,
description TEXT NULL
);
''')
db_cur.executemany('''
INSERT INTO sage2_reading VALUES (
CURRENT_TIMESTAMP,
:boiler,
:register,
:raw_value,
:numeric_value,
:title,
:value
);
''', readings)
db_con.commit()
db_con.close()
print(boiler.tabulate())