-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_sqm_data.py
executable file
·43 lines (34 loc) · 1.26 KB
/
read_sqm_data.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
#!/usr/bin/env python
import psycopg2
import re
import socket
import time
from settings import SQM_IP, SQM_PORT, DB_CONNECTION, DB_TABLE, INTERVALL
# r, 00.00m,0000722291Hz,0000000000c,0000000.000s, 031.5C
prog = re.compile(r'(\d+\.\d+)m.*?(\d+)Hz.*?(\d+)c.*?(\d+\.\d+)s.*?(\d+\.\d+)C')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((SQM_IP, SQM_PORT))
conn = psycopg2.connect(DB_CONNECTION)
cur = conn.cursor()
while 1:
s.sendall('rx')
string = s.recv(1024)
match = prog.search(string)
if match:
try:
values = {
'magnitude': float(match.group(1)),
'frequency': int(match.group(2)),
'counts': int(match.group(3)),
'period': float(match.group(4)),
'temperature': float(match.group(5))
}
if values['magnitude'] > 0:
insert_stmt = 'INSERT INTO %s ' % DB_TABLE
insert_stmt += '(magnitude, frequency, counts, period, temperature) '
insert_stmt += 'VALUES (%(magnitude)s, %(frequency)s, %(counts)s, %(period)s, %(temperature)s)'
cur.execute(insert_stmt, values)
conn.commit()
except ValueError:
pass
time.sleep(INTERVALL)