7
7
8
8
from labthings import create_app , semantics , find_component , fields
9
9
from labthings .views import ActionView , PropertyView , op
10
+ from labthings .example_components import PretendSpectrometer
11
+ from labthings .json import encode_json
10
12
11
13
12
14
"""
15
17
"""
16
18
17
19
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
-
54
20
"""
55
- Create a view to view and change our magic_denoise value,
21
+ Create a view to view and change our integration_time value,
56
22
and register is as a Thing property
57
23
"""
58
24
59
25
60
26
# Wrap in a semantic annotation to autmatically set schema and args
61
27
@semantics .moz .LevelProperty (100 , 500 , example = 200 )
62
28
class DenoiseProperty (PropertyView ):
63
- """Value of magic_denoise """
29
+ """Value of integration_time """
64
30
65
31
@op .readproperty
66
32
def get (self ):
67
33
# When a GET request is made, we'll find our attached component
68
34
my_component = find_component ("org.labthings.example.mycomponent" )
69
- return my_component .magic_denoise
35
+ return my_component .integration_time
70
36
71
37
@op .writeproperty
72
38
def put (self , new_property_value ):
73
39
# Find our attached component
74
40
my_component = find_component ("org.labthings.example.mycomponent" )
75
41
76
42
# Apply the new value
77
- my_component .magic_denoise = new_property_value
43
+ my_component .integration_time = new_property_value
78
44
79
- return my_component .magic_denoise
45
+ return my_component .integration_time
80
46
81
47
82
48
"""
@@ -96,6 +62,14 @@ def get(self):
96
62
my_component = find_component ("org.labthings.example.mycomponent" )
97
63
return my_component .data
98
64
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
+
99
73
100
74
"""
101
75
Create a view to start an averaged measurement, and register is as a Thing action
@@ -144,10 +118,13 @@ def cleanup():
144
118
)
145
119
146
120
# 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
+
148
125
149
126
# Add routes for the API views we created
150
- labthing .add_view (DenoiseProperty , "/denoise " )
127
+ labthing .add_view (DenoiseProperty , "/integration_time " )
151
128
labthing .add_view (QuickDataProperty , "/quick-data" )
152
129
labthing .add_view (MeasurementAction , "/actions/measure" )
153
130
0 commit comments