FactGroup.cc 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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 23 24 25

QGC_LOGGING_CATEGORY(FactGroupLog, "FactGroupLog")

FactGroup::FactGroup(int updateRateMsecs, const QString& metaDataFile, QObject* parent)
    : QObject(parent)
    , _updateRateMSecs(updateRateMsecs)
26 27 28 29 30 31 32 33 34 35 36 37 38 39
{
    _setupTimer();
    _nameToFactMetaDataMap = FactMetaData::createMapFromJsonFile(metaDataFile, this);
}

FactGroup::FactGroup(int updateRateMsecs, QObject* parent)
    : QObject(parent)
    , _updateRateMSecs(updateRateMsecs)
{
    _setupTimer();
}

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

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

Fact* FactGroup::getFact(const QString& name)
{
56
    Fact* fact = nullptr;
57 58 59 60 61

    if (name.contains(".")) {
        QStringList parts = name.split(".");
        if (parts.count() != 2) {
            qWarning() << "Only single level of hierarchy supported";
62
            return nullptr;
63 64 65 66 67
        }

        FactGroup * factGroup = getFactGroup(parts[0]);
        if (!factGroup) {
            qWarning() << "Unknown FactGroup" << parts[0];
68
            return nullptr;
69 70 71 72 73
        }

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

Don Gagne's avatar
Don Gagne committed
74
    if (_nameToFactMap.contains(name)) {
75 76
        fact = _nameToFactMap[name];
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
77 78 79 80
    } else {
        qWarning() << "Unknown Fact" << name;
    }

81
    return fact;
Don Gagne's avatar
Don Gagne committed
82 83 84 85
}

FactGroup* FactGroup::getFactGroup(const QString& name)
{
86
    FactGroup* factGroup = nullptr;
87

Don Gagne's avatar
Don Gagne committed
88
    if (_nameToFactGroupMap.contains(name)) {
89 90
        factGroup = _nameToFactGroupMap[name];
        QQmlEngine::setObjectOwnership(factGroup, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
91 92 93 94
    } else {
        qWarning() << "Unknown FactGroup" << name;
    }

95
    return factGroup;
Don Gagne's avatar
Don Gagne committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

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;
110
    _factNames.append(name);
Don Gagne's avatar
Don Gagne committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

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

    _nameToFactGroupMap[name] = factGroup;
}

void FactGroup::_updateAllValues(void)
{
125
    for(Fact* fact: _nameToFactMap) {
Don Gagne's avatar
Don Gagne committed
126 127 128
        fact->sendDeferredValueChangedSignal();
    }
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

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

    if (liveUpdates) {
        _updateTimer.stop();
    } else {
        _updateTimer.start();
    }
    for(Fact* fact: _nameToFactMap) {
        fact->setSendValueChangedSignals(liveUpdates);
    }
}