2024-04-09 20:31:43 +02:00
|
|
|
#include <chrono>
|
2024-03-16 21:15:15 +01:00
|
|
|
#include <iostream>
|
2024-03-20 11:28:43 +01:00
|
|
|
#include <string>
|
2024-04-09 12:21:21 +02:00
|
|
|
#include <thread>
|
2024-04-09 20:31:43 +02: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-04-09 12:21:21 +02:00
|
|
|
Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) {
|
2024-04-09 20:31:43 +02:00
|
|
|
if (tcp) {
|
2024-04-09 12:21:21 +02:00
|
|
|
this->modbus = modbus_new_tcp(address, id);
|
|
|
|
|
}
|
2024-04-09 20:31:43 +02:00
|
|
|
if (rtu) {
|
2024-04-09 12:21:21 +02:00
|
|
|
this->modbus = modbus_new_rtu(address, 9600, 'N', 8, 1);
|
|
|
|
|
modbus_rtu_set_serial_mode(this->modbus, MODBUS_RTU_RS485);
|
|
|
|
|
}
|
2024-03-17 21:33:21 +01:00
|
|
|
|
2024-04-09 12:21:21 +02:00
|
|
|
this->connected = false;
|
|
|
|
|
if (modbus_connect(this->modbus) == -1) {
|
2024-03-28 11:05:07 +01:00
|
|
|
std::cerr << "NOT CONNECTED" << std::endl;
|
2024-04-09 20:31:43 +02:00
|
|
|
} else {
|
2024-04-09 12:21:21 +02:00
|
|
|
this->connected = true;
|
2024-04-09 20:31:43 +02:00
|
|
|
if (rtu) {
|
2024-04-09 12:21:21 +02:00
|
|
|
modbus_set_slave(this->modbus, id);
|
|
|
|
|
}
|
2024-03-19 12:54:05 +01:00
|
|
|
this->populateMicroinverters();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:21:21 +02:00
|
|
|
bool Dtu::isConnected() { return this->connected; }
|
2024-03-16 21:15:15 +01:00
|
|
|
|
2024-04-09 12:21:21 +02:00
|
|
|
Dtu::~Dtu() {
|
|
|
|
|
modbus_close(this->modbus);
|
|
|
|
|
modbus_free(this->modbus);
|
|
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
|
|
|
|
|
void Dtu::populateMicroinverters() {
|
2024-04-05 15:54:36 +02:00
|
|
|
int portStartAddress = 0x4000;
|
|
|
|
|
uint16_t registers[19];
|
2024-03-18 22:49:32 +01:00
|
|
|
|
2024-04-09 20:47:44 +02:00
|
|
|
while (portStartAddress <= (0x4000 + (0x0019 * 99))) {
|
2024-04-09 20:31:43 +02:00
|
|
|
int registerCount;
|
|
|
|
|
registerCount = modbus_read_registers(this->modbus, portStartAddress, 19, registers);
|
2024-04-09 20:47:44 +02:00
|
|
|
portStartAddress += 0x0019;
|
2024-04-09 20:31:43 +02:00
|
|
|
if (registers[0] == 12) {
|
|
|
|
|
Port port{portStartAddress};
|
|
|
|
|
port.setParametersFromMicroinverterArray(registers, 0);
|
2024-03-28 19:44:54 +01:00
|
|
|
|
2024-04-09 20:31:43 +02:00
|
|
|
if (!this->getMicroinverterBySerialNumber(port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i).second) {
|
|
|
|
|
Microinverter microinverter{this->modbus, portStartAddress, port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i};
|
|
|
|
|
this->microinverters.push_back(microinverter);
|
|
|
|
|
}
|
2024-03-28 19:44:54 +01:00
|
|
|
|
2024-04-09 20:31:43 +02:00
|
|
|
this->getMicroinverterBySerialNumber(port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i).first->ports.push_back(port);
|
2024-03-18 22:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:21:21 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
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) {
|
2024-03-28 19:44:54 +01:00
|
|
|
microinverterPair.first->updateParameters(parametersToGet, allParameters);
|
2024-04-06 00:32:49 +02:00
|
|
|
microinverterPair.first->updateStatusParameters();
|
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) {
|
2024-04-09 20:31:43 +02:00
|
|
|
std::cout << " "
|
|
|
|
|
<< "Microinverter: " << microinverterPair.first->serialNumber << std::endl;
|
|
|
|
|
std::cout << " "
|
|
|
|
|
<< "Microinverter Data Age: " << microinverterPair.first->age << std::endl;
|
2024-03-28 19:44:54 +01:00
|
|
|
if (printTodayProduction) {
|
2024-04-09 20:31:43 +02:00
|
|
|
std::cout << " "
|
|
|
|
|
<< "TodayProduction: " << microinverterPair.first->getTodayProduction() << "Wh" << std::endl;
|
2024-03-20 22:48:59 +01:00
|
|
|
}
|
2024-03-28 19:44:54 +01:00
|
|
|
if (printTotalProduction) {
|
2024-04-09 20:31:43 +02:00
|
|
|
std::cout << " "
|
|
|
|
|
<< "TotalProduction: " << microinverterPair.first->getTotalProduction() << "Wh" << std::endl;
|
2024-03-20 22:48:59 +01:00
|
|
|
}
|
|
|
|
|
microinverterPair.first->printPorts(parametersToGet, allParameters, shortNames);
|
2024-03-28 19:44:54 +01:00
|
|
|
std::cout << std::endl;
|
2024-03-20 14:40:02 +01:00
|
|
|
}
|
|
|
|
|
microinvertersToGetIterator++;
|
2024-03-20 11:28:43 +01:00
|
|
|
}
|
2024-04-06 16:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 20:31:43 +02:00
|
|
|
void Dtu::setStatusMicroinverters(uint16_t value, std::string statusName, std::vector<long long> µinvertersToSet) {
|
2024-04-06 16:29:02 +02:00
|
|
|
if (microinvertersToSet.empty()) {
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while (microinvertersIterator != this->microinverters.end()) {
|
|
|
|
|
microinvertersToSet.push_back(microinvertersIterator->serialNumber);
|
|
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<long long>::iterator microinvertersToSetIterator = microinvertersToSet.begin();
|
2024-04-09 20:31:43 +02:00
|
|
|
while (microinvertersToSetIterator != microinvertersToSet.end()) {
|
2024-04-06 16:29:02 +02:00
|
|
|
std::pair<Microinverter *, bool> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToSetIterator);
|
2024-04-09 20:31:43 +02:00
|
|
|
if (microinverterPair.second) {
|
2024-04-06 16:29:02 +02:00
|
|
|
microinverterPair.first->setStatusWholeMicroinverter(value, statusName);
|
|
|
|
|
}
|
|
|
|
|
microinvertersToSetIterator++;
|
|
|
|
|
}
|
2024-04-06 19:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 20:31:43 +02:00
|
|
|
bool Dtu::empty() { return this->microinverters.empty(); }
|
2024-04-09 12:21:21 +02:00
|
|
|
|
|
|
|
|
void Dtu::listOfMicroinverters() {
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
std::cout << "Microinverter list:" << std::endl;
|
2024-04-09 20:31:43 +02:00
|
|
|
while (microinvertersIterator != this->microinverters.end()) {
|
2024-04-09 12:21:21 +02:00
|
|
|
std::cout << " " << microinvertersIterator->serialNumber << std::endl;
|
|
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|