Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit 89983bf

Browse files
committed
TD fully describe Action objects
1 parent 5ada025 commit 89983bf

3 files changed

Lines changed: 58 additions & 33 deletions

File tree

example/single-thing.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,30 @@ async def fade_function(args):
6565
metadata={
6666
"title": "Fade",
6767
"description": "Fade the lamp to a given level",
68-
"input": {
69-
"type": "object",
70-
"required": [
71-
"brightness",
72-
"duration",
73-
],
74-
"properties": {
75-
"brightness": {
76-
"type": "integer",
77-
"minimum": 0,
78-
"maximum": 100,
79-
"unit": "percent",
80-
},
81-
"duration": {
82-
"type": "integer",
83-
"minimum": 1,
84-
"unit": "milliseconds",
85-
},
68+
},
69+
input_ = {
70+
"type": "object",
71+
"required": [
72+
"brightness",
73+
"duration",
74+
],
75+
"properties": {
76+
"brightness": {
77+
"type": "integer",
78+
"minimum": 0,
79+
"maximum": 100,
80+
"unit": "percent",
81+
},
82+
"duration": {
83+
"type": "integer",
84+
"minimum": 1,
85+
"unit": "milliseconds",
8686
},
8787
},
8888
},
89+
output = {
90+
"type": "string"
91+
}
8992
)
9093

9194
thing.add_action(fade_action)

webthing/action.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ def as_action_description(self):
4141
Returns a dictionary describing the action.
4242
"""
4343
description = {
44-
self.name: {
45-
"href": self.href_prefix + self.href,
46-
"timeRequested": self.time_requested,
47-
"status": self.status,
48-
},
44+
"name": self.name,
45+
"id": self.id,
46+
"href": self.href_prefix + self.href,
47+
"timeRequested": self.time_requested,
48+
"timeCompleted": self.time_completed,
49+
"status": self.status,
4950
}
5051

5152
if self.input is not None:
52-
description[self.name]["input"] = self.input
53+
description["input"] = self.input
5354

5455
if self.output is not None:
55-
description[self.name]["output"] = self.output
56-
57-
if self.time_completed is not None:
58-
description[self.name]["timeCompleted"] = self.time_completed
56+
description["output"] = self.output
5957

6058
return description
6159

@@ -135,12 +133,15 @@ def finish(self, future):
135133
class Action:
136134
"""An Action represents a class of actions on a thing."""
137135

138-
def __init__(self, thing, name, invokeaction=None, metadata=None):
136+
def __init__(self, thing, name, invokeaction=None, metadata=None, input_=None, output=None):
139137
self.thing = thing
140138
self.name = name
141139
self.href_prefix = ""
142140
self.href = "/actions/{}".format(self.name)
141+
143142
self.metadata = metadata if metadata is not None else {}
143+
self.input = input_ if input_ is not None else {}
144+
self.output = output if output is not None else {}
144145

145146
self.invokeaction_forwarder = invokeaction or (lambda: None)
146147

@@ -154,6 +155,25 @@ def as_action_description(self):
154155
"""
155156
description = deepcopy(self.metadata)
156157

158+
# Create WoT TD v1 input and output keys
159+
if "input" not in description:
160+
description["input"] = self.input
161+
162+
if "output" not in description:
163+
description["output"] = {
164+
"type": "object",
165+
"properties": {
166+
"name": {"type": "string"},
167+
"id": {"type": "string"},
168+
"href": {"type": "string", "format": "uri"},
169+
"timeRequested": {"type": "string", "format": "date-time"},
170+
"timeCompleted": {"type": "string", "format": "date-time"},
171+
"status": {"type": "string", "enum": ["created", "pending", "completed", "cancelled", "error"]},
172+
**({"output": self.output} if self.output else {}),
173+
**({"input": self.input} if self.input else {}),
174+
}
175+
}
176+
157177
# Create forms
158178
if "forms" not in description:
159179
description["forms"] = []

webthing/thing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,15 @@ def get_action_object_descriptions(self, action_name=None):
190190
191191
Returns the action descriptions.
192192
"""
193-
descriptions = []
194-
193+
195194
if action_name is None:
196-
for action in self.actions.values():
195+
descriptions = {}
196+
for name, action in self.actions.items():
197+
descriptions[name] = []
197198
for action_obj in action.queue:
198-
descriptions.append(action_obj.as_action_description())
199+
descriptions[name].append(action_obj.as_action_description())
199200
elif action_name in self.actions:
201+
descriptions = []
200202
for action_obj in self.actions[action_name].queue:
201203
descriptions.append(action_obj.as_action_description())
202204

0 commit comments

Comments
 (0)