forked from EVNotify/EVNotiPi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevnotipi.py
More file actions
executable file
·132 lines (105 loc) · 3.71 KB
/
evnotipi.py
File metadata and controls
executable file
·132 lines (105 loc) · 3.71 KB
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
#!/usr/bin/env python3
from gpspoller import GpsPoller
from subprocess import check_call, check_output
from time import sleep,time
import os
import sys
import signal
import sdnotify
import logging
import evnotify
Systemd = sdnotify.SystemdNotifier()
# load config
if os.path.exists('config.json'):
import json
with open('config.json', encoding='utf-8') as config_file:
config = json.loads(config_file.read())
elif os.path.exists('config.yaml'):
import yaml
with open('config.yaml', encoding='utf-8') as config_file:
config = yaml.load(config_file, Loader=yaml.SafeLoader)
else:
raise Exception('No config found')
logging.basicConfig(level=config['loglevel'] if 'loglevel' in config else logging.INFO)
log = logging.getLogger("EVNotiPi")
# Load OBD2 interface module
if not "{}.py".format(config['dongle']['type']) in os.listdir('dongles'):
raise Exception('Unsupported dongle {}'.format(config['dongle']['type']))
# Init ODB2 adapter
sys.path.insert(0, 'dongles')
exec("from {0} import {0} as DONGLE".format(config['dongle']['type']))
sys.path.remove('dongles')
if not "{}.py".format(config['car']['type']) in os.listdir('cars'):
raise Exception('Unsupported car {}'.format(config['car']['type']))
sys.path.insert(0, 'cars')
exec("from {0} import {0} as CAR".format(config['car']['type']))
sys.path.remove('cars')
Threads = []
if 'watchdog' in config and config['watchdog']['enable'] == True:
import watchdog
Watchdog = watchdog.Watchdog(config['watchdog'])
else:
Watchdog = None
dongle = DONGLE(config['dongle'], watchdog = Watchdog)
car = CAR(config['car'], dongle)
Threads.append(car)
# Init GPS interface
gps = GpsPoller()
Threads.append(gps)
# Init EVNotify
EVNotify = evnotify.EVNotify(config['evnotify'], car, gps)
Threads.append(EVNotify)
# Init WiFi control
if 'wifi' in config and config['wifi']['enable'] == True:
from wifi_ctrl import WiFiCtrl
wifi = WiFiCtrl()
else:
wifi = None
# Init some variables
main_running = True
# Set up signal handling
def exit_gracefully(signum, frame):
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")
try:
while main_running:
now = time()
watchdogs_ok = True
for t in Threads:
if t.checkWatchdog() == False:
log.error("Watchdog Failed " + str(t))
watchdogs_ok = False
if watchdogs_ok:
Systemd.notify("WATCHDOG=1")
if config['system']['shutdown_delay'] != None:
if now - car.last_data > config['system']['shutdown_delay'] and dongle.isCarAvailable() == False:
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'])
sleep(5)
else:
log.info("Not charging and car off; Not shutting down, users connected")
if wifi and config['wifi']['shutdown_delay'] != None:
if now - car.last_data > config['wifi']['shutdown_delay'] and dongle.isCarAvailable() == False:
wifi.disable()
else:
wifi.enable()
sys.stdout.flush()
if main_running:
loop_delay = 1 - (time()-now)
if loop_delay > 0: sleep(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.")