Skip to content

Commit

Permalink
dap,ocpp,sf: Only try to connect/download if network is connected
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed Oct 28, 2024
1 parent d8a9b2d commit a020322
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion software/src/modules/day_ahead_prices/day_ahead_prices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ void DayAheadPrices::update()
return;
}

if (!network.connected) {
return;
}

// Only update if clock is synced
struct timeval tv_now;
if (!rtc.clock_synced(&tv_now)) {
Expand Down Expand Up @@ -593,4 +597,4 @@ bool DayAheadPrices::has_triggered(const Config *conf, void *data)

return false;
}
#endif
#endif
1 change: 1 addition & 0 deletions software/src/modules/day_ahead_prices/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Requires = Task Scheduler
Event Log
API
Network
Rtc

Optional = Certs
Expand Down
6 changes: 4 additions & 2 deletions software/src/modules/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ void Network::register_events()
#if MODULE_ETHERNET_AVAILABLE()
event.registerEvent("ethernet/state", {"connection_state"}, [this](const Config *connection_state) {
ethernet_connected = connection_state->asEnum<EthernetState>() == EthernetState::Connected;
state.get("connected")->updateBool(ethernet_connected || wifi_connected);
connected = ethernet_connected || wifi_connected;
state.get("connected")->updateBool(connected);
return EventResult::OK;
});
#endif

#if MODULE_WIFI_AVAILABLE()
event.registerEvent("wifi/state", {"connection_state"}, [this](const Config *connection_state) {
wifi_connected = connection_state->asEnum<WifiState>() == WifiState::Connected;
state.get("connected")->updateBool(ethernet_connected || wifi_connected);
connected = ethernet_connected || wifi_connected;
state.get("connected")->updateBool(connected);
return EventResult::OK;
});
#endif
Expand Down
1 change: 1 addition & 0 deletions software/src/modules/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Network final : public IModule

ConfigRoot config;
ConfigRoot state;
bool connected = false;

private:
bool ethernet_connected = false;
Expand Down
23 changes: 18 additions & 5 deletions software/src/modules/ocpp/ESP32Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i

extern "C" esp_err_t esp_crt_bundle_attach(void *conf);

tf_websocket_client_handle_t client;
static tf_websocket_client_handle_t client;
static bool client_running = false;
static std::unique_ptr<String[]> auth_headers;
static size_t auth_headers_count = 0;
static size_t next_auth_header = 0;
Expand Down Expand Up @@ -151,7 +152,10 @@ void *platform_init(const char *websocket_url, BasicAuthCredentials *credentials
client = tf_websocket_client_init(&websocket_cfg);
tf_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client);

tf_websocket_client_start(client);
if (network.connected) {
tf_websocket_client_start(client);
client_running = true;
)

return client;
}
Expand All @@ -163,20 +167,29 @@ bool platform_has_fixed_cable(int connectorId)

void platform_disconnect(void *ctx)
{
tf_websocket_client_close(client, pdMS_TO_TICKS(1000));
if (client_running) {
tf_websocket_client_close(client, pdMS_TO_TICKS(1000));
client_running = false;
}
}

void platform_reconnect(void *ctx)
{
tf_websocket_client_stop(client);
if (client_running) {
tf_websocket_client_stop(client);
client_running = false;
}

// Try next set of credentials if available.
if (auth_headers_count > 0) {
tf_websocket_client_set_headers(client, auth_headers[next_auth_header].c_str());
next_auth_header = (next_auth_header + 1) % auth_headers_count;
}

tf_websocket_client_start(client);
if (network.connected) {
tf_websocket_client_start(client);
client_running = true;
)
}

void platform_destroy(void *ctx)
Expand Down
1 change: 1 addition & 0 deletions software/src/modules/ocpp/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Requires = Task Scheduler
Event Log
API
Network
Evse Common
Meters Legacy API
Device Name
Expand Down
1 change: 1 addition & 0 deletions software/src/modules/solar_forecast/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Requires = Task Scheduler
Event Log
API
Network
Rtc

Optional = Certs
4 changes: 4 additions & 0 deletions software/src/modules/solar_forecast/solar_forecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ void SolarForecast::update()
return;
}

if (!network.connected) {
return;
}

// Only update if NTP is available
struct timeval tv_now;
if (!rtc.clock_synced(&tv_now)) {
Expand Down

0 comments on commit a020322

Please sign in to comment.