implemented web server send moderator using std::map

This commit is contained in:
rljonesau 2018-12-12 06:42:52 +11:00
parent 4fc80e6b9c
commit 838fb98151

View file

@ -28,6 +28,7 @@
#include "pins.h" #include "pins.h"
#include "Index.h" #include "Index.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <map>
WebServer server(80); WebServer server(80);
@ -37,23 +38,54 @@ bool bTxWebData = false;
DynamicJsonBuffer jsonBuffer(512); // create a JSON buffer on the heap DynamicJsonBuffer jsonBuffer(512); // create a JSON buffer on the heap
struct sHistory { class CModerator {
float actualTemp; std::map<const char*, float> fMemory;
float desiredTemp; std::map<const char*, int> iMemory;
int runState; public:
int errState; bool check(const char* name, float value);
sHistory() { bool check(const char* name, int value);
reset(); void reset();
};
void reset() {
actualTemp = -1000;
desiredTemp = -1000;
runState = -1;
errState = -1;
};
}; };
sHistory History; void
CModerator::reset()
{
// install invalid values, retain maps (memory defrag reasons)
for(auto it = fMemory.begin(); it != fMemory.end(); ++it) it->second = -100;
for(auto it = iMemory.begin(); it != iMemory.end(); ++it) it->second = -100;
}
bool
CModerator::check(const char* name, float value)
{
bool retval = true;
auto it = fMemory.find(name);
if(it != fMemory.end()) {
retval = it->second != value;
it->second = value;
}
else {
fMemory[name] = value;
}
return retval;
}
bool
CModerator::check(const char* name, int value)
{
bool retval = true;
auto it = iMemory.find(name);
if(it != iMemory.end()) {
retval = it->second != value;
it->second = value;
}
else {
iMemory[name] = value;
}
return retval;
}
CModerator Moderator;
const int led = 13; const int led = 13;
@ -106,7 +138,7 @@ bool doWebServer(void) {
int numClients = webSocket.connectedClients(); int numClients = webSocket.connectedClients();
if(numClients != prevNumClients) { if(numClients != prevNumClients) {
prevNumClients = numClients; prevNumClients = numClients;
History.reset(); // force full update of params if number of clients change Moderator.reset(); // force full update of params if number of clients change
DebugPort.println("Changed number of web clients, resetting history"); DebugPort.println("Changed number of web clients, resetting history");
} }
@ -117,18 +149,15 @@ bool doWebServer(void) {
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
float tidyTemp = int(getActualTemperature() * 10) * 0.1f; // round to 0.1 resolution (hopefully!) float tidyTemp = int(getActualTemperature() * 10) * 0.1f; // round to 0.1 resolution (hopefully!)
if(History.actualTemp != tidyTemp) { // only send actual changes if(Moderator.check("CurrentTemp", tidyTemp)) {
History.actualTemp = tidyTemp;
root.set("CurrentTemp", tidyTemp); root.set("CurrentTemp", tidyTemp);
bSend = true; bSend = true;
} }
if(History.runState != getHeaterInfo().getRunState()) { // only send actual changes if(Moderator.check("RunState", getHeaterInfo().getRunState())) {
History.runState = getHeaterInfo().getRunState();
root.set("RunState", getHeaterInfo().getRunState()); root.set("RunState", getHeaterInfo().getRunState());
bSend = true; bSend = true;
} }
if(History.desiredTemp != getHeaterInfo().getTemperature_Desired()) { // only send actual changes if(Moderator.check("DesiredTemp", getHeaterInfo().getTemperature_Desired())) {
History.desiredTemp = getHeaterInfo().getTemperature_Desired();
root.set("DesiredTemp", getHeaterInfo().getTemperature_Desired()); root.set("DesiredTemp", getHeaterInfo().getTemperature_Desired());
bSend = true; bSend = true;
} }