Skip to content

Commit

Permalink
Implemented connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Jun 20, 2020
1 parent 043a0ca commit 460d0df
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.0.0rc12
0.4.0.0rc13
2 changes: 1 addition & 1 deletion meross_iot/controller/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from typing import List, Union, Optional, Iterable, Coroutine, Callable, Any, Awaitable

from meross_iot.model.enums import OnlineStatus, Namespace
from meross_iot.model.enums import OnlineStatus, Namespace, ConnectionEvent
from meross_iot.model.http.device import HttpDeviceInfo
from datetime import datetime

Expand Down
20 changes: 18 additions & 2 deletions meross_iot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from meross_iot.controller.device import BaseDevice, HubDevice, GenericSubDevice
from meross_iot.device_factory import build_meross_device, build_meross_subdevice
from meross_iot.http_api import MerossHttpClient
from meross_iot.model.enums import Namespace, OnlineStatus
from meross_iot.model.enums import Namespace, OnlineStatus, ConnectionEvent
from meross_iot.model.exception import CommandTimeoutError, CommandError
from meross_iot.model.exception import UnconnectedError
from meross_iot.model.http.device import HttpDeviceInfo
Expand Down Expand Up @@ -319,6 +319,10 @@ def _on_disconnect(self, client, userdata, rc):
_LOGGER.warning("Client has been disconnected, however auto_reconnect flag is set. "
"Won't stop the looping thread, as it will retry to connect.")

# When a disconnection occurs, we need to set "unavailable" status.
asyncio.run_coroutine_threadsafe(self._notify_connection_event(ConnectionEvent.CONNECTION_DROP),
loop=self._loop)

def _on_unsubscribe(self):
# NOTE! This method is called by the paho-mqtt thread, thus any invocation to the
# asyncio platform must be scheduled via `self._loop.call_soon_threadsafe()` method.
Expand All @@ -327,11 +331,14 @@ def _on_unsubscribe(self):
def _on_subscribe(self, client, userdata, mid, granted_qos):
# NOTE! This method is called by the paho-mqtt thread, thus any invocation to the
# asyncio platform must be scheduled via `self._loop.call_soon_threadsafe()` method.
_LOGGER.debug("Succesfully subscribed to topics.")
_LOGGER.debug("Successfully subscribed to topics.")
self._loop.call_soon_threadsafe(
self._mqtt_connected_and_subscribed.set
)

asyncio.run_coroutine_threadsafe(self._notify_connection_event(ConnectionEvent.CONNECTION_ESTABLISHED),
loop=self._loop)

def _on_message(self, client, userdata, msg):
# NOTE! This method is called by the paho-mqtt thread, thus any invocation to the
# asyncio platform must be scheduled via `self._loop.call_soon_threadsafe()` method.
Expand Down Expand Up @@ -528,6 +535,15 @@ async def _async_send_and_wait_ack(self, future: Future, target_device_uuid: str
f"{target_device_uuid}. Timeout was: {timeout} seconds")
raise CommandTimeoutError()

async def _notify_connection_event(self, status: ConnectionEvent):
for d in self._device_registry.find_all_by():
payload = {
'online': {
'status': OnlineStatus.UNKNOWN.value
}
}
await d.async_handle_push_notification(namespace=Namespace.SYSTEM_ONLINE, data=payload)

def _build_mqtt_message(self, method: str, namespace: Namespace, payload: dict):
"""
Sends a message to the Meross MQTT broker, respecting the protocol payload.
Expand Down
5 changes: 5 additions & 0 deletions meross_iot/model/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
_LOGGER = logging.getLogger(__name__)


class ConnectionEvent(Enum):
CONNECTION_DROP = 1
CONNECTION_ESTABLISHED = 2


class OnlineStatus(Enum):
ONLINE = 1
OFFLINE = 2
Expand Down

0 comments on commit 460d0df

Please sign in to comment.