Line data Source code
1 : // Webthing-CPP
2 : // SPDX-FileCopyrightText: 2023-present Benno Waldhauer
3 : // SPDX-License-Identifier: MIT
4 :
5 : #pragma once
6 :
7 : #include <memory>
8 : #include <optional>
9 : #include <bw/webthing/json.hpp>
10 : #include <bw/webthing/utils.hpp>
11 :
12 : namespace bw::webthing {
13 :
14 : //#include <bw/webthing/thing.hpp>
15 : class Thing;
16 :
17 : // An Event represents an individual event from a thing.
18 : class Event
19 : {
20 : public:
21 2000027 : Event(Thing* thing, std::string name, std::optional<json> data = std::nullopt)
22 2000027 : : thing(thing)
23 2000027 : , name(name)
24 2000027 : , data(data)
25 2000027 : , time(bw::webthing::timestamp())
26 2000027 : {}
27 :
28 : // Get the event description of the event as a json object.
29 38 : json as_event_description() const
30 : {
31 38 : json description;
32 38 : description[name]["timestamp"] = time;
33 :
34 38 : if(data)
35 38 : description[name]["data"] = *data;
36 :
37 38 : return description;
38 0 : }
39 :
40 : Thing* get_thing() const
41 : {
42 : return thing;
43 : }
44 :
45 2000072 : std::string get_name() const
46 : {
47 2000072 : return name;
48 : }
49 :
50 : std::optional<json> get_data()
51 : {
52 : return data;
53 : }
54 :
55 : std::string get_time()
56 : {
57 : return time;
58 : }
59 :
60 : private:
61 : Thing* thing;
62 : std::string name;
63 : std::optional<json> data;
64 : std::string time;
65 : };
66 :
67 14 : inline json event_message(const Event& event)
68 : {
69 14 : json description = event.as_event_description();
70 : return json({
71 : {"messageType", "event"},
72 : {"data", description}
73 112 : });
74 84 : }
75 :
76 :
77 : } // bw::webthing
|