-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelem.py
executable file
·137 lines (115 loc) · 4.09 KB
/
telem.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from influxdb import InfluxDBClient
import os
from time import sleep
import obd
import datetime
import socket
import logging
import logging.handlers
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('telem')
syslogHandler = logging.handlers.SysLogHandler(address=('localhost', 6514), facility='user', socktype=socket.SOCK_DGRAM)
#stdoutHandler = logging.StreamHandler(sys.stdout)
logger.addHandler(syslogHandler)
#logger.addHandler(stdoutHandler)
# Load tokenfile
if os.path.exists('/home/pi/.influxcred'):
#logger.debug("Opening secret...")
f = open('/home/pi/.influxcred', 'r')
influx_pass = f.readline().rstrip()
if influx_pass != "":
logger.debug("Influx cred opened and read")
else:
logger.debug("Failed to open ~/.influxcred")
client = InfluxDBClient('race.focism.com', 8086, 'car_252', influx_pass, 'stats_252')
def new_value(r):
client = InfluxDBClient('race.focism.com', 8086, 'car_252', influx_pass, 'stats_252')
ts = datetime.datetime.utcnow()
try:
measurement = str(r.command).split(":")[1]
measurement = measurement.replace(" ", "-")
except IndexError:
logger.debug("Caught IndexError in new_value")
main()
try:
json_body = [
{
"measurement": measurement,
"time": ts,
"fields": {
"value": r.value.magnitude
}
}]
client.write_points(json_body)
except TypeError:
logger.debug("Caught TypeError in new_value")
main()
except AttributeError:
logger.debug("Caught AttributeError in new_value")
main()
def new_fuel_status(r):
logger.debug(r.value)
try:
if not r.value[0]:
raise TypeError
except TypeError:
logger.debug("Caught TypeError in new_fuel_status")
main()
ts = datetime.datetime.utcnow()
measurement = str(r.command).split(":")[0]
measurement = measurement.replace(" ", "-")
if "Open loop due to insufficient engine temperature" in r.value:
fuel_status = 0
elif "Closed loop, using oxygen sensor feedback to determine fuel mix" in r.value:
fuel_status = 1
elif "Open loop due to engine load OR fuel cut due to deceleration" in r.value:
fuel_status = 2
elif "Open loop due to system failure" in r.value:
print("Caught open loop due to system failure")
fuel_status = 3
elif "Closed loop, using at least one oxygen sensor but there is a fault in the feedback system" in r.value:
fuel_status = 4
if fuel_status is None:
fuel_status = 255
json_body = [
{
"measurement": measurement,
"time": ts,
"fields": {
"value": fuel_status
}
}]
client.write_points(json_body)
def main():
obd.logger.setLevel(obd.logging.DEBUG)
connection = obd.Async()
status = connection.status()
while "Car Connected" not in status:
connection.close()
logger.debug("No car connected, sleeping...")
sleep(1)
connection = obd.Async()
status = connection.status()
logger.debug(connection.status())
supported_commands = connection.supported_commands
watch_commands = {}
for command in supported_commands:
if "DTC" not in command.name:
if "MIDS" not in command.name:
if "PIDS" not in command.name:
if "O2_SENSORS" not in command.name:
if command.name != "STATUS":
if "ELM" not in command.name:
if "OBD" not in command.name:
if "FUEL_STATUS" in command.name:
#logger.info(command.name, " supported, watching...")
connection.watch(command, callback=new_fuel_status)
else:
connection.watch(command, callback=new_value)
connection.start()
while True:
sleep(0.5)
if __name__== "__main__":
main()