Backend is mostly done, mapping out the plant automagically works
This commit is contained in:
parent
ce64341ccc
commit
b48d2241d2
5 changed files with 64 additions and 27 deletions
|
|
@ -19,6 +19,8 @@ class Dtu {
|
||||||
|
|
||||||
void populateMicroinverters();
|
void populateMicroinverters();
|
||||||
|
|
||||||
|
std::pair<bool, Microinverter*> getMicroinverterBySerialNumber(long serialNumber);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dtu(const char *ip_address, int port);
|
Dtu(const char *ip_address, int port);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,14 @@ class Microinverter {
|
||||||
std::shared_ptr<modbus_t*> modbus_context;
|
std::shared_ptr<modbus_t*> modbus_context;
|
||||||
std::mutex *modbus_context_mutex;
|
std::mutex *modbus_context_mutex;
|
||||||
|
|
||||||
std::vector<Port> ports;
|
|
||||||
|
|
||||||
void populatePorts();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex);
|
Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, long serialNumber);
|
||||||
|
|
||||||
|
long serialNumber;
|
||||||
|
|
||||||
|
std::vector<Port> ports;
|
||||||
|
|
||||||
void updatePorts();
|
void updatePorts();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
|
|
||||||
#include "dtu.h"
|
#include "dtu.h"
|
||||||
#include "microinverter.h"
|
#include "microinverter.h"
|
||||||
|
|
||||||
|
#include "portParameters.h"
|
||||||
|
|
||||||
struct _modbus;
|
struct _modbus;
|
||||||
typedef _modbus modbus_t;
|
typedef _modbus modbus_t;
|
||||||
|
|
||||||
|
|
@ -27,15 +30,61 @@ Dtu::~Dtu() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dtu::populateMicroinverters() {
|
void Dtu::populateMicroinverters() {
|
||||||
uint16_t address{0x1000};
|
uint16_t portStartAddress = 0x1000;
|
||||||
Microinverter microinverter{this->modbus_context, &this->modbus_context_mutex};
|
uint16_t readArray[1];
|
||||||
|
|
||||||
|
int registerCount;
|
||||||
|
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
||||||
|
while(registerCount != -1 && readArray[0] == 0x700) {
|
||||||
|
Port port{ this->modbus_context, &this->modbus_context_mutex, portStartAddress };
|
||||||
|
|
||||||
|
PortParameterMicroinverterSerialNumber portParameterMicroinverterSerialNumber{};
|
||||||
|
portParameterMicroinverterSerialNumber.updateValue(this->modbus_context, &this->modbus_context_mutex, portStartAddress);
|
||||||
|
long serialNumber = portParameterMicroinverterSerialNumber.getValue().first.i;
|
||||||
|
|
||||||
|
std::pair<bool, Microinverter*> getMicroinverterBySerialNumber = this->getMicroinverterBySerialNumber(serialNumber);
|
||||||
|
if(getMicroinverterBySerialNumber.first) {
|
||||||
|
getMicroinverterBySerialNumber.second->ports.push_back(port);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Microinverter microinverter{ this->modbus_context, &this->modbus_context_mutex, serialNumber };
|
||||||
this->microinverters.push_back(microinverter);
|
this->microinverters.push_back(microinverter);
|
||||||
|
this->microinverters.back().ports.push_back(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dtu::updateMicroinverters() {
|
portStartAddress += 0x0028;
|
||||||
|
|
||||||
|
this->modbus_context_mutex.lock();
|
||||||
|
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
||||||
|
this->modbus_context_mutex.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<bool, Microinverter*> Dtu::getMicroinverterBySerialNumber(long serialNumber) {
|
||||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||||
while(microinvertersIterator != this->microinverters.end()) {
|
while(microinvertersIterator != this->microinverters.end()) {
|
||||||
microinvertersIterator->updatePorts();
|
if(microinvertersIterator->serialNumber == serialNumber) {
|
||||||
|
return std::pair<bool, Microinverter*>(true, &*microinvertersIterator);
|
||||||
|
}
|
||||||
|
else{
|
||||||
microinvertersIterator++;
|
microinvertersIterator++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return std::pair<bool, Microinverter*>(false, &*microinvertersIterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Dtu::updateMicroinverters() {
|
||||||
|
std::vector<std::thread> updateThreads;
|
||||||
|
|
||||||
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||||
|
while (microinvertersIterator != this->microinverters.end()) {
|
||||||
|
updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
|
||||||
|
microinvertersIterator++;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
|
||||||
|
while(updateThreadsIterator != updateThreads.end()) {
|
||||||
|
updateThreadsIterator->join();
|
||||||
|
updateThreadsIterator++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,28 +8,11 @@
|
||||||
struct _modbus;
|
struct _modbus;
|
||||||
typedef _modbus modbus_t;
|
typedef _modbus modbus_t;
|
||||||
|
|
||||||
Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex) {
|
Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, long serialNumber) {
|
||||||
this->modbus_context = modbus_context;
|
this->modbus_context = modbus_context;
|
||||||
this->modbus_context_mutex = modbus_context_mutex;
|
this->modbus_context_mutex = modbus_context_mutex;
|
||||||
|
|
||||||
this->populatePorts();
|
this->serialNumber = serialNumber;
|
||||||
}
|
|
||||||
|
|
||||||
void Microinverter::populatePorts() {
|
|
||||||
uint16_t portStartAddress = 0x1000;
|
|
||||||
uint16_t readArray[1];
|
|
||||||
|
|
||||||
int registerCount;
|
|
||||||
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
|
||||||
while(registerCount != -1 && readArray[0] == 0x700) {
|
|
||||||
Port port{ this->modbus_context, this->modbus_context_mutex, portStartAddress };
|
|
||||||
this->ports.push_back(port);
|
|
||||||
portStartAddress += 0x0028;
|
|
||||||
|
|
||||||
this->modbus_context_mutex->lock();
|
|
||||||
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
|
||||||
this->modbus_context_mutex->unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microinverter::updatePorts() {
|
void Microinverter::updatePorts() {
|
||||||
|
|
|
||||||
|
|
@ -55,5 +55,6 @@ void Port::updateParameters() {
|
||||||
while (parametersIterator != this->parameters.end()) {
|
while (parametersIterator != this->parameters.end()) {
|
||||||
parametersIterator->get()->updateValue(this->modbus_context, this->modbus_context_mutex, this->portStartAddress);
|
parametersIterator->get()->updateValue(this->modbus_context, this->modbus_context_mutex, this->portStartAddress);
|
||||||
parametersIterator++;
|
parametersIterator++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue