/*************************************************************************** BionxControl © 2025 -2026 christoph holzheuer christoph.holzheuer@gmail.com Using: mhs_can_drv.c © 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany Klaus Demlehner, klaus@mhs-elektronik.de @see www.mhs-elektronik.de Based on Bionx data type descriptions from: BigXionFlasher USB V 0.2.4 rev. 97 © 2011-2013 by Thomas Koenig @see www.bigxionflasher.org Bionx Bike Info © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) @see www.ts-soft.de This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. @see https://github.com/bikemike/bionx-bikeinfo ***************************************************************************/ #include #include #include #include #include #include using namespace Qt::StringLiterals; BCXmlLoader::BCXmlLoader(QObject *parent) : QObject(parent) { //qRegisterMetaType("BCValue*"); //qRegisterMetaType(); } void BCXmlLoader::loadXmlBikeData( const QString& fileName ) { /* auto printAttrs = [](const QXmlStreamReader& xml) { QStringList parts; for (const auto &attr : xml.attributes()) { parts << attr.name().toString() + "=\"" + attr.value().toString() + "\""; } qDebug().noquote() << parts.join(" "); }; */ QMetaEnum bcDeviceEnum{QMetaEnum::fromType()}; QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) throw BCException( "Fehler", "Datei konnte nicht geöffnet werden."); _xml.setDevice(&file); if (_xml.readNextStartElement()) { if (_xml.name() != "Bike"_L1 ) throw BCException( "Fehler", "Falsches Datenformat."); } // ?? Q_ASSERT(_xml.isStartElement() && _xml.name() == "Bike"_L1); while (!_xml.atEnd() && !_xml.hasError()) { QXmlStreamReader::TokenType token = _xml.readNext(); if (token == QXmlStreamReader::StartElement) { QString deviceType = _xml.attributes().value("Type"_L1).toString(); // Wir wollen die Device-ID aus dem XML Tag ermitteln if( deviceType.isEmpty() ) continue; QByteArray byteArray = deviceType.toUtf8(); const char* deviceKey = byteArray.constData(); bool ok=false; auto optDeviceID = bcDeviceEnum.keyToValue(deviceKey,&ok); //_currentDeviceID = BCDevice::ID( deviceID.value_or( BCDevice::ID::Invalid ) ); //if( optDeviceID.has_value()) if(!ok) throw BCException( "Fehler", QString("Devicetype %1 existiert nicht.").arg(deviceType) ); //BCDevice::ID currentDeviceID = BCDevice::ID( optDeviceID.value() ); BCDevice::ID currentDeviceID = BCDevice::ID( optDeviceID ); loadXmlBikeDeviceData(currentDeviceID); } // if startElement } // end while } /** * @brief Lädt deie Daten des BionX eBikes * @param deviceID */ void BCXmlLoader::loadXmlBikeDeviceData(BCDevice::ID deviceID) { auto printAttrs = [](const QXmlStreamReader& xml) { QStringList parts; for (const auto &attr : xml.attributes()) parts << attr.name().toString() + "=\"" + attr.value().toString() + "\""; qDebug().noquote() << parts.join(" "); }; printAttrs (_xml); Q_ASSERT(_xml.isStartElement() && _xml.name() == BCTags::Device ); // temporäre Wertliste für neues Device BCValueList currentValues; while( _xml.readNextStartElement() ) { if( _xml.attributes().hasAttribute(BCTags::ID) ) { // füllen des Parameter packs BCValueParams params { .ID = _xml.attributes().value(BCTags::ID).toString(), .Label = _xml.attributes().value(BCTags::Label).toString(), .UnitLabel = _xml.attributes().value(BCTags::UnitLabel).toString(), .Factor = _xml.attributes().value(BCTags::Factor).toString(), .Min = _xml.attributes().value(BCTags::Min).toString(), .Max = _xml.attributes().value(BCTags::Max).toString(), .IsWord = _xml.attributes().value(BCTags::IsWord).toString(), .ReadOnly = _xml.attributes().value(BCTags::ReadOnly).toString(), .ValueType = _xml.attributes().value(BCTags::ValueType).toString(), }; // nur gültige Werte sind vorhanden und können gespeichert werden std::optional newValue = makeValue( deviceID, params ); if(newValue) { // wir merken uns gleich den index in der Werteliste (*newValue)->setIndexRow( currentValues.size() ); currentValues.push_back( *newValue ); } } // weiter zum nächsten Element _xml.skipCurrentElement(); } // Wenn dieses Device fertig geladen wurde, soll das MainWindow es abholen if( !currentValues.isEmpty() ) emit valueListReady( deviceID, currentValues ); } /** * @brief Erzeugt einen BCValue für die DeviceID aus dem gegebenen parameter pack */ std::optional BCXmlLoader::makeValue( BCDevice::ID deviceID, const BCValueParams& params ) { static QHash s_valueTypes { { "Plain", BCValue::ValueType::Plain }, { "Bool", BCValue::ValueType::Bool }, { "Number", BCValue::ValueType::Number }, { "Float", BCValue::ValueType::Float } }; auto setIfExists = [&]( T& target, QStringView source ) { if( !source.isEmpty() ) { bool ok; double testVal = source.toDouble(&ok); if (ok) target = testVal; } }; // Wir brauchen: // - einen gültige ID String um die enum ID herauszufinden. // - einen gültige UnitType String um den ValueType herauszufinden. // geht nicht auf qt6.8 von debian trixie //std::optional IDVal = s_bcValueEnum.keyToValue64( params.ID.toLatin1().constData() ); bool ok; static QMetaEnum s_bcValueEnum{QMetaEnum::fromType()}; QByteArray byteArray = params.ID.toUtf8(); int IDVal = s_bcValueEnum.keyToValue( params.ID.toUtf8().constData(), &ok ); //if( IDVal.has_value() ) if( !ok ) throw BCException( "Fehler", QString("Devicetype %1 existiert nicht.").arg(params.ID) ); BCValuePtr newValuePtr = std::make_shared( deviceID, static_cast(IDVal) ); BCValue& newValue = *newValuePtr.get(); // ValueType if( !s_valueTypes.contains( params.ValueType ) ) throw BCException( "Fehler", QString("ValueType %1 existiert nicht.").arg(params.ValueType) ); newValue._valueType = s_valueTypes[params.ValueType]; newValue._label = params.Label; newValue._unitLabel = params.UnitLabel; setIfExists( newValue._factor, params.Factor ); setIfExists( newValue._optMin, params.Min ); setIfExists( newValue._optMax, params.Max ); if( params.IsWord == "true") newValue._valueFlags.setFlag( BCValue::Flag::IsWord, true ); if( params.ReadOnly == "true") newValue._valueFlags.setFlag( BCValue::Flag::ReadOnly, true ); //newValue.dumpValue(); return std::optional( newValuePtr ); }