Tidied BTCwebserver by moving CModerator class into a module
This commit is contained in:
parent
838fb98151
commit
ae51dd2271
|
@ -28,64 +28,17 @@
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
#include "Index.h"
|
#include "Index.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <map>
|
#include "Moderator.h"
|
||||||
|
|
||||||
|
|
||||||
WebServer server(80);
|
WebServer server(80);
|
||||||
WebSocketsServer webSocket = WebSocketsServer(81);
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
||||||
bool bRxWebData = false;
|
|
||||||
|
bool bRxWebData = false; // flags for OLED animation
|
||||||
bool bTxWebData = false;
|
bool bTxWebData = false;
|
||||||
|
|
||||||
DynamicJsonBuffer jsonBuffer(512); // create a JSON buffer on the heap
|
DynamicJsonBuffer jsonBuffer(512); // create a JSON buffer on the heap
|
||||||
|
|
||||||
class CModerator {
|
CModerator Moderator; // check for settings that are not actually changing, avoid sending these
|
||||||
std::map<const char*, float> fMemory;
|
|
||||||
std::map<const char*, int> iMemory;
|
|
||||||
public:
|
|
||||||
bool check(const char* name, float value);
|
|
||||||
bool check(const char* name, int value);
|
|
||||||
void reset();
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
@ -145,26 +98,27 @@ bool doWebServer(void) {
|
||||||
if(numClients) {
|
if(numClients) {
|
||||||
if(millis() > lastTx) { // moderate the delivery of new messages - we simply cannot send every pass of the main loop!
|
if(millis() > lastTx) { // moderate the delivery of new messages - we simply cannot send every pass of the main loop!
|
||||||
lastTx = millis() + 100;
|
lastTx = millis() + 100;
|
||||||
bool bSend = false;
|
|
||||||
|
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
||||||
float tidyTemp = int(getActualTemperature() * 10) * 0.1f; // round to 0.1 resolution (hopefully!)
|
|
||||||
if(Moderator.check("CurrentTemp", tidyTemp)) {
|
Moderator.shouldSend(false); // reset global should send flag
|
||||||
|
|
||||||
|
float tidyTemp = int(getActualTemperature() * 10) * 0.1f; // round to 0.1 resolution
|
||||||
|
if( Moderator.shouldSend("CurrentTemp", tidyTemp) )
|
||||||
root.set("CurrentTemp", tidyTemp);
|
root.set("CurrentTemp", tidyTemp);
|
||||||
bSend = true;
|
|
||||||
}
|
if( Moderator.shouldSend("RunState", getHeaterInfo().getRunState() ) )
|
||||||
if(Moderator.check("RunState", getHeaterInfo().getRunState())) {
|
|
||||||
root.set("RunState", getHeaterInfo().getRunState());
|
root.set("RunState", getHeaterInfo().getRunState());
|
||||||
bSend = true;
|
|
||||||
}
|
if( Moderator.shouldSend("DesiredTemp", getHeaterInfo().getTemperature_Desired() ) )
|
||||||
if(Moderator.check("DesiredTemp", getHeaterInfo().getTemperature_Desired())) {
|
|
||||||
root.set("DesiredTemp", getHeaterInfo().getTemperature_Desired());
|
root.set("DesiredTemp", getHeaterInfo().getTemperature_Desired());
|
||||||
bSend = true;
|
|
||||||
}
|
if( Moderator.shouldSend("ErrorState", getHeaterInfo().getErrState() ) )
|
||||||
|
root.set("ErrorState", getHeaterInfo().getErrState());
|
||||||
|
|
||||||
if(bSend) {
|
if( Moderator.shouldSend() ) { // test global should send flags
|
||||||
bTxWebData = true;
|
bTxWebData = true; // OLED tx data animation flag
|
||||||
String jsonToSend;
|
char jsonToSend[512];
|
||||||
root.printTo(jsonToSend);
|
root.printTo(jsonToSend);
|
||||||
webSocket.broadcastTXT(jsonToSend);
|
webSocket.broadcastTXT(jsonToSend);
|
||||||
}
|
}
|
||||||
|
@ -249,4 +203,3 @@ void interpretJsonCommand(char* pLine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
70
Arduino/BTCDieselHeater/Moderator.cpp
Normal file
70
Arduino/BTCDieselHeater/Moderator.cpp
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
#include "Moderator.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
CModerator::shouldSend(bool set)
|
||||||
|
{
|
||||||
|
_bShouldSend = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CModerator::shouldSend()
|
||||||
|
{
|
||||||
|
return _bShouldSend;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
for(auto it = cMemory.begin(); it != cMemory.end(); ++it) it->second = -100;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CModerator::shouldSend(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;
|
||||||
|
}
|
||||||
|
_bShouldSend |= retval;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CModerator::shouldSend(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;
|
||||||
|
}
|
||||||
|
_bShouldSend |= retval;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
CModerator::shouldSend(const char* name, unsigned char value)
|
||||||
|
{
|
||||||
|
bool retval = true;
|
||||||
|
auto it = cMemory.find(name);
|
||||||
|
if(it != cMemory.end()) {
|
||||||
|
retval = it->second != value;
|
||||||
|
it->second = value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cMemory[name] = value;
|
||||||
|
}
|
||||||
|
_bShouldSend |= retval;
|
||||||
|
return retval;
|
||||||
|
}
|
20
Arduino/BTCDieselHeater/Moderator.h
Normal file
20
Arduino/BTCDieselHeater/Moderator.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef __BTC_MODERATOR_H__
|
||||||
|
#define __BTC_MODERATOR_H__
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class CModerator {
|
||||||
|
bool _bShouldSend;
|
||||||
|
std::map<const char*, float> fMemory;
|
||||||
|
std::map<const char*, int> iMemory;
|
||||||
|
std::map<const char*, unsigned char> cMemory;
|
||||||
|
public:
|
||||||
|
void shouldSend(bool reset);
|
||||||
|
bool shouldSend();
|
||||||
|
bool shouldSend(const char* name, float value);
|
||||||
|
bool shouldSend(const char* name, int value);
|
||||||
|
bool shouldSend(const char* name, unsigned char value);
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __BTC_MODERATOR_H__
|
|
@ -15,11 +15,11 @@ const char* MAIN_PAGE PROGMEM = R"=====(
|
||||||
for(key in heater) {
|
for(key in heater) {
|
||||||
switch(key) {
|
switch(key) {
|
||||||
case "CurrentTemp":
|
case "CurrentTemp":
|
||||||
console.log("Actual temp = ", heater.CurrentTemp);
|
console.log("JSON Rx: CurrentTemp:", heater.CurrentTemp);
|
||||||
document.getElementById("TempCurrent").innerHTML = heater.CurrentTemp;
|
document.getElementById("TempCurrent").innerHTML = heater.CurrentTemp;
|
||||||
break;
|
break;
|
||||||
case "RunState":
|
case "RunState":
|
||||||
console.log("Runstate = ", heater.RunState);
|
console.log("JSON Rx: RunState:", heater.RunState);
|
||||||
if (heater.RunState == 0) {
|
if (heater.RunState == 0) {
|
||||||
document.getElementById("myonoffswitch").checked = false;
|
document.getElementById("myonoffswitch").checked = false;
|
||||||
document.getElementById("myonoffswitch").style = "block";
|
document.getElementById("myonoffswitch").style = "block";
|
||||||
|
@ -32,10 +32,13 @@ const char* MAIN_PAGE PROGMEM = R"=====(
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "DesiredTemp":
|
case "DesiredTemp":
|
||||||
console.log("Desired temp = ", heater.DesiredTemp);
|
console.log("JSON Rx: DesiredTemp:", heater.DesiredTemp);
|
||||||
document.getElementById("slide").value = heater.DesiredTemp;
|
document.getElementById("slide").value = heater.DesiredTemp;
|
||||||
document.getElementById("sliderAmount").innerHTML = heater.DesiredTemp;
|
document.getElementById("sliderAmount").innerHTML = heater.DesiredTemp;
|
||||||
break;
|
break;
|
||||||
|
case "ErrorState":
|
||||||
|
console.log("JSON Rx: ErrorState:", heater.ErrorState);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +85,7 @@ function OnOffCheck(){
|
||||||
var checkBox = document.getElementById("myonoffswitch");
|
var checkBox = document.getElementById("myonoffswitch");
|
||||||
|
|
||||||
// Send a message to the Devel console of web browser for debugging
|
// Send a message to the Devel console of web browser for debugging
|
||||||
console.log(document.getElementById("myonoffswitch").checked);
|
console.log("OnOffCheck:", document.getElementById("myonoffswitch").checked);
|
||||||
|
|
||||||
// If the checkbox is checked, display the output text
|
// If the checkbox is checked, display the output text
|
||||||
// We also need to send a message back into the esp as we cannot directly run Arduino Functions from within the javascript
|
// We also need to send a message back into the esp as we cannot directly run Arduino Functions from within the javascript
|
||||||
|
@ -94,7 +97,7 @@ function OnOffCheck(){
|
||||||
const cmd = { RunState: 1 };
|
const cmd = { RunState: 1 };
|
||||||
var str = JSON.stringify(cmd);
|
var str = JSON.stringify(cmd);
|
||||||
|
|
||||||
console.log("Sending: ", str);
|
console.log("JSON Tx:", str);
|
||||||
Socket.send(str);
|
Socket.send(str);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -104,7 +107,7 @@ function OnOffCheck(){
|
||||||
const cmd = { RunState: 0 };
|
const cmd = { RunState: 0 };
|
||||||
var str = JSON.stringify(cmd);
|
var str = JSON.stringify(cmd);
|
||||||
|
|
||||||
console.log("Sending: ", str);
|
console.log("JSON Tx:", str);
|
||||||
Socket.send(str);
|
Socket.send(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,13 +115,11 @@ function OnOffCheck(){
|
||||||
function onSlide(newVal) {
|
function onSlide(newVal) {
|
||||||
document.getElementById("sliderAmount").innerHTML = newVal;
|
document.getElementById("sliderAmount").innerHTML = newVal;
|
||||||
|
|
||||||
console.log("Sending desired temp", newVal);
|
|
||||||
|
|
||||||
const cmd = { DesiredTemp: 0 };
|
const cmd = { DesiredTemp: 0 };
|
||||||
cmd.DesiredTemp = newVal;
|
cmd.DesiredTemp = newVal;
|
||||||
var str = JSON.stringify(cmd);
|
var str = JSON.stringify(cmd);
|
||||||
|
|
||||||
console.log("Sending: ", str);
|
console.log("JSON Tx:", str);
|
||||||
Socket.send(str);
|
Socket.send(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue