Skip to content

Commit

Permalink
Better handle top level messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jtc42 committed May 5, 2020
1 parent bb35494 commit dd8f67c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/labthings/server/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .view import View
from .find import current_labthing
from .utilities import unpack
from .event import PropertyStatusEvent, ActionStatusEvent

from labthings.core.tasks.pool import TaskThread
from labthings.core.utilities import merge
Expand Down Expand Up @@ -122,6 +123,7 @@ def ThingAction(viewcls: View):
Returns:
View: View class with Action spec tags
"""
# TODO: Handle actionStatus messages
# Update Views API spec
tag_spec(viewcls, "actions")
return viewcls
Expand Down Expand Up @@ -190,8 +192,8 @@ def wrapped(*args, **kwargs):
)

if current_labthing():
current_labthing().emit(
"propertyStatus", {property_name: property_value},
current_labthing().message(
PropertyStatusEvent(property_name), property_value,
)

return original_response
Expand Down
33 changes: 28 additions & 5 deletions src/labthings/server/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@


class Event:
# Some event types are formatted slightly differently
magic_types = {"propertyStatus", "actionStatus"}

def __init__(self, name, schema=None):
self.name = name
self.schema = schema
Expand All @@ -13,9 +10,35 @@ def __init__(self, name, schema=None):

def emit(self, data):
response = {
"messageType": self.name if self.name in Event.magic_types else "event",
"messageType": "event",
"timestamp": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"data": data if self.name in Event.magic_types else {self.name: data},
"data": {self.name: data},
} # TODO: Format data with schema
self.events.append(response)
return response


class PropertyStatusEvent:
def __init__(self, property_name, schema=None):
self.name = property_name

def emit(self, data):
response = {
"messageType": "propertyStatus",
"timestamp": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"data": {self.name: data},
}
return response


class ActionStatusEvent:
def __init__(self, property_name, schema=None):
self.name = property_name

def emit(self, data):
response = {
"messageType": "actionStatus",
"timestamp": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"data": {self.name: data},
}
return response
13 changes: 9 additions & 4 deletions src/labthings/server/labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ def init_app(self, app):
self._create_base_sockets()

# Create base events
self.add_event("propertyStatus")
self.add_event("actionStatus")
self.add_event("logging")

def _create_base_routes(self):
Expand Down Expand Up @@ -336,9 +334,16 @@ def add_event(self, name, schema=None):

def emit(self, event_type: str, data: dict):
"""
Emit an event to all subscribers
Find a matching event type if one exists, and emit some data to it
"""
event_response = self.events[event_type].emit(data)
event_object = self.events[event_type]
self.message(event_object, data)

def message(self, event: Event, data: dict):
"""
Emit an event object to all subscribers
"""
event_response = event.emit(data)
for sub in self.subscribers:
sub.emit(event_response)

Expand Down
2 changes: 1 addition & 1 deletion src/labthings/server/spec/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def to_dict(self):

def event_to_thing_event(self, event: Event):
# TODO: Include event schema
return {"forms": []}
return {}

def view_to_thing_property(self, rules: list, view: View):
prop_urls = [rule_to_path(rule) for rule in rules]
Expand Down

0 comments on commit dd8f67c

Please sign in to comment.