hoymilesClient/src/hoymiles/portParameters/portParametersGeneric.cpp

83 lines
3 KiB
C++
Raw Normal View History

#include <cmath>
#include <memory>
#include <mutex>
#include <sstream>
#include <iomanip>
#include "modbus.h"
#include "portParametersGeneric.h"
PortParameter::PortParameter(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) {
this->name = name;
2024-03-20 20:17:15 +01:00
this->shortName = shortName;
this->unit = unit;
this->r = r;
this->w = w;
this->parameterAddressOffset = parameterAddressOffset;
this->registerSize = registerSize;
}
PortParameter::~PortParameter() {}
2024-04-06 00:32:49 +02:00
void PortParameter::getValueFromRegisters(uint16_t *readArray, int portOffset) {}
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
}
2024-04-06 00:32:49 +02:00
PortParameter& PortParameter::writeValue(uint16_t value, class modbus& modbus, int portStartAddress) {
2024-04-06 16:29:02 +02:00
int writeCount;
writeCount = modbus.modbus_write_register(this->parameterAddressOffset + portStartAddress, value);
2024-04-06 00:32:49 +02:00
return *this;
}
2024-03-19 16:41:58 +01:00
std::string PortParameter::getOutputValue() {
return "yeet";
}
PortParameterFloat::PortParameterFloat(std::string name, std::string shortName, std::string unit, bool r, bool w, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) {
this->decimalPlaces = decimalPlaces;
this->valueType = Float;
this->value.f = 0;
}
2024-04-06 00:32:49 +02:00
void PortParameterFloat::getValueFromRegisters(uint16_t *registers, int addressOffset) {
std::string readValueString{""};
for(int i{0}; i<this->registerSize; i++) {
std::stringstream readValueStringStream;
readValueStringStream << (int) registers[addressOffset + this->parameterAddressOffset + i];
readValueString.append(readValueStringStream.str().c_str());
}
this->value.f = std::stoll(readValueString) / std::pow(10, this->decimalPlaces);
}
std::string PortParameterFloat::getOutputValue() {
std::stringstream valueStringStream;
valueStringStream << std::fixed << std::setprecision(this->decimalPlaces) << this->value.f;
return valueStringStream.str().append(this->unit.c_str());
}
PortParameterInt::PortParameterInt(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) {
this->valueType = Int;
this->value.i = 0;
}
2024-04-06 00:32:49 +02:00
void PortParameterInt::getValueFromRegisters(uint16_t *registers, int addressOffset) {
std::string readValueString{""};
for (int i{0}; i < this->registerSize; i++) {
std::stringstream readValueStringStream;
readValueStringStream << (int) registers[addressOffset + this->parameterAddressOffset + i];
readValueString.append(readValueStringStream.str().c_str());
}
2024-03-20 16:39:25 +01:00
this->value.i = std::stoll(readValueString);
}
std::string PortParameterInt::getOutputValue() {
return std::to_string(this->value.i).append(this->unit.c_str());
}