SensESP 3.5.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
signalk_metadata.cpp
Go to the documentation of this file.
1#include "signalk_metadata.h"
2
3#include <cmath>
4
5namespace sensesp {
6
7SKMetadata::SKMetadata(const String& units, const String& display_name,
8 const String& description, const String& short_name,
9 float timeout, bool supports_put)
10 : display_name_{display_name},
11 units_{units},
12 description_{description},
13 short_name_{short_name},
14 timeout_{timeout},
15 supports_put_{supports_put} {}
16
17void SKMetadata::add_entry(const String& sk_path, JsonArray& meta) {
18 JsonObject json = meta.add<JsonObject>();
19 json["path"] = sk_path;
20 JsonObject val = json["value"].to<JsonObject>();
21
22 if (!this->display_name_.isEmpty()) {
23 val["displayName"] = this->display_name_;
24 }
25
26 if (!this->units_.isEmpty()) {
27 val["units"] = this->units_;
28 }
29
30 if (!this->description_.isEmpty()) {
31 val["description"] = this->description_;
32 }
33
34 if (!this->short_name_.isEmpty()) {
35 val["shortName"] = this->short_name_;
36 }
37
38 if (this->timeout_ >= 0.0) {
39 val["timeout"] = this->timeout_;
40 }
41
42 if (this->supports_put_) {
43 val["supportsPut"] = this->supports_put_;
44 }
45
46 if (!this->example_.isEmpty()) {
47 val["example"] = this->example_;
48 }
49
50 if (!std::isnan(this->display_scale_lower_) &&
51 !std::isnan(this->display_scale_upper_)) {
52 JsonObject scale = val["displayScale"].to<JsonObject>();
53 scale["lower"] = this->display_scale_lower_;
54 scale["upper"] = this->display_scale_upper_;
55 }
56
57 if (!this->zones_.empty()) {
58 JsonArray zones_arr = val["zones"].to<JsonArray>();
59 for (const SKMetadataZone& zone : this->zones_) {
60 JsonObject zone_obj = zones_arr.add<JsonObject>();
61 zone_obj["state"] = alarm_state_to_string(zone.state);
62 if (!zone.message.isEmpty()) {
63 zone_obj["message"] = zone.message;
64 }
65 if (!std::isnan(zone.lower)) zone_obj["lower"] = zone.lower;
66 if (!std::isnan(zone.upper)) zone_obj["upper"] = zone.upper;
67 }
68 }
69}
70
72 switch (state) {
74 return "nominal";
76 return "normal";
78 return "alert";
80 return "warn";
82 return "alarm";
84 return "emergency";
85 }
86 return "normal";
87}
88
89} // namespace sensesp
std::vector< SKMetadataZone > zones_
Alarm zones, emitted in the order they were added.
float display_scale_upper_
NAN = not set; displayScale is emitted only when both bounds are set.
virtual void add_entry(const String &sk_path, JsonArray &meta)
float display_scale_lower_
NAN = not set; displayScale is emitted only when both bounds are set.
static const char * alarm_state_to_string(SKAlarmState state)
SKMetadata()
Default constructor creates a blank Metadata structure.
SKAlarmState
Alarm state values for Signal K Zone metadata.
Represents a single Signal K alarm zone within metadata.