Added ValueModel, first run.

This commit is contained in:
2025-12-16 22:42:35 +01:00
parent afb5828a65
commit 4c5e42fcfd
8 changed files with 110 additions and 76 deletions

View File

@@ -50,15 +50,19 @@ BCValueManager::BCValueManager()
}
BCValueManager::~BCValueManager()
{
// nothing to do here for now,
// our models are autokilled.
}
std::optional<BCValueModel*> BCValueManager::getModel(const QString& key )
{
if( _valueModels.contains( key) )
return _valueModels[key];
return std::nullopt;
}
void BCValueManager::loadXml()
{
auto printAttrs = [](const QXmlStreamReader& xml)
@@ -94,19 +98,30 @@ void BCValueManager::loadXml()
// ??
Q_ASSERT(_xml.isStartElement() && _xml.name() == "Bike"_L1);
while (!_xml.atEnd() && !_xml.hasError())
{
QXmlStreamReader::TokenType token = _xml.readNext();
if (token == QXmlStreamReader::StartElement)
{
qDebug() << " --- Device: " << _xml.name() << ": " << _xml.attributes().value("Type"_L1);
QString deviceType = _xml.attributes().value("Type"_L1).toString();
qDebug() << " --- Device: " << _xml.name() << ": " << deviceType;
printAttrs (_xml);
const char* deviceKey = _xml.attributes().value("Type"_L1).toLatin1().constData();
auto deviceID = _bcDeviceEnum.keyToValue64(deviceKey);
//_currentDeviceID = BCDevice::ID( deviceID.value_or( BCDevice::ID::Invalid ) );
_currentDeviceID = deviceID.has_value() ? BCDevice::ID( deviceID.value() ) : BCDevice::ID::Invalid;
readDevice();
if(deviceID.has_value())
{
BCValueList parsedValues;
readDevice( parsedValues );
if( parsedValues.count() )
{
BCValueModel* valueModel = new BCValueModel( this );
valueModel->setValueList(parsedValues);
_valueModels.insert( deviceType, valueModel );
}
}
}
}
@@ -125,7 +140,7 @@ void BCValueManager::loadXml()
}
void BCValueManager::readDevice()
void BCValueManager::readDevice( BCValueList& parsedValues )
{
auto printAttrs = [](const QXmlStreamReader& xml)
{
@@ -136,66 +151,42 @@ void BCValueManager::readDevice()
qDebug().noquote() << parts.join(" ");
};
printAttrs (_xml);
printAttrs (_xml);
Q_ASSERT(_xml.isStartElement() && _xml.name() == "Device"_L1);
qDebug() << " ---------------";
qDebug() << " ---------------";
BCValueList parsedValues;
//while (!_xml.atEnd() && !_xml.hasError())
while( _xml.readNextStartElement() )
{
if( _xml.attributes().hasAttribute(BCTags::ID) )
{
makeValue(parsedValues);
//qDebug() << " --- found: " << _xml.name() << " : " << _xml.attributes().value(BCTags::ID);
BCValueParams params
{
.ID = _xml.attributes().value(BCTags::ID).toString(),
.Default = _xml.attributes().value(BCTags::Default).toString(),
.Current = _xml.attributes().value(BCTags::Current).toString(),
.Enabled = _xml.attributes().value(BCTags::Enabled).toString(),
.UnitType = _xml.attributes().value(BCTags::UnitType).toString(),
.Min = _xml.attributes().value(BCTags::Min).toString(),
.Max = _xml.attributes().value(BCTags::Max).toString(),
.Factor = _xml.attributes().value(BCTags::Factor).toString()
};
BCValue* newValue = BCValue::makeValue( _currentDeviceID, params );
if(newValue)
parsedValues.push_back( BCValue::makeValue( _currentDeviceID, params ) );
}
//printAttrs (_xml);
_xml.skipCurrentElement();
/*
if (token == QXmlStreamReader::StartElement )
{
qDebug() << " --- moo: " << _xml.name();
//Q_ASSERT(_xml.name() == "Value"_L1);
//makeValue();
//d.name = xml.attributes().value(u"name")
}
*/
}
//qDebug()xml.attributes().value(u"name").toString();
/*
Device d;
d.name = xml.attributes().value(u"name").toString();
d.ip = xml.attributes().value(u"ip").toString();
parsedValues.append(d);
*/
}
void BCValueManager::makeValue(BCValueList& parsedValues)
{
const char* IDKey = _xml.attributes().value(BCTags::ID).toLatin1().constData();
qDebug() << " --- found: " << _xml.name() << " : " << IDKey;
auto IDVal = _bcValueEnum.keyToValue64( IDKey );
if( IDVal.has_value() )
{
BCValueParams params
{
.DeviceID = _currentDeviceID,
.ID = BC::ID( IDVal.value() ),
.Default = _xml.attributes().value(BCTags::Default).toString(),
.Current = _xml.attributes().value(BCTags::Current).toString(),
.UnitType = _xml.attributes().value(BCTags::UnitType).toString(),
.Min = _xml.attributes().value(BCTags::Min).toString(),
.Max = _xml.attributes().value(BCTags::Max).toString(),
.Factor = _xml.attributes().value(BCTags::Factor).toString()
};
parsedValues.push_back( BCValue::makeValue( params ) );
}
}
// --- NEU: Speichern mit QXmlStreamWriter ---
void BCValueManager::saveXml()
{