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

Commit 36a38a9

Browse files
committed
Added forms and async
1 parent 12cb29a commit 36a38a9

1 file changed

Lines changed: 42 additions & 15 deletions

File tree

webthing/property.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,25 @@ def __init__(
1515
self,
1616
thing,
1717
name,
18-
initial_value=None,
19-
writeproperty=None,
20-
readproperty=None,
18+
value,
2119
metadata=None,
20+
content_type="application/json",
2221
):
2322
"""
2423
Initialize the object.
2524
thing -- the Thing this property belongs to
2625
name -- name of the property
27-
writeproperty -- Callable to pass value updates to
28-
readproperty -- Callable to obtain the property value
26+
value -- Value object to hold the property value
2927
metadata -- property metadata, i.e. type, description, unit, etc.,
3028
as a dict
3129
"""
32-
self.value = Value(
33-
initial_value=initial_value,
34-
read_forwarder=readproperty,
35-
write_forwarder=writeproperty,
36-
)
37-
30+
self.value = value
3831
self.thing = thing
3932
self.name = name
4033
self.href_prefix = ""
4134
self.href = "/properties/{}".format(self.name)
4235
self.metadata = metadata if metadata is not None else {}
36+
self.content_type = content_type
4337

4438
# Add the property change observer to notify the Thing about a property
4539
# change.
@@ -67,6 +61,11 @@ def as_property_description(self):
6761
"""
6862
description = deepcopy(self.metadata)
6963

64+
# Imply readonly if not explicitly given
65+
if "readOnly" not in description and self.value.readonly:
66+
description["readOnly"] = True
67+
68+
# Create links
7069
if "links" not in description:
7170
description["links"] = []
7271

@@ -76,6 +75,30 @@ def as_property_description(self):
7675
"href": self.href_prefix + self.href,
7776
}
7877
)
78+
79+
# Create forms
80+
if "forms" not in description:
81+
description["forms"] = []
82+
83+
description["forms"].append(
84+
{
85+
"op": "readproperty",
86+
"href": self.href_prefix + self.href,
87+
"contentType": self.content_type,
88+
"htv:methodName": "GET",
89+
}
90+
)
91+
92+
if not description.get("readOnly", False):
93+
description["forms"].append(
94+
{
95+
"op": "writeproperty",
96+
"href": self.href_prefix + self.href,
97+
"contentType": self.content_type,
98+
"htv:methodName": "PUT",
99+
}
100+
)
101+
79102
return description
80103

81104
def set_href_prefix(self, prefix):
@@ -94,22 +117,22 @@ def get_href(self):
94117
"""
95118
return self.href_prefix + self.href
96119

97-
def get_value(self):
120+
async def get_value(self):
98121
"""
99122
Get the current property value.
100123
101124
Returns the value.
102125
"""
103-
return self.value.get()
126+
return await self.value.get()
104127

105-
def set_value(self, value):
128+
async def set_value(self, value):
106129
"""
107130
Set the current value of the property.
108131
109132
value -- the value to set
110133
"""
111134
self.validate_value(value)
112-
self.value.set(value)
135+
await self.value.set(value)
113136

114137
def get_name(self):
115138
"""
@@ -126,3 +149,7 @@ def get_thing(self):
126149
def get_metadata(self):
127150
"""Get the metadata associated with this property."""
128151
return self.metadata
152+
153+
def get_content_type(self):
154+
"""Get the content type of this property."""
155+
return self.content_type

0 commit comments

Comments
 (0)