Skip to content

Commit

Permalink
Allow forwarding Node Events (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Jul 25, 2023
1 parent b630c31 commit 2b20d42
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
14 changes: 14 additions & 0 deletions matter_server/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
EventMessage,
EventType,
MatterNodeData,
MatterNodeEvent,
MessageType,
ResultMessageBase,
ServerDiagnostics,
Expand Down Expand Up @@ -486,6 +487,19 @@ def _handle_event_message(self, msg: EventMessage) -> None:
node_id = msg.data["node_id"]
endpoint_id = msg.data["endpoint_id"]
self.logger.debug("Endpoint added: %s/%s", node_id, endpoint_id)
if msg.event == EventType.NODE_EVENT:
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug(
"Node event: %s",
msg.data,
)
node_event = dataclass_from_dict(MatterNodeEvent, msg.data)
self._signal_event(
EventType.NODE_EVENT,
data=node_event,
node_id=node_event.node_id,
)
return
# simply forward all other events as-is
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug("Received event: %s", msg)
Expand Down
15 changes: 15 additions & 0 deletions matter_server/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ class MatterNodeData:
)


@dataclass
class MatterNodeEvent:
"""Representation of a NodeEvent for a Matter node."""

node_id: int
endpoint_id: int
cluster_id: int
event_id: int
event_number: int
priority: int
timestamp: int
timestamp_type: int
data: dict[str, Any] | None


@dataclass
class ServerDiagnostics:
"""Full dump of the server information and data."""
Expand Down
17 changes: 14 additions & 3 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
dataclass_from_dict,
parse_attribute_path,
)
from ..common.models import APICommand, EventType, MatterNodeData
from ..common.models import APICommand, EventType, MatterNodeData, MatterNodeEvent
from .const import PAA_ROOT_CERTS_DIR
from .helpers.paa_certificates import fetch_certificates

Expand Down Expand Up @@ -674,9 +674,20 @@ def event_callback(
node_logger.debug(
"Received node event: %s - transaction: %s", data, transaction
)
self.event_history.append(data)
node_event = MatterNodeEvent(
node_id=node_id,
endpoint_id=data.Header.EndpointId,
cluster_id=data.Header.ClusterId,
event_id=data.Header.EventId,
event_number=data.Header.EventNumber,
priority=data.Header.Priority,
timestamp=data.Header.Timestamp,
timestamp_type=data.Header.TimestampType,
data=data.Data,
)
self.event_history.append(node_event)
self.server.loop.call_soon_threadsafe(
self.server.signal_event, EventType.NODE_EVENT, data
self.server.signal_event, EventType.NODE_EVENT, node_event
)

def error_callback(
Expand Down

0 comments on commit 2b20d42

Please sign in to comment.