-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_motion_client.py
executable file
·80 lines (67 loc) · 2.42 KB
/
capture_motion_client.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python3
import argparse
import json
import os
import socket
import struct
import sys
from motion_logger import MotionLogger
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Connect to message bus and save motion data to csv file.")
parser.add_argument("-t","--ts-id", help="Id of the test subject performing the exercise", required=True)
parser.add_argument("-e","--exercise", help="Name of exercise", required=True)
parser.add_argument("-m","--mode", help="Mode code number representing an execution type "
"(for instance an error type or correct)", required=True)
parser.add_argument("-n","--num", help="Sample number", required=True)
args = parser.parse_args()
ts_dir = "ts" + args.ts_id
if not os.path.exists(ts_dir):
print("Creating directory {}...".format(ts_dir))
os.makedirs(ts_dir)
filepath = ts_dir + "/" + "_".join([args.exercise, args.mode, args.num])
print("Saving to file {}".format(filepath))
# Create UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server_address = '/tmp/sensor_consumer'
print("Connecting to message bus at {}".format( server_address ) )
header_length = struct.calcsize("!l")
try:
sock.connect(server_address)
except socket.error:
print(socket.error)
sys.exit(1)
else:
print("Connected to message bus")
logger = MotionLogger(filepath)
try:
while True:
data = None
tmp_sz = sock.recv( header_length )
if len(tmp_sz) == 0:
print("Connection dropped! Exiting")
sys.exit(1)
size = int(struct.unpack("!l", tmp_sz)[0])
data = sock.recv( size )
while size-len(data) > 0:
tmp = sock.recv(size-len(data))
if len(tmp) > 0:
data += tmp
else:
print("Connection dropped!")
sys.exit(1)
deserialized_data = json.loads(data.decode('utf-8'))
logger.addData(deserialized_data)
except KeyboardInterrupt:
print("\nExiting...")
logger.close()
sock.close()
sys.exit(1)
except Exception as e:
raise
logger.close()
print("Exit: {}".format(e.args))
sock.close()
sys.exit(1)
else:
logger.close()
sock.close()