2024-03-16 21:15:15 +01:00
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
2024-03-19 12:54:05 +01:00
|
|
|
// #include <thread>
|
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-19 16:59:41 +01:00
|
|
|
// struct _modbus;
|
|
|
|
|
// typedef _modbus modbus_t;
|
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-16 21:15:15 +01:00
|
|
|
std::cerr << "conn_error";
|
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 13:02:41 +01:00
|
|
|
// this->modbus_context_mutex.lock();
|
2024-03-19 18:13:27 +01:00
|
|
|
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 1, readArray);
|
2024-03-19 13:02:41 +01:00
|
|
|
// this->modbus_context_mutex.unlock();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dtu::updateMicroinverters() {
|
2024-03-19 12:54:05 +01:00
|
|
|
// std::vector<std::thread> updateThreads;
|
2024-03-18 22:49:32 +01:00
|
|
|
|
2024-03-16 21:15:15 +01:00
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while (microinvertersIterator != this->microinverters.end()) {
|
2024-03-19 12:54:05 +01:00
|
|
|
// updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
|
|
|
|
|
microinvertersIterator->updatePorts();
|
2024-03-16 21:15:15 +01:00
|
|
|
microinvertersIterator++;
|
|
|
|
|
}
|
2024-03-18 22:49:32 +01:00
|
|
|
|
2024-03-19 12:54:05 +01:00
|
|
|
// std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
|
|
|
|
|
// while(updateThreadsIterator != updateThreads.end()) {
|
|
|
|
|
// updateThreadsIterator->join(); updateThreadsIterator++;
|
|
|
|
|
// }
|
|
|
|
|
// std::cout << std::endl;
|
2024-03-18 23:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dtu::printMicroinverters() {
|
|
|
|
|
std::cout << "DTU:" << std::endl;
|
|
|
|
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
|
|
|
while(microinvertersIterator != this->microinverters.end()) {
|
|
|
|
|
microinvertersIterator->printPorts();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
microinvertersIterator++;
|
2024-03-18 22:49:32 +01:00
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|