Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #10

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PVCharge.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
time.sleep(2) # Delay to allow stop command to complete
else:
logging.warning("Slow poll, Car discovered charging and was NOT stopped successfully")
Energy.sample_sensor(timeout=5) # Force sensor refresh to increase accuracy of subsequent loop
Energy.sample_sensor(timeout=10) # Force sensor refresh to increase accuracy of subsequent loop
else:
# Prevent non_solar_charge or delay, wait condition
time.sleep(config["SLOW_POLLING"])
Expand Down
42 changes: 31 additions & 11 deletions routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def sample_sensor(self):

def calculate_charge_rate(self, new_sample):
if new_sample:
if self.sample_register(timeout=5) == 'Timeout':
if self.sample_register(timeout=30) == 'Timeout':
logging.warning("eGauge Register read timed out")
return self.new_charge_rate
if self.sample_sensor(timeout=5) == 'Timeout':
if self.sample_sensor(timeout=30) == 'Timeout':
logging.warning("eGauge Sensor read timed out")
return self.new_charge_rate
# Calculate the charge rate
Expand All @@ -85,7 +85,7 @@ def calculate_charge_rate(self, new_sample):

def verify_new_charge_rate(self, new_charge_rate):
for attempts in range(0, 6):
if self.sample_sensor(timeout=5) == 'Timeout':
if self.sample_sensor(timeout=10) == 'Timeout':
logging.warning("eGauge Sensor read timed out")
# Use round() on the verify step (vs math.floor()) to prevent constant requests for the same value
if round(self.charge_rate_sensor) == new_charge_rate:
Expand Down Expand Up @@ -147,22 +147,28 @@ def set_charge_rate(self, charge_rate):
command = self.tesla_base_command + ['charging-set-amps']
command.append(str(charge_rate))
logging.debug(command)
return call_sub_error_handler(command, timeout=15)
result, delay = call_sub_error_handler(command, timeout=25)
return result

def start_charging(self):
command = self.tesla_base_command + ['charging-start']
logging.debug(command)
return call_sub_error_handler(command, timeout=20)
result, delay = call_sub_error_handler(command, timeout=25)
return result

def stop_charging(self):
command = self.tesla_base_command + ['charging-stop']
logging.debug(command)
return call_sub_error_handler(command, timeout=10)
result, delay = call_sub_error_handler(command, timeout=25)
if delay > 0:
time.sleep(delay)
return result

def wake(self):
command = self.tesla_base_command + ['-domain', 'vcsec', 'wake']
logging.debug(command)
return call_sub_error_handler(command, timeout=20)
result, delay = call_sub_error_handler(command, timeout=25)
return result


@timeoutable('Timeout')
Expand All @@ -172,10 +178,24 @@ def call_sub_error_handler(cmd):
if result.stdout != "":
logging.debug(result.stdout)
except subprocess.CalledProcessError as error:
logging.warning(f"{type(error).__name__} - {error}")
logging.warning(f"Error: {error.stderr}")
return False
return True
logging.debug(f"{type(error).__name__} - {error}")
logging.debug(f"Error: {error.stderr}")
delay = 0
if "not_charging" in error.stderr:
# We have a match for "car could not execute command: not_charging" (precooling error)
logging.info("Attempted to stop charging when car was only Pre-Cooling! Delaying: 60 seconds")
delay = 60
elif "context deadline exceeded" in error.stderr:
# We have a match for the timeout error
logging.warning("Last Tesla command timed out")
elif "read/write on closed pipe" in error.stderr:
# Match for ATT request failed read/write on closed pipe
logging.warning("Last Tesla command failed to connect over Bluetooth")
else:
logging.warning("Unknown error, note error output")
logging.warning(f"Error: {error.stderr}")
return False, delay
return True, 0

def check_elapsed_time(loop_time, compare_time, wait_time):
if compare_time == 0:
Expand Down