-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple-things.cpp
221 lines (192 loc) · 7.16 KB
/
multiple-things.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Webthing-CPP
// SPDX-FileCopyrightText: 2023-present Benno Waldhauer
// SPDX-License-Identifier: MIT
#include <csignal>
#include <filesystem>
#include <bw/webthing/webthing.hpp>
using namespace bw::webthing;
struct ExampleDimmableLight : public Thing
{
struct OverheatedEvent : public Event
{
OverheatedEvent(Thing* thing, double temperature)
: Event(thing, "overheated", temperature)
{
logger::warn("Overheated " + std::to_string(temperature));
}
};
struct FadeAction : public Action
{
FadeAction(Thing* thing, json fade_input)
: Action(generate_uuid(), thing, this, "fade", fade_input)
, cancel(false)
{
logger::info("Fade to " + fade_input["brightness"].dump() + " in " +
fade_input["duration"].dump() + "ms");
}
bool cancel;
void perform_action()
{
int duration = (*get_input())["duration"];
int destination = (*get_input())["brightness"];
bool interpolate = (*get_input()).value("interpolate", false);
if(!interpolate)
{
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
get_thing<Thing>()->set_property("brightness", destination);
}
else
{
int current = *get_thing<Thing>()->get_property<int>("brightness");
int steps = 1 + std::ceil(std::abs(destination - current) / 10);
int inc = 1 + (destination - current) / steps;
int sleep = std::ceil(duration / steps);
while(!cancel && current != destination)
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
if(cancel)
return;
int incremented = current + inc;
int next_step = inc > 0 ? (incremented < destination ? incremented : destination)
: (incremented > destination ? incremented : destination);
get_thing<Thing>()->set_property("brightness", next_step);
current = *get_thing<Thing>()->get_property<int>("brightness");
}
}
emit_event(get_thing<Thing>(), OverheatedEvent(get_thing<Thing>(), 102));
}
void cancel_action()
{
logger::info("Cancel fade instance " + get_id());
cancel = true;
}
};
ExampleDimmableLight() : Thing(
"urn:dev:ops:my-lamp-1234", "My Lamp", std::vector<std::string>{"OnOffSwitch", "Light"},
"A web connected lamp")
{
auto on_value = make_value(true, [](auto v){
logger::info("On-State is now " + std::string( v ? "on" : "off"));
});
link_property(this, "on", on_value, {
{"@type", "OnOffProperty"},
{"title", "On/Off"},
{"type", "boolean"},
{"description", "Whether the lamp is turned on"}});
auto brightness_value = make_value(50, [](auto v){
logger::info("Brightness is now " + std::to_string(v));
});
link_property(this, "brightness", brightness_value, {
{"@type", "BrightnessProperty"},
{"title", "Brightness"},
{"type", "integer"},
{"description", "The level of light from 0-100"},
{"minimum", 0},
{"maximum", 100},
{"unit", "percent"}});
link_action<FadeAction>(this, "fade", {
{"title", "Fade"},
{"description", "Fade the lamp to a given level"},
{"input", {
{"type", "object"},
{"required", {"brightness", "duration"}},
{"properties", {
{"brightness", {
{"type", "integer"},
{"minimum", 0},
{"maximum", 100},
{"unit", "percent"}}},
{"duration", {
{"type", "integer"},
{"minimum", 1},
{"unit", "milliseconds"}}},
{"interpolate", {
{"type", "boolean"},
{"default", false}}}}}}}});
link_event(this, "overheated", {
{"description", "The lamp has exceeded its safe operating temperature"},
{"type", "number"},
{"unit", "degree celsius"}});
}
};
struct FakeGpioHumiditySensor : public Thing
{
FakeGpioHumiditySensor() : Thing(
"urn:dev:ops:my-humidity-sensor-1234", "My Humidity Sensor",
"MultiLevelSensor", "A web connected humidity sensor")
{
level = make_value(0.0);
link_property(this, "level", level, {
{"@type", "LevelProperty"},
{"title", "Humidity"},
{"type", "number"},
{"description", "The current humidity in %"},
{"minimum", 0},
{"maximum", 100},
{"unit", "percent"},
{"readOnly", true}});
// Start a thread that polls the sensor reading every 3 seconds
runner = std::thread([this]{
while(read_from_sensor)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
if(!read_from_sensor)
return;
// Update the underlying value, which in turn notifies
// all listeners
double new_level = read_from_gpio();
logger::info("setting new humidity level: " + std::to_string(new_level));
level->notify_of_external_update(new_level);
}
});
}
void cancel()
{
logger::info("canceling the sensor");
read_from_sensor = false;
runner.join();
}
// Mimic an actual sensor updating its reading every couple seconds.
double read_from_gpio()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(0.0, 100.0);
return dist(gen);
}
std::shared_ptr<Value<double>> level;
std::thread runner;
bool read_from_sensor = true;
};
void run_server()
{
// Create a thing that represents a dimmable light
static ExampleDimmableLight light;
// Create a thing that represents a humidity sensor
static FakeGpioHumiditySensor sensor;
MultipleThings multiple_things({&light, &sensor}, "LightAndTempDevice");
try
{
// If adding more than one thing, use MultipleThings() with a name.
// In the single thing case, the thing's name will be broadcast.
static auto server = WebThingServer::host(multiple_things).port(8888).build();
std::signal(SIGINT, [](int signal) {
if (signal == SIGINT)
{
sensor.cancel();
server.stop();
}
});
server.start();
}
catch(std::exception& ex)
{
logger::error(std::string("exception when running server: ") + ex.what());
}
}
int main()
{
logger::set_level(log_level::info);
run_server();
return 0;
}