Skip to content

Commit

Permalink
Improved error handling and output
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed May 10, 2023
1 parent d9129d1 commit 8045696
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
10 changes: 5 additions & 5 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[flake8]
max-line-length = 180
exclude =
./dbus-mqtt-battery/ext
./dbus-mqtt-battery/ext
extend-ignore:
# E203 whitespace before ':' conflicts with black code formatting. Will be ignored in flake8
E203
# E402 module level import not at top of file
E402
# E203 whitespace before ':' conflicts with black code formatting. Will be ignored in flake8
E203
# E402 module level import not at top of file
E402
39 changes: 27 additions & 12 deletions dbus-mqtt-battery/dbus-mqtt-battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@

# get values from config.ini file
try:
config = configparser.ConfigParser()
config.read("%s/config.ini" % (os.path.dirname(os.path.realpath(__file__))))
if (config['MQTT']['broker_address'] == "IP_ADDR_OR_FQDN"):
print("ERROR:config.ini file is using invalid default values like IP_ADDR_OR_FQDN. The driver restarts in 60 seconds.")
config_file = (os.path.dirname(os.path.realpath(__file__))) + "/config.ini"
if os.path.exists(config_file):
config = configparser.ConfigParser()
config.read(config_file)
if (config['MQTT']['broker_address'] == "IP_ADDR_OR_FQDN"):
print("ERROR:The \"config.ini\" is using invalid default values like IP_ADDR_OR_FQDN. The driver restarts in 60 seconds.")
sleep(60)
sys.exit()
else:
print("ERROR:The \"" + config_file + "\" is not found. Did you copy or rename the \"config.sample.ini\" to \"config.ini\"? The driver restarts in 60 seconds.")
sleep(60)
sys.exit()
except Exception as e:
print("ERROR: %s" % e)
print("If config.ini file is not found then copy or rename the config.sample.ini to config.ini. The driver restarts in 60 seconds.")

except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
print(f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}")
print("ERROR:The driver restarts in 60 seconds.")
sleep(60)
sys.exit()

Expand Down Expand Up @@ -416,8 +426,11 @@ def on_message(client, userdata, msg):
logging.error("Received message is not a valid JSON. %s" % e)
logging.debug("MQTT payload: " + str(msg.payload)[1:])

except Exception as e:
logging.error("Exception occurred: %s" % e)
except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logging.error(f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}")
logging.debug("MQTT payload: " + str(msg.payload)[1:])


Expand Down Expand Up @@ -478,9 +491,11 @@ def _update(self):
logging.error("Received key \"" + setting + "\" with value \"" + str(data['value']) + "\" is not valid: " + str(e))
sys.exit()

except Exception as e:
logging.error("Exception occurred: %s" % e)
sys.exit()
except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logging.error(f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}")

logging.info("Battery SoC: {:.2f} V - {:.2f} %".format(battery_dict['/Dc/0/Power']['value'], battery_dict['/Soc']['value']))

Expand Down

0 comments on commit 8045696

Please sign in to comment.