Skip to content

Commit

Permalink
Robustness and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuinigeRijder committed Nov 29, 2022
1 parent 3b300bd commit a3de58f
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 525 deletions.
676 changes: 303 additions & 373 deletions README.md

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions examples/summary.day.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Period, date , info , odometer, delta km, +kWh, -kWh, km/kWh, kWh/100km, cost Euro, SOC%CUR,AVG,MIN,MAX, 12V%CUR,AVG,MIN,MAX, #charges, #trips, #moves
DAY , 2022-09-17, Sat , 17324.2, , 2.8, , , , , 58, 55, 55, 58, 91, 91, 91, 91, 1 , ,
DAY , 2022-09-18, Sun , 17324.2, , 0.7, , , , , 59, 59, 59, 60, 91, 91, 91, 91, , ,
DAY , 2022-09-19, Mon , 17330.7, 6.5, , , , , , 59, 60, 59, 61, 86, 88, 85, 91, , 2 , 2
DAY , 2022-09-20, Tue , 17378.3, 47.6, , -7.0, 6.8, 14.7, 1.72, 45, 48, 45, 59, 91, 91, 87, 92, , 3 , 6
DAY , 2022-09-21, Wed , 17383.5, 5.2, 18.2, -0.7, , , , 70, 63, 46, 70, 91, 91, 91, 92, 2 , 2 , 2
DAY , 2022-09-22, Thu , 17383.5, , , , , , , 72, 72, 72, 72, 91, 91, 91, 91, 1 , ,
DAY , 2022-09-23, Fri , 17387.1, 3.6, 20.3, -0.7, , , , 100, 86, 71,100, 87, 87, 87, 88, 2 , 2 , 2
DAY , 2022-09-24, Sat , 17794.9, 407.8, 25.9, -66.5, 6.1, 16.3, 16.36, 42, 40, 5,100, 97, 96, 92, 98, 1 , 5 , 15
DAY , 2022-09-25, Sun , 17794.9, , 5.6, , , , , 50, 50, 43, 50, 97, 97, 97, 97, , ,
Period, date , info , odometer, delta km, +kWh, -kWh, km/kWh, kWh/100km, cost Euro, SOC%CUR,AVG,MIN,MAX, 12V%CUR,AVG,MIN,MAX, #charges, #trips
DAY , 2022-09-17, Sat , 17324.2, , 2.8, , , , , 58, 55, 55, 58, 91, 91, 91, 91, 1 ,
DAY , 2022-09-18, Sun , 17324.2, , 0.7, , , , , 59, 59, 59, 60, 91, 91, 91, 91, ,
DAY , 2022-09-19, Mon , 17330.7, 6.5, , , , , , 59, 60, 59, 61, 86, 88, 85, 91, , 2
DAY , 2022-09-20, Tue , 17378.3, 47.6, , -7.0, 6.8, 14.7, 1.72, 45, 48, 45, 59, 91, 91, 87, 92, , 3
DAY , 2022-09-21, Wed , 17383.5, 5.2, 18.2, -0.7, , , , 70, 63, 46, 70, 91, 91, 91, 92, 2 , 2
DAY , 2022-09-22, Thu , 17383.5, , , , , , , 72, 72, 72, 72, 91, 91, 91, 91, 1 ,
DAY , 2022-09-23, Fri , 17387.1, 3.6, 20.3, -0.7, , , , 100, 86, 71,100, 87, 87, 87, 88, 2 , 2
DAY , 2022-09-24, Sat , 17794.9, 407.8, 25.9, -66.5, 6.1, 16.3, 16.36, 42, 40, 5,100, 97, 96, 92, 98, 1 , 5
DAY , 2022-09-25, Sun , 17794.9, , 5.6, , , , , 50, 50, 43, 50, 97, 97, 97, 97, ,
Binary file modified examples/summary.day.xlsx
Binary file not shown.
66 changes: 33 additions & 33 deletions examples/summary.py_output.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
Period, date , info , odometer, delta km, +kWh, -kWh, km/kWh, kWh/100km, cost Euro, SOC%CUR,AVG,MIN,MAX, 12V%CUR,AVG,MIN,MAX, #charges, #trips, #moves
DAY , 2022-09-17, Sat , 17324.2, , 2.8, , , , , 58, 55, 55, 58, 91, 91, 91, 91, 1 , ,
DAY , 2022-09-18, Sun , 17324.2, , 0.7, , , , , 59, 59, 59, 60, 91, 91, 91, 91, , ,
WEEK , 2022-09-18, WK 37, 17324.2, , 3.5, , , , , 59, 59, 55, 60, 91, 91, 91, 91, 1 , ,
TRIP , 2022-09-19, 15:00, 17324.3, 0.1, , , , , , 61, 60, 60, 61, 85, 90, 85, 91, , 1 , 1
TRIP , 2022-09-19, 16:00, 17330.7, 6.4, , -1.4, , , , 59, 60, 59, 59, 86, 85, 86, 86, , 1 , 1
DAY , 2022-09-19, Mon , 17330.7, 6.5, , , , , , 59, 60, 59, 61, 86, 88, 85, 91, , 2 , 2
TRIP , 2022-09-20, 08:00, 17358.9, 28.2, , -4.2, 6.7, 14.9, 1.03, 53, 57, 53, 59, 91, 88, 88, 91, , 1 , 2
TRIP , 2022-09-20, 15:30, 17371.5, 12.6, , -2.1, 6.0, 16.7, 0.52, 48, 50, 48, 51, 92, 90, 87, 92, , 1 , 3
TRIP , 2022-09-20, 15:58, 17378.3, 6.8, , -0.7, , , , 47, 47, 47, 47, 91, 91, 91, 91, , 1 , 1
DAY , 2022-09-20, Tue , 17378.3, 47.6, , -7.0, 6.8, 14.7, 1.72, 45, 48, 45, 59, 91, 91, 87, 92, , 3 , 6
TRIP , 2022-09-21, 12:30, 17380.8, 2.5, 4.9, , , , , 52, 48, 46, 52, 92, 91, 91, 92, 1 , 1 , 1
TRIP , 2022-09-21, 13:00, 17383.5, 2.7, , -0.7, , , , 51, 51, 51, 51, 91, 91, 91, 91, , 1 , 1
DAY , 2022-09-21, Wed , 17383.5, 5.2, 18.2, -0.7, , , , 70, 63, 46, 70, 91, 91, 91, 92, 2 , 2 , 2
DAY , 2022-09-22, Thu , 17383.5, , , , , , , 72, 72, 72, 72, 91, 91, 91, 91, 1 , ,
TRIP , 2022-09-23, 11:21, 17385.4, 1.9, , -0.7, , , , 71, 71, 71, 71, 88, 89, 88, 88, , 1 , 1
TRIP , 2022-09-23, 12:00, 17387.1, 1.7, 0.7, , , , , 72, 71, 72, 72, 87, 87, 87, 87, 1 , 1 , 1
DAY , 2022-09-23, Fri , 17387.1, 3.6, 20.3, -0.7, , , , 100, 86, 71,100, 87, 87, 87, 88, 2 , 2 , 2
TRIP , 2022-09-24, 09:57, 17390.8, 3.7, , -0.7, , , , 99,100, 99,100, 95, 94, 95, 95, , 1 ,
TRIP , 2022-09-24, 13:21, 17589.2, 198.4, , -32.9, 6.0, 16.6, 8.09, 52, 80, 52, 98, 96, 96, 92, 98, , 1 , 6
TRIP , 2022-09-24, 14:31, 17592.5, 3.3, , -0.7, , , , 51, 51, 51, 51, 94, 95, 94, 94, , 1 , 1
TRIP , 2022-09-24, 15:23, 17597.3, 4.8, , -0.7, , , , 50, 51, 50, 51, 96, 94, 93, 96, , 1 , 2
TRIP , 2022-09-24, 19:00, 17794.9, 197.6, , -31.5, 6.3, 15.9, 7.75, 5, 30, 5, 50, 97, 95, 94, 97, 1 , 1 , 6
DAY , 2022-09-24, Sat , 17794.9, 407.8, 25.9, -66.5, 6.1, 16.3, 16.36, 42, 40, 5,100, 97, 96, 92, 98, 1 , 5 , 15
DAY , 2022-09-25, Sun , 17794.9, , 5.6, , , , , 50, 50, 43, 50, 97, 97, 97, 97, , ,
WEEK , 2022-09-25, WK 38, 17794.9, 470.7, 67.2, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 6 , 14 , 27
MONTH , 2022-09-25, Sep , 17794.9, 470.7, 70.7, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 7 , 14 , 27
YEAR , 2022-09-25, 2022 , 17794.9, 470.7, 70.7, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 7 , 14 , 27
TRIPAVG , 2022-09-25, 14t , 17794.9, 33.6, 5.0, -5.2, 6.4, 15.6, 1.29, 50, 50, 5,100, 97, 97, 85, 98, 0.5, 1 , 1.9
DAYAVG , 2022-09-25, 9d , 17794.9, 52.3, 7.9, -8.2, 6.4, 15.6, 2.01, 50, 50, 5,100, 97, 97, 85, 98, 0.8, 1.6, 3
WEEKAVG , 2022-09-25, 9d , 17794.9, 366.1, 55.0, -57.2, 6.4, 15.6, 14.06, 50, 50, 5,100, 97, 97, 85, 98, 5.4, 10.9, 21
MONTHAVG, 2022-09-25, 9d , 17794.9, 1590.8, 238.9, -248.4, 6.4, 15.6, 61.11, 50, 50, 5,100, 97, 97, 85, 98, 23.7, 47.3, 91.2
YEARLY , 2022-09-25, 9d , 17794.9, 19089.5, 2867.3, -2980.8, 6.4, 15.6, 733.29, 50, 50, 5,100, 97, 97, 85, 98, 283.9, 567.8, 1095
Period, date , info , odometer, delta km, +kWh, -kWh, km/kWh, kWh/100km, cost Euro, SOC%CUR,AVG,MIN,MAX, 12V%CUR,AVG,MIN,MAX, #charges, #trips
DAY , 2022-09-17, Sat , 17324.2, , 2.8, , , , , 58, 55, 55, 58, 91, 91, 91, 91, 1 ,
DAY , 2022-09-18, Sun , 17324.2, , 0.7, , , , , 59, 59, 59, 60, 91, 91, 91, 91, ,
WEEK , 2022-09-18, WK 37, 17324.2, , 3.5, , , , , 59, 59, 55, 60, 91, 91, 91, 91, 1 ,
TRIP , 2022-09-19, 15:00, 17324.3, 0.1, , , , , , 61, 60, 60, 61, 85, 90, 85, 91, , 1
TRIP , 2022-09-19, 16:00, 17330.7, 6.4, , -1.4, , , , 59, 60, 59, 59, 86, 85, 86, 86, , 1
DAY , 2022-09-19, Mon , 17330.7, 6.5, , , , , , 59, 60, 59, 61, 86, 88, 85, 91, , 2
TRIP , 2022-09-20, 08:00, 17358.9, 28.2, , -4.2, 6.7, 14.9, 1.03, 53, 57, 53, 59, 91, 88, 88, 91, , 1
TRIP , 2022-09-20, 15:30, 17371.5, 12.6, , -2.1, 6.0, 16.7, 0.52, 48, 50, 48, 51, 92, 90, 87, 92, , 1
TRIP , 2022-09-20, 15:58, 17378.3, 6.8, , -0.7, , , , 47, 47, 47, 47, 91, 91, 91, 91, , 1
DAY , 2022-09-20, Tue , 17378.3, 47.6, , -7.0, 6.8, 14.7, 1.72, 45, 48, 45, 59, 91, 91, 87, 92, , 3
TRIP , 2022-09-21, 12:30, 17380.8, 2.5, 4.9, , , , , 52, 48, 46, 52, 92, 91, 91, 92, 1 , 1
TRIP , 2022-09-21, 13:00, 17383.5, 2.7, , -0.7, , , , 51, 51, 51, 51, 91, 91, 91, 91, , 1
DAY , 2022-09-21, Wed , 17383.5, 5.2, 18.2, -0.7, , , , 70, 63, 46, 70, 91, 91, 91, 92, 2 , 2
DAY , 2022-09-22, Thu , 17383.5, , , , , , , 72, 72, 72, 72, 91, 91, 91, 91, 1 ,
TRIP , 2022-09-23, 11:21, 17385.4, 1.9, , -0.7, , , , 71, 71, 71, 71, 88, 89, 88, 88, , 1
TRIP , 2022-09-23, 12:00, 17387.1, 1.7, 0.7, , , , , 72, 71, 72, 72, 87, 87, 87, 87, 1 , 1
DAY , 2022-09-23, Fri , 17387.1, 3.6, 20.3, -0.7, , , , 100, 86, 71,100, 87, 87, 87, 88, 2 , 2
TRIP , 2022-09-24, 09:57, 17390.8, 3.7, , -0.7, , , , 99,100, 99,100, 95, 94, 95, 95, , 1
TRIP , 2022-09-24, 13:21, 17589.2, 198.4, , -32.9, 6.0, 16.6, 8.09, 52, 80, 52, 98, 96, 96, 92, 98, , 1
TRIP , 2022-09-24, 14:31, 17592.5, 3.3, , -0.7, , , , 51, 51, 51, 51, 94, 95, 94, 94, , 1
TRIP , 2022-09-24, 15:23, 17597.3, 4.8, , -0.7, , , , 50, 51, 50, 51, 96, 94, 93, 96, , 1
TRIP , 2022-09-24, 19:00, 17794.9, 197.6, , -31.5, 6.3, 15.9, 7.75, 5, 30, 5, 50, 97, 95, 94, 97, 1 , 1
DAY , 2022-09-24, Sat , 17794.9, 407.8, 25.9, -66.5, 6.1, 16.3, 16.36, 42, 40, 5,100, 97, 96, 92, 98, 1 , 5
DAY , 2022-09-25, Sun , 17794.9, , 5.6, , , , , 50, 50, 43, 50, 97, 97, 97, 97, ,
WEEK , 2022-09-25, WK 38, 17794.9, 470.7, 67.2, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 6 , 14
MONTH , 2022-09-25, Sep , 17794.9, 470.7, 70.7, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 7 , 14
YEAR , 2022-09-25, 2022 , 17794.9, 470.7, 70.7, -73.5, 6.4, 15.6, 18.08, 50, 50, 5,100, 97, 97, 85, 98, 7 , 14
TRIPAVG , 2022-09-25, 14t , 17794.9, 33.6, 5.0, -5.2, 6.4, 15.6, 1.29, 50, 50, 5,100, 97, 97, 85, 98, 0.5, 1
DAYAVG , 2022-09-25, 9d , 17794.9, 52.3, 7.9, -8.2, 6.4, 15.6, 2.01, 50, 50, 5,100, 97, 97, 85, 98, 0.8, 1.6
WEEKAVG , 2022-09-25, 9d , 17794.9, 366.1, 55.0, -57.2, 6.4, 15.6, 14.06, 50, 50, 5,100, 97, 97, 85, 98, 5.4, 10.9
MONTHAVG, 2022-09-25, 9d , 17794.9, 1590.8, 238.9, -248.4, 6.4, 15.6, 61.11, 50, 50, 5,100, 97, 97, 85, 98, 23.7, 47.3
YEARLY , 2022-09-25, 9d , 17794.9, 19089.5, 2867.3, -2980.8, 6.4, 15.6, 733.29, 50, 50, 5,100, 97, 97, 85, 98, 283.9, 567.8
33 changes: 17 additions & 16 deletions kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import sys
from datetime import datetime, timezone
from pathlib import Path
from time import sleep
from geopy.geocoders import Nominatim

INPUT = Path("monitor.csv")
OUTPUT = Path("monitor.kml")
Expand All @@ -46,6 +44,7 @@
SOC = 6 # SOC%
CHARGING = 7 # charging
PLUGGED = 8 # plugged
LOCATION = 9 # location address (optional field)


def arg_has(string):
Expand All @@ -57,7 +56,11 @@ def arg_has(string):
return False


ADDRESS = arg_has('address')
def to_float(string):
""" convert to float """
if "None" in string:
return 0.0
return float(string)


def write(outputfile, line):
Expand Down Expand Up @@ -90,16 +93,12 @@ def strip_datetime(string) -> str:
return string


def get_address(lat, lon):
def get_address(split):
""" get address """
location_str = ""
if ADDRESS:
sleep(1) # do not abuse Nominatim, 1 request per second
geolocator = \
Nominatim(user_agent="hyundai_kia_connect_monitor")
location = geolocator.reverse(
lat + ", " + lon)
location_str = ' "' + location.address + '"'
location_str = ''
if len(split) > 9:
location_str = split[LOCATION].strip()

return location_str


Expand All @@ -112,7 +111,9 @@ def write_kml(outputfile, count, items, prev_items):
voltage_12 = items[V12].strip()
if voltage_12 == 'None':
voltage_12 = '-1'
odometer = float(items[ODO].strip())
odometer = to_float(items[ODO].strip())
if odometer == 0.0:
return # bad line
soc = items[SOC].strip()
if soc == 'None':
soc = '-1'
Expand All @@ -129,16 +130,16 @@ def write_kml(outputfile, count, items, prev_items):
name += ' '

coordinates = f'<coordinates>{lon}, {lat}</coordinates>'
address = get_address(lat, lon)
address = get_address(items)
if address == '':
description = f'SOC:{soc:>3}% 12V:{voltage_12:>3}% ODO:{odometer:8.1f}' # noqa pylint:disable=line-too-long
else:
description = f'SOC:{soc:>3}% 12V:{voltage_12:>3}% ODO:{odometer:8.1f} Address:{address} ' # noqa pylint:disable=line-too-long
description = f'SOC:{soc:>3}% 12V:{voltage_12:>3}% ODO:{odometer:8.1f} Address: {address}' # noqa pylint:disable=line-too-long

delta_odometer = 0
if len(prev_items) > 8:
delta_odometer = round(
odometer - float(prev_items[ODO].strip()), 1)
odometer - to_float(prev_items[ODO].strip()), 1)
if delta_odometer != 0.0:
if delta_odometer > 0.0:
description += ' (+' + str(delta_odometer)
Expand Down
15 changes: 13 additions & 2 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ def get_append_data():
debug(f"check_and_force_update_vehicles: {FORCE_SECONDS}")
manager.check_and_force_update_vehicles(FORCE_SECONDS)

line = ''
for _, vehicle in manager.vehicles.items():
geocode = ''
if USE_GEOCODE:
if len(vehicle.geocode) > 0:
# replace comma by semicolon for easier splitting
geocode = ', ' + vehicle.geocode[0].replace(',', ';')

writeln(
line = (
str(vehicle.last_updated_at) +
', ' + str(vehicle.location_longitude) +
', ' + str(vehicle.location_latitude) +
Expand All @@ -146,7 +147,17 @@ def get_append_data():
', ' + str(vehicle.ev_battery_is_plugged_in) +
geocode
)
retries = 0 # successfully end while loop
if 'None, None' in line: # something gone wrong, retry
log(f"Skipping Unexpected line: {line}")
else:
writeln(line)

if 'None, None' in line: # something gone wrong, retry
retries -= 1
log("Sleeping a minute")
time.sleep(60)
else:
retries = 0 # successfully end while loop
except Exception as ex: # pylint: disable=broad-except
log('Exception: ' + str(ex))
traceback.print_exc()
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
geopy==2.2.0
hyundai_kia_connect_api==1.34.4
beautifulsoup4==4.11.1
python_dateutil==2.8.2
Expand Down
Loading

0 comments on commit a3de58f

Please sign in to comment.