-
Notifications
You must be signed in to change notification settings - Fork 3
/
dblogger.py
186 lines (158 loc) · 4.93 KB
/
dblogger.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
import time
writestart = time.time()
format = '%x %X'
import csv
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read('brybus.cfg')
serialport = cfg.get('brybus','serialport')
database = cfg.get('db','database')
mysql_host = cfg.get('db','mysql_host')
mysql_user = cfg.get('db','mysql_user')
mysql_pass = cfg.get('db','mysql_pass')
mysql_db = cfg.get('db','mysql_db')
sqlitefile = cfg.get('db','sqlitefile')
if database == 'mysql':
try:
import _mysql
db=_mysql.connect(mysql_host,mysql_user,mysql_pass,mysql_db)
schema_sql = """
CREATE TABLE if not exists data (
ts DATETIME NOT NULL,
request VARCHAR(25) NULL DEFAULT NULL,
response VARCHAR(350) NULL DEFAULT NULL,
INDEX ts (ts)
);
"""
schema_sql2 = """
CREATE TABLE if not exists realtime (
ts DATETIME NULL DEFAULT NULL,
request VARCHAR(25) NULL DEFAULT NULL,
response VARCHAR(350) NULL DEFAULT NULL,
UNIQUE INDEX request_idx (request)
);
"""
db.query(schema_sql)
db.query(schema_sql2)
except _mysql.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
quit()
elif database == 'sqlite':
try:
import sqlite3
dbh=sqlite3.connect(sqlitefile, isolation_level=None)
db = dbh.cursor();
schema_sql = """
begin;
create table if not exists data (
ts timestamp not null,
request varchar(25),
response varchar(350)
);
create index if not exists ts_index on data(ts);
commit;""";
db.executescript(schema_sql)
except sqlite3.Error, e:
print "sqlite error: %s" % (e.args[0])
quit()
import brybus
ByteToHex = brybus.ByteToHex
HexToByte = brybus.HexToByte
def scantable():
#make queue for 3b table
scan_q = brybus.writequeue()
print "Building Queue"
#table 3c isn't complete so use this array of valid rows
table_3c = (
'200130010300000B003C01',
'200130010300000B003C03',
'200130010300000B003C04',
'200130010300000B003C05',
'200130010300000B003C06',
'200130010300000B003C08',
'200130010300000B003C09',
'200130010300000B003C0A',
'200130010300000B003C0B',
'200130010300000B003C0C',
'200130010300000B003C0D',
'200130010300000B003C0E',
'200130010300000B003C0F',
'200130010300000B003C10',
'200130010300000B003C11',
'200130010300000B003C12',
'200130010300000B003C13',
'200130010300000B003C14',
'200130010300000B003C1E',
'200130010300000B003C1F',
'200130010300000B003C28',
'200130010300000B003C29'
)
for r in range(1,16):
reg = '00' + '3B' + "{0:02X}".format(r)
f = brybus.frame(reg,'C','2001','3001','0B')
scan_q.pushframe(f)
#use the list above
for r in table_3c:
f = brybus.frame(r,'S')
scan_q.pushframe(f)
for r in range(1,4):
reg = '00' + '3D' + "{0:02X}".format(r)
f = brybus.frame(reg,'C','2001','3001','0B')
scan_q.pushframe(f)
return scan_q
def db_insert(head,data):
if database == 'mysql':
query = "insert into data values (now(),'"+head+"','"+data+"')"
db.query(query)
db_update(head,data)
elif database == 'sqlite':
query = "insert into data values (datetime('now'),'"+head+"','"+data+"')"
db.execute(query)
def db_update(head,data):
if database == 'mysql':
query = "insert into realtime (ts,request,response) values (now(),'"+head+"','"+data+"') ON DUPLICATE KEY UPDATE ts=VALUES(ts), response=VALUES(response)"
db.query(query)
#=======main========
q = scantable()
#q.printqueue()
#setup the stream and bus
s = brybus.stream('S',serialport)
b = brybus.bus(s)
table=[]
db_insert("START","START")
while(1):
#get write frame and write it
#write blocks, writes if the timeout passes, but returns without writing if data is received on the serial port
wf_raw = q.writeframe()
w = b.write(wf_raw)
f = brybus.frame(b.read(),"B")
#if w==1:
#print "write", q.printstatus()
#print wf.dst,wf.src,wf.len,wf.func,wf.data,wf.crc
#print f.dst,f.src,f.len,f.func,f.data,f.crc
#check the frame that was read against the queue to match the response with the request
q.checkframe(f)
#test for end of queue, then restart the queue
if q.writeframe() == '':
print "Write Queue Done. Seconds Elapsed:",(time.time()-writestart)
writestart = time.time()
for k,v in q.queue.iteritems():
v.done = False
found = 0
for row in table:
if row[0] == ByteToHex(f.raw[0:11]):
if row[1] == f.data[6:]:
1==1 #no change
db_update(ByteToHex(f.raw[0:11]),f.data[6:])
#print "NC", time.strftime(format), ByteToHex(f.raw[0:11]),f.data[6:]
else:
row[1]=f.data[6:]
db_insert(ByteToHex(f.raw[0:11]),f.data[6:])
#db.query(query)
print " C", time.strftime(format), ByteToHex(f.raw[0:11]),f.data[6:]
found=1
if found==0 and (f.func in ('0C','06')):
table.append([ByteToHex(f.raw[0:11]),f.data[6:]])
db_insert(ByteToHex(f.raw[0:11]),f.data[6:])
print " A",time.strftime(format), ByteToHex(f.raw[0:11]),f.data[6:]