FactGroup.cc 3.22 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 40 41 42 43
{
    _setupTimer();
    _nameToFactMetaDataMap = FactMetaData::createMapFromJsonFile(metaDataFile, this);
}

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

void FactGroup::_loadFromJsonArray(const QJsonArray jsonArray)
{
    _nameToFactMetaDataMap = FactMetaData::createMapFromJsonArray(jsonArray, this);
}

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

Fact* FactGroup::getFact(const QString& name)
{
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    Fact* fact = NULL;

    if (name.contains(".")) {
        QStringList parts = name.split(".");
        if (parts.count() != 2) {
            qWarning() << "Only single level of hierarchy supported";
            return NULL;
        }

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

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

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

79
    return fact;
Don Gagne's avatar
Don Gagne committed
80 81 82 83
}

FactGroup* FactGroup::getFactGroup(const QString& name)
{
84 85
    FactGroup* factGroup = NULL;

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

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

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

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

    _nameToFactGroupMap[name] = factGroup;
}

void FactGroup::_updateAllValues(void)
{
    foreach(Fact* fact, _nameToFactMap) {
        fact->sendDeferredValueChangedSignal();
    }
}