Skip to content

Commit

Permalink
HyundaiBlueLinkAPIUSA Vehicle.daily_stats NOT consistent with other r…
Browse files Browse the repository at this point in the history
…egion implementations
  • Loading branch information
ZuinigeRijder committed Aug 31, 2024
1 parent 85b6149 commit 3d138ec
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 230 deletions.
37 changes: 34 additions & 3 deletions dailystats.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def dbg(line: str) -> bool:
parser.read(get_filepath("monitor.cfg"))
monitor_settings = dict(parser.items("monitor"))
ODO_METRIC = get(monitor_settings, "odometer_metric", "km").lower()
REGION = monitor_settings["region"]
INCLUDE_REGENERATE_IN_CONSUMPTION = (
get(monitor_settings, "include_regenerate_in_consumption", "False").lower()
== "true"
Expand Down Expand Up @@ -678,24 +679,54 @@ def reverse_print_dailystats_one_line(val: list[str]) -> None:
print_output(",,,,,,") # empty line/row


def compute_cumulative_dailystats(val: list[str], total: list[str]) -> list[str]:
"""compute_cumulative_dailystats"""
total[DATE] = val[DATE]
total[DISTANCE] = str(to_float(val[DISTANCE]) + to_float(total[DISTANCE]))
total[CONSUMED] = str(to_int(val[CONSUMED]) + to_int(total[CONSUMED]))
total[REGENERATED] = str(to_int(val[REGENERATED]) + to_int(total[REGENERATED]))
total[ENGINE] = str(to_int(val[ENGINE]) + to_int(total[ENGINE]))
total[CLIMATE] = str(to_int(val[CLIMATE]) + to_int(total[CLIMATE]))
total[ELECTRONICS] = str(to_int(val[ELECTRONICS]) + to_int(total[ELECTRONICS]))
total[BATTERY_CARE] = str(to_int(val[BATTERY_CARE]) + to_int(total[BATTERY_CARE]))
return total


def reverse_print_dailystats(totals: bool) -> None:
"""reverse print dailystats"""
if DAILYSTATS_CSV_FILE.is_file():
usa = int(REGION) == 3
cumulative_splitted: list[str] = []
prev_date_str = ""
for line in read_reverse_order(DAILYSTATS_CSV_FILE.name):
splitted = split_on_comma(line)
if len(splitted) != 9 or splitted[0].startswith("date"):
continue # nothing to do
date_str = splitted[0].split(" ")[0]
if date_str != prev_date_str: # skip identical dates
date_str = splitted[0].split(" ")[0] # yyyymmdd
if usa or date_str != prev_date_str: # skip identical dates for non-usa
if totals:
increment_dailystats_totals(splitted)
else:
reverse_print_dailystats_one_line(splitted)
if usa:
if prev_date_str != "":
if date_str == prev_date_str:
cumulative_splitted = compute_cumulative_dailystats(
splitted, cumulative_splitted
)
else:
reverse_print_dailystats_one_line(cumulative_splitted)
cumulative_splitted = splitted
else:
cumulative_splitted = splitted
else:
reverse_print_dailystats_one_line(splitted)
prev_date_str = date_str
else:
_ = D and dbg(f"skipping: {splitted}")

if usa and not totals and len(cumulative_splitted) > 0:
reverse_print_dailystats_one_line(cumulative_splitted)


def get_format(range_str: str, special: bool):
"""get_format"""
Expand Down
20 changes: 16 additions & 4 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,42 @@ def handle_daily_stats(vehicle: Vehicle, number_of_vehicles: int) -> None:
file.write(
"date, distance, distance_unit, total_consumed, regenerated_energy, engine_consumption, climate_consumption, onboard_electronics_consumption, battery_care_consumption\n" # noqa
)
usa = int(REGION) == 3
today_time_str = datetime.now().strftime("%H:%M")
last_line = get_last_line(Path(filename))
last_date = last_line.split(",")[0].strip().split(" ")[0].strip()
last_date = last_line.split(",")[0].strip() # get yyymmdd hh:mm
if not usa:
# get rid of timestamp, always write new cumulative data from today
last_date = last_date.split(" ")[0].strip()
last_line = re.sub("^[^,]*,", "", last_line).strip() # get rid of first column
_ = D and dbg(f"daily_stats: {daily_stats}")
i = len(daily_stats)
while i > 0:
i = i - 1
stat = daily_stats[i]
dailystats_date = stat.date.strftime("%Y%m%d")
if usa:
# write new (non-cumulative) data from today
dailystats_date = stat.date.strftime("%Y%m%d %H:%M")
else:
# always write new cumulative data from today
dailystats_date = stat.date.strftime("%Y%m%d")
if D:
print(
f"{i} dailystats_date:[{dailystats_date}] [{last_date}] {stat}" # noqa
)
if dailystats_date >= last_date:
# only append not already written daily stats
distance = float(stat.distance)
distance_unit = stat.distance_unit
distance_unit = vehicle.odometer_unit
if to_miles_needed(vehicle):
distance = km_to_mile(distance)
distance_unit = ODO_METRIC
line = f"{float_to_string_no_trailing_zero(distance)}, {distance_unit}, {stat.total_consumed}, {stat.regenerated_energy}, {stat.engine_consumption}, {stat.climate_consumption}, {stat.onboard_electronics_consumption}, {stat.battery_care_consumption}" # noqa
if dailystats_date > last_date or last_line != line:
dailystats_date = f"{dailystats_date} {today_time_str}"
if not usa:
# use request time to keep track of updates
dailystats_date = f"{dailystats_date} {today_time_str}"

full_line = f"{dailystats_date}, {line}"
if D:
print(
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hyundai_kia_connect_api>=3.22.2
hyundai_kia_connect_api>=3.22.10
beautifulsoup4>=4.10.0
python_dateutil>=2.8.2
pytz>=2021.3
Expand Down
Loading

0 comments on commit 3d138ec

Please sign in to comment.