Skip to content

Commit 282d6c0

Browse files
committed
Started allowing more general event notifications
1 parent 66e6731 commit 282d6c0

File tree

5 files changed

+32
-59
lines changed

5 files changed

+32
-59
lines changed

src/labthings/server/decorators.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,20 @@ def wrapped(*args, **kwargs):
180180
# Call the update function first to update property value
181181
original_response = func(*args, **kwargs)
182182

183-
# Once updated, then notify all subscribers
184-
subscribers = getattr(current_labthing(), "subscribers", [])
185-
for sub in subscribers:
186-
sub.property_notify(viewcls)
183+
if hasattr(viewcls, "get_value") and callable(viewcls.get_value):
184+
property_value = viewcls().get_value()
185+
else:
186+
property_value = None
187+
188+
property_name = getattr(viewcls, "endpoint", None) or getattr(
189+
viewcls, "__name__", "unknown"
190+
)
191+
192+
if current_labthing():
193+
current_labthing().emit(
194+
{property_name: property_value}, type="propertyStatus"
195+
)
196+
187197
return original_response
188198

189199
return wrapped

src/labthings/server/labthing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ def _register_view(self, app, view, *urls, endpoint=None, **kwargs):
319319
self.thing_description.property(flask_rules, view)
320320
self._property_views[view.endpoint] = view
321321

322+
# Event stuff
323+
def emit(self, data: dict, type="event"):
324+
"""Emit an event to all subscribers
325+
326+
Arguments:
327+
data {dict} -- Event data
328+
329+
Keyword Arguments:
330+
type {str} -- Event type (default: {"event"})
331+
"""
332+
for sub in self.subscribers:
333+
sub.emit({"messageType": type, "data": data})
334+
322335
# Utilities
323336

324337
def url_for(self, view, **values):

src/labthings/server/logging.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ def emit(self, record):
1212
log_event = self.rest_format_record(record)
1313

1414
# Broadcast to subscribers
15-
subscribers = getattr(current_labthing(), "subscribers", [])
16-
for sub in subscribers:
17-
sub.event_notify(log_event)
15+
if current_labthing():
16+
current_labthing().emit(log_event)
1817

1918
def rest_format_record(self, record):
2019
data = {

src/labthings/server/sockets/base.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,9 @@ class SocketSubscriber:
1010
def __init__(self, ws):
1111
self.ws = ws
1212

13-
def property_notify(self, viewcls):
14-
if hasattr(viewcls, "get_value") and callable(viewcls.get_value):
15-
property_value = viewcls().get_value()
16-
else:
17-
property_value = None
18-
19-
property_name = getattr(viewcls, "endpoint", None) or getattr(
20-
viewcls, "__name__", "unknown"
21-
)
22-
23-
response = encode_json(
24-
{"messageType": "propertyStatus", "data": {property_name: property_value}}
25-
)
26-
27-
self.ws.send(response)
28-
29-
def event_notify(self, event_dict: dict):
30-
response = encode_json({"messageType": "event", "data": event_dict})
31-
13+
def emit(self, event: dict):
14+
response = encode_json(event)
15+
# TODO: Logic surrounding if this subscriber is subscribed to the requested event type
3216
self.ws.send(response)
3317

3418

tests/test_server_sockets.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,6 @@
44
from flask import Blueprint
55

66

7-
def test_socket_subscriber_property_notify(view_cls, fake_websocket):
8-
setattr(view_cls, "endpoint", "index")
9-
ws = fake_websocket("", recieve_once=True)
10-
sub = base.SocketSubscriber(ws)
11-
12-
sub.property_notify(view_cls)
13-
assert json.loads(ws.response) == {
14-
"messageType": "propertyStatus",
15-
"data": {"index": "GET"},
16-
}
17-
18-
19-
def test_socket_subscriber_property_notify_empty_view(flask_view_cls, fake_websocket):
20-
ws = fake_websocket("", recieve_once=True)
21-
sub = base.SocketSubscriber(ws)
22-
23-
sub.property_notify(flask_view_cls)
24-
assert json.loads(ws.response) == {
25-
"messageType": "propertyStatus",
26-
"data": {flask_view_cls.__name__: None},
27-
}
28-
29-
30-
def test_socket_subscriber_event_notify(fake_websocket):
31-
ws = fake_websocket("", recieve_once=True)
32-
sub = base.SocketSubscriber(ws)
33-
34-
data = {"key": "value"}
35-
36-
sub.event_notify(data)
37-
assert json.loads(ws.response) == {"messageType": "event", "data": data}
38-
39-
407
def test_sockets_flask_init(app):
418
original_wsgi_app = app.wsgi_app
429
socket = gsocket.Sockets(app)

0 commit comments

Comments
 (0)