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"
|
|
|
|
|
|
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-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-17 21:33:21 +01:00
|
|
|
|
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()) {
|
2024-03-19 12:54:05 +01:00
|
|
|
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];
|
2024-03-18 22:49:32 +01:00
|
|
|
|
|
|
|
|
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};
|
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-20 16:39:25 +01:00
|
|
|
long long serialNumber = portParameterMicroinverterSerialNumber.getValue().first.i;
|
2024-03-18 22:49:32 +01:00
|
|
|
|
2024-03-20 22:48:59 +01:00
|
|
|
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};
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 22:48:59 +01:00
|
|
|
std::pair<Microinverter *, bool> Dtu::getMicroinverterBySerialNumber(long long serialNumber) {
|
2024-03-18 22:49:32 +01:00
|
|
|
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) {
|
2024-03-20 22:48:59 +01:00
|
|
|
return std::pair<Microinverter *, bool>(&*microinvertersIterator, true);
|
2024-03-20 19:55:36 +01:00
|
|
|
} else {
|
2024-03-18 22:49:32 +01:00
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-20 22:48:59 +01:00
|
|
|
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> ¶metersToGet, bool allParameters, std::vector<long long> µinvertersToGet) {
|
2024-03-20 19:55:36 +01:00
|
|
|
if (microinvertersToGet.empty()) {
|
2024-03-20 14:40:02 +01:00
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
2024-03-20 19:55:36 +01:00
|
|
|
while (microinvertersIterator != this->microinverters.end()) {
|
2024-03-20 14:40:02 +01:00
|
|
|
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 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()) {
|
2024-03-20 22:48:59 +01:00
|
|
|
std::pair<Microinverter *, bool> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
|
|
|
|
|
if (microinverterPair.second) {
|
|
|
|
|
microinverterPair.first->updatePorts(parametersToGet, allParameters);
|
2024-03-20 14:40:02 +01:00
|
|
|
}
|
|
|
|
|
microinvertersToGetIterator++;
|
2024-03-20 11:28:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 22:48:59 +01:00
|
|
|
void Dtu::printMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long long> µinvertersToGet, bool shortNames, bool printTodayProduction, bool printTotalProduction) {
|
2024-03-20 19:55:36 +01:00
|
|
|
if (microinvertersToGet.empty()) {
|
2024-03-20 14:40:02 +01:00
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
2024-03-20 19:55:36 +01:00
|
|
|
while (microinvertersIterator != this->microinverters.end()) {
|
2024-03-20 14:40:02 +01:00
|
|
|
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 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()) {
|
2024-03-20 22:48:59 +01:00
|
|
|
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);
|
2024-03-20 14:40:02 +01:00
|
|
|
}
|
|
|
|
|
microinvertersToGetIterator++;
|
2024-03-20 11:28:43 +01:00
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|