Skip to content

Commit 5ca0295

Browse files
author
Joel Collins
committed
Added websocket view example
1 parent cf12312 commit 5ca0295

File tree

1 file changed

+20
-43
lines changed

1 file changed

+20
-43
lines changed

examples/simple_thing.py

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from labthings import create_app, semantics, find_component, fields
99
from labthings.views import ActionView, PropertyView, op
10+
from labthings.example_components import PretendSpectrometer
11+
from labthings.json import encode_json
1012

1113

1214
"""
@@ -15,68 +17,32 @@
1517
"""
1618

1719

18-
class MyComponent:
19-
def __init__(self):
20-
self.x_range = range(-100, 100)
21-
self.magic_denoise = 200
22-
23-
def noisy_pdf(self, x, mu=0.0, sigma=25.0):
24-
"""
25-
Generate a noisy gaussian function (to act as some pretend data)
26-
27-
Our noise is inversely proportional to self.magic_denoise
28-
"""
29-
x = float(x - mu) / sigma
30-
return (
31-
math.exp(-x * x / 2.0) / math.sqrt(2.0 * math.pi) / sigma
32-
+ (1 / self.magic_denoise) * random.random()
33-
)
34-
35-
@property
36-
def data(self):
37-
"""Return a 1D data trace."""
38-
return [self.noisy_pdf(x) for x in self.x_range]
39-
40-
def average_data(self, n: int):
41-
"""Average n-sets of data. Emulates a measurement that may take a while."""
42-
summed_data = self.data
43-
44-
logging.warning("Starting an averaged measurement. This may take a while...")
45-
for _ in range(n):
46-
summed_data = [summed_data[i] + el for i, el in enumerate(self.data)]
47-
time.sleep(0.1)
48-
49-
summed_data = [i / n for i in summed_data]
50-
51-
return summed_data
52-
53-
5420
"""
55-
Create a view to view and change our magic_denoise value,
21+
Create a view to view and change our integration_time value,
5622
and register is as a Thing property
5723
"""
5824

5925

6026
# Wrap in a semantic annotation to autmatically set schema and args
6127
@semantics.moz.LevelProperty(100, 500, example=200)
6228
class DenoiseProperty(PropertyView):
63-
"""Value of magic_denoise"""
29+
"""Value of integration_time"""
6430

6531
@op.readproperty
6632
def get(self):
6733
# When a GET request is made, we'll find our attached component
6834
my_component = find_component("org.labthings.example.mycomponent")
69-
return my_component.magic_denoise
35+
return my_component.integration_time
7036

7137
@op.writeproperty
7238
def put(self, new_property_value):
7339
# Find our attached component
7440
my_component = find_component("org.labthings.example.mycomponent")
7541

7642
# Apply the new value
77-
my_component.magic_denoise = new_property_value
43+
my_component.integration_time = new_property_value
7844

79-
return my_component.magic_denoise
45+
return my_component.integration_time
8046

8147

8248
"""
@@ -96,6 +62,14 @@ def get(self):
9662
my_component = find_component("org.labthings.example.mycomponent")
9763
return my_component.data
9864

65+
@op.observeproperty
66+
def websocket(self, ws):
67+
# Find our attached component
68+
my_component = find_component("org.labthings.example.mycomponent")
69+
while not ws.closed:
70+
ws.send(encode_json(my_component.data))
71+
# time.sleep(0.5)
72+
9973

10074
"""
10175
Create a view to start an averaged measurement, and register is as a Thing action
@@ -144,10 +118,13 @@ def cleanup():
144118
)
145119

146120
# Attach an instance of our component
147-
labthing.add_component(MyComponent(), "org.labthings.example.mycomponent")
121+
# Usually a Python object controlling some piece of hardware
122+
my_spectrometer = PretendSpectrometer()
123+
labthing.add_component(my_spectrometer, "org.labthings.example.mycomponent")
124+
148125

149126
# Add routes for the API views we created
150-
labthing.add_view(DenoiseProperty, "/denoise")
127+
labthing.add_view(DenoiseProperty, "/integration_time")
151128
labthing.add_view(QuickDataProperty, "/quick-data")
152129
labthing.add_view(MeasurementAction, "/actions/measure")
153130

0 commit comments

Comments
 (0)