go-eCharger: improve API V2 refreshing to reduce wallbox load

This commit is contained in:
Simon Stürz 2023-06-26 22:17:17 +02:00
parent 17489941cf
commit e70ab71cd6
3 changed files with 47 additions and 8 deletions

View File

@ -49,6 +49,11 @@ IntegrationPluginGoECharger::IntegrationPluginGoECharger()
} }
void IntegrationPluginGoECharger::init()
{
connect(this, &IntegrationPlugin::configValueChanged, this, &IntegrationPluginGoECharger::onConfigValueChanged);
}
void IntegrationPluginGoECharger::discoverThings(ThingDiscoveryInfo *info) void IntegrationPluginGoECharger::discoverThings(ThingDiscoveryInfo *info)
{ {
if (!hardwareManager()->networkDeviceDiscovery()->available()) { if (!hardwareManager()->networkDeviceDiscovery()->available()) {
@ -142,7 +147,7 @@ void IntegrationPluginGoECharger::setupThing(ThingSetupInfo *info)
// The device is reachable again and we have already set it up. // The device is reachable again and we have already set it up.
// Update data and optionally reconfigure the mqtt channel // Update data and optionally reconfigure the mqtt channel
QNetworkReply *reply = hardwareManager()->networkManager()->get(buildStatusRequest(thing)); QNetworkReply *reply = hardwareManager()->networkManager()->get(buildStatusRequest(thing, true));
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, thing, [=](){ connect(reply, &QNetworkReply::finished, thing, [=](){
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
@ -211,8 +216,10 @@ void IntegrationPluginGoECharger::postSetupThing(Thing *thing)
if (thing->thingClassId() == goeHomeThingClassId) { if (thing->thingClassId() == goeHomeThingClassId) {
// Set up refresh timer if needed and if we are not using mqtt // Set up refresh timer if needed and if we are not using mqtt
if (!thing->paramValue(goeHomeThingUseMqttParamTypeId).toBool() && !m_refreshTimer) { if (!thing->paramValue(goeHomeThingUseMqttParamTypeId).toBool() && !m_refreshTimer) {
qCDebug(dcGoECharger()) << "Enabling HTTP refresh timer...";
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(4); uint interval = configValue(goEChargerPluginHttpRefreshIntervalParamTypeId).toUInt();
qCDebug(dcGoECharger()) << "Enabling HTTP refresh timer" << interval;
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(interval);
connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginGoECharger::refreshHttp); connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginGoECharger::refreshHttp);
m_refreshTimer->start(); m_refreshTimer->start();
} }
@ -437,7 +444,7 @@ void IntegrationPluginGoECharger::executeAction(ThingActionInfo *info)
void IntegrationPluginGoECharger::setupGoeHome(ThingSetupInfo *info) void IntegrationPluginGoECharger::setupGoeHome(ThingSetupInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
QNetworkReply *reply = hardwareManager()->networkManager()->get(buildStatusRequest(thing)); QNetworkReply *reply = hardwareManager()->networkManager()->get(buildStatusRequest(thing, true));
connect(info, &ThingSetupInfo::aborted, reply, &QNetworkReply::abort); connect(info, &ThingSetupInfo::aborted, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, info, [=](){ connect(reply, &QNetworkReply::finished, info, [=](){
@ -513,7 +520,7 @@ void IntegrationPluginGoECharger::setupGoeHome(ThingSetupInfo *info)
}); });
} }
QNetworkRequest IntegrationPluginGoECharger::buildStatusRequest(Thing *thing) QNetworkRequest IntegrationPluginGoECharger::buildStatusRequest(Thing *thing, bool fullStatus)
{ {
QHostAddress address = getHostAddress(thing); QHostAddress address = getHostAddress(thing);
ApiVersion apiVersion = getApiVersion(thing); ApiVersion apiVersion = getApiVersion(thing);
@ -528,6 +535,11 @@ QNetworkRequest IntegrationPluginGoECharger::buildStatusRequest(Thing *thing)
break; break;
case ApiVersion2: case ApiVersion2:
requestUrl.setPath("/api/status"); requestUrl.setPath("/api/status");
if (!fullStatus) {
QUrlQuery query;
query.addQueryItem("filter", "alw,car,ast,tma,eto,wh,upd,fwv,amp,adi,fhz,cbl,ama,var,pnp,nrg");
requestUrl.setQuery(query);
}
break; break;
} }
@ -1090,7 +1102,7 @@ void IntegrationPluginGoECharger::reconfigureMqttChannelV1(Thing *thing, const Q
void IntegrationPluginGoECharger::updateV2(Thing *thing, const QVariantMap &statusMap) void IntegrationPluginGoECharger::updateV2(Thing *thing, const QVariantMap &statusMap)
{ {
qCDebug(dcGoECharger()) << "Update V2:" << qUtf8Printable(QJsonDocument::fromVariant(statusMap).toJson()); qCDebug(dcGoECharger()) << "Update V2:" << qUtf8Printable(QJsonDocument::fromVariant(statusMap).toJson(QJsonDocument::Compact));
if (statusMap.contains("alw")) if (statusMap.contains("alw"))
thing->setStateValue(goeHomePowerStateTypeId, (statusMap.value("alw").toUInt() == 0 ? false : true)); thing->setStateValue(goeHomePowerStateTypeId, (statusMap.value("alw").toUInt() == 0 ? false : true));
@ -1526,6 +1538,19 @@ void IntegrationPluginGoECharger::refreshHttp()
} }
} }
void IntegrationPluginGoECharger::onConfigValueChanged(const ParamTypeId &paramTypeId, const QVariant &value)
{
if (paramTypeId == goEChargerPluginHttpRefreshIntervalParamTypeId) {
uint interval = value.toUInt();
qCDebug(dcGoECharger()) << "Reconfigure HTTP refresh timer" << interval << "seconds";
m_refreshTimer->stop();
hardwareManager()->pluginTimerManager()->unregisterTimer(m_refreshTimer);
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(interval);
connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginGoECharger::refreshHttp);
m_refreshTimer->start();
}
}
void IntegrationPluginGoECharger::onMqttClientV1Connected(MqttChannel *channel) void IntegrationPluginGoECharger::onMqttClientV1Connected(MqttChannel *channel)
{ {
@ -1615,3 +1640,4 @@ void IntegrationPluginGoECharger::markAsDisconnected(Thing *thing)
thing->setStateValue("currentPowerPhaseC", 0); thing->setStateValue("currentPowerPhaseC", 0);
thing->setStateValue("frequency", 0); thing->setStateValue("frequency", 0);
} }

View File

@ -95,7 +95,7 @@ public:
Q_ENUM(CableLockMode) Q_ENUM(CableLockMode)
explicit IntegrationPluginGoECharger(); explicit IntegrationPluginGoECharger();
void init() override;
void discoverThings(ThingDiscoveryInfo *info) override; void discoverThings(ThingDiscoveryInfo *info) override;
void setupThing(ThingSetupInfo *info) override; void setupThing(ThingSetupInfo *info) override;
void postSetupThing(Thing *thing) override; void postSetupThing(Thing *thing) override;
@ -114,7 +114,7 @@ private:
// General methods // General methods
void setupGoeHome(ThingSetupInfo *info); void setupGoeHome(ThingSetupInfo *info);
QNetworkRequest buildStatusRequest(Thing *thing); QNetworkRequest buildStatusRequest(Thing *thing, bool fullStatus = false);
QHostAddress getHostAddress(Thing *thing); QHostAddress getHostAddress(Thing *thing);
ApiVersion getApiVersion(Thing *thing); ApiVersion getApiVersion(Thing *thing);
@ -134,6 +134,8 @@ private:
private slots: private slots:
void refreshHttp(); void refreshHttp();
void onConfigValueChanged(const ParamTypeId &paramTypeId, const QVariant &value);
// API V1 // API V1
void onMqttClientV1Connected(MqttChannel* channel); void onMqttClientV1Connected(MqttChannel* channel);
void onMqttClientV1Disconnected(MqttChannel* channel); void onMqttClientV1Disconnected(MqttChannel* channel);

View File

@ -2,6 +2,17 @@
"name": "GoECharger", "name": "GoECharger",
"displayName": "go-eCharger", "displayName": "go-eCharger",
"id": "a1dfca21-3f41-4a67-bc8c-c8b333411bd9", "id": "a1dfca21-3f41-4a67-bc8c-c8b333411bd9",
"paramTypes": [
{
"id": "7746a28e-c125-40bc-958c-27d8aeeb06a0",
"name": "httpRefreshInterval",
"displayName": "HTTP refresh interval",
"type": "uint",
"minValue": 2,
"unit": "Seconds",
"defaultValue": 5
}
],
"vendors": [ "vendors": [
{ {
"name": "goE", "name": "goE",