forked from EVNotify/EVNotiPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevnotipi.py
executable file
·202 lines (164 loc) · 5.67 KB
/
evnotipi.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
""" EVNotiPi main module """
from gevent.monkey import patch_all; patch_all()
from time import monotonic, sleep
from subprocess import check_call, check_output
from argparse import ArgumentParser
import sys
import signal
import os
import logging
import sdnotify
from gpspoller import GpsPoller
import watchdog
import dongle
import car
import evnotify
Systemd = sdnotify.SystemdNotifier()
class ThreadFailure(Exception):
""" Raised when a sub thread fails """
parser = ArgumentParser(description='EVNotiPi')
parser.add_argument('-d', '--debug', dest='debug',
action='store_true', default=False)
parser.add_argument('-c', '--config', dest='config',
action='store', default='config.yaml')
args = parser.parse_args()
del parser
# load config
if os.path.exists(args.config):
if args.config[-5:] == '.json':
import json
with open(args.config, encoding='utf-8') as config_file:
config = json.load(config_file)
elif args.config[-5:] == '.yaml':
import yaml
with open(args.config, encoding='utf-8') as config_file:
config = None
# use the last document in config.yaml as config
for c in yaml.load_all(config_file, Loader=yaml.SafeLoader):
config = c
else:
raise Exception('Unknown config type')
else:
raise Exception('No config found')
if args.debug:
logging.basicConfig(level=logging.DEBUG)
elif 'loglevel' in config:
logging.basicConfig(level=config['loglevel'])
else:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("EVNotiPi")
del args
# emulate old config if watchdog section is missing
if 'watchdog' not in config or 'type' not in config['watchdog']:
log.warning('Old watchdog config syntax detected. Please adjust according to config.yaml.template.')
config['watchdog'] = {
'type': 'GPIO',
'shutdown_pin': config['dongle'].get('shutdown_pin', 24),
'pup_down': config['dongle'].get('pup_down', 21),
}
# Load OBD2 interface module
DONGLE = dongle.load(config['dongle']['type'])
# Load car module
CAR = car.load(config['car']['type'])
# Load watchdog module
WATCHDOG = watchdog.load(config['watchdog']['type'])
Threads = []
# Init watchdog
watchdog = WATCHDOG(config['watchdog'])
# Init dongle
dongle = DONGLE(config['dongle'])
# Init GPS interface
gps = GpsPoller(config['gps'])
Threads.append(gps)
# Init car
car = CAR(config['car'], dongle, watchdog, gps)
Threads.append(car)
# Init EVNotify
EVNotify = evnotify.EVNotify(config['evnotify'], car)
Threads.append(EVNotify)
# Init ABRP
if 'abrp' in config and config['abrp'].get('enable') is True:
import abrp
ABRP = abrp.ABRP(config['abrp'], car)
Threads.append(ABRP)
# Init influx interface
if 'influxdb' in config and config['influxdb'].get('enable') is True:
import influx_telemetry
Influx = influx_telemetry.InfluxTelemetry(
config['influxdb'], car, gps, EVNotify)
Threads.append(Influx)
if 'telemetry_proxy' in config and \
config['telemetry_proxy'].get('enable') is True:
import telemetry_proxy
TelPx = telemetry_proxy.TelemetryProxy(
config['telemetry_proxy'], car, gps, EVNotify)
Threads.append(TelPx)
# Init WiFi control
if 'wifi' in config and config['wifi'].get('enable') is True:
from wifi_ctrl import WiFiCtrl
wifi = WiFiCtrl()
else:
wifi = None
# Init web service
if 'webservice' in config and config['webservice'].get('enable') is True:
import webservice
WebService = webservice.WebService(config['webservice'], car)
Threads.append(WebService)
# Set up signal handling
def exit_gracefully(signum, frame):
""" Signalhandler for SIGTERM """
sys.exit(0)
signal.signal(signal.SIGTERM, exit_gracefully)
# Start polling loops
for t in Threads:
t.start()
Systemd.notify('READY=1')
log.info('Starting main loop')
# Suppress duplicate logs
LOG_USER = 1
log_flags = 0
main_running = True
try:
while main_running:
now = monotonic()
threads_ok = True
for t in Threads:
status = t.check_thread()
if not status:
log.error('Thread Failed (%s)', str(t))
threads_ok = False
raise ThreadFailure(str(t))
if threads_ok:
Systemd.notify('WATCHDOG=1')
if 'system' in config and 'shutdown_delay' in config['system']:
if (now - car.last_data > config['system']['shutdown_delay'] and
not car.is_available()):
usercnt = int(check_output(['who', '-q']).split(b'\n')[1].split(b'=')[1])
if usercnt == 0:
log.info('Not charging and car off => Shutdown')
check_call(['/bin/systemctl', 'poweroff'])
main_running = False
elif not log_flags & LOG_USER:
log.info('Not charging and car off; Not shutting down, users connected')
log_flags |= LOG_USER
elif log_flags & LOG_USER:
log_flags &= ~LOG_USER
if wifi and config['wifi'].get('shutdown_delay') is not None:
if (now - car.last_data > config['wifi']['shutdown_delay'] and
not car.is_available()):
wifi.disable()
else:
wifi.enable()
if main_running:
loop_delay = 1 - (monotonic()-now)
sleep(max(0, loop_delay))
except (KeyboardInterrupt, SystemExit): # when you press ctrl+c
main_running = False
Systemd.notify('STOPPING=1')
finally:
Systemd.notify('STOPPING=1')
log.info('Exiting ...')
for t in Threads[::-1]: # reverse Threads
t.stop()
log.info('Bye.')