hoymilesClient/src/hoymiles/dtu.cpp

166 lines
6 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-04-09 12:21:21 +02:00
#include <chrono>
#include <thread>
2024-03-16 21:15:15 +01:00
#include "modbus.h"
#include "dtu.h"
#include "microinverter.h"
#include "portParameters.h"
2024-04-09 12:21:21 +02:00
Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) {
if(tcp) {
this->modbus = modbus_new_tcp(address, id);
}
if(rtu) {
this->modbus = modbus_new_rtu(address, 9600, 'N', 8, 1);
modbus_rtu_set_serial_mode(this->modbus, MODBUS_RTU_RS485);
}
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-03-16 21:15:15 +01:00
}
2024-04-09 12:21:21 +02:00
else {
this->connected = true;
if(rtu) {
modbus_set_slave(this->modbus, id);
}
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() {
int portStartAddress = 0x4000;
uint16_t registers[19];
int registerCount;
2024-04-09 12:21:21 +02:00
registerCount = modbus_read_registers(this->modbus, portStartAddress, 19, registers);
2024-04-09 12:21:21 +02:00
if (registerCount == -1) {
return;
}
2024-04-09 12:21:21 +02:00
while (registerCount != -1) {
if(registers[0] != 12) {
break;
}
Port port{portStartAddress};
port.setParametersFromMicroinverterArray(registers, 0);
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);
}
this->getMicroinverterBySerialNumber(port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i).first->ports.push_back(port);
portStartAddress += 0x0019;
2024-04-09 12:21:21 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(10));
registerCount = modbus_read_registers(this->modbus, portStartAddress, 19, registers);
}
}
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->updateParameters(parametersToGet, allParameters);
2024-04-06 00:32:49 +02:00
microinverterPair.first->updateStatusParameters();
}
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;
std::cout << " " << "Microinverter Data Age: " << microinverterPair.first->age << std::endl;
if (printTodayProduction) {
std::cout << " " << "TodayProduction: " << microinverterPair.first->getTodayProduction() << "Wh" << std::endl;
}
if (printTotalProduction) {
std::cout << " " << "TotalProduction: " << microinverterPair.first->getTotalProduction() << "Wh" << std::endl;
}
microinverterPair.first->printPorts(parametersToGet, allParameters, shortNames);
std::cout << std::endl;
}
microinvertersToGetIterator++;
2024-03-20 11:28:43 +01:00
}
2024-04-06 16:29:02 +02:00
}
void Dtu::setStatusMicroinverters(uint16_t value, std::string statusName, std::vector<long long>& microinvertersToSet) {
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();
while(microinvertersToSetIterator != microinvertersToSet.end()) {
std::pair<Microinverter *, bool> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToSetIterator);
if(microinverterPair.second) {
microinverterPair.first->setStatusWholeMicroinverter(value, statusName);
}
microinvertersToSetIterator++;
}
2024-04-06 19:29:23 +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;
while(microinvertersIterator != this->microinverters.end()) {
std::cout << " " << microinvertersIterator->serialNumber << std::endl;
microinvertersIterator++;
}
2024-03-16 21:15:15 +01:00
}