Revision 1944
Added objects and some object functions to server Python script.
trunk/code/projects/calibration_platform/server/cal_sta_server.py | ||
---|---|---|
1 | 1 |
#!/usr/bin/python |
2 |
import sys, serial |
|
3 |
import sqlite3 |
|
2 |
import sys, serial, MySQLdb |
|
4 | 3 |
|
5 | 4 |
class Station(serial.Serial): |
6 | 5 |
def __init__(self, serial_path='/dev/arduino', baudrate=19200): |
... | ... | |
17 | 16 |
db_cursor.execute('insert into %s (value) values (%s)' % t) |
18 | 17 |
db_conn.commit() |
19 | 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 |
|
|
20 | 34 |
# SQLite3 database setup |
21 |
db_path = '/home/colony/calibration_station/cal_sta_database.db' |
|
22 |
ST_conn = sqlite3.connect(db_path) |
|
35 |
ST_conn = MySQLdb.connect(host = 'localhost', |
|
36 |
user = 'colony', |
|
37 |
passwd = 'mrbickles', |
|
38 |
db = 'calibration_station') |
|
23 | 39 |
ST_cursor = ST_conn.cursor() |
24 | 40 |
|
41 |
ST_cursor.execute('SELECT * FROM raw_data;') |
|
42 |
for row in ST_cursor: |
|
43 |
print row |
|
44 |
|
|
45 |
|
|
25 | 46 |
ST = Station() |
26 |
ST.db_register('sensor0', ST_cursor) |
|
27 |
ST.db_register('sensor1', ST_cursor) |
|
47 |
#ST.db_register('sensor0', ST_cursor)
|
|
48 |
#ST.db_register('sensor1', ST_cursor)
|
|
28 | 49 |
|
29 |
# RO_serial = serial.Serial(port='/dev/ttyUSB1', baudrate=115200) # Robot |
|
50 |
RO = Robot('/dev/ttyUSB1') |
|
51 |
|
|
30 | 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 |
|
|
31 | 62 |
if ST.inWaiting() > 0: |
32 | 63 |
try: |
33 | 64 |
msg = ST.readline()[:-1] |
... | ... | |
37 | 68 |
t = msg.split(':') |
38 | 69 |
print t |
39 | 70 |
|
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]) |
|
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])
|
|
44 | 75 |
|
45 | 76 |
|
46 | 77 |
ST_cursor.close() |
Also available in: Unified diff