2018-12-13 08:42:35 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-12 09:03:44 +00:00
|
|
|
#ifndef __BTC_MODERATOR_H__
|
|
|
|
#define __BTC_MODERATOR_H__
|
|
|
|
|
|
|
|
#include <map>
|
2018-12-13 12:19:10 +00:00
|
|
|
#include <ArduinoJSON.h>
|
2018-12-12 09:03:44 +00:00
|
|
|
|
2018-12-13 12:19:10 +00:00
|
|
|
template <class T>
|
|
|
|
class TModerator {
|
|
|
|
std::map<const char*, T> Memory;
|
2018-12-12 09:03:44 +00:00
|
|
|
public:
|
2018-12-13 12:19:10 +00:00
|
|
|
bool shouldSend(const char* name, T value);
|
2018-12-15 09:34:58 +00:00
|
|
|
bool addJson(const char* name, T value, JsonObject& root);
|
2018-12-12 09:03:44 +00:00
|
|
|
void reset();
|
|
|
|
};
|
|
|
|
|
2018-12-13 12:19:10 +00:00
|
|
|
template<class T>
|
|
|
|
bool TModerator<T>::shouldSend(const char* name, T value)
|
|
|
|
{
|
|
|
|
bool retval = true;
|
|
|
|
auto it = Memory.find(name);
|
|
|
|
if(it != Memory.end()) {
|
|
|
|
retval = it->second != value;
|
|
|
|
it->second = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Memory[name] = value;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-12-15 09:34:58 +00:00
|
|
|
bool TModerator<T>::addJson(const char* name, T value, JsonObject& root)
|
2018-12-13 12:19:10 +00:00
|
|
|
{
|
|
|
|
bool retval;
|
|
|
|
if( retval = shouldSend(name, value ) )
|
|
|
|
root.set(name, value);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-12-15 09:34:58 +00:00
|
|
|
|
2018-12-13 12:19:10 +00:00
|
|
|
template<class T>
|
|
|
|
void TModerator<T>::reset()
|
|
|
|
{
|
|
|
|
for(auto it = Memory.begin(); it != Memory.end(); ++it) {
|
2018-12-16 02:45:13 +00:00
|
|
|
if(std::is_pointer<T>::value) {
|
|
|
|
it->second = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
it->second = it->second+100;
|
|
|
|
}
|
2018-12-13 12:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CModerator {
|
|
|
|
TModerator<int> iModerator;
|
|
|
|
TModerator<float> fModerator;
|
|
|
|
TModerator<unsigned char> ucModerator;
|
2018-12-16 02:45:13 +00:00
|
|
|
TModerator<const char*> szModerator;
|
2018-12-13 12:19:10 +00:00
|
|
|
public:
|
2018-12-16 02:45:13 +00:00
|
|
|
// integer values
|
2018-12-15 09:34:58 +00:00
|
|
|
bool shouldSend(const char* name, int value) {
|
|
|
|
return iModerator.shouldSend(name, value);
|
|
|
|
};
|
|
|
|
bool addJson(const char* name, int value, JsonObject& root) {
|
|
|
|
return iModerator.addJson(name, value, root);
|
2018-12-13 12:19:10 +00:00
|
|
|
};
|
2018-12-16 02:45:13 +00:00
|
|
|
// float values
|
|
|
|
bool shouldSend(const char* name, float value) {
|
|
|
|
return fModerator.shouldSend(name, value);
|
|
|
|
};
|
2018-12-15 09:34:58 +00:00
|
|
|
bool addJson(const char* name, float value, JsonObject& root) {
|
|
|
|
return fModerator.addJson(name, value, root);
|
2018-12-13 12:19:10 +00:00
|
|
|
};
|
2018-12-16 02:45:13 +00:00
|
|
|
// unsigned char values
|
|
|
|
bool shouldSend(const char* name, unsigned char value) {
|
|
|
|
return ucModerator.shouldSend(name, value);
|
|
|
|
};
|
2018-12-15 09:34:58 +00:00
|
|
|
bool addJson(const char* name, unsigned char value, JsonObject& root) {
|
|
|
|
return ucModerator.addJson(name, value, root);
|
2018-12-13 12:19:10 +00:00
|
|
|
};
|
2018-12-16 02:45:13 +00:00
|
|
|
// const char* values
|
|
|
|
bool shouldSend(const char* name, const char* value) {
|
|
|
|
return szModerator.shouldSend(name, value);
|
|
|
|
};
|
|
|
|
bool addJson(const char* name, const char* value, JsonObject& root) {
|
|
|
|
return szModerator.addJson(name, value, root);
|
|
|
|
};
|
|
|
|
// force changes on all held values
|
2018-12-13 12:19:10 +00:00
|
|
|
void reset() {
|
|
|
|
iModerator.reset();
|
|
|
|
fModerator.reset();
|
|
|
|
ucModerator.reset();
|
2018-12-16 02:45:13 +00:00
|
|
|
szModerator.reset();
|
2018-12-13 12:19:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-12-12 09:03:44 +00:00
|
|
|
#endif // __BTC_MODERATOR_H__
|