mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 09:19:51 +03:00
add web for Modules
This commit is contained in:
@@ -37,12 +37,14 @@ WebSettingsService EMSESP::webSettingsService = WebSettingsService(&we
|
||||
WebCustomizationService EMSESP::webCustomizationService = WebCustomizationService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebSchedulerService EMSESP::webSchedulerService = WebSchedulerService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebCustomEntityService EMSESP::webCustomEntityService = WebCustomEntityService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebModulesService EMSESP::webModulesService = WebModulesService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#else
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
|
||||
WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebCustomizationService EMSESP::webCustomizationService = WebCustomizationService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebSchedulerService EMSESP::webSchedulerService = WebSchedulerService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebCustomEntityService EMSESP::webCustomEntityService = WebCustomEntityService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
WebModulesService EMSESP::webModulesService = WebModulesService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#endif
|
||||
|
||||
WebActivityService EMSESP::webActivityService = WebActivityService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
@@ -75,7 +77,6 @@ TemperatureSensor EMSESP::temperaturesensor_; // Temperature sensors
|
||||
AnalogSensor EMSESP::analogsensor_; // Analog sensors
|
||||
Shower EMSESP::shower_; // Shower logic
|
||||
Preferences EMSESP::nvs_; // NV Storage
|
||||
ModuleLibrary EMSESP::module_; // Module Library
|
||||
|
||||
// static/common variables
|
||||
uint16_t EMSESP::watch_id_ = WATCH_ID_NONE; // for when log is TRACE. 0 means no trace set
|
||||
@@ -1635,6 +1636,8 @@ void EMSESP::start() {
|
||||
analogsensor_.start(); // Analog external sensors
|
||||
webLogService.start(); // apply settings to weblog service
|
||||
|
||||
webModulesService.begin(); // setup the external library modules
|
||||
|
||||
// Load our library of known devices into stack mem. Names are stored in Flash memory
|
||||
device_library_ = {
|
||||
#include "device_library.h"
|
||||
@@ -1646,8 +1649,6 @@ void EMSESP::start() {
|
||||
#endif
|
||||
|
||||
webServer.begin(); // start the web server
|
||||
|
||||
module_.start(this); // setup the external library modules
|
||||
}
|
||||
|
||||
// main loop calling all services
|
||||
@@ -1666,7 +1667,7 @@ void EMSESP::loop() {
|
||||
publish_all_loop(); // with HA messages in parts to avoid flooding the mqtt queue
|
||||
mqtt_.loop(); // sends out anything in the MQTT queue
|
||||
webSchedulerService.loop(); // handle any scheduled jobs
|
||||
module_.loop(); // loop the external library modules
|
||||
webModulesService.loop(); // loop through the external library modules
|
||||
|
||||
// force a query on the EMS devices to fetch latest data at a set interval (1 min)
|
||||
scheduled_fetch_values();
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include "web/WebAPIService.h"
|
||||
#include "web/WebLogService.h"
|
||||
#include "web/WebCustomEntityService.h"
|
||||
#include "web/WebModulesService.h"
|
||||
|
||||
#include "emsdevicevalue.h"
|
||||
#include "emsdevice.h"
|
||||
@@ -222,7 +223,6 @@ class EMSESP {
|
||||
static RxService rxservice_;
|
||||
static TxService txservice_;
|
||||
static Preferences nvs_;
|
||||
static ModuleLibrary module_;
|
||||
|
||||
// web controllers
|
||||
static ESP8266React esp8266React;
|
||||
@@ -235,6 +235,7 @@ class EMSESP {
|
||||
static WebCustomizationService webCustomizationService;
|
||||
static WebSchedulerService webSchedulerService;
|
||||
static WebCustomEntityService webCustomEntityService;
|
||||
static WebModulesService webModulesService;
|
||||
|
||||
private:
|
||||
static std::string device_tostring(const uint8_t device_id);
|
||||
@@ -280,7 +281,6 @@ class EMSESP {
|
||||
#endif
|
||||
|
||||
protected:
|
||||
// EMSESP();
|
||||
static uuid::log::Logger logger_;
|
||||
};
|
||||
|
||||
|
||||
82
src/web/WebModulesService.cpp
Normal file
82
src/web/WebModulesService.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020-2024 Paul Derbyshire
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "emsesp.h"
|
||||
#include "WebModulesService.h"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
ModuleLibrary moduleLibrary; // Module Library
|
||||
EMSESP * emsesp_; // forward declaration
|
||||
|
||||
WebModulesService::WebModulesService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
|
||||
: _httpEndpoint(WebModules::read, WebModules::update, this, server, EMSESP_MODULES_SERVICE_PATH, securityManager, AuthenticationPredicates::IS_AUTHENTICATED)
|
||||
, _fsPersistence(WebModules::read, WebModules::update, this, fs, EMSESP_MODULES_FILE) {
|
||||
}
|
||||
|
||||
// load the settings when the service starts
|
||||
void WebModulesService::begin() {
|
||||
EMSESP::logger().info("Starting Modules service");
|
||||
moduleLibrary.start(emsesp_);
|
||||
|
||||
_fsPersistence.readFromFS(); // read the file from FS
|
||||
}
|
||||
|
||||
void WebModulesService::loop() {
|
||||
moduleLibrary.loop(); // loop the external library modules
|
||||
}
|
||||
|
||||
// this creates the modules file, saving it to the FS
|
||||
// and also calls when the Modules web page is refreshed
|
||||
void WebModules::read(WebModules & webModules, JsonObject root) {
|
||||
// EMSESP::logger().debug("module read called"); // TODO remove
|
||||
|
||||
JsonDocument doc_modules;
|
||||
JsonObject root_modules = doc_modules.to<JsonObject>();
|
||||
moduleLibrary.list(root_modules); // get list the external library modules, put in root json object
|
||||
|
||||
JsonArray modules = root["modules"].to<JsonArray>();
|
||||
uint8_t counter = 0;
|
||||
for (const JsonObject module : root_modules["modules"].as<JsonArray>()) {
|
||||
JsonObject mi = modules.add<JsonObject>();
|
||||
mi["id"] = counter++; // id is only used to render the table and must be unique
|
||||
mi["name"] = module["name"].as<std::string>();
|
||||
mi["author"] = module["author"].as<std::string>();
|
||||
mi["version"] = module["version"].as<std::string>();
|
||||
mi["status"] = module["status"].as<std::string>();
|
||||
mi["enabled"] = module["enabled"].as<bool>();
|
||||
}
|
||||
}
|
||||
|
||||
// read any Module settings from the FS settings
|
||||
// and then apply the enable/disable
|
||||
// it's also called on a save
|
||||
StateUpdateResult WebModules::update(JsonObject root, WebModules & webModules) {
|
||||
// EMSESP::logger().debug("module update called"); // TODO remove
|
||||
|
||||
if (root["modules"].is<JsonArray>()) {
|
||||
for (const JsonObject module : root["modules"].as<JsonArray>()) {
|
||||
// set enabled/disabled
|
||||
moduleLibrary.enable(module["name"], module["enabled"].as<bool>());
|
||||
}
|
||||
}
|
||||
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
50
src/web/WebModulesService.h
Normal file
50
src/web/WebModulesService.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020-2024 Paul Derbyshire
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef WebModulesService_h
|
||||
#define WebModulesService_h
|
||||
|
||||
#define EMSESP_MODULES_FILE "/config/emsespModules.json"
|
||||
#define EMSESP_MODULES_SERVICE_PATH "/rest/modules" // GET and POST
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class WebModules {
|
||||
public:
|
||||
static void read(WebModules & webModules, JsonObject root);
|
||||
static StateUpdateResult update(JsonObject root, WebModules & webModules);
|
||||
};
|
||||
|
||||
class WebModulesService : public StatefulService<WebModules> {
|
||||
public:
|
||||
WebModulesService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
|
||||
|
||||
void begin();
|
||||
void loop();
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
private:
|
||||
#endif
|
||||
|
||||
HttpEndpoint<WebModules> _httpEndpoint;
|
||||
FSPersistence<WebModules> _fsPersistence;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user