Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Sep 19, 2023
2 parents 4d0f958 + b081845 commit ae39232
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 63 deletions.
6 changes: 5 additions & 1 deletion include/HttpPowerMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdint.h>
#include <Arduino.h>
#include <HTTPClient.h>

class HttpPowerMeterClass {
public:
Expand All @@ -14,9 +15,12 @@ class HttpPowerMeterClass {
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value);

private:
float power[POWERMETER_MAX_PHASES];
void extractUrlComponents(const String& url, String& protocol, String& hostname, String& uri);
void prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue);
HTTPClient httpClient;
float power[POWERMETER_MAX_PHASES];
String sha256(const String& data);

};

extern HttpPowerMeterClass HttpPowerMeter;
2 changes: 1 addition & 1 deletion include/MqttHandleDtu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MqttHandleDtuClass {
void loop();

private:
uint32_t _lastPublish;
uint32_t _lastPublish = 0;
};

extern MqttHandleDtuClass MqttHandleDtu;
4 changes: 2 additions & 2 deletions include/MqttHandleInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MqttHandleInverterClass {
void publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);

uint32_t _lastPublishStats[INV_MAX_COUNT];
uint32_t _lastPublish;
uint32_t _lastPublishStats[INV_MAX_COUNT] = { 0 };
uint32_t _lastPublish = 0;

FieldId_t _publishFields[14] = {
FLD_UDC,
Expand Down
3 changes: 3 additions & 0 deletions lib/Hoymiles/src/Hoymiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Hoymiles.h"
#include "Utils.h"
#include "inverters/HMS_1CH.h"
#include "inverters/HMS_1CHv2.h"
#include "inverters/HMS_2CH.h"
#include "inverters/HMS_4CH.h"
#include "inverters/HMT_6CH.h"
Expand Down Expand Up @@ -147,6 +148,8 @@ std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, u
i = std::make_shared<HMS_2CH>(_radioCmt.get(), serial);
} else if (HMS_1CH::isValidSerial(serial)) {
i = std::make_shared<HMS_1CH>(_radioCmt.get(), serial);
} else if (HMS_1CHv2::isValidSerial(serial)) {
i = std::make_shared<HMS_1CHv2>(_radioCmt.get(), serial);
} else if (HM_4CH::isValidSerial(serial)) {
i = std::make_shared<HM_4CH>(_radioNrf.get(), serial);
} else if (HM_2CH::isValidSerial(serial)) {
Expand Down
54 changes: 54 additions & 0 deletions lib/Hoymiles/src/inverters/HMS_1CHv2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2023 Thomas Basler and others
*/
#include "HMS_1CHv2.h"

static const byteAssign_t byteAssignment[] = {
{ TYPE_DC, CH0, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 },
{ TYPE_DC, CH0, FLD_IDC, UNIT_A, 6, 2, 100, false, 2 },
{ TYPE_DC, CH0, FLD_PDC, UNIT_W, 10, 2, 10, false, 1 },
{ TYPE_DC, CH0, FLD_YD, UNIT_WH, 22, 2, 1, false, 0 },
{ TYPE_DC, CH0, FLD_YT, UNIT_KWH, 14, 4, 1000, false, 3 },
{ TYPE_DC, CH0, FLD_IRR, UNIT_PCT, CALC_IRR_CH, CH0, CMD_CALC, false, 3 },

{ TYPE_AC, CH0, FLD_UAC, UNIT_V, 26, 2, 10, false, 1 },
{ TYPE_AC, CH0, FLD_IAC, UNIT_A, 34, 2, 100, false, 2 },
{ TYPE_AC, CH0, FLD_PAC, UNIT_W, 30, 2, 10, false, 1 },
{ TYPE_AC, CH0, FLD_Q, UNIT_VAR, 20, 2, 10, false, 1 },
{ TYPE_AC, CH0, FLD_F, UNIT_HZ, 28, 2, 100, false, 2 },
{ TYPE_AC, CH0, FLD_PF, UNIT_NONE, 36, 2, 1000, false, 3 },

{ TYPE_INV, CH0, FLD_T, UNIT_C, 38, 2, 10, true, 1 },
{ TYPE_INV, CH0, FLD_EVT_LOG, UNIT_NONE, 18, 2, 1, false, 0 },

{ TYPE_AC, CH0, FLD_YD, UNIT_WH, CALC_YD_CH0, 0, CMD_CALC, false, 0 },
{ TYPE_AC, CH0, FLD_YT, UNIT_KWH, CALC_YT_CH0, 0, CMD_CALC, false, 3 },
{ TYPE_AC, CH0, FLD_PDC, UNIT_W, CALC_PDC_CH0, 0, CMD_CALC, false, 1 },
{ TYPE_AC, CH0, FLD_EFF, UNIT_PCT, CALC_EFF_CH0, 0, CMD_CALC, false, 3 }
};

HMS_1CHv2::HMS_1CHv2(HoymilesRadio* radio, uint64_t serial)
: HMS_Abstract(radio, serial) {};

bool HMS_1CHv2::isValidSerial(uint64_t serial)
{
// serial >= 0x112500000000 && serial <= 0x112599999999
uint16_t preSerial = (serial >> 32) & 0xffff;
return preSerial == 0x1125;
}

String HMS_1CHv2::typeName()
{
return "HMS-500 v2";
}

const byteAssign_t* HMS_1CHv2::getByteAssignment()
{
return byteAssignment;
}

uint8_t HMS_1CHv2::getByteAssignmentSize()
{
return sizeof(byteAssignment) / sizeof(byteAssignment[0]);
}
14 changes: 14 additions & 0 deletions lib/Hoymiles/src/inverters/HMS_1CHv2.h
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 "HMS_Abstract.h"
#include <list>

class HMS_1CHv2 : public HMS_Abstract {
public:
explicit HMS_1CHv2(HoymilesRadio* radio, uint64_t serial);
static bool isValidSerial(uint64_t serial);
String typeName();
const byteAssign_t* getByteAssignment();
uint8_t getByteAssignmentSize();
};
1 change: 1 addition & 0 deletions lib/Hoymiles/src/parser/DevInfoParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const devInfo_t devInfo[] = {
{ { 0x10, 0x20, 0x41, ALL }, 400, "HMS-400" }, // 00
{ { 0x10, 0x10, 0x51, ALL }, 450, "HMS-450" }, // 01
{ { 0x10, 0x10, 0x71, ALL }, 500, "HMS-500" }, // 02
{ { 0x10, 0x20, 0x71, ALL }, 500, "HMS-500 v2" }, // 02
{ { 0x10, 0x21, 0x11, ALL }, 600, "HMS-600" }, // 01
{ { 0x10, 0x21, 0x41, ALL }, 800, "HMS-800" }, // 00
{ { 0x10, 0x11, 0x51, ALL }, 900, "HMS-900" }, // 01
Expand Down
37 changes: 22 additions & 15 deletions src/HttpPowerMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "HttpPowerMeter.h"
#include "MessageOutput.h"
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <FirebaseJson.h>
#include <Crypto.h>
#include <SHA256.h>
Expand Down Expand Up @@ -87,30 +86,19 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
wifiClient = std::make_unique<WiFiClient>();
}

HTTPClient httpClient;

if (!httpClient.begin(*wifiClient, newUrl)) {
snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str());
return false;
}

httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpClient.setUserAgent("OpenDTU-OnBattery");
httpClient.setConnectTimeout(timeout);
httpClient.setTimeout(timeout);
httpClient.addHeader("Content-Type", "application/json");
httpClient.addHeader("Accept", "application/json");

if (strlen(httpHeader) > 0) {
httpClient.addHeader(httpHeader, httpValue);
}

prepareRequest(timeout, httpHeader, httpValue);

if (authType == Auth::digest) {
const char *headers[1] = {"WWW-Authenticate"};
httpClient.collectHeaders(headers, 1);
}

int httpCode = httpClient.GET();

if (httpCode == HTTP_CODE_UNAUTHORIZED && authType == Auth::digest) {
// Handle authentication challenge
char realm[256]; // Buffer to store the realm received from the server
Expand Down Expand Up @@ -160,6 +148,12 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
authorization += "\", nc=00000001, qop=auth, response=\"";
authorization += response;
authorization += "\", algorithm=SHA-256";
httpClient.end();
if (!httpClient.begin(*wifiClient, newUrl)) {
snprintf_P(error, errorSize, "httpClient.begin(%s) for digest auth failed", newUrl.c_str());
return false;
}
prepareRequest(timeout, httpHeader, httpValue);
httpClient.addHeader("Authorization", authorization);
httpCode = httpClient.GET();
}
Expand Down Expand Up @@ -259,4 +253,17 @@ String HttpPowerMeterClass::sha256(const String& data) {
return hashStr;
}

void HttpPowerMeterClass::prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue) {
httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpClient.setUserAgent("OpenDTU-OnBattery");
httpClient.setConnectTimeout(timeout);
httpClient.setTimeout(timeout);
httpClient.addHeader("Content-Type", "application/json");
httpClient.addHeader("Accept", "application/json");

if (strlen(httpHeader) > 0) {
httpClient.addHeader(httpHeader, httpValue);
}
}

HttpPowerMeterClass HttpPowerMeter;
8 changes: 4 additions & 4 deletions src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void MqttHandleHassClass::publishField(std::shared_ptr<InverterAbstract> inv, Ch
root["stat_cla"] = stateCls;
}

char buffer[512];
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
} else {
Expand Down Expand Up @@ -181,7 +181,7 @@ void MqttHandleHassClass::publishInverterButton(std::shared_ptr<InverterAbstract
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, inv);

char buffer[512];
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ void MqttHandleHassClass::publishInverterNumber(
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, inv);

char buffer[512];
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
Expand Down Expand Up @@ -249,7 +249,7 @@ void MqttHandleHassClass::publishInverterBinarySensor(std::shared_ptr<InverterAb
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, inv);

char buffer[512];
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
Expand Down
12 changes: 6 additions & 6 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
},
"dependencies": {
"@popperjs/core": "^2.11.8",
"bootstrap": "^5.3.1",
"bootstrap": "^5.3.2",
"bootstrap-icons-vue": "^1.10.3",
"mitt": "^3.0.1",
"sortablejs": "^1.15.0",
"spark-md5": "^3.0.2",
"vue": "^3.3.4",
"vue-i18n": "^9.4.0",
"vue-i18n": "^9.4.1",
"vue-router": "^4.2.4"
},
"devDependencies": {
"@intlify/unplugin-vue-i18n": "^1.2.0",
"@rushstack/eslint-patch": "^1.3.3",
"@rushstack/eslint-patch": "^1.4.0",
"@tsconfig/node18": "^18.2.2",
"@types/bootstrap": "^5.2.6",
"@types/node": "^20.6.0",
"@types/bootstrap": "^5.2.7",
"@types/node": "^20.6.2",
"@types/sortablejs": "^1.15.2",
"@types/spark-md5": "^3.0.2",
"@vitejs/plugin-vue": "^4.3.4",
Expand All @@ -35,7 +35,7 @@
"eslint": "^8.49.0",
"eslint-plugin-vue": "^9.17.0",
"npm-run-all": "^4.1.5",
"sass": "^1.66.1",
"sass": "^1.67.0",
"terser": "^5.19.4",
"typescript": "^5.2.2",
"vite": "^4.4.9",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
"eventlog": {
"Start": "Begin",
"Stop": "Ende",
"Id": "Fehlernummer",
"Id": "ID",
"Message": "Meldung"
},
"devinfo": {
Expand Down
Loading

0 comments on commit ae39232

Please sign in to comment.