Skip to content

Commit

Permalink
code cleanup and logging review
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Dec 29, 2024
1 parent eeea5b7 commit aabefae
Show file tree
Hide file tree
Showing 23 changed files with 159 additions and 120 deletions.
59 changes: 31 additions & 28 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,51 @@ extern Mycila::Task dashboardUpdateTask;

// 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
extern Mycila::Grid grid;
extern void yasolr_init_grid();
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;

// Remote JSY
extern AsyncUDP* udp;
extern Mycila::CircularBuffer<float, 15>* udpMessageRateBuffer;

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

// JSY Remote
extern AsyncUDP* udp;
extern Mycila::CircularBuffer<float, 15>* udpMessageRateBuffer;
extern Mycila::Task* jsyRemoteTask;
extern void yasolr_init_jsy_remote();

// DS18
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;
Expand All @@ -111,6 +122,7 @@ 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;
Expand All @@ -119,16 +131,31 @@ extern Mycila::Task* pzemO1PairingTask;
extern Mycila::Task* pzemO2PairingTask;
extern Mycila::Task* pzemTask;
extern Mycila::TaskManager* pzemTaskManager;
extern void yasolr_init_pzem();

// Lights
extern Mycila::TrafficLight lights;
extern void yasolr_init_lights();

// System
extern Mycila::Task resetTask;
extern Mycila::Task restartTask;
extern Mycila::Task safeBootTask;
extern Mycila::TaskManager coreTaskManager;
extern Mycila::TaskManager unsafeTaskManager;
extern void yasolr_boot();
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();

extern Mycila::Dimmer dimmerO1;
extern Mycila::Dimmer dimmerO2;
Expand All @@ -144,27 +171,3 @@ extern Mycila::RouterRelay routerRelay2;
extern Mycila::Task calibrationTask;
extern Mycila::Task relayTask;
extern Mycila::Task routerTask;

// fn
extern float yasolr_frequency();
extern void yasolr_boot();
extern void yasolr_configure();
extern void yasolr_divert();

extern void yasolr_init_config();
extern void yasolr_init_display();
extern void yasolr_init_ds18();
extern void yasolr_init_grid();
extern void yasolr_init_jsy_remote_listener();
extern void yasolr_init_jsy();
extern void yasolr_init_lights();
extern void yasolr_init_logging();
extern void yasolr_init_mqtt();
extern void yasolr_init_network();
extern void yasolr_init_pzem();
extern void yasolr_init_system();
extern void yasolr_init_trial();
extern void yasolr_init_web_server();
extern void yasolr_init_zcd();

extern void yasolr_configure_logging();
4 changes: 2 additions & 2 deletions src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ dash::LineChart<int8_t, int16_t> _pidDTermHistory(dashboard, YASOLR_LBL_176);
#endif

void YaSolR::Website::begin() {
logger.debug(TAG, "Initializing layout");
logger.info(TAG, "Initialize dashboard layout...");

for (int i = 0; i < YASOLR_GRAPH_POINTS; i++)
_historyX[i] = i - YASOLR_GRAPH_POINTS;
Expand Down Expand Up @@ -829,7 +829,7 @@ void YaSolR::Website::begin() {
}

void YaSolR::Website::initCards() {
logger.debug(TAG, "Initializing cards");
logger.debug(TAG, "Initialize dashboard cards...");

// Statistics

Expand Down
19 changes: 13 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ Mycila::RouterOutput output1("output1", dimmerO1, bypassRelayO1);
Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2);

void setup() {
// boot
yasolr_boot();
// config
yasolr_init_config();

yasolr_configure();

// logging
yasolr_init_logging();
// system
yasolr_init_system();
// hardware
yasolr_init_display();
yasolr_init_ds18();
yasolr_init_grid();
yasolr_init_jsy();
yasolr_init_jsy_remote();
yasolr_init_lights();
yasolr_init_logging();
yasolr_init_mqtt();
yasolr_init_network();
yasolr_init_pzem();
yasolr_init_system();
yasolr_init_trial();
yasolr_init_web_server();
yasolr_init_zcd();

// logging configuration
yasolr_configure_logging();

yasolr_configure(); // TODO

// core task manager
assert(coreTaskManager.asyncStart(512 * 8, 1, 1, 100, true));

// task manager for long running tasks like mqtt / pzem
if (unsafeTaskManager.getSize())
assert(unsafeTaskManager.asyncStart(512 * 8, 1, 1, 100, false));

Expand Down
7 changes: 0 additions & 7 deletions src/tasks/calibrationTask.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions src/tasks/relayTask.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/tasks/routerTask.cpp

This file was deleted.

34 changes: 34 additions & 0 deletions src/yasolr_configure.cpp → src/todo/yasolr_configure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@

#include <string>

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

Mycila::Task relayTask("Relay", [](void* params) {
if (grid.getPower().isAbsent())
return;

Mycila::Router::Metrics routerMetrics;
router.getRouterMeasurements(routerMetrics);

float virtualGridPower = grid.getPower().get() - routerMetrics.power;

if (routerRelay1.tryRelayStateAuto(true, virtualGridPower))
return;
if (routerRelay2.tryRelayStateAuto(true, virtualGridPower))
return;
if (routerRelay2.tryRelayStateAuto(false, virtualGridPower))
return;
if (routerRelay1.tryRelayStateAuto(false, virtualGridPower))
return;
});

Mycila::Task routerTask("Router", [](void* params) {
std::optional<float> voltage = grid.getVoltage();

if (!voltage.has_value() || grid.getPower().isAbsent())
router.noDivert();

output1.applyTemperatureLimit();
output2.applyTemperatureLimit();

output1.applyAutoBypass();
output2.applyAutoBypass();
});

void yasolr_divert() {
if (router.isCalibrationRunning())
return;
Expand Down
2 changes: 1 addition & 1 deletion src/yasolr_boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void yasolr_boot() {

// early logging
logger.forwardTo(&Serial);
logger.info(TAG, "Booting %s", Mycila::AppInfo.nameModelVersion.c_str());
logger.info(TAG, "Booting %s...", Mycila::AppInfo.nameModelVersion.c_str());

// system
Mycila::System::init(true, "fs");
Expand Down
6 changes: 2 additions & 4 deletions src/yasolr_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Mycila::Config config;

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

// setup config system
config.begin("YASOLR");
config.configure(KEY_ADMIN_PASSWORD);
Expand Down Expand Up @@ -239,8 +241,4 @@ void yasolr_init_config() {
if (mqttPublishTask)
mqttPublishTask->requestEarlyRun();
});

// pre-init logging
logger.setLevel(config.getBool(KEY_ENABLE_DEBUG) ? ARDUHAL_LOG_LEVEL_DEBUG : ARDUHAL_LOG_LEVEL_INFO);
esp_log_level_set("*", static_cast<esp_log_level_t>(logger.getLevel()));
}
6 changes: 5 additions & 1 deletion src/yasolr_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ void yasolr_init_display() {
assert(!displayCarouselTask);
assert(!displayTask);

logger.info(TAG, "Initialize display...");

display = new Mycila::EasyDisplay(YASOLR_DISPLAY_LINES, YASOLR_DISPLAY_LINE_SIZE, 4, u8g2_font_6x12_tf);

const std::string& displayType = config.getString(KEY_DISPLAY_TYPE);
Expand All @@ -34,8 +36,10 @@ void yasolr_init_display() {
else if (displayType == "SH1106")
display->begin(Mycila::EasyDisplayType::SH1106, config.getLong(KEY_PIN_DISPLAY_SCL), config.getLong(KEY_PIN_DISPLAY_SDA), config.getLong(KEY_DISPLAY_ROTATION));

if (!display->isEnabled())
if (!display->isEnabled()) {
logger.error(TAG, "Display failed to initialize!");
return;
}

display->clearDisplay();
display->setActive(true);
Expand Down
7 changes: 7 additions & 0 deletions src/yasolr_ds18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Mycila::DS18* ds18Sys;
Mycila::Task* ds18Task;

void yasolr_init_ds18() {
logger.info(TAG, "Initialize DS18 probes...");
uint8_t count = 0;

if (config.getBool(KEY_ENABLE_DS18_SYSTEM)) {
Expand All @@ -27,6 +28,8 @@ void yasolr_init_ds18() {
mqttPublishTask->requestEarlyRun();
}
});
} else {
logger.error(TAG, "DS18 system probe failed to initialize!");
}
}

Expand All @@ -46,6 +49,8 @@ void yasolr_init_ds18() {
mqttPublishTask->requestEarlyRun();
}
});
} else {
logger.error(TAG, "DS18 output 1 probe failed to initialize!");
}
}

Expand All @@ -66,6 +71,8 @@ void yasolr_init_ds18() {
}
});
ds18O2 = new Mycila::DS18();
} else {
logger.error(TAG, "DS18 output 2 probe failed to initialize!");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/yasolr_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ float yasolr_frequency() {
}

void yasolr_init_grid() {
logger.info(TAG, "Initialize grid electricity...");

grid.localMetrics().setExpiration(10000); // local is fast
grid.remoteMetrics().setExpiration(10000); // remote JSY is fast
grid.pzemMetrics().setExpiration(10000); // local is fast
Expand Down
6 changes: 5 additions & 1 deletion src/yasolr_jsy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ void yasolr_init_jsy() {
assert(!jsyTask);
assert(!jsyTaskManager);

logger.info(TAG, "Initialize JSY...");

jsy = new Mycila::JSY();
jsy->begin(YASOLR_JSY_SERIAL, config.getLong(KEY_PIN_JSY_RX), config.getLong(KEY_PIN_JSY_TX));

if (!jsy->isEnabled())
if (!jsy->isEnabled()) {
logger.error(TAG, "JSY failed to initialize!");
return;
}

if (jsy->getBaudRate() != jsy->getMaxAvailableBaudRate())
jsy->setBaudRate(jsy->getMaxAvailableBaudRate());
Expand Down
Loading

0 comments on commit aabefae

Please sign in to comment.