hoymilesClient/src/hoymiles/dtu.cpp

118 lines
4.5 KiB
C++
Raw Normal View History

2024-03-16 21:15:15 +01:00
#include <iostream>
2024-03-20 11:28:43 +01:00
#include <string>
2024-03-20 19:55:36 +01:00
#include <vector>
2024-03-16 21:15:15 +01:00
#include "modbus.h"
#include "dtu.h"
#include "microinverter.h"
#include "portParameters.h"
2024-03-16 21:15:15 +01:00
Dtu::Dtu(const char *ip_address, int port) {
2024-03-20 19:55:36 +01:00
class modbus modbus {
ip_address, (uint16_t)port
};
2024-03-19 16:59:41 +01:00
this->modbus = std::make_shared<class modbus>(modbus);
2024-03-19 16:59:41 +01:00
if (!this->modbus.get()->modbus_connect()) {
2024-03-28 11:05:07 +01:00
std::cerr << "NOT CONNECTED" << std::endl;
2024-03-16 21:15:15 +01:00
}
2024-03-28 11:05:07 +01:00
if (this->modbus.get()->is_connected()) {
this->populateMicroinverters();
}
}
2024-03-28 11:05:07 +01:00
bool Dtu::isConnected() { return this->modbus.get()->is_connected(); }
bool Dtu::modbusError() { return this->modbus.get()->err; }
std::string Dtu::modbusErrorMessage() { return this->modbus.get()->error_msg; }
2024-03-16 21:15:15 +01:00
2024-03-20 19:55:36 +01:00
Dtu::~Dtu() { this->modbus.get()->modbus_close(); }
2024-03-16 21:15:15 +01:00
void Dtu::populateMicroinverters() {
2024-03-28 11:05:07 +01:00
int portStartAddress = 0x1000;
uint16_t readArray[20];
int registerCount;
2024-03-28 11:05:07 +01:00
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0001, 1, readArray);
2024-03-20 19:55:36 +01:00
while (registerCount != -1 && readArray[0] == 0x700) {
Port port{this->modbus, portStartAddress};
PortParameterMicroinverterSerialNumber portParameterMicroinverterSerialNumber{};
2024-03-19 16:59:41 +01:00
portParameterMicroinverterSerialNumber.updateValue(this->modbus, portStartAddress);
2024-03-20 16:39:25 +01:00
long long serialNumber = portParameterMicroinverterSerialNumber.getValue().first.i;
std::pair<Microinverter *, bool> getMicroinverterBySerialNumber = this->getMicroinverterBySerialNumber(serialNumber);
if (getMicroinverterBySerialNumber.second) {
getMicroinverterBySerialNumber.first->ports.push_back(port);
2024-03-20 19:55:36 +01:00
} else {
Microinverter microinverter{this->modbus, serialNumber};
this->microinverters.push_back(microinverter);
this->microinverters.back().ports.push_back(port);
}
portStartAddress += 0x0028;
2024-03-19 18:13:27 +01:00
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 1, readArray);
}
}
std::pair<Microinverter *, bool> Dtu::getMicroinverterBySerialNumber(long long serialNumber) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
2024-03-20 19:55:36 +01:00
while (microinvertersIterator != this->microinverters.end()) {
if (microinvertersIterator->serialNumber == serialNumber) {
return std::pair<Microinverter *, bool>(&*microinvertersIterator, true);
2024-03-20 19:55:36 +01:00
} else {
microinvertersIterator++;
}
}
return std::pair<Microinverter *, bool>(&*microinvertersIterator, false);
2024-03-16 21:15:15 +01:00
}
2024-03-20 16:39:25 +01:00
void Dtu::updateMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet) {
2024-03-20 19:55:36 +01:00
if (microinvertersToGet.empty()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
2024-03-20 19:55:36 +01:00
while (microinvertersIterator != this->microinverters.end()) {
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
microinvertersIterator++;
}
2024-03-16 21:15:15 +01:00
}
2024-03-20 16:39:25 +01:00
std::vector<long long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
2024-03-20 19:55:36 +01:00
while (microinvertersToGetIterator != microinvertersToGet.end()) {
std::pair<Microinverter *, bool> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
if (microinverterPair.second) {
microinverterPair.first->updatePorts(parametersToGet, allParameters);
}
microinvertersToGetIterator++;
2024-03-20 11:28:43 +01:00
}
}
void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet, bool shortNames, bool printTodayProduction, bool printTotalProduction) {
2024-03-20 19:55:36 +01:00
if (microinvertersToGet.empty()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
2024-03-20 19:55:36 +01:00
while (microinvertersIterator != this->microinverters.end()) {
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
microinvertersIterator++;
}
}
2024-03-20 11:28:43 +01:00
2024-03-20 16:39:25 +01:00
std::vector<long long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
2024-03-20 19:55:36 +01:00
while (microinvertersToGetIterator != microinvertersToGet.end()) {
std::pair<Microinverter *, bool> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
if (microinverterPair.second) {
std::cout << "Microinverter: " << microinverterPair.first->serialNumber << std::endl;
if(printTodayProduction) {
std::cout << "TodayProduction: " << microinverterPair.first->getTodayProduction() << std::endl;
}
if(printTotalProduction) {
std::cout << "TotalProduction: " << microinverterPair.first->getTotalProduction() << std::endl;
}
microinverterPair.first->printPorts(parametersToGet, allParameters, shortNames);
}
microinvertersToGetIterator++;
2024-03-20 11:28:43 +01:00
}
2024-03-16 21:15:15 +01:00
}