-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemetry.py
140 lines (122 loc) · 4.21 KB
/
telemetry.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""https://www.influxdata.com/blog/getting-started-python-influxdb/"""
"""http://localhost:8086/orgs/d3cf7dd0f1213a5e/load-data/client-libraries/python"""
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
import serial
import logging
from util import get_cfg
# for uplink
import uplink.uplink as uplink
""" Serial connection lost. Reconnecting...
Traceback (most recent call last):
File "/Users/curtisbucher/Desktop/Clubs/CPSS-GUI/telemetry.py", line 126, in <module>
main()
File "/Users/curtisbucher/Desktop/Clubs/CPSS-GUI/telemetry.py", line 69, in main
relay_data(cfg, logger, write_api, ser, )
File "/Users/curtisbucher/Desktop/Clubs/CPSS-GUI/telemetry.py", line 98, in relay_data
vars = [ float(v) for v in line.split(b',')]
UnboundLocalError: local variable 'line' referenced before assignment"""
#defining the order of the items in packet to be sent
PACKET_ITEMS = [
"id",
"pressure",
"ASL",
"AGL",
"smoothAGL",
"temperature",
"accelX",
"accelY",
"accelZ",
"gyroX",
"gyroY",
"gyroZ",
"velocityX",
"longitude",
"latitude",
"altitude",
"satellites",
"state",
]
## generaitng format string from indices
fstring = "telemetry "
for i, var in enumerate(PACKET_ITEMS):
fstring += var + "={{{0}}},".format(i)
fstring = fstring[:-1]
# TODO: Make not global
first_id = -1 ## the id of the first packet sent
total_packets = 0
sent_packets = 0
def main():
""" Main function """
cfg = get_cfg()
# Loading logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Setting up console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
logger.addHandler(console_handler)
# Setting up file handler
file_handler = logging.FileHandler(cfg.logfile, mode='w')
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)
# Starting uplink
# setting up serial connection
with serial.Serial(cfg.port, cfg.rate, timeout=1) as ser:
# Setting up InfluxDB client
with InfluxDBClient(url=cfg.url, token=cfg.token, org=cfg.org) as client:
logger.info("Starting telemetry relay")
write_api = client.write_api(write_options=SYNCHRONOUS)
try:
while( True ):
relay_data(cfg, logger, write_api, ser, )
except KeyboardInterrupt:
logger.info("\nExiting...")
logging.shutdown()
# for calculating packet loss
def relay_data(cfg, logger=None, db_write_api=None, ser=None):
""" Loads data from serial port into Influx Database"""
#TODO: Make not global
global total_packets
global sent_packets
global first_id
# getting serial data
try:
line = ser.readline()
total_packets += 1
except Exception:
logger.error("Serial connection lost. Reconnecting...")
# trying to connect again
try:
ser = serial.Serial(cfg.port, cfg.rate, timeout=1)
except Exception:
logger.critical("Could not reconnect. Exiting...")
return
# parsing serial data
try:
vars = [ float(v) for v in line.split(b',')]
vars[0] = int(vars[0]) #id
except ValueError as e:
logger.warning("{0}:{1}".format(line, e))
vars = []
# sending data to influx
if (vars and len(vars) == len(PACKET_ITEMS)):
# calculating packet drop (last id - first id)
if( first_id == -1 ):
first_id = vars[0]
total_packets = vars[0] - first_id + 1
log = fstring.format(*vars)
# writing sensor data
db_write_api.write(cfg.bucket, cfg.org, log)
# writing status data
lost = total_packets - sent_packets
db_write_api.write(cfg.bucket, cfg.org,
"telemetry percent_dropped={}".format(lost/total_packets))
sent_packets += 1
logger.info(log)
else:
lost = total_packets - sent_packets
logger.warning("Bad data. Total Loss: {} packets".format(lost))
logger.debug("Expected {} items, got {}. Received: {}".format(len(PACKET_ITEMS), len(vars), vars))
if __name__ == "__main__":
main()