Statistics
| Revision:

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

History | View | Annotate | Download (2.1 KB)

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

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

    
19
class Robot(serial.Serial):
20
        def __init__(self, serial_path='/dev/ttyUSB0', baudrate=115200):
21
                try:
22
                        serial.Serial.__init__(self, serial_path, baudrate)
23
                except serial.SerialException as e:
24
                        print 'Warning: Unable to open serial communication to robot (%s)'%e
25
                        sys.exit(0)
26
        def db_register(self, sensor_name, db_cursor):
27
                t = (sensor_name,)
28
                db_cursor.execute('create table %s (value INTEGER)' % t)
29
        def db_add(self, sensor_name, db_cursor, db_conn, value):
30
                t = (sensor_name, value)
31
                db_cursor.execute('insert into %s (value) values (%s)' % t)
32
                db_conn.commit()
33

    
34
# SQLite3 database setup
35
ST_conn = MySQLdb.connect(host = 'localhost',
36
                          user = 'colony',
37
                          passwd = 'mrbickles',
38
                          db = 'calibration_station')
39
ST_cursor = ST_conn.cursor()
40

    
41
ST_cursor.execute('SELECT * FROM raw_data;')
42
for row in ST_cursor:
43
        print row
44

    
45

    
46
ST = Station()
47
#ST.db_register('sensor0', ST_cursor)
48
#ST.db_register('sensor1', ST_cursor)
49

    
50
RO = Robot('/dev/ttyUSB1')
51

    
52
while True:
53
        if RO.inWaiting() > 0:
54
                try:
55
                        msg = RO.readline()[:-1]
56
                except OSError as e:
57
                        print 'Error: %s' %e
58

    
59
                t = msg.split(':')
60
                print t
61

    
62
        if ST.inWaiting() > 0:
63
                try:
64
                        msg = ST.readline()[:-1]
65
                except OSError as e:
66
                        print 'Error: %s' %e
67

    
68
                t = msg.split(':')
69
                print t
70

    
71
#                if (len(t) == 3 and t[1] == '0'):
72
#                        ST.db_add('sensor0', ST_cursor, ST_conn, t[2])
73
#                elif (len(t) == 3 and t[1] == '1'):
74
#                        ST.db_add('sensor1', ST_cursor, ST_conn, t[2])
75

    
76

    
77
ST_cursor.close()
78
ST.close()