Statistics
| Revision:

root / trunk / code / projects / calibration_platform / server / cal_sta_server.py @ 1941

History | View | Annotate | Download (1.28 KB)

1
#!/usr/bin/python
2
import sys, serial
3
import sqlite3
4

    
5
class Station(serial.Serial):
6
        def __init__(self, serial_path='/dev/arduino', baudrate=19200):
7
                try:
8
                        serial.Serial.__init__(self, serial_path, baudrate)
9
                except serial.SerialException as e:
10
                        print 'Warning: Unable to open serial communication to station (%s)'%e
11
                        sys.exit(0)
12
        def db_register(self, sensor_name, db_cursor):
13
                t = (sensor_name,)
14
                db_cursor.execute('create table %s (value INTEGER)' % t)
15
        def db_add(self, sensor_name, db_cursor, db_conn, value):
16
                t = (sensor_name, value)
17
                db_cursor.execute('insert into %s (value) values (%s)' % t)
18
                db_conn.commit()
19

    
20
# SQLite3 database setup
21
db_path = '/home/colony/calibration_station/cal_sta_database.db'
22
ST_conn = sqlite3.connect(db_path)
23
ST_cursor = ST_conn.cursor()
24

    
25
ST = Station()
26
ST.db_register('sensor0', ST_cursor)
27
ST.db_register('sensor1', ST_cursor)
28

    
29
#        RO_serial = serial.Serial(port='/dev/ttyUSB1', baudrate=115200)   # Robot
30
while True:
31
        if ST.inWaiting() > 0:
32
                try:
33
                        msg = ST.readline()[:-1]
34
                except OSError as e:
35
                        print 'Error: %s' %e
36

    
37
                t = msg.split(':')
38
                print t
39

    
40
                if (len(t) == 3 and t[1] == '0'):
41
                        ST.db_add('sensor0', ST_cursor, ST_conn, t[2])
42
                elif (len(t) == 3 and t[1] == '1'):
43
                        ST.db_add('sensor1', ST_cursor, ST_conn, t[2])
44

    
45

    
46
ST_cursor.close()
47
ST.close()