-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
71 lines (58 loc) · 2.42 KB
/
stats.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
from datetime import datetime
class Stats:
def __init__(self, command: str, id: int):
self.command = command
self.response = None
self.id = id
self.start_time = datetime.now()
self.end_time = None
self.duration = None
def add_response(self, response: str):
self.response = str(response)
# Calculating total time taken to execute command
self.end_time = datetime.now()
self.duration = (self.end_time-self.start_time).total_seconds()
def got_response(self):
if self.response is None:
return False
else:
return True
def get_raw_response(self):
return self.response
def numeric_response(self, data: str):
if data != None:
num_val = ''.join(i for i in data if i.isdigit() or i=='-' or i=='.')
return num_val
else:
return 0
def int_response(self, data: str):
return int(self.numeric_response(data))
def float_response(self, data: str):
return float(self.numeric_response(data))
def attitude_response(self):
raw_att = self.response.split(';')
att_data = (self.int_response(raw_att[0]), self.int_response(raw_att[1]), self.int_response(raw_att[2]))
return att_data
def acceleration_response(self):
raw_acc = self.response.split(';')
acc_data = (self.float_response(raw_acc[0]), self.float_response(raw_acc[1]), self.float_response(raw_acc[2]))
return acc_data
def temp_response(self):
raw_temp = self.response.split('~')
temp = (self.int_response(raw_temp[0]) + self.int_response(raw_temp[1]))/2
return temp
def get_response(self):
if 'attitude?' in self.command:
return self.attitude_response()
elif 'acceleration?' in self.command:
return self.acceleration_response()
elif 'temp?' in self.command:
return self.temp_response()
elif 'baro?' in self.command or 'speed?' in self.command:
return self.float_response(self.response)
elif '?' not in self.command:
return self.get_raw_response()
else:
return self.int_response(self.response)
def get_stats(self):
return "id: {};command: {};response: {};start_time: {};end_time: {};duration: {}".format(self.id, self.command, self.response, self.start_time, self.end_time, self.duration)