FactGroup.cc 4.68 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15 16 17 18

#include "FactGroup.h"
#include "JsonHelper.h"

#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonArray>
#include <QDebug>
#include <QFile>
19
#include <QQmlEngine>
Don Gagne's avatar
Don Gagne committed
20 21 22

QGC_LOGGING_CATEGORY(FactGroupLog, "FactGroupLog")

23
FactGroup::FactGroup(int updateRateMsecs, const QString& metaDataFile, QObject* parent, bool ignoreCamelCase)
Don Gagne's avatar
Don Gagne committed
24 25
    : QObject(parent)
    , _updateRateMSecs(updateRateMsecs)
26
    , _ignoreCamelCase(ignoreCamelCase)
27 28 29 30 31
{
    _setupTimer();
    _nameToFactMetaDataMap = FactMetaData::createMapFromJsonFile(metaDataFile, this);
}

32
FactGroup::FactGroup(int updateRateMsecs, QObject* parent, bool ignoreCamelCase)
33 34
    : QObject(parent)
    , _updateRateMSecs(updateRateMsecs)
35
    , _ignoreCamelCase(ignoreCamelCase)
36 37 38 39 40 41
{
    _setupTimer();
}

void FactGroup::_loadFromJsonArray(const QJsonArray jsonArray)
{
42 43
    QMap<QString, QString> defineMap;
    _nameToFactMetaDataMap = FactMetaData::createMapFromJsonArray(jsonArray, defineMap, this);
44 45 46
}

void FactGroup::_setupTimer()
Don Gagne's avatar
Don Gagne committed
47 48 49 50
{
    if (_updateRateMSecs > 0) {
        connect(&_updateTimer, &QTimer::timeout, this, &FactGroup::_updateAllValues);
        _updateTimer.setSingleShot(false);
51 52
        _updateTimer.setInterval(_updateRateMSecs);
        _updateTimer.start();
Don Gagne's avatar
Don Gagne committed
53 54 55
    }
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
bool FactGroup::factExists(const QString& name)
{
    if (name.contains(".")) {
        QStringList parts = name.split(".");
        if (parts.count() != 2) {
            qWarning() << "Only single level of hierarchy supported";
            return false;
        }

        FactGroup * factGroup = getFactGroup(parts[0]);
        if (!factGroup) {
            qWarning() << "Unknown FactGroup" << parts[0];
            return false;
        }

        return factGroup->factExists(parts[1]);
    }

    QString camelCaseName = _ignoreCamelCase ? name : _camelCase(name);

    return _nameToFactMap.contains(camelCaseName);
}

Don Gagne's avatar
Don Gagne committed
79 80
Fact* FactGroup::getFact(const QString& name)
{
81 82 83 84
    if (name.contains(".")) {
        QStringList parts = name.split(".");
        if (parts.count() != 2) {
            qWarning() << "Only single level of hierarchy supported";
85
            return nullptr;
86 87 88 89 90
        }

        FactGroup * factGroup = getFactGroup(parts[0]);
        if (!factGroup) {
            qWarning() << "Unknown FactGroup" << parts[0];
91
            return nullptr;
92 93 94 95 96
        }

        return factGroup->getFact(parts[1]);
    }

97
    Fact*   fact =          nullptr;
98
    QString camelCaseName = _ignoreCamelCase ? name : _camelCase(name);
99 100 101

    if (_nameToFactMap.contains(camelCaseName)) {
        fact = _nameToFactMap[camelCaseName];
102
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
103
    } else {
104
        qWarning() << "Unknown Fact" << camelCaseName;
Don Gagne's avatar
Don Gagne committed
105 106
    }

107
    return fact;
Don Gagne's avatar
Don Gagne committed
108 109 110 111
}

FactGroup* FactGroup::getFactGroup(const QString& name)
{
112
    FactGroup*  factGroup = nullptr;
113
    QString     camelCaseName = _ignoreCamelCase ? name : _camelCase(name);
114

115 116
    if (_nameToFactGroupMap.contains(camelCaseName)) {
        factGroup = _nameToFactGroupMap[camelCaseName];
117
        QQmlEngine::setObjectOwnership(factGroup, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
118
    } else {
119
        qWarning() << "Unknown FactGroup" << camelCaseName;
Don Gagne's avatar
Don Gagne committed
120 121
    }

122
    return factGroup;
Don Gagne's avatar
Don Gagne committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

void FactGroup::_addFact(Fact* fact, const QString& name)
{
    if (_nameToFactMap.contains(name)) {
        qWarning() << "Duplicate Fact" << name;
        return;
    }

    fact->setSendValueChangedSignals(_updateRateMSecs == 0);
    if (_nameToFactMetaDataMap.contains(name)) {
        fact->setMetaData(_nameToFactMetaDataMap[name]);
    }
    _nameToFactMap[name] = fact;
137
    _factNames.append(name);
Don Gagne's avatar
Don Gagne committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

void FactGroup::_addFactGroup(FactGroup* factGroup, const QString& name)
{
    if (_nameToFactGroupMap.contains(name)) {
        qWarning() << "Duplicate FactGroup" << name;
        return;
    }

    _nameToFactGroupMap[name] = factGroup;
}

void FactGroup::_updateAllValues(void)
{
152
    for(Fact* fact: _nameToFactMap) {
Don Gagne's avatar
Don Gagne committed
153 154 155
        fact->sendDeferredValueChangedSignal();
    }
}
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

void FactGroup::setLiveUpdates(bool liveUpdates)
{
    if (_updateTimer.interval() == 0) {
        return;
    }

    if (liveUpdates) {
        _updateTimer.stop();
    } else {
        _updateTimer.start();
    }
    for(Fact* fact: _nameToFactMap) {
        fact->setSendValueChangedSignals(liveUpdates);
    }
}
172 173 174 175 176 177


QString FactGroup::_camelCase(const QString& text)
{
    return text[0].toLower() + text.right(text.length() - 1);
}