2024-03-16 21:15:15 +01:00
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
2024-03-20 11:28:43 +01:00
|
|
|
#include <string>
|
2024-03-16 21:15:15 +01:00
|
|
|
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
|
|
|
|
|
#include "dtu.h"
|
|
|
|
|
#include "microinverter.h"
|
|
|
|
|
|
2024-03-18 22:49:32 +01:00
|
|
|
#include "portParameters.h"
|
|
|
|
|
|
2024-03-16 21:15:15 +01:00
|
|
|
|
|
|
|
|
Dtu::Dtu(const char *ip_address, int port) {
|
2024-03-19 16:59:41 +01:00
|
|
|
class modbus modbus{ip_address, (uint16_t) port};
|
|
|
|
|
this->modbus = std::make_shared<class modbus>(modbus);
|
2024-03-17 21:33:21 +01:00
|
|
|
|
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;
|
2024-03-19 12:54:05 +01:00
|
|
|
this->connected = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this->connected = true;
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-19 12:54:05 +01:00
|
|
|
if(this->connected) {
|
|
|
|
|
this->populateMicroinverters();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Dtu::isConnected() {
|
|
|
|
|
return this->connected;
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dtu::~Dtu() {
|
2024-03-19 16:59:41 +01:00
|
|
|
this->modbus.get()->modbus_close();
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dtu::populateMicroinverters() {
|
2024-03-18 22:49:32 +01:00
|
|
|
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-18 22:49:32 +01:00
|
|
|
while(registerCount != -1 && readArray[0] == 0x700) {
|
2024-03-19 16:59:41 +01:00
|
|
|
Port port{ this->modbus, portStartAddress };
|
2024-03-18 22:49:32 +01:00
|
|
|
|
|
|
|
|
PortParameterMicroinverterSerialNumber portParameterMicroinverterSerialNumber{};
|
2024-03-19 16:59:41 +01:00
|
|
|
portParameterMicroinverterSerialNumber.updateValue(this->modbus, portStartAddress);
|
2024-03-18 22:49:32 +01:00
|
|
|
long serialNumber = portParameterMicroinverterSerialNumber.getValue().first.i;
|
|
|
|
|
|
|
|
|
|
std::pair<bool, Microinverter*> getMicroinverterBySerialNumber = this->getMicroinverterBySerialNumber(serialNumber);
|
|
|
|
|
if(getMicroinverterBySerialNumber.first) {
|
|
|
|
|
getMicroinverterBySerialNumber.second->ports.push_back(port);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-03-19 18:13:27 +01:00
|
|
|
Microinverter microinverter{ this->modbus, serialNumber };
|
2024-03-18 22:49:32 +01:00
|
|
|
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-18 22:49:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<bool, Microinverter*> Dtu::getMicroinverterBySerialNumber(long serialNumber) {
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while(microinvertersIterator != this->microinverters.end()) {
|
|
|
|
|
if(microinvertersIterator->serialNumber == serialNumber) {
|
|
|
|
|
return std::pair<bool, Microinverter*>(true, &*microinvertersIterator);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return std::pair<bool, Microinverter*>(false, &*microinvertersIterator);
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-20 14:40:02 +01:00
|
|
|
void Dtu::updateMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet) {
|
|
|
|
|
if(microinvertersToGet.empty()) {
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while(microinvertersIterator != this->microinverters.end()) {
|
|
|
|
|
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
|
|
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
2024-03-18 22:49:32 +01:00
|
|
|
|
2024-03-20 14:40:02 +01:00
|
|
|
std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
|
|
|
|
|
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 14:40:02 +01:00
|
|
|
void Dtu::printMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet) {
|
|
|
|
|
if(microinvertersToGet.empty()) {
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while(microinvertersIterator != this->microinverters.end()) {
|
|
|
|
|
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
|
|
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
2024-03-18 22:49:32 +01:00
|
|
|
}
|
2024-03-20 11:28:43 +01:00
|
|
|
|
2024-03-20 14:40:02 +01:00
|
|
|
std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
|
|
|
|
|
while(microinvertersToGetIterator != microinvertersToGet.end()) {
|
|
|
|
|
std::pair<bool, Microinverter*> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
|
|
|
|
|
if(microinverterPair.first) {
|
|
|
|
|
microinverterPair.second->printPorts(parametersToGet, allParameters);
|
|
|
|
|
}
|
|
|
|
|
microinvertersToGetIterator++;
|
2024-03-20 11:28:43 +01:00
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|