#include #include #include #include #include "modbus.h" #include "port.h" #include "portParameters.h" Port::Port(std::shared_ptr modbus, uint16_t portStartAddress) { this->modbus = modbus; this->portStartAddress = portStartAddress; this->currentFixed = false; this->populateParameters(); } void Port::populateParameters() { this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); this->parameters.push_back(std::make_shared()); } std::pair, bool> Port::getParameterByName(std::string name) { std::pair, bool> result; result.second = false; std::vector>::iterator parametersIterator = this->parameters.begin(); while(parametersIterator != this->parameters.end() && !result.second) { if(parametersIterator->get()->name == name) { result.first = *parametersIterator; result.second = true; } } return result; } void Port::fixCurrent() { if(this->currentFixed) { return; } if(this->parameters.size() < 8) { return; } if(this->parameters.at(7).get()->getValue().first.f == 0) { return; } if(this->parameters.at(2).get()->getValue().first.f * this->parameters.at(4).get()->getValue().first.f < this->parameters.at(7).get()->getValue().first.f) { this->parameters.erase(this->parameters.begin() + 4); } else { this->parameters.erase(this->parameters.begin() + 3); } this->currentFixed = true; } void Port::updateParameters() { std::vector>::iterator parametersIterator{this->parameters.begin()}; while (parametersIterator != this->parameters.end()) { parametersIterator->get()->updateValue(this->modbus, this->portStartAddress); parametersIterator++; } this->fixCurrent(); } void Port::updateParameters(std::vector ¶metersToGet) { std::vector::iterator parametersToGetIterator = parametersToGet.begin(); while(parametersToGetIterator != parametersToGet.end()) { std::pair, bool> parameterPair; parameterPair = this->getParameterByName(*parametersToGetIterator); if(parameterPair.second) { parameterPair.first->updateValue(this->modbus, this->portStartAddress); } parametersToGetIterator++; } } void Port::printParameters() { std::vector>::iterator parametersIterator = this->parameters.begin(); if(parametersIterator != this->parameters.end()) { std::cout << "|"; } while(parametersIterator != this->parameters.end()) { std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |"; parametersIterator++; } } void Port::printParameters(std::vector ¶metersToGet) { std::vector::iterator parametersToGetIterator = parametersToGet.begin(); if(parametersToGetIterator != parametersToGet.end()) { std::cout << "|"; } while(parametersToGetIterator != parametersToGet.end()) { std::pair, bool> parameterPair; parameterPair = this->getParameterByName(*parametersToGetIterator); if(parameterPair.second) { std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |"; } } }