summaryrefslogtreecommitdiff
path: root/reader/reader.py
blob: 5868bcb0c139b8197804b6a5792ae26645a6c8af (plain)
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
#!/usr/bin/env python2

from websocket_server import WebsocketServer

import thread
import serial
import time
import json

measures = {
        'pressure_bottom': 0,
        'pressure_top': 0,
        'flexion': 0,
        'angle_x': 0,
        'angle_y': 0,
        'angle_z': 0,
        }

def server_procedure(server):
    server.run_forever()

def main():
    server = WebsocketServer(1234, '0.0.0.0')

    thread.start_new_thread(server_procedure, (server,))

    with serial.Serial('/dev/ttyACM0', 9600) as ser:
    #with open('/tmp/fifo') as ser:
        while True:
            line = ser.readline()

            if not line:
                time.sleep(0.01)
                continue

            line = line.strip()

            if line.startswith('PRESSURE_BOTTOM:'):
                measures['pressure_bottom'] = line.split('PRESSURE_BOTTOM:')[1]
            elif line.startswith('PRESSURE_TOP:'):
                measures['pressure_top'] = line.split('PRESSURE_TOP:')[1]
            elif line.startswith('FLEXION:'):
                measures['flexion'] = line.split('FLEXION:')[1]
            elif line.startswith('ANGLE_X:'):
                measures['angle_x'] = line.split('ANGLE_X:')[1]
                server.send_message_to_all(json.dumps(measures))
            elif line.startswith('ANGLE_Y:'):
                measures['angle_y'] = line.split('ANGLE_Y:')[1]
            elif line.startswith('ANGLE_Z:'):
                measures['angle_z'] = line.split('ANGLE_Z:')[1]
            else:
                print(line)



if __name__ == '__main__':
    main()