hoymilesClient/src/hoymiles/dtu.cpp

110 lines
4 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-20 11:28:43 +01:00
std::cerr << "conn_error" << std::endl;
this->connected = false;
2024-03-20 19:55:36 +01:00
} else {
this->connected = true;
2024-03-16 21:15:15 +01:00
}
2024-03-20 19:55:36 +01:00
if (this->connected) {
this->populateMicroinverters();
}
}
2024-03-20 19:55:36 +01:00
bool Dtu::isConnected() { return this->connected; }
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() {
uint16_t portStartAddress = 0x1000;
uint16_t readArray[1];
int registerCount;
2024-03-19 16:59:41 +01:00
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 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;
2024-03-20 19:55:36 +01:00
std::pair<bool, Microinverter *> getMicroinverterBySerialNumber = this->getMicroinverterBySerialNumber(serialNumber);
if (getMicroinverterBySerialNumber.first) {
getMicroinverterBySerialNumber.second->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);
}
}
2024-03-20 19:55:36 +01:00
std::pair<bool, Microinverter *> 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<bool, Microinverter *>(true, &*microinvertersIterator);
} else {
microinvertersIterator++;
}
}
2024-03-20 19:55:36 +01:00
return std::pair<bool, Microinverter *>(false, &*microinvertersIterator);
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<bool, Microinverter *> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
if (microinverterPair.first) {
microinverterPair.second->updatePorts(parametersToGet, allParameters);
}
microinvertersToGetIterator++;
2024-03-20 11:28:43 +01:00
}
}
2024-03-20 20:17:15 +01:00
void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet, bool shortNames) {
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<bool, Microinverter *> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
if (microinverterPair.first) {
2024-03-20 20:17:15 +01:00
microinverterPair.second->printPorts(parametersToGet, allParameters, shortNames);
}
microinvertersToGetIterator++;
2024-03-20 11:28:43 +01:00
}
2024-03-16 21:15:15 +01:00
}