Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
remove paho auto-reconnect and replace with our custom reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmeli committed Dec 3, 2021
1 parent 0623cfb commit d0dc2e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pyduke-energy
version = 0.0.13
version = 0.0.14
author = Michael Meli
author_email = [email protected]
description = Python Wrapper for unofficial Duke Energy REST API
Expand Down
28 changes: 17 additions & 11 deletions src/pyduke_energy/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ def on_discon(self, _client: mqtt.Client, _userdata, conn_res):
"MQTT disconnected with result code: %s",
mqtt.error_string(conn_res),
)
if self.disconnecting:
self.disconnected.set_result(conn_res)
else:
_LOGGER.warning("Unexpected MQTT disconnect. Attempting reconnect.")
self.mqtt_client.reconnect()
if not self.disconnecting:
_LOGGER.warning(
"Unexpected MQTT disconnect. Will attempt reconnect shortly."
)
self.disconnected.set_result(conn_res)

@staticmethod
def on_msg(msg):
Expand Down Expand Up @@ -196,7 +196,9 @@ async def connect_and_subscribe(self):
self.topicid = f'DESH/{self.mqtt_auth["gateway"]}/out/sm/1/live'

self.mqtt_client = mqtt.Client(
self.mqtt_auth["clientid"], transport="websockets"
self.mqtt_auth["clientid"],
transport="websockets",
reconnect_on_failure=False,
)
self.mqtt_client.on_connect = self.on_conn
self.mqtt_client.on_subscribe = self.on_sub
Expand All @@ -208,7 +210,6 @@ async def connect_and_subscribe(self):
self.mqtt_client.username_pw_set(
self.mqtt_auth["user"], password=self.mqtt_auth["pass"]
)
self.mqtt_client.reconnect_delay_set(3, 60)
# create default ssl context to get SSLKEYLOGFILE env variable
self.mqtt_client.tls_set_context(ssl.create_default_context())

Expand All @@ -226,12 +227,17 @@ async def connect_and_subscribe(self):
self.retrycount = 0
except asyncio.TimeoutError:
self.retrycount += 1
if self.retrycount <= FASTPOLL_RETRY_COUNT:
_LOGGER.debug("Message timeout, requesting fastpoll")
await self._fastpoll_req()
else:
if self.disconnected.done():
_LOGGER.debug(
"Unexpected disconnect detected, attemping reconnect"
)
await self._reconnect()
elif self.retrycount > FASTPOLL_RETRY_COUNT:
_LOGGER.debug("Multiple msg timeout, attempting reconnect")
await self._reconnect()
else:
_LOGGER.debug("Message timeout, requesting fastpoll")
await self._fastpoll_req()
self.rx_msg = None
finally:
res = self.mqtt_client.unsubscribe(self.topicid)
Expand Down

0 comments on commit d0dc2e8

Please sign in to comment.