diff --git a/include/MqttHandleInverter.h b/include/MqttHandleInverter.h index ea3a6e3..d758017 100644 --- a/include/MqttHandleInverter.h +++ b/include/MqttHandleInverter.h @@ -20,6 +20,11 @@ public: private: void loop(); + + static void patrixSend(const std::shared_ptr& inv); + + static double patrixGetField(const std::shared_ptr& inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId); + void publishField(std::shared_ptr inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId); Task _loopTask; diff --git a/include/MqttHandleInverterTotal.h b/include/MqttHandleInverterTotal.h index ae4d66c..ce4d056 100644 --- a/include/MqttHandleInverterTotal.h +++ b/include/MqttHandleInverterTotal.h @@ -8,6 +8,8 @@ public: MqttHandleInverterTotalClass(); void init(Scheduler& scheduler); + static void patrixSend(); + private: void loop(); diff --git a/src/MqttHandleInverter.cpp b/src/MqttHandleInverter.cpp index 70a7222..c8360ae 100644 --- a/src/MqttHandleInverter.cpp +++ b/src/MqttHandleInverter.cpp @@ -107,12 +107,44 @@ void MqttHandleInverterClass::loop() } } } + patrixSend(inv); } yield(); } } +void MqttHandleInverterClass::patrixSend(const std::shared_ptr& inv) { + const auto totalKWh = patrixGetField(inv, TYPE_INV, CH0, FLD_YT); + const auto totalW = patrixGetField(inv, TYPE_AC, CH0, FLD_PAC); + + const auto string0KWh = patrixGetField(inv, TYPE_DC, CH0, FLD_YT); + const auto string0W = patrixGetField(inv, TYPE_DC, CH0, FLD_PDC); + + const auto string1KWh = patrixGetField(inv, TYPE_DC, CH1, FLD_YT); + const auto string1W = patrixGetField(inv, TYPE_DC, CH1, FLD_PDC); + + char buffer[300]; + snprintf(buffer, sizeof buffer, R"({"inverter": "%s", "timestamp": %ld, "totalKWh": %s, "totalW": %s, "string0KWh": %s, "string0W": %s, "string1KWh": %s, "string1W": %s})", + inv->serialString().c_str(), + std::time(nullptr), + isnan(totalKWh) ? "null" : String(totalKWh).c_str(), + isnan(totalW) ? "null" : String(totalW).c_str(), + isnan(string0KWh) ? "null" : String(string0KWh).c_str(), + isnan(string0W) ? "null" : String(string0W).c_str(), + isnan(string1KWh) ? "null" : String(string1KWh).c_str(), + isnan(string1W) ? "null" : String(string1W).c_str() + ); + MqttSettings.publish("patrix/json2", buffer); +} + +double MqttHandleInverterClass::patrixGetField(const std::shared_ptr& inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId) { + if (inv->Statistics()->getAssignmentByChannelField(type, channel, fieldId) == nullptr) { + return NAN; + } + return inv->Statistics()->getChannelFieldValue(type, channel, fieldId); +} + void MqttHandleInverterClass::publishField(std::shared_ptr inv, const ChannelType_t type, const ChannelNum_t channel, const FieldId_t fieldId) { const String topic = getTopic(inv, type, channel, fieldId); diff --git a/src/MqttHandleInverterTotal.cpp b/src/MqttHandleInverterTotal.cpp index 5f5be6a..b59c028 100644 --- a/src/MqttHandleInverterTotal.cpp +++ b/src/MqttHandleInverterTotal.cpp @@ -32,6 +32,7 @@ void MqttHandleInverterTotalClass::loop() return; } + patrixSend(); MqttSettings.publish("ac/power", String(Datastore.getTotalAcPowerEnabled(), Datastore.getTotalAcPowerDigits())); MqttSettings.publish("ac/yieldtotal", String(Datastore.getTotalAcYieldTotalEnabled(), Datastore.getTotalAcYieldTotalDigits())); MqttSettings.publish("ac/yieldday", String(Datastore.getTotalAcYieldDayEnabled(), Datastore.getTotalAcYieldDayDigits())); @@ -40,3 +41,13 @@ void MqttHandleInverterTotalClass::loop() MqttSettings.publish("dc/irradiation", String(Datastore.getTotalDcIrradiation(), 3)); MqttSettings.publish("dc/is_valid", String(Datastore.getIsAllEnabledReachable())); } + +void MqttHandleInverterTotalClass::patrixSend() { + const auto power = Datastore.getTotalAcPowerEnabled(); + const auto energy = Datastore.getTotalAcYieldTotalEnabled(); + if (!isnan(power) && !isnan(energy)) { + char buffer[200]; + snprintf(buffer, sizeof buffer, R"({"timestamp": %ld, "energyProducedKWh": %f, "powerW": %f})", std::time(nullptr), energy, power); + MqttSettings.publish("patrix/json", buffer); + } +}