serializers and deserializers
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "HTTPClient.h"
|
||||||
|
|
||||||
|
HTTPClient http;
|
||||||
|
|
||||||
String serializeSensorValue (
|
String serializeSensorValue (
|
||||||
int sensorId,
|
int sensorId,
|
||||||
@@ -15,14 +18,28 @@ String serializeSensorValue (
|
|||||||
);
|
);
|
||||||
|
|
||||||
String serializeActuatorStatus (
|
String serializeActuatorStatus (
|
||||||
|
int actuatorId,
|
||||||
|
int deviceId,
|
||||||
|
int status,
|
||||||
|
long timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
String serializeDevice (
|
String serializeDevice (
|
||||||
|
int sensorId,
|
||||||
|
int deviceId,
|
||||||
|
String sensorType,
|
||||||
|
int status,
|
||||||
|
long timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
void deserializeSensorValue (
|
void deserializeSensorValue (
|
||||||
|
int httpResponseCode
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeDevice (
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
@@ -2,24 +2,11 @@
|
|||||||
|
|
||||||
String response;
|
String response;
|
||||||
|
|
||||||
String serializeSensorValue (
|
|
||||||
int sensorId,
|
String serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp)
|
||||||
int deviceId,
|
|
||||||
String sensorType,
|
|
||||||
String unit,
|
|
||||||
int sensorStatus,
|
|
||||||
float temperature,
|
|
||||||
float humidity,
|
|
||||||
float carbonMonoxide,
|
|
||||||
float lat,
|
|
||||||
float lon,
|
|
||||||
long timestamp
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
DynamicJsonDocument doc(2048);
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
String output;
|
|
||||||
|
|
||||||
doc["sensorId"] = sensorId;
|
doc["sensorId"] = sensorId;
|
||||||
doc["deviceId"] = deviceId;
|
doc["deviceId"] = deviceId;
|
||||||
doc["sensorType"] = sensorType;
|
doc["sensorType"] = sensorType;
|
||||||
@@ -32,7 +19,161 @@ String serializeSensorValue (
|
|||||||
doc["lon"] = lon;
|
doc["lon"] = lon;
|
||||||
doc["timestamp"] = timestamp;
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
serializeJson(doc, output);
|
serializeJson(doc, output);
|
||||||
Serial.println(output);
|
Serial.println(output);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String serializeActuatorStatus (int actuatorId, int deviceId, int status, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["actuatorId"] = actuatorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["status"] = status;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
String serializeDevice(int sensorId, int deviceId, String sensorType, int status, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["sensorId"] = sensorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["sensorType"] = sensorType;
|
||||||
|
doc["status"] = status;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeSensorValue (int httpResponseCode){
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject sensor : array)
|
||||||
|
{
|
||||||
|
int sensorId = sensor["sensorId"];
|
||||||
|
int deviceId = sensor["deviceId"];
|
||||||
|
String sensorType = sensor["sensorType"];
|
||||||
|
String unit = sensor["unit"];
|
||||||
|
int sesnsorStatuts = sensor["sesnsorStatuts"];
|
||||||
|
float temperature = sensor["temperature"];
|
||||||
|
float humidity = sensor["humidity"];
|
||||||
|
float carbonMonoxide = sensor["carbonMonoxide"];
|
||||||
|
float lat = sensor["lat"];
|
||||||
|
float lon = sensor["lon"];
|
||||||
|
long timestamp = sensor["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) + ", deviceId: " + String(deviceId) + ", sensorType: " + sensorType + ", unit: " + unit +", sesnsorStatuts: " + String(sesnsorStatuts) +", temperature: " + String(temperature) +", humidity: " + String(humidity) +", carbonMonoxide: " + String(carbonMonoxide) +", lat: " + String(lat) +", lon: " + String(lon) +", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (int httpResponseCode){
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject actuator : array)
|
||||||
|
{
|
||||||
|
int actuadorId = actuator["actuadorId"];
|
||||||
|
int deviceId = actuator["deviceId"];
|
||||||
|
int statuts = actuator["statuts"];
|
||||||
|
long timestamp = actuator["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Actuador deserialized: [actuadorId: " + String(actuadorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", statuts: " + String(statuts) +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeDevice (int httpResponseCode){
|
||||||
|
|
||||||
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
String responseJson = http.getString();
|
||||||
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
|
Serial.println(error.f_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray array = doc.as<JsonArray>();
|
||||||
|
for (JsonObject device : array)
|
||||||
|
{
|
||||||
|
int sensorId = device["sensorId"];
|
||||||
|
int deviceId = device["deviceId"];
|
||||||
|
String sensorType = device["sensorType"];
|
||||||
|
long timestamp = device["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", sensorType: " + sensorType +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user