Skip to content

Commit

Permalink
add some exception handling to mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
jblance committed Feb 14, 2021
1 parent ddb13a6 commit e024193
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions mppsolar/outputs/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ def output(self, *args, **kwargs):
if data is None:
return
mqtt_broker = get_kwargs(kwargs, "mqtt_broker", "localhost")
mqtt_port = get_kwargs(kwargs, "mqtt_port", 1883)
_port = get_kwargs(kwargs, "mqtt_port", 1883)
try:
mqtt_port = int(_port)
except ValueError as e:
log.warn(f"Unable to cast {_port} to int - check value supplied for mqttport")
log.warn(e)
return
except Exception as e:
log.warn(e)
return

mqtt_user = get_kwargs(kwargs, "mqtt_user")
mqtt_pass = get_kwargs(kwargs, "mqtt_pass")

Expand All @@ -87,7 +97,13 @@ def output(self, *args, **kwargs):
for msg in msgs:
print(msg)
else:
publish.multiple(msgs, hostname=mqtt_broker, port=mqtt_port, auth=auth)
try:
publish.multiple(msgs, hostname=mqtt_broker, port=mqtt_port, auth=auth)
except Exception as e:
log.warn(
f"Error publishing MQTT messages to broker '{mqtt_broker}' on port '{mqtt_port}' with auth '{auth}'"
)
log.warn(e)
else:
if mqtt_broker == "screen":
print("MQTT build_msgs returned no messages")
Expand Down
4 changes: 2 additions & 2 deletions mppsolar/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.7.31"
__version_comment__ = "recent changes: add filters to config file"
__version__ = "0.7.32"
__version_comment__ = "recent changes: bug fix on mqtt, mqtt prints messages is mqtthost is screen"

0 comments on commit e024193

Please sign in to comment.