Skip to content

Commit

Permalink
code cleanup + logging set for once at boot time (debug mode requires…
Browse files Browse the repository at this point in the history
… restart)
  • Loading branch information
mathieucarbou committed Dec 30, 2024
1 parent 32a6d6a commit 26cbc8f
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 182 deletions.
35 changes: 8 additions & 27 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@
#include <MycilaWebSerial.h>
#endif

#include <YaSolRDefines.h>
#include <yasolr_macros.h>

// web server
extern AsyncWebServer webServer;
extern LoggingMiddleware loggingMiddleware;
extern ESPDash dashboard;
extern Mycila::Task dashboardInitTask;
extern Mycila::Task dashboardUpdateTask;
extern void yasolr_init_web_server();

// Config
extern Mycila::Config config;
extern void yasolr_init_config();

// Network
extern Mycila::ESPConnect espConnect;
extern Mycila::Task networkStartTask;
extern void yasolr_init_network();

// grid electricity
Expand All @@ -81,15 +80,9 @@ extern float yasolr_frequency();
// logging
extern Mycila::Logger logger;
extern void yasolr_init_logging();
extern void yasolr_configure_logging();

// router
extern Mycila::PID pidController;
extern Mycila::Router router;

// JSY
extern Mycila::JSY* jsy;
extern Mycila::Task* jsyTask;
extern Mycila::TaskManager* jsyTaskManager;
extern void yasolr_init_jsy();

Expand All @@ -103,33 +96,28 @@ extern void yasolr_init_jsy_remote();
extern Mycila::DS18* ds18O1;
extern Mycila::DS18* ds18O2;
extern Mycila::DS18* ds18Sys;
extern Mycila::Task* ds18Task;
extern void yasolr_init_ds18();

// Display
extern Mycila::EasyDisplay* display;
extern Mycila::Task* displayCarouselTask;
extern Mycila::Task* displayTask;
extern void yasolr_init_display();

// ZCD
extern Mycila::PulseAnalyzer* pulseAnalyzer;
extern void yasolr_init_zcd();

// MQTT
extern Mycila::MQTT* mqtt;
extern Mycila::Task* mqttConnectTask;
extern Mycila::Task* mqttPublishConfigTask;
extern Mycila::Task* mqttPublishStaticTask;
extern Mycila::Task* mqttPublishTask;
extern Mycila::Task* haDiscoveryTask;
extern void yasolr_init_mqtt();

// PZEM
extern Mycila::PZEM* pzemO1;
extern Mycila::PZEM* pzemO2;
extern Mycila::Task* pzemO1PairingTask;
extern Mycila::Task* pzemO2PairingTask;
extern Mycila::Task* pzemTask;
extern Mycila::TaskManager* pzemTaskManager;
extern void yasolr_init_pzem();

Expand All @@ -149,25 +137,18 @@ extern void yasolr_init_system();
// Trial
extern void yasolr_init_trial();

// init
extern void yasolr_init_web_server();
extern void yasolr_init_zcd();

// TODO
extern void yasolr_divert();
extern void yasolr_configure();

// router
extern Mycila::Dimmer dimmerO1;
extern Mycila::Dimmer dimmerO2;
extern Mycila::PID pidController;
extern Mycila::Relay bypassRelayO1;
extern Mycila::Relay bypassRelayO2;
extern Mycila::Relay relay1;
extern Mycila::Relay relay2;
extern Mycila::Router router;
extern Mycila::RouterOutput output1;
extern Mycila::RouterOutput output2;
extern Mycila::RouterRelay routerRelay1;
extern Mycila::RouterRelay routerRelay2;

extern Mycila::Task calibrationTask;
extern Mycila::Task relayTask;
extern Mycila::Task routerTask;
extern void yasolr_divert();
extern void yasolr_init_router();
2 changes: 1 addition & 1 deletion include/YaSolRWebsite.h → include/yasolr_dashboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
#pragma once

#include <YaSolR.h>
#include <yasolr.h>

#include <string>
#include <unordered_map>
Expand Down
File renamed without changes.
33 changes: 8 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>

Mycila::PID pidController;
Mycila::Router router(pidController);
Mycila::Dimmer dimmerO1;
Mycila::Dimmer dimmerO2;
Mycila::Relay bypassRelayO1;
Mycila::Relay bypassRelayO2;
Mycila::Relay relay1;
Mycila::Relay relay2;
Mycila::RouterRelay routerRelay1(relay1);
Mycila::RouterRelay routerRelay2(relay2);
Mycila::RouterOutput output1("output1", dimmerO1, bypassRelayO1);
Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2);
#include <yasolr.h>

void setup() {
// boot
yasolr_boot();
// config
yasolr_init_config();
// logging
yasolr_init_logging();
// system
yasolr_init_system();
yasolr_boot(); // boot sequence
yasolr_init_config(); // load configuration from NVS
yasolr_init_logging(); // init logging
yasolr_init_system(); // init system (safeboot, restart, reset, etc)

// hardware
yasolr_init_display();
yasolr_init_ds18();
Expand All @@ -39,10 +23,9 @@ void setup() {
yasolr_init_trial();
yasolr_init_web_server();
yasolr_init_zcd();
// logging configuration
yasolr_configure_logging();

yasolr_configure(); // TODO
// router
yasolr_init_router();

// core task manager
assert(coreTaskManager.asyncStart(512 * 8, 1, 1, 100, true));
Expand Down
53 changes: 35 additions & 18 deletions src/todo/yasolr_configure.cpp → src/todo/yasolr_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <YaSolRWebsite.h>
#include <yasolr.h>
#include <yasolr_dashboard.h>

#include <string>

Mycila::PID pidController;
Mycila::Router router(pidController);
Mycila::Dimmer dimmerO1;
Mycila::Dimmer dimmerO2;
Mycila::Relay bypassRelayO1;
Mycila::Relay bypassRelayO2;
Mycila::Relay relay1;
Mycila::Relay relay2;
Mycila::RouterRelay routerRelay1(relay1);
Mycila::RouterRelay routerRelay2(relay2);
Mycila::RouterOutput output1("output1", dimmerO1, bypassRelayO1);
Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2);

Mycila::Task calibrationTask("Calibration", [](void* params) { router.calibrate(); });

Mycila::Task relayTask("Relay", [](void* params) {
Expand Down Expand Up @@ -58,7 +71,7 @@ void yasolr_divert() {
}
}

void yasolr_configure() {
void yasolr_init_router() {
logger.info(TAG, "Configuring %s", Mycila::AppInfo.nameModelVersion.c_str());

// PID Controller
Expand Down Expand Up @@ -119,21 +132,6 @@ void yasolr_configure() {
routerRelay1.setLoad(config.getLong(KEY_RELAY1_LOAD));
routerRelay2.setLoad(config.getLong(KEY_RELAY2_LOAD));

// Dimmers

// coreTaskManager
calibrationTask.setEnabledWhen([]() { return router.isCalibrationRunning(); });
calibrationTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
relayTask.setEnabledWhen([]() { return !router.isCalibrationRunning() && (routerRelay1.isAutoRelayEnabled() || routerRelay2.isAutoRelayEnabled()); });
relayTask.setInterval(7 * Mycila::TaskDuration::SECONDS);
routerTask.setEnabledWhen([]() { return !router.isCalibrationRunning(); });
routerTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);

// coreTaskManager
calibrationTask.setManager(coreTaskManager);
relayTask.setManager(coreTaskManager);
routerTask.setManager(coreTaskManager);

// Router
router.addOutput(output1);
router.addOutput(output2);
Expand All @@ -158,4 +156,23 @@ void yasolr_configure() {
if (mqttPublishTask)
mqttPublishTask->requestEarlyRun();
});

// coreTaskManager
calibrationTask.setEnabledWhen([]() { return router.isCalibrationRunning(); });
calibrationTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
calibrationTask.setManager(coreTaskManager);

relayTask.setEnabledWhen([]() { return !router.isCalibrationRunning() && (routerRelay1.isAutoRelayEnabled() || routerRelay2.isAutoRelayEnabled()); });
relayTask.setInterval(7 * Mycila::TaskDuration::SECONDS);
relayTask.setManager(coreTaskManager);

routerTask.setEnabledWhen([]() { return !router.isCalibrationRunning(); });
routerTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
routerTask.setManager(coreTaskManager);

if (config.getBool(KEY_ENABLE_DEBUG)) {
calibrationTask.enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);
relayTask.enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);
routerTask.enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);
}
}
2 changes: 1 addition & 1 deletion src/yasolr_boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

void yasolr_boot() {
Serial.begin(YASOLR_SERIAL_BAUDRATE);
Expand Down
7 changes: 2 additions & 5 deletions src/yasolr_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

#include <string>

Expand Down Expand Up @@ -129,10 +129,7 @@ void yasolr_init_config() {
logger.info(TAG, "'%s' => '%s'", k, newValue.c_str());
const std::string key = k;

if (key == KEY_ENABLE_DEBUG) {
yasolr_configure_logging();

} else if (key == KEY_RELAY1_LOAD) {
if (key == KEY_RELAY1_LOAD) {
routerRelay1.setLoad(config.getLong(KEY_RELAY1_LOAD));

} else if (key == KEY_RELAY2_LOAD) {
Expand Down
8 changes: 5 additions & 3 deletions src/Website.cpp → src/yasolr_dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolRWebsite.h>
#include <yasolr_dashboard.h>

#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -967,13 +967,15 @@ void YaSolR::Website::initCards() {

// management

const bool debug = config.getBool(KEY_ENABLE_DEBUG);
_configBackup.setValue("/api/config/backup");
_configRestore.setValue("/api/config/restore");
_consoleLink.setValue("/console");
_debugInfo.setValue("/api/debug");
_debugMode.setValue(config.getBool(KEY_ENABLE_DEBUG));
_debugMode.setValue(debug);
_energyReset.setDisplay(jsyEnabled || pzem1Enabled || pzem2Enabled);
_debugInfo.setDisplay(config.getBool(KEY_ENABLE_DEBUG));
_consoleLink.setDisplay(debug);
_debugInfo.setDisplay(debug);

// network

Expand Down
4 changes: 3 additions & 1 deletion src/yasolr_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

#include <string>

Expand Down Expand Up @@ -201,5 +201,7 @@ void yasolr_init_display() {

displayTask->setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
displayTask->setManager(coreTaskManager);
if (config.getBool(KEY_ENABLE_DEBUG))
displayTask->enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);
}
}
4 changes: 3 additions & 1 deletion src/yasolr_ds18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

Mycila::DS18* ds18O1;
Mycila::DS18* ds18O2;
Expand Down Expand Up @@ -93,5 +93,7 @@ void yasolr_init_ds18() {
});
ds18Task->setInterval(10 * Mycila::TaskDuration::SECONDS);
ds18Task->setManager(coreTaskManager);
if (config.getBool(KEY_ENABLE_DEBUG))
ds18Task->enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);
}
}
2 changes: 1 addition & 1 deletion src/yasolr_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

Mycila::Grid grid;

Expand Down
8 changes: 7 additions & 1 deletion src/yasolr_jsy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

Mycila::JSY* jsy;
Mycila::Task* jsyTask;
Expand Down Expand Up @@ -96,10 +96,16 @@ void yasolr_init_jsy() {
});

// async task

jsyTaskManager = new Mycila::TaskManager("y-jsy");

jsyTask = new Mycila::Task("JSY", [](void* params) { jsy->read(); });
jsyTask->setManager(*jsyTaskManager);
if (config.getBool(KEY_ENABLE_DEBUG))
jsyTask->enableProfiling(10, Mycila::TaskTimeUnit::MILLISECONDS);

assert(jsyTaskManager->asyncStart(512 * 4, 5, 0, 100, true));

Mycila::TaskMonitor.addTask(jsyTaskManager->getName());
}
}
2 changes: 1 addition & 1 deletion src/yasolr_jsy_remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

AsyncUDP* udp;
Mycila::CircularBuffer<float, 15>* udpMessageRateBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/yasolr_lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>
#include <yasolr.h>

Mycila::TrafficLight lights;

Expand Down
Loading

0 comments on commit 26cbc8f

Please sign in to comment.