forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampledValue.cpp
More file actions
100 lines (94 loc) · 3.65 KB
/
Copy pathSampledValue.cpp
File metadata and controls
100 lines (94 loc) · 3.65 KB
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
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2022
// MIT License
#include <ArduinoOcpp/Tasks/Metering/SampledValue.h>
#include <ArduinoOcpp/Debug.h>
using ArduinoOcpp::SampledValue;
//helper function
namespace ArduinoOcpp {
namespace Ocpp16 {
const char *serializeReadingContext(ReadingContext context) {
switch (context) {
case (ReadingContext::InterruptionBegin):
return "Interruption.Begin";
case (ReadingContext::InterruptionEnd):
return "Interruption.End";
case (ReadingContext::Other):
return "Other";
case (ReadingContext::SampleClock):
return "Sample.Clock";
case (ReadingContext::SamplePeriodic):
return "Sample.Periodic";
case (ReadingContext::TransactionBegin):
return "Transaction.Begin";
case (ReadingContext::TransactionEnd):
return "Transaction.End";
case (ReadingContext::Trigger):
return "Trigger";
default:
AO_DBG_ERR("ReadingContext not specified");
/* fall through */
case (ReadingContext::NOT_SET):
return nullptr;
}
}
ReadingContext deserializeReadingContext(const char *context) {
if (!context) {
AO_DBG_ERR("Invalid argument");
return ReadingContext::NOT_SET;
}
if (!strcmp(context, "NOT_SET")) {
AO_DBG_DEBUG("Deserialize Null-ReadingContext");
return ReadingContext::NOT_SET;
} else if (!strcmp(context, "Sample.Periodic")) {
return ReadingContext::SamplePeriodic;
} else if (!strcmp(context, "Sample.Clock")) {
return ReadingContext::SampleClock;
} else if (!strcmp(context, "Transaction.Begin")) {
return ReadingContext::TransactionBegin;
} else if (!strcmp(context, "Transaction.End")) {
return ReadingContext::TransactionEnd;
} else if (!strcmp(context, "Other")) {
return ReadingContext::Other;
} else if (!strcmp(context, "Interruption.Begin")) {
return ReadingContext::InterruptionBegin;
} else if (!strcmp(context, "Interruption.End")) {
return ReadingContext::InterruptionEnd;
} else if (!strcmp(context, "Trigger")) {
return ReadingContext::Trigger;
}
AO_DBG_ERR("ReadingContext not specified %.10s", context);
return ReadingContext::NOT_SET;
}
}} //end namespaces
std::unique_ptr<DynamicJsonDocument> SampledValue::toJson() {
auto value = serializeValue();
if (value.empty()) {
return nullptr;
}
size_t capacity = 0;
capacity += JSON_OBJECT_SIZE(8);
capacity += value.length() + 1
+ properties.getFormat().length() + 1
+ properties.getMeasurand().length() + 1
+ properties.getPhase().length() + 1
+ properties.getLocation().length() + 1
+ properties.getUnit().length() + 1;
auto result = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(capacity + 100)); //TODO remove safety space
auto payload = result->to<JsonObject>();
payload["value"] = value;
auto context_cstr = Ocpp16::serializeReadingContext(context);
if (context_cstr)
payload["context"] = context_cstr;
if (!properties.getFormat().empty())
payload["format"] = properties.getFormat();
if (!properties.getMeasurand().empty())
payload["measurand"] = properties.getMeasurand();
if (!properties.getPhase().empty())
payload["phase"] = properties.getPhase();
if (!properties.getLocation().empty())
payload["location"] = properties.getLocation();
if (!properties.getUnit().empty())
payload["unit"] = properties.getUnit();
return result;
}