-
Notifications
You must be signed in to change notification settings - Fork 6
/
zoe-connector.py
85 lines (61 loc) · 2.64 KB
/
zoe-connector.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
#!/usr/bin/env python
import paho.mqtt.client as paho # pip install paho-mqtt
import time
import logging
import sys
from pyze.api import Gigya, Kamereon, Vehicle
from config import *
from secrets import *
FREQUENCY = 1200 # sec
EXCEPTION_DELAY = 300
def getSocRange(gigya):
k = Kamereon(api_key=KAMEREON_API_KEY, gigya=gigya, country='DE')
v = Vehicle(ZOE_VIN, k)
b = v.battery_status()
soc = b['batteryLevel']
remaining_range = b['batteryAutonomy']
logging.info("Zoe API: soc: {}%, range: {}km".format(soc, remaining_range))
return soc, remaining_range
def update(gigya):
soc, remaining_range = getSocRange(gigya)
(result, mid) = mqttc.publish("{}/{}".format(ZOE_MQTT_PREFIX, 'soc'), str(soc), 0, retain=True)
# logging.info("Pubish Result: {} MID: {} for {}: {}".format(result, mid, 'soc', soc))
(result, mid) = mqttc.publish("{}/{}".format(ZOE_MQTT_PREFIX, 'range'), str(remaining_range), 0, retain=True)
# logging.info("Pubish Result: {} MID: {} for {}: {}".format(result, mid, 'range', remaining_range))
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
mqttc = paho.Client('zoe-connector', clean_session=True)
# mqttc.enable_logger()
try:
g = Gigya(api_key=GIGYA_API_KEY)
g.login(ZOE_ZE_USERNAME, ZOE_ZE_PASSWORD) # You should only need to do this once
g.account_info() # Retrieves and caches person ID
except Exception:
logging.exception("Exception during Gigya login, sleeping 30s")
time.sleep(30)
raise
mqttc.connect(BROKER_HOST, BROKER_PORT, 60)
logging.info("Connected to {}:{}".format(BROKER_HOST, BROKER_PORT))
exception_counter = 0
mqttc.loop_start()
while True:
try:
update(g)
time.sleep(FREQUENCY)
except KeyboardInterrupt:
logging.warning("Keyboard interruption")
break
except Exception:
exception_counter = exception_counter + 1
if exception_counter > 24:
logging.exception("Exception occured in main loop too many times ({exception_counter}), giving up....")
raise
else:
logging.exception(f"Exception occured in main loop, retrying after {EXCEPTION_DELAY} sec")
time.sleep(EXCEPTION_DELAY)
mqttc.disconnect()
mqttc.loop_stop() # waits, until DISCONNECT message is sent out
logging.info("Disconnected from to {}:{}".format(BROKER_HOST, BROKER_PORT))