-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_reader.py
47 lines (41 loc) · 1.51 KB
/
raw_reader.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
import os
import time
import logging
from datetime import datetime
class RawReader:
def __init__(self, filename, baudrate=115200, influx=None):
self.filename = filename
self.influx = influx
def run_safe(self):
""" Runs the RawReader and restarts it if there is an exception """
while True:
try:
self.run()
except Exception as e:
logging.exception(e)
def run(self):
"""Runs the RawReader"""
with open(self.filename) as f:
for line in f:
if line:
logging.debug("[%s] %s" % (self.filename, line))
data = {}
for i, v in enumerate(line.split()):
name = "raw_%d" % i
if '=' in v:
name = v.split('=')[0]
v = v.split('=')[1]
data[name] = float(v)
print("%f %s" % (time.time(), data))
self.store_metrics(data)
def store_metrics(self, data):
if self.influx:
measurements = []
for name, value in list(data.items()):
measurements.append({
"measurement": name,
"tags": {},
"time": datetime.utcnow().isoformat() + "Z",
"fields": {"value": value}})
self.influx.write_points(measurements)
print("... Stored!")