-
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement go-e Controller integration
- Loading branch information
1 parent
b7f830f
commit 4239a0d
Showing
23 changed files
with
440 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <HTTPClient.h> | ||
#include "NetworkSettings.h" | ||
#include <TaskSchedulerDeclarations.h> | ||
|
||
class IntegrationsGoeControllerClass { | ||
public: | ||
IntegrationsGoeControllerClass(); | ||
void init(Scheduler& scheduler); | ||
|
||
private: | ||
void loop(); | ||
void NetworkEvent(network_event event); | ||
|
||
Task _loopTask; | ||
|
||
bool _networkConnected = false; | ||
HTTPClient _http; | ||
}; | ||
|
||
extern IntegrationsGoeControllerClass IntegrationsGoeController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <ESPAsyncWebServer.h> | ||
#include <TaskSchedulerDeclarations.h> | ||
|
||
class WebApiIntegrationsClass { | ||
public: | ||
void init(AsyncWebServer& server, Scheduler& scheduler); | ||
|
||
private: | ||
void onIntegrationsAdminGet(AsyncWebServerRequest* request); | ||
void onIntegrationsAdminPost(AsyncWebServerRequest* request); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2023-2024 Thomas Basler and others | ||
*/ | ||
#include "IntegrationsGoeController.h" | ||
#include "Configuration.h" | ||
#include "Datastore.h" | ||
#include "MessageOutput.h" | ||
#include "NetworkSettings.h" | ||
#include <Hoymiles.h> | ||
|
||
IntegrationsGoeControllerClass IntegrationsGoeController; | ||
|
||
IntegrationsGoeControllerClass::IntegrationsGoeControllerClass() | ||
: _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&IntegrationsGoeControllerClass::loop, this)) | ||
{ | ||
} | ||
|
||
void IntegrationsGoeControllerClass::init(Scheduler& scheduler) | ||
{ | ||
using std::placeholders::_1; | ||
|
||
NetworkSettings.onEvent(std::bind(&IntegrationsGoeControllerClass::NetworkEvent, this, _1)); | ||
|
||
scheduler.addTask(_loopTask); | ||
_loopTask.setInterval(Configuration.get().Integrations.GoeControllerUpdateInterval * TASK_SECOND); | ||
_loopTask.enable(); | ||
} | ||
|
||
void IntegrationsGoeControllerClass::NetworkEvent(network_event event) | ||
{ | ||
switch (event) { | ||
case network_event::NETWORK_GOT_IP: | ||
_networkConnected = true; | ||
break; | ||
case network_event::NETWORK_DISCONNECTED: | ||
_networkConnected = false; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void IntegrationsGoeControllerClass::loop() | ||
{ | ||
const auto& integrationsConfig = Configuration.get().Integrations; | ||
|
||
const bool reachable = Datastore.getIsAllEnabledReachable(); | ||
|
||
_loopTask.setInterval((reachable ? integrationsConfig.GoeControllerUpdateInterval : std::min(integrationsConfig.GoeControllerUpdateInterval, 5U)) * TASK_SECOND); | ||
|
||
if (!integrationsConfig.GoeControllerEnabled) { | ||
return; | ||
} | ||
|
||
if (!_networkConnected || !Hoymiles.isAllRadioIdle()) { | ||
_loopTask.forceNextIteration(); | ||
return; | ||
} | ||
|
||
const auto value = reachable ? Datastore.getTotalAcPowerEnabled() : 0; | ||
|
||
// home, grid, car, relais, solar | ||
// ecp is an array of numbers or null: [{power}, null, null, null, {power}] | ||
// setting the home category to the power should be configurable | ||
// url is this: http://{hostname}/api/set?ecp= | ||
|
||
auto url = "http://" + String(integrationsConfig.GoeControllerHostname) + "/api/set?ecp="; | ||
|
||
url += "["; | ||
url += integrationsConfig.GoeControllerPublishHomeCategory ? String(value) : "null"; | ||
url += ",null,null,null,"; | ||
url += value; | ||
url += "]"; | ||
|
||
const auto timeout = std::max(2U, std::min(integrationsConfig.GoeControllerUpdateInterval-1, 3U)) * 1000U; | ||
|
||
_http.setConnectTimeout(timeout); | ||
_http.setTimeout(timeout); | ||
_http.setReuse(true); | ||
_http.begin(url); | ||
|
||
int httpCode = _http.GET(); | ||
|
||
if (httpCode > 0) { | ||
if (httpCode == HTTP_CODE_OK) { | ||
MessageOutput.println("go-e Controller updated"); | ||
} else { | ||
MessageOutput.printf("HTTP error: %d\n", httpCode); | ||
} | ||
} else { | ||
MessageOutput.println("HTTP error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2022-2024 Thomas Basler and others | ||
*/ | ||
#include "WebApi_integrations.h" | ||
#include "Configuration.h" | ||
#include "WebApi.h" | ||
#include "WebApi_errors.h" | ||
#include "helper.h" | ||
#include <AsyncJson.h> | ||
|
||
void WebApiIntegrationsClass::init(AsyncWebServer& server, Scheduler& scheduler) | ||
{ | ||
using std::placeholders::_1; | ||
|
||
server.on("/api/integrations/config", HTTP_GET, std::bind(&WebApiIntegrationsClass::onIntegrationsAdminGet, this, _1)); | ||
server.on("/api/integrations/config", HTTP_POST, std::bind(&WebApiIntegrationsClass::onIntegrationsAdminPost, this, _1)); | ||
} | ||
|
||
void WebApiIntegrationsClass::onIntegrationsAdminGet(AsyncWebServerRequest* request) | ||
{ | ||
if (!WebApi.checkCredentials(request)) { | ||
return; | ||
} | ||
|
||
AsyncJsonResponse* response = new AsyncJsonResponse(); | ||
auto& root = response->getRoot(); | ||
const CONFIG_T& config = Configuration.get(); | ||
|
||
root["goe_ctrl_hostname"] = config.Integrations.GoeControllerHostname; | ||
root["goe_ctrl_enabled"] = config.Integrations.GoeControllerEnabled; | ||
root["goe_ctrl_publish_home_category"] = config.Integrations.GoeControllerPublishHomeCategory; | ||
root["goe_ctrl_update_interval"] = config.Integrations.GoeControllerUpdateInterval; | ||
|
||
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); | ||
} | ||
|
||
void WebApiIntegrationsClass::onIntegrationsAdminPost(AsyncWebServerRequest* request) | ||
{ | ||
if (!WebApi.checkCredentials(request)) { | ||
return; | ||
} | ||
|
||
AsyncJsonResponse* response = new AsyncJsonResponse(); | ||
JsonDocument root; | ||
if (!WebApi.parseRequestData(request, response, root)) { | ||
return; | ||
} | ||
|
||
auto& retMsg = response->getRoot(); | ||
|
||
if (!(root["goe_ctrl_hostname"].is<String>() | ||
&& root["goe_ctrl_enabled"].is<bool>() | ||
&& root["goe_ctrl_publish_home_category"].is<bool>() | ||
&& root["goe_ctrl_update_interval"].is<uint32_t>())) { | ||
retMsg["message"] = "Values are missing!"; | ||
retMsg["code"] = WebApiError::GenericValueMissing; | ||
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); | ||
return; | ||
} | ||
|
||
if (root["goe_ctrl_enabled"].as<bool>()) { | ||
if (root["goe_ctrl_hostname"].as<String>().length() == 0 || root["goe_ctrl_hostname"].as<String>().length() > INTEGRATIONS_GOE_MAX_HOSTNAME_STRLEN) { | ||
retMsg["message"] = "go-e Controller hostname must between 1 and " STR(INTEGRATIONS_GOE_MAX_HOSTNAME_STRLEN) " characters long!"; | ||
retMsg["code"] = WebApiError::IntegrationsGoeControllerHostnameLength; | ||
retMsg["param"]["max"] = INTEGRATIONS_GOE_MAX_HOSTNAME_STRLEN; | ||
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); | ||
return; | ||
} | ||
|
||
if (root["goe_ctrl_update_interval"].as<uint32_t>() < 3 || root["goe_ctrl_update_interval"].as<uint32_t>() > 65535) { | ||
retMsg["message"] = "go-e Controller update interval must between 3 and 65535!"; | ||
retMsg["code"] = WebApiError::IntegrationsGoeControllerUpdateInterval; | ||
retMsg["param"]["min"] = 3; | ||
retMsg["param"]["max"] = 65535; | ||
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); | ||
return; | ||
} | ||
} | ||
|
||
CONFIG_T& config = Configuration.get(); | ||
strlcpy(config.Integrations.GoeControllerHostname, root["goe_ctrl_hostname"].as<String>().c_str(), sizeof(config.Integrations.GoeControllerHostname)); | ||
config.Integrations.GoeControllerEnabled = root["goe_ctrl_enabled"].as<bool>(); | ||
config.Integrations.GoeControllerPublishHomeCategory = root["goe_ctrl_publish_home_category"].as<bool>(); | ||
config.Integrations.GoeControllerUpdateInterval = root["goe_ctrl_update_interval"].as<uint32_t>(); | ||
|
||
WebApi.writeConfig(retMsg); | ||
|
||
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
22.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,8 @@ | |
"vite-plugin-css-injected-by-js": "^3.5.2", | ||
"vue-tsc": "^2.1.6" | ||
}, | ||
"engines": { | ||
"node": ">=21.1.0" | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.