Usermod class vars pt3:

Moved initDone, addToConfig and readFromConfig to Usermod superclass

Updated cleanup procedure:

Part 1
- remove bool enabled = false/true (now default false)
- remove static const char _name[] and _enabled[]
- add constructor which calls superclass (temp?): XXXUsermod(const char *name, bool enabled):Usermod(name, enabled) {} 
- replace _enabled with "enabled"
- remove const char PROGMEM init for  _name[] and _enabled[]
Part 2
- Remove bool initDone = false;
- addToConfig: replace createNestedObject with Usermod::addToConfig(root); JsonObject top = root[FPSTR(_name)];
- readFromConfig: replace !top.isNull and enabled with bool configComplete = Usermod::readFromConfig(root);JsonObject top = root[FPSTR(_name)];

See Temperature, MPU6050 and weather as examples (rest to be done)
This commit is contained in:
Ewoud
2023-03-16 15:41:23 +01:00
parent e3c359a4a4
commit c928df9d70
7 changed files with 34 additions and 32 deletions

View File

@@ -142,7 +142,6 @@ void httpGet(WiFiClient &client, const char *url, char *errorMessage) {
class WeatherUsermod : public Usermod {
private:
// strings to reduce flash memory usage (used more than twice)
static const char _name[]; //usermod name
String apiKey = ""; //config var
unsigned long lastTime = 0; //will be used to download new forecast every hour
@@ -150,6 +149,7 @@ class WeatherUsermod : public Usermod {
bool isConnected = false; //only call openweathermap if connected
public:
WeatherUsermod(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM: this shouldn't be necessary (passthrough of constructor), maybe because Usermod is an abstract class
void setup() {
strip.addEffect(255, &mode_2DWeather, _data_FX_MODE_2DWEATHER);
@@ -256,7 +256,6 @@ class WeatherUsermod : public Usermod {
/*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
* Below it is shown how this could be used for e.g. a light sensor
*/
void addToJsonInfo(JsonObject& root)
{
@@ -292,7 +291,8 @@ class WeatherUsermod : public Usermod {
void addToConfig(JsonObject& root)
{
JsonObject top = root.createNestedObject(FPSTR(_name));
Usermod::addToConfig(root);
JsonObject top = root[FPSTR(_name)];
top[F("apiKey")] = apiKey;
top[F("units")] = weather_units;
top[F("minTemp")] = weather_minTemp;
@@ -302,10 +302,9 @@ class WeatherUsermod : public Usermod {
bool readFromConfig(JsonObject& root)
{
bool configComplete = Usermod::readFromConfig(root);
JsonObject top = root[FPSTR(_name)];
bool configComplete = !top.isNull();
configComplete &= getJsonValue(top[F("apiKey")], apiKey);
configComplete &= getJsonValue(top[F("units")], weather_units);
configComplete &= getJsonValue(top[F("minTemp")], weather_minTemp);
@@ -317,8 +316,8 @@ class WeatherUsermod : public Usermod {
void appendConfigData()
{
oappend(SET_F("addInfo('Weather:help',0,'<button onclick=\"location.href=&quot;https://mm.kno.wled.ge/moonmodules/Weather&quot;\" type=\"button\">?</button>');"));
oappend(SET_F("addHB('Weather');")); // WLEDMM
oappend(SET_F("dd=addDropdown('Weather','units');"));
oappend(SET_F("addOption(dd,'Kelvin',0);"));
oappend(SET_F("addOption(dd,'Celcius',1);"));
@@ -349,9 +348,6 @@ class WeatherUsermod : public Usermod {
}
};
// strings to reduce flash memory usage (used more than twice)
const char WeatherUsermod::_name[] PROGMEM = "Weather";
// example openweathermap data
// {"cod":"200","message":0,"cnt":40,"list":[
// {"dt":1663945200,"main":{"temp":18.05,"feels_like":17.79,"temp_min":17.64,"temp_max":18.05,"pressure":1014,"sea_level":1014,"grnd_level":1013,"humidity":72,"temp_kf":0.41},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":100},"wind":{"speed":4.32,"deg":238,"gust":5.6},"visibility":10000,"pop":0.48,"rain":{"3h":0.15},"sys":{"pod":"d"},"dt_txt":"2022-09-23 15:00:00"},