Added BCValueType

This commit is contained in:
2025-12-17 17:50:24 +01:00
parent 09ff2f00bb
commit 35112ba334
5 changed files with 123 additions and 67 deletions

3
bc.h
View File

@@ -716,9 +716,6 @@ public:
Reg_Sensor_Rev_Sub = 0x83 // Software Subversion
};
//const
// SECONDS_PER_DAY = SecsPerDay; //60 * 60 * 24;
Q_ENUM(ID)
}; // struct BionxID

View File

@@ -47,41 +47,3 @@ uint8_t BCValue::getLongValue()
return 0;
}
BCValue BCValue::makeValue( BCDevice::ID deviceID, const BCValueParams& params )
{
auto setIfExists = [&]( QStringView source, optDouble& target )
{
if( !source.isEmpty() )
{
bool ok;
double testVal = source.toDouble(&ok);
if (ok)
target = testVal;
}
};
static QMetaEnum s_bcValueEnum{QMetaEnum::fromType<BC::ID>()};
/*
Wir brauchen:
- eine gültige ID
*/
BCValue newValue{};
auto IDVal = s_bcValueEnum.keyToValue64( params.ID.toLatin1().constData() );
if( IDVal.has_value() )
{
newValue = BCValue( deviceID, BC::ID( IDVal.value() ) );
setIfExists( params.Factor, newValue.factor );
setIfExists( params.Min, newValue.min );
setIfExists( params.Max, newValue.max );
newValue.defaultValue.setValue( params.Label );
newValue.value.setValue( params.Current );
newValue.label = params.Label;
}
return newValue;
}

101
bcvalue.h
View File

@@ -28,39 +28,81 @@
***************************************************************************/
#ifndef BCRawValue_H
#define BCRawValue_H
#ifndef BCVALUE_H
#define BCVALUE_H
#include <QObject>
#include <QString>
#include <QList>
#include <QVariant>
#include <bc.h>
/*
Das ist natürlich annalog zu den ItemTypes:
- ein Value hat einen ValueType, der bestimmt folgendes:
- long or short
- unit (mm, km/h, odo ... )
-
*/
using optDouble = std::optional<double>;
struct BCValueParams
struct BCValueType
{
QString ID;
QString Label;
QString Default;
QString Current;
QString Enabled;
QString UnitType;
QString Min;
QString Max;
QString Factor;
Q_GADGET
public:
enum class TypeID : uint8_t
{
Invalid = 0x0,
};
Q_ENUM(TypeID)
struct BCUnit
enum class UnitID : uint8_t
{
enum class ID
{
Invalid = 0x0,
Text,
Number,
Float,
Percent,
KWh,
Km,
Mm,
Sec,
SOC,
Date
};
Q_ENUM(UnitID)
static QString getUnitName( UnitID unitID)
{
static QMap<UnitID,QString> s_unitNames
{
{UnitID::Invalid, "" },
{UnitID::Text, ""},
{UnitID::Number, ""},
{UnitID::Float, "" },
{UnitID::Percent, "%"},
{UnitID::KWh, "kWh" },
{UnitID::Km, "km"},
{UnitID::Mm, "mm"},
{UnitID::Sec, "s"},
{UnitID::SOC, "%"},
{UnitID::Date, ""}
};
return s_unitNames[unitID];
}
UnitID ID;
optDouble min;
optDouble max;
optDouble factor;
};
class BCValue
@@ -79,19 +121,30 @@ public:
BCDevice::ID deviceID{BCDevice::ID::Invalid};
BC::ID targetID{BC::ID::Invalid};
optDouble min;
optDouble max;
optDouble factor;
//BCValueType::ID typeID{BCValueType::ID::Invalid};
BCValueType::TypeID firz;
QVariant defaultValue;
QVariant value;
static BCValue makeValue(BCDevice::ID deviceID, const BCValueParams& params );
};
// Damit QVariant dieses Struct transportieren kann:
Q_DECLARE_METATYPE(BCValue)
struct BCValueParams
{
QString ID;
QString Label;
QString Default;
QString Current;
QString Enabled;
QString UnitType;
QString Min;
QString Max;
QString Factor;
};
// abbreviations:
// SOC = State Of Charge
// LMD = Last Measured Discharge
@@ -111,4 +164,4 @@ constexpr auto to_u(E e) noexcept {
using BCValueList = QVector<BCValue>;
#endif // BCRawValue_H
#endif // BCVALUE_H

View File

@@ -181,7 +181,7 @@ void BCValueManager::readDevice( BCValueList& parsedValues )
//BCValue newValue = BCValue::makeValue( _currentDeviceID, params );
//if(newValue)
// parsedValues.push_back( newValue );
parsedValues.push_back( BCValue::makeValue( _currentDeviceID, params ) );
parsedValues.push_back( makeValue( _currentDeviceID, params ) );
}
//printAttrs (_xml);
@@ -229,3 +229,45 @@ void BCValueManager::saveXml()
}
*/
}
BCValue BCValueManager::makeValue( BCDevice::ID deviceID, const BCValueParams& params )
{
auto setIfExists = [&]( QStringView source, optDouble& target )
{
if( !source.isEmpty() )
{
bool ok;
double testVal = source.toDouble(&ok);
if (ok)
target = testVal;
}
};
static QMetaEnum s_bcValueEnum{QMetaEnum::fromType<BC::ID>()};
/*
Wir brauchen:
- eine gültige ID
*/
BCValue newValue{};
auto IDVal = s_bcValueEnum.keyToValue64( params.ID.toLatin1().constData() );
if( IDVal.has_value() )
{
newValue = BCValue( deviceID, BC::ID( IDVal.value() ) );
/*
setIfExists( params.Factor, newValue.factor );
setIfExists( params.Min, newValue.min );
setIfExists( params.Max, newValue.max );
*/
newValue.defaultValue.setValue( params.Label );
newValue.value.setValue( params.Current );
newValue.label = params.Label;
}
return newValue;
}

View File

@@ -52,6 +52,8 @@ public:
std::optional<BCValueModel*> getModel(const QString& key );
BCValue makeValue(BCDevice::ID deviceID, const BCValueParams& params );
protected:
void readDevice( BCValueList& parsedValues );