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
|
|
@ -1,11 +1,14 @@
|
|||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
#include "dtu.h"
|
||||
#include "microinverter.h"
|
||||
|
||||
#include "portParameters.h"
|
||||
|
||||
struct _modbus;
|
||||
typedef _modbus modbus_t;
|
||||
|
||||
|
|
@ -27,15 +30,61 @@ Dtu::~Dtu() {
|
|||
}
|
||||
|
||||
void Dtu::populateMicroinverters() {
|
||||
uint16_t address{0x1000};
|
||||
Microinverter microinverter{this->modbus_context, &this->modbus_context_mutex};
|
||||
this->microinverters.push_back(microinverter);
|
||||
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 };
|
||||
|
||||
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.back().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();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void Dtu::updateMicroinverters() {
|
||||
std::vector<std::thread> updateThreads;
|
||||
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while (microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->updatePorts();
|
||||
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;
|
||||
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_mutex = modbus_context_mutex;
|
||||
|
||||
this->populatePorts();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
this->serialNumber = serialNumber;
|
||||
}
|
||||
|
||||
void Microinverter::updatePorts() {
|
||||
|
|
|
|||
|
|
@ -55,5 +55,6 @@ void Port::updateParameters() {
|
|||
while (parametersIterator != this->parameters.end()) {
|
||||
parametersIterator->get()->updateValue(this->modbus_context, this->modbus_context_mutex, this->portStartAddress);
|
||||
parametersIterator++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue