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

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

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

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

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

Gus Grubba's avatar
Gus Grubba committed
108 109 110 111
//-----------------------------------------------------------------------------
void
AirMapRuleFeature::setValue(const QVariant val)
{
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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
131 132 133 134 135 136 137 138 139
    _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
140 141 142
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(QObject* parent)
    : AirspaceRule(parent)
143 144 145
{
}

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

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

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

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

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

203 204 205 206 207 208
//-----------------------------------------------------------------------------
AirMapRulesetsManager::AirMapRulesetsManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

209 210 211 212 213 214 215 216 217 218
//-----------------------------------------------------------------------------
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();
}

219
//-----------------------------------------------------------------------------
220
void AirMapRulesetsManager::setROI(const QGCGeoBoundingCube& roi)
221 222 223 224 225 226
{
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
227
        qCWarning(AirMapManagerLog) << "AirMapRulesetsManager::updateROI: state not idle";
228 229
        return;
    }
230
    qCDebug(AirMapManagerLog) << "Rulesets Request (ROI Changed)";
231
    _valid = false;
232
    _ruleSets.clearAndDeleteContents();
233 234
    _state = State::RetrieveItems;
    RuleSets::Search::Parameters params;
235 236 237 238 239 240 241 242 243
    //-- 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);
244 245 246 247 248 249
    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) {
250 251 252 253 254 255 256 257 258 259
            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;
260
                //-- TODO: This should be persistent and if the new incoming set has the
261 262 263
                //   same, previosuly selected rulesets, it should "remember".
                if(pRuleSet->_isDefault) {
                    pRuleSet->_selected = true;
264 265 266
                }
                switch(ruleset.selection_type) {
                case RuleSet::SelectionType::pickone:
267
                    pRuleSet->_selectionType = AirspaceRuleSet::Pickone;
268 269
                    break;
                case RuleSet::SelectionType::required:
270 271
                    pRuleSet->_selectionType = AirspaceRuleSet::Required;
                    pRuleSet->_selected = true;
272 273 274
                    break;
                default:
                case RuleSet::SelectionType::optional:
275
                    pRuleSet->_selectionType = AirspaceRuleSet::Optional;
276 277
                    break;
                }
278 279 280
                //-- Iterate Rules
                for (const auto& rule : ruleset.rules) {
                    AirMapRule* pRule = new AirMapRule(rule, this);
Gus Grubba's avatar
Gus Grubba committed
281
                    //-- Iterate Rule Features
Gus Grubba's avatar
Gus Grubba committed
282 283 284 285
                    for (const auto& feature : rule.features) {
                        AirMapRuleFeature* pFeature = new AirMapRuleFeature(feature, this);
                        pRule->_features.append(pFeature);
                    }
286 287 288 289
                    pRuleSet->_rules.append(pRule);
                }
                //-- Sort rules by display order
                std::sort(pRuleSet->_rules.objectList()->begin(), pRuleSet->_rules.objectList()->end(), rules_sort);
290
                _ruleSets.append(pRuleSet);
Gus Grubba's avatar
Gus Grubba committed
291
                qCDebug(AirMapManagerLog) << "Adding ruleset" << pRuleSet->name();
292
                /*
293 294 295 296 297 298 299 300
                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
301
                    qDebug() << "  " << airspaceType.data();
302 303
                }
                qDebug() << "Rules:";
304
                for (const auto& rule : ruleset.rules) {
305
                    qDebug() << "    --------------------------------------";
Gus Grubba's avatar
Gus Grubba committed
306 307 308 309 310 311
                    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:";
312
                    for (const auto& feature : rule.features) {
Gus Grubba's avatar
Gus Grubba committed
313 314 315 316 317 318
                        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;
319 320
                    }
                }
Gus Grubba's avatar
Gus Grubba committed
321
                */
322
            }
323
            _valid = true;
324 325
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
326
            emit error("Failed to retrieve RuleSets", QString::fromStdString(result.error().message()), description);
327 328
        }
        _state = State::Idle;
329 330
        emit ruleSetsChanged();
        emit selectedRuleSetsChanged();
331 332
    });
}
333 334 335

//-----------------------------------------------------------------------------
QString
336
AirMapRulesetsManager::selectedRuleSets()
337
{
338
    QString selection;
339 340
    for(int i = 0; i < _ruleSets.count(); i++) {
        AirMapRuleSet* rule = qobject_cast<AirMapRuleSet*>(_ruleSets.get(i));
341 342
        if(rule && rule->selected()) {
            selection += rule->shortName() + ", ";
343 344
        }
    }
345 346 347 348 349
    int idx = selection.lastIndexOf(", ");
    if(idx >= 0) {
        selection = selection.left(idx);
    }
    return selection;
350 351 352 353
}

//-----------------------------------------------------------------------------
void
354
AirMapRulesetsManager::_selectedChanged()
355
{
356
    emit selectedRuleSetsChanged();
357 358
    //-- TODO: Do whatever it is you do to select a rule
}