AirMapRulesetsManager.cc 13.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"
12
#include "QGCApplication.h"
Gus Grubba's avatar
Gus Grubba committed
13
#include <QSettings>
14

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

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

19 20 21 22 23 24 25 26 27 28 29
//-----------------------------------------------------------------------------
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
30 31 32
    //-- Restore persisted value (if it exists)
    QSettings settings;
    settings.beginGroup(kAirMapFeatureGroup);
33 34
    switch(_feature.type) {
    case RuleSet::Feature::Type::boolean:
35
        //-- For boolean, we have 3 states: 0 - false, 1 - true and 2 - not set
36
        _value = settings.value(name(), 2);
37 38
        break;;
    case RuleSet::Feature::Type::floating_point:
39
        _value = settings.value(name(), NAN);
40 41 42 43 44 45 46
        break;;
    case RuleSet::Feature::Type::string:
        _value = settings.value(name(), QString());
        break;;
    default:
        break;
    }
Gus Grubba's avatar
Gus Grubba committed
47
    settings.endGroup();
48 49
}

50 51 52 53 54 55 56 57
//-----------------------------------------------------------------------------
QVariant
AirMapRuleFeature::value()
{
    //qCDebug(AirMapManagerLog) << "Value of" << name() << "==>" << _value << type();
    return _value;
}

58 59 60 61
//-----------------------------------------------------------------------------
AirspaceRuleFeature::Type
AirMapRuleFeature::type()
{
Gus Grubba's avatar
Gus Grubba committed
62 63 64 65 66 67 68 69 70 71
    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;
    }
72 73 74 75 76 77 78
    return AirspaceRuleFeature::Unknown;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Unit
AirMapRuleFeature::unit()
{
Gus Grubba's avatar
Gus Grubba committed
79 80 81 82 83 84 85 86 87 88
    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;
    }
89 90 91 92 93 94 95
    return AirspaceRuleFeature::UnknownUnit;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Measurement
AirMapRuleFeature::measurement()
{
Gus Grubba's avatar
Gus Grubba committed
96 97 98 99 100 101 102 103 104 105
    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;
    }
106 107 108
    return AirspaceRuleFeature::UnknownMeasurement;
}

Gus Grubba's avatar
Gus Grubba committed
109 110 111 112
//-----------------------------------------------------------------------------
void
AirMapRuleFeature::setValue(const QVariant val)
{
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    switch(_feature.type) {
    case RuleSet::Feature::Type::boolean:
        if(val.toInt() != 0 && val.toInt() != 1) {
            return;
        }
        break;
    case RuleSet::Feature::Type::floating_point:
        if(!std::isfinite(val.toDouble())) {
            return;
        }
        break;;
    case RuleSet::Feature::Type::string:
        if(val.toString().isEmpty()) {
            return;
        }
        break;;
    default:
        return;
    }
Gus Grubba's avatar
Gus Grubba committed
132 133 134 135 136 137 138 139 140
    _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
141 142 143
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(QObject* parent)
    : AirspaceRule(parent)
144 145 146
{
}

147 148 149 150 151 152 153
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(const airmap::RuleSet::Rule& rule, QObject* parent)
    : AirspaceRule(parent)
    , _rule(rule)
{
}

Gus Grubba's avatar
Gus Grubba committed
154 155 156 157 158 159
//-----------------------------------------------------------------------------
AirMapRule::~AirMapRule()
{
    _features.deleteListAndContents();
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
//-----------------------------------------------------------------------------
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
180
    , _isDefault(false)
Gus Grubba's avatar
Gus Grubba committed
181
    , _selected(false)
182
    , _selectionType(AirspaceRuleSet::Optional)
Gus Grubba's avatar
Gus Grubba committed
183 184 185
{
}

186 187 188 189 190 191
//-----------------------------------------------------------------------------
AirMapRuleSet::~AirMapRuleSet()
{
    _rules.deleteListAndContents();
}

Gus Grubba's avatar
Gus Grubba committed
192 193 194 195 196
//-----------------------------------------------------------------------------
void
AirMapRuleSet::setSelected(bool sel)
{
    if(_selectionType != AirspaceRuleSet::Required) {
197 198 199 200
        if(_selected != sel) {
            _selected = sel;
            emit selectedChanged();
        }
Gus Grubba's avatar
Gus Grubba committed
201
    } else {
202 203 204 205
        if(!_selected) {
            _selected = true;
            emit selectedChanged();
        }
Gus Grubba's avatar
Gus Grubba committed
206 207 208
    }
}

209 210 211 212 213 214
//-----------------------------------------------------------------------------
AirMapRulesetsManager::AirMapRulesetsManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

215 216 217 218 219 220 221
//-----------------------------------------------------------------------------
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;
222
    return static_cast<int>(aa->order()) > static_cast<int>(bb->order());
223 224
}

225
//-----------------------------------------------------------------------------
226
void AirMapRulesetsManager::setROI(const QGCGeoBoundingCube& roi, bool reset)
227
{
228
    Q_UNUSED(reset);
229 230 231 232 233
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
234
        qCWarning(AirMapManagerLog) << "AirMapRulesetsManager::updateROI: state not idle";
235 236
        return;
    }
237
    qCDebug(AirMapManagerLog) << "Rulesets Request (ROI Changed)";
238
    _valid = false;
239 240 241 242 243 244
    //-- Save current selection state
    QMap<QString, bool> selectionSet;
    for(int rs = 0; rs < ruleSets()->count(); rs++) {
        AirMapRuleSet* ruleSet = qobject_cast<AirMapRuleSet*>(ruleSets()->get(rs));
        selectionSet[ruleSet->id()] = ruleSet->selected();
    }
245
    _ruleSets.clearAndDeleteContents();
246 247
    _state = State::RetrieveItems;
    RuleSets::Search::Parameters params;
248 249
    //-- Geometry: Polygon
    Geometry::Polygon polygon;
250 251
    //-- Get ROI bounding box, clipping to max area of interest
    for (const auto& qcoord : roi.polygon2D(qgcApp()->toolbox()->airspaceManager()->maxAreaOfInterest())) {
252 253 254 255 256 257
        Geometry::Coordinate coord;
        coord.latitude  = qcoord.latitude();
        coord.longitude = qcoord.longitude();
        polygon.outer_ring.coordinates.push_back(coord);
    }
    params.geometry = Geometry(polygon);
258 259
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->rulesets().search(params,
260
            [this, isAlive, selectionSet](const RuleSets::Search::Result& result) {
261 262 263
        if (!isAlive.lock()) return;
        if (_state != State::RetrieveItems) return;
        if (result) {
264 265 266 267 268 269 270 271 272 273
            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;
274 275 276 277 278 279 280
                //-- Restore selection set (if any)
                if(selectionSet.contains(pRuleSet->id())) {
                    pRuleSet->_selected = selectionSet[pRuleSet->id()];
                } else {
                    if(pRuleSet->_isDefault) {
                        pRuleSet->_selected = true;
                    }
281 282 283
                }
                switch(ruleset.selection_type) {
                case RuleSet::SelectionType::pickone:
284
                    pRuleSet->_selectionType = AirspaceRuleSet::Pickone;
285 286
                    break;
                case RuleSet::SelectionType::required:
287 288
                    pRuleSet->_selectionType = AirspaceRuleSet::Required;
                    pRuleSet->_selected = true;
289 290
                    break;
                case RuleSet::SelectionType::optional:
291
                    pRuleSet->_selectionType = AirspaceRuleSet::Optional;
292 293
                    break;
                }
294 295 296
                //-- Iterate Rules
                for (const auto& rule : ruleset.rules) {
                    AirMapRule* pRule = new AirMapRule(rule, this);
Gus Grubba's avatar
Gus Grubba committed
297
                    //-- Iterate Rule Features
Gus Grubba's avatar
Gus Grubba committed
298 299 300 301
                    for (const auto& feature : rule.features) {
                        AirMapRuleFeature* pFeature = new AirMapRuleFeature(feature, this);
                        pRule->_features.append(pFeature);
                    }
302 303 304 305
                    pRuleSet->_rules.append(pRule);
                }
                //-- Sort rules by display order
                std::sort(pRuleSet->_rules.objectList()->begin(), pRuleSet->_rules.objectList()->end(), rules_sort);
306
                _ruleSets.append(pRuleSet);
Gus Grubba's avatar
Gus Grubba committed
307
                qCDebug(AirMapManagerLog) << "Adding ruleset" << pRuleSet->name();
308
                /*
309 310 311 312 313 314 315 316
                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
317
                    qDebug() << "  " << airspaceType.data();
318 319
                }
                qDebug() << "Rules:";
320
                for (const auto& rule : ruleset.rules) {
321
                    qDebug() << "    --------------------------------------";
Gus Grubba's avatar
Gus Grubba committed
322 323 324 325 326 327
                    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:";
328
                    for (const auto& feature : rule.features) {
Gus Grubba's avatar
Gus Grubba committed
329 330 331 332 333 334
                        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;
335 336
                    }
                }
Gus Grubba's avatar
Gus Grubba committed
337
                */
338
            }
339
            _valid = true;
340 341
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
342
            emit error("Failed to retrieve RuleSets", QString::fromStdString(result.error().message()), description);
343 344
        }
        _state = State::Idle;
345 346
        emit ruleSetsChanged();
        emit selectedRuleSetsChanged();
347 348
    });
}
349 350 351

//-----------------------------------------------------------------------------
QString
352
AirMapRulesetsManager::selectedRuleSets()
353
{
354
    QString selection;
355 356
    for(int i = 0; i < _ruleSets.count(); i++) {
        AirMapRuleSet* rule = qobject_cast<AirMapRuleSet*>(_ruleSets.get(i));
357 358
        if(rule && rule->selected()) {
            selection += rule->shortName() + ", ";
359 360
        }
    }
361 362 363 364 365
    int idx = selection.lastIndexOf(", ");
    if(idx >= 0) {
        selection = selection.left(idx);
    }
    return selection;
366 367 368 369
}

//-----------------------------------------------------------------------------
void
370
AirMapRulesetsManager::_selectedChanged()
371
{
372
    emit selectedRuleSetsChanged();
373 374
    //-- TODO: Do whatever it is you do to select a rule
}
375 376 377 378 379 380 381 382 383 384 385

//-----------------------------------------------------------------------------
void
AirMapRulesetsManager::clearAllFeatures()
{
    QSettings settings;
    settings.beginGroup(kAirMapFeatureGroup);
    settings.remove("");
    settings.endGroup();
}