AirMapRulesetsManager.cc 11.9 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.
 *
 ****************************************************************************/

10
#include "AirMapRulesetsManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
Gus Grubba's avatar
Gus Grubba committed
12
#include <QSettings>
13

Gus Grubba's avatar
Gus Grubba committed
14 15
using namespace airmap;

Gus Grubba's avatar
Gus Grubba committed
16 17
static const char* kAirMapFeatureGroup = "AirMapFeatureGroup";

18 19 20 21 22 23 24 25 26 27 28
//-----------------------------------------------------------------------------
AirMapRuleFeature::AirMapRuleFeature(QObject* parent)
    : AirspaceRuleFeature(parent)
{
}

//-----------------------------------------------------------------------------
AirMapRuleFeature::AirMapRuleFeature(airmap::RuleSet::Feature feature, QObject* parent)
    : AirspaceRuleFeature(parent)
    , _feature(feature)
{
Gus Grubba's avatar
Gus Grubba committed
29 30 31 32 33
    //-- Restore persisted value (if it exists)
    QSettings settings;
    settings.beginGroup(kAirMapFeatureGroup);
    _value = settings.value(name());
    settings.endGroup();
34 35 36 37 38 39
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Type
AirMapRuleFeature::type()
{
Gus Grubba's avatar
Gus Grubba committed
40 41 42 43 44 45 46 47 48 49
    switch(_feature.type) {
    case RuleSet::Feature::Type::boolean:
        return AirspaceRuleFeature::Boolean;
    case RuleSet::Feature::Type::floating_point:
        return AirspaceRuleFeature::Float;
    case RuleSet::Feature::Type::string:
        return AirspaceRuleFeature::String;
    default:
        break;
    }
50 51 52 53 54 55 56
    return AirspaceRuleFeature::Unknown;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Unit
AirMapRuleFeature::unit()
{
Gus Grubba's avatar
Gus Grubba committed
57 58 59 60 61 62 63 64 65 66
    switch(_feature.unit) {
    case RuleSet::Feature::Unit::kilograms:
        return AirspaceRuleFeature::Kilogram;
    case RuleSet::Feature::Unit::meters:
        return AirspaceRuleFeature::Meters;
    case RuleSet::Feature::Unit::meters_per_sec:
        return AirspaceRuleFeature::MetersPerSecond;
    default:
        break;
    }
67 68 69 70 71 72 73
    return AirspaceRuleFeature::UnknownUnit;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Measurement
AirMapRuleFeature::measurement()
{
Gus Grubba's avatar
Gus Grubba committed
74 75 76 77 78 79 80 81 82 83
    switch(_feature.measurement) {
    case RuleSet::Feature::Measurement::speed:
        return AirspaceRuleFeature::Speed;
    case RuleSet::Feature::Measurement::weight:
        return AirspaceRuleFeature::Weight;
    case RuleSet::Feature::Measurement::distance:
        return AirspaceRuleFeature::Distance;
    default:
        break;
    }
84 85 86
    return AirspaceRuleFeature::UnknownMeasurement;
}

Gus Grubba's avatar
Gus Grubba committed
87 88 89 90 91 92 93 94 95 96 97 98 99
//-----------------------------------------------------------------------------
void
AirMapRuleFeature::setValue(const QVariant val)
{
    _value = val;
    //-- Make value persistent
    QSettings settings;
    settings.beginGroup(kAirMapFeatureGroup);
    settings.setValue(name(), _value);
    settings.endGroup();
    emit valueChanged();
}

Gus Grubba's avatar
Gus Grubba committed
100 101 102
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(QObject* parent)
    : AirspaceRule(parent)
103 104 105
{
}

106 107 108 109 110 111 112
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(const airmap::RuleSet::Rule& rule, QObject* parent)
    : AirspaceRule(parent)
    , _rule(rule)
{
}

Gus Grubba's avatar
Gus Grubba committed
113 114 115 116 117 118
//-----------------------------------------------------------------------------
AirMapRule::~AirMapRule()
{
    _features.deleteListAndContents();
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//-----------------------------------------------------------------------------
AirspaceRule::Status
AirMapRule::status()
{
    switch(_rule.status) {
    case RuleSet::Rule::Status::conflicting:
        return AirspaceRule::Conflicting;
    case RuleSet::Rule::Status::not_conflicting:
        return AirspaceRule::NotConflicting;
    case RuleSet::Rule::Status::missing_info:
        return AirspaceRule::MissingInfo;
    case RuleSet::Rule::Status::unknown:
    default:
        return AirspaceRule::Unknown;
    }
}

//-----------------------------------------------------------------------------
AirMapRuleSet::AirMapRuleSet(QObject* parent)
    : AirspaceRuleSet(parent)
Gus Grubba's avatar
Gus Grubba committed
139
    , _isDefault(false)
Gus Grubba's avatar
Gus Grubba committed
140
    , _selected(false)
141
    , _selectionType(AirspaceRuleSet::Optional)
Gus Grubba's avatar
Gus Grubba committed
142 143 144
{
}

145 146 147 148 149 150
//-----------------------------------------------------------------------------
AirMapRuleSet::~AirMapRuleSet()
{
    _rules.deleteListAndContents();
}

Gus Grubba's avatar
Gus Grubba committed
151 152 153 154 155 156 157 158 159 160 161 162
//-----------------------------------------------------------------------------
void
AirMapRuleSet::setSelected(bool sel)
{
    if(_selectionType != AirspaceRuleSet::Required) {
        _selected = sel;
    } else {
        _selected = true;
    }
    emit selectedChanged();
}

163 164 165 166 167 168
//-----------------------------------------------------------------------------
AirMapRulesetsManager::AirMapRulesetsManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

169 170 171 172 173 174 175 176 177 178
//-----------------------------------------------------------------------------
static bool
rules_sort(QObject* a, QObject* b)
{
    AirMapRule* aa = qobject_cast<AirMapRule*>(a);
    AirMapRule* bb = qobject_cast<AirMapRule*>(b);
    if(!aa || !bb) return false;
    return (int)aa->order() > (int)bb->order();
}

179
//-----------------------------------------------------------------------------
180
void AirMapRulesetsManager::setROI(const QGCGeoBoundingCube& roi)
181 182 183 184 185 186
{
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
187
        qCWarning(AirMapManagerLog) << "AirMapRulesetsManager::updateROI: state not idle";
188 189 190
        return;
    }
    qCDebug(AirMapManagerLog) << "Setting ROI for Rulesets";
191
    _valid = false;
192
    _ruleSets.clearAndDeleteContents();
193 194
    _state = State::RetrieveItems;
    RuleSets::Search::Parameters params;
195 196 197 198 199 200 201 202 203
    //-- Geometry: Polygon
    Geometry::Polygon polygon;
    for (const auto& qcoord : roi.polygon2D()) {
        Geometry::Coordinate coord;
        coord.latitude  = qcoord.latitude();
        coord.longitude = qcoord.longitude();
        polygon.outer_ring.coordinates.push_back(coord);
    }
    params.geometry = Geometry(polygon);
204 205 206 207 208 209
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->rulesets().search(params,
            [this, isAlive](const RuleSets::Search::Result& result) {
        if (!isAlive.lock()) return;
        if (_state != State::RetrieveItems) return;
        if (result) {
210 211 212 213 214 215 216 217 218 219
            const std::vector<RuleSet> rulesets = result.value();
            qCDebug(AirMapManagerLog) << "Successful rulesets search. Items:" << rulesets.size();
            for (const auto& ruleset : rulesets) {
                AirMapRuleSet* pRuleSet = new AirMapRuleSet(this);
                connect(pRuleSet, &AirspaceRuleSet::selectedChanged, this, &AirMapRulesetsManager::_selectedChanged);
                pRuleSet->_id          = QString::fromStdString(ruleset.id);
                pRuleSet->_name        = QString::fromStdString(ruleset.name);
                pRuleSet->_shortName   = QString::fromStdString(ruleset.short_name);
                pRuleSet->_description = QString::fromStdString(ruleset.description);
                pRuleSet->_isDefault   = ruleset.is_default;
220
                //-- TODO: This should be persistent and if the new incoming set has the
221 222 223
                //   same, previosuly selected rulesets, it should "remember".
                if(pRuleSet->_isDefault) {
                    pRuleSet->_selected = true;
224 225 226
                }
                switch(ruleset.selection_type) {
                case RuleSet::SelectionType::pickone:
227
                    pRuleSet->_selectionType = AirspaceRuleSet::Pickone;
228 229
                    break;
                case RuleSet::SelectionType::required:
230 231
                    pRuleSet->_selectionType = AirspaceRuleSet::Required;
                    pRuleSet->_selected = true;
232 233 234
                    break;
                default:
                case RuleSet::SelectionType::optional:
235
                    pRuleSet->_selectionType = AirspaceRuleSet::Optional;
236 237
                    break;
                }
238 239 240
                //-- Iterate Rules
                for (const auto& rule : ruleset.rules) {
                    AirMapRule* pRule = new AirMapRule(rule, this);
Gus Grubba's avatar
Gus Grubba committed
241
                    //-- Iterate Rule Features
Gus Grubba's avatar
Gus Grubba committed
242 243 244 245
                    for (const auto& feature : rule.features) {
                        AirMapRuleFeature* pFeature = new AirMapRuleFeature(feature, this);
                        pRule->_features.append(pFeature);
                    }
246 247 248 249
                    pRuleSet->_rules.append(pRule);
                }
                //-- Sort rules by display order
                std::sort(pRuleSet->_rules.objectList()->begin(), pRuleSet->_rules.objectList()->end(), rules_sort);
250
                _ruleSets.append(pRuleSet);
Gus Grubba's avatar
Gus Grubba committed
251
                /*
Gus Grubba's avatar
Gus Grubba committed
252
                qCDebug(AirMapManagerLog) << "Adding ruleset" << pRuleSet->name();
253 254 255 256 257 258 259 260
                qDebug() << "------------------------------------------";
                qDebug() << "Jurisdiction:" << ruleset.jurisdiction.name.data() << (int)ruleset.jurisdiction.region;
                qDebug() << "Name:        " << ruleset.name.data();
                qDebug() << "Short Name:  " << ruleset.short_name.data();
                qDebug() << "Description: " << ruleset.description.data();
                qDebug() << "Is default:  " << ruleset.is_default;
                qDebug() << "Applicable to these airspace types:";
                for (const auto& airspaceType : ruleset.airspace_types) {
Gus Grubba's avatar
Gus Grubba committed
261
                    qDebug() << "  " << airspaceType.data();
262 263
                }
                qDebug() << "Rules:";
264
                for (const auto& rule : ruleset.rules) {
265
                    qDebug() << "    --------------------------------------";
Gus Grubba's avatar
Gus Grubba committed
266 267 268 269 270 271
                    qDebug() << "    short_text:   " << rule.short_text.data();
                    qDebug() << "    description:  " << rule.description.data();
                    qDebug() << "    display_order:" << rule.display_order;
                    qDebug() << "    status:       " << (int)rule.status;
                    qDebug() << "            ------------------------------";
                    qDebug() << "            Features:";
272
                    for (const auto& feature : rule.features) {
Gus Grubba's avatar
Gus Grubba committed
273 274 275 276 277 278
                        qDebug() << "            name:       " << feature.name.data();
                        qDebug() << "            description:" << feature.description.data();
                        qDebug() << "            status:     " << (int)feature.status;
                        qDebug() << "            type:       " << (int)feature.type;
                        qDebug() << "            measurement:" << (int)feature.measurement;
                        qDebug() << "            unit:       " << (int)feature.unit;
279 280
                    }
                }
Gus Grubba's avatar
Gus Grubba committed
281
                */
282
            }
283
            _valid = true;
284 285
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
286
            emit error("Failed to retrieve RuleSets", QString::fromStdString(result.error().message()), description);
287 288
        }
        _state = State::Idle;
289 290
        emit ruleSetsChanged();
        emit selectedRuleSetsChanged();
291 292
    });
}
293 294 295

//-----------------------------------------------------------------------------
QString
296
AirMapRulesetsManager::selectedRuleSets()
297
{
298
    QString selection;
299 300
    for(int i = 0; i < _ruleSets.count(); i++) {
        AirMapRuleSet* rule = qobject_cast<AirMapRuleSet*>(_ruleSets.get(i));
301 302
        if(rule && rule->selected()) {
            selection += rule->shortName() + ", ";
303 304
        }
    }
305 306 307 308 309
    int idx = selection.lastIndexOf(", ");
    if(idx >= 0) {
        selection = selection.left(idx);
    }
    return selection;
310 311 312 313
}

//-----------------------------------------------------------------------------
void
314
AirMapRulesetsManager::_selectedChanged()
315
{
316
    emit selectedRuleSetsChanged();
317 318
    //-- TODO: Do whatever it is you do to select a rule
}