-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkTpms.py
executable file
·161 lines (138 loc) · 6.02 KB
/
kTpms.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python3
"""
kTpms -- Sniff anything TPMS
"""
import ast
import os
import psycopg2
import sh
import time
from configparser import ConfigParser
from easyThread import Backgrounder
class Tpms(object):
"""A base class for handling tpms
"""
def __init__(self):
## Static string for now --
self.tString = 'rtl_433 -M level -f 315000000 -F json:log.json -T0 -R 110 -Y minlevel=-0.5'
## db creds
class Foo(object):
pass
self.conf = Foo()
parser = ConfigParser()
parser.read('system.conf')
self.conf.dbUser = parser.get('creds', 'dbUser')
self.conf.dbPass = parser.get('creds', 'dbPass')
self.conf.dbHost = parser.get('creds', 'dbHost')
self.conf.dbName = parser.get('creds', 'dbName')
self.conf.devid = int(parser.get('creds', 'devid'))
self.conf.seenMax = int(parser.get('prop', 'seenMax'))
## Connect to the db
self.con, self.db, self.dbName = self.pgsqlPrep()
def pgsqlPrep(self):
""" Connect and prep the pgsql db"""
try:
cStr = f"dbname='{self.conf.dbName}' user='{self.conf.dbUser}' host='{self.conf.dbHost}' password='{self.conf.dbPass}'"
con = psycopg2.connect(cStr)
con.autocommit = True
db = con.cursor()
## db prep
db.execute("""
CREATE TABLE IF NOT EXISTS tpms(dev_timestamp TIMESTAMPTZ,
model TEXT,
id TEXT,
status INT,
battery_ok INT,
counter INT,
failed TEXT,
pressure_kpa REAL,
temperature_c REAL,
freq1 REAL,
freq2 REAL,
rssi REAL,
snr REAL,
noise REAL);
""")
except Exception as E:
print (f'I am unable to connect to the database {self.conf.dbName}')
print(E)
sys.exit(1)
return (con, db, self.conf.dbName)
def tpmsBackground(self):
"""Holds the instance of rtl_433
Will be redone with a nicer shell in the future
"""
os.system(tpms.tString)
if __name__ == '__main__':
## os prep
try:
os.remove('log.json')
except:
pass
## Grab tpms
tpms = Tpms()
## Add our function to Backgrounder
Backgrounder.theThread = tpmsBackground
## Instantiate using #s other than defaults
bg = Backgrounder()
## Start the work
bg.easyLaunch()
## Read the stream
tail = sh.tail('-f', './log.json', _iter = True)
while True:
newTpms = tail.next()
## Evaluate output from rtl_433
try:
tLog = ast.literal_eval(newTpms)
### CLEARED HOT TO LOG
try:
## Update the db
tpms.db.execute(f"""
INSERT INTO tpms (dev_timestamp,
model,
id,
status,
battery_ok,
counter,
failed,
pressure_kpa,
temperature_c,
freq1,
freq2,
rssi,
snr,
noise)
VALUES (%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s);
""", (tLog.get('time'),
tLog.get('model'),
tLog.get('id'),
tLog.get('status'),
tLog.get('battery_ok'),
tLog.get('counter'),
tLog.get('failed'),
tLog.get('pressure_kPa'),
tLog.get('temperature_C'),
tLog.get('freq1'),
tLog.get('freq2'),
tLog.get('rssi'),
tLog.get('snr'),
tLog.get('noise')))
except Exception as E:
print('inner ~~', E)
except Exception as E:
print('outer ~~', E)
print(tLog)
print()