Skip to content

Commit

Permalink
fix: bug in serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanzyTHEbar committed Jun 20, 2023
1 parent 43727ae commit 8999751
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 50 deletions.
16 changes: 8 additions & 8 deletions Code/PlatformIO_Project/extras/data.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"ntp": "23:41:40Z",
"temperature": ["0.000"],
"ntp": "12:13:10Z",
"temperature": [0.0],
"humidity": {
"sht31_2_temp": "0.000",
"sht31_2_hum": "0.000",
"sht31_1_temp": "0.000",
"sht31_1_hum": "0.000",
"dht_temp": "0.000",
"dht_hum": "0.000"
"sht31_2_temp": 0.0,
"sht31_2_hum": 0.0,
"sht31_1_temp": 0.0,
"sht31_1_hum": 0.0,
"dht_temp": 0.0,
"dht_hum": 0.0
},
"ldr": 0.0,
"water_level_sensor": 0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,15 @@ void SensorSerializer<std::string>::visit(
template <>
void SensorSerializer<std::vector<float>>::visit(
SensorInterface<std::vector<float>>* sensor) {
std::string specifier_multi = "\"%.3f\",";
std::string specifier_single = "\"%.3f\"";

auto read = sensor->read();
auto beginning = read.begin();
auto end = read.end();

serializedData.assign(
Helpers::format_string("\"%s\":[", sensor->getSensorName().c_str()));
// check if the vector is of size 1 or if we are at the last element - and use
// iterators instead of index
for (auto it = beginning; it != end; ++it) {
serializedData.append(Helpers::format_string(
(read.size() == 1 || it == end) ? specifier_single : specifier_multi,
*it));
for (auto&& value : read) {
serializedData.append(Helpers::format_string("%.3f,", value));
}
// remove the last comma
serializedData.pop_back();
serializedData.append("]");
sensorName.assign(sensor->getSensorName());
value = read;
Expand All @@ -51,21 +44,15 @@ void SensorSerializer<std::vector<float>>::visit(
template <>
void SensorSerializer<std::vector<std::string>>::visit(
SensorInterface<std::vector<std::string>>* sensor) {
std::string specifier_multi = "\"%s\",";
std::string specifier_single = "\"%s\"";

auto read = sensor->read();
auto beginning = read.begin();
auto end = read.end();

serializedData.assign(
Helpers::format_string("\"%s\":[", sensor->getSensorName().c_str()));
// iterators instead of index
for (auto it = beginning; it != end; ++it) {
serializedData.append(Helpers::format_string(
(read.size() == 1 || it == end) ? specifier_single : specifier_multi,
*it));
for (auto&& value : read) {
serializedData.append(Helpers::format_string("\"%s\",", value.c_str()));
}
// remove the last comma
serializedData.pop_back();
serializedData.append("]");
sensorName.assign(sensor->getSensorName());
value = read;
Expand All @@ -75,19 +62,14 @@ void SensorSerializer<std::vector<std::string>>::visit(
template <>
void SensorSerializer<std::unordered_map<std::string, float>>::visit(
SensorInterface<std::unordered_map<std::string, float>>* sensor) {
const char* specifier_multi = "\"%s\":\"%.3f\",";
const char* specifier_single = "\"%s\":\"%.3f\"";

auto read = sensor->read();
auto beginning = read.begin();
auto end = read.end();

serializedData.assign(
Helpers::format_string("\"%s\":{", sensor->getSensorName().c_str()));
for (auto it = beginning; it != end; ++it) {
serializedData.append(Helpers::format_string(
(read.size() >= 1) ? specifier_multi : specifier_single,
it->first.c_str(), it->second));

for (auto&& kv : read) {
serializedData.append(Helpers::format_string("\"%s\":%.3f,",
kv.first.c_str(), kv.second));
}

// remove the last comma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ API::API(APIServer& server, GreenHouseConfig& configManager)
API::~API() {}

void API::setupServer() {
/* server.updateCommandHandlers("helloWorld",
[&](AsyncWebServerRequest* request) {
printHelloWorld();
request->send(200, "text/plain", "OK");
});
server.updateCommandHandlers(
"addRelay", [&](AsyncWebServerRequest* request) { addRelay(request); });
server.updateCommandHandlers(
"removeRelay",
[&](AsyncWebServerRequest* request) { removeRelay(request); }); */
server.begin();
}

Expand Down
4 changes: 2 additions & 2 deletions Code/PlatformIO_Project/wokwi.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[wokwi]
version = 1
elf = ".pio/build/esp32dev_debug/esp32dev_debug-v0.0.1-main.elf"
firmware = ".pio/build/esp32dev_debug/esp32dev_debug-v0.0.1-main.bin"
elf = ".pio/build/esp32dev_hardware_debug/esp32dev_hardware_debug-v0.0.1-main.elf"
firmware = ".pio/build/esp32dev_hardware_debug/esp32dev_hardware_debug-v0.0.1-main.bin"
[[net.forward]]
from = "localhost:8180"
to = "target:80"

0 comments on commit 8999751

Please sign in to comment.