UASMessageHandler.cc 6.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/*!
 * @file
 *   @brief Message Handler
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "QGCApplication.h"
31
#include "UASMessageHandler.h"
32
#include "MultiVehicleManager.h"
33
#include "UAS.h"
34

35
UASMessage::UASMessage(int componentid, int severity, QString text)
36 37 38 39 40 41
{
    _compId   = componentid;
    _severity = severity;
    _text     = text;
}

42 43 44 45 46 47 48 49 50 51 52 53 54
bool UASMessage::severityIsError()
{
    switch (_severity) {
        case MAV_SEVERITY_EMERGENCY:
        case MAV_SEVERITY_ALERT:
        case MAV_SEVERITY_CRITICAL:
        case MAV_SEVERITY_ERROR:
            return true;
        default:
            return false;
    }
}

55 56
UASMessageHandler::UASMessageHandler(QGCApplication* app)
    : QGCTool(app)
57
    , _activeUAS(NULL)
Lorenz Meier's avatar
Lorenz Meier committed
58 59
    , _activeComponent(-1)
    , _multiComp(false)
dogmaphobic's avatar
dogmaphobic committed
60
    , _errorCount(0)
61
    , _errorCountTotal(0)
dogmaphobic's avatar
dogmaphobic committed
62 63
    , _warningCount(0)
    , _normalCount(0)
64
    , _showErrorsInToolbar(false)
65
    , _multiVehicleManager(NULL)
66
{
67

68 69
}

70
UASMessageHandler::~UASMessageHandler()
71
{
72
    clearMessages();
73 74
}

75 76 77 78 79 80 81 82 83 84 85
void UASMessageHandler::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);

   _multiVehicleManager = _toolbox->multiVehicleManager();

   connect(_multiVehicleManager, &MultiVehicleManager::activeVehicleChanged, this, &UASMessageHandler::_activeVehicleChanged);
   emit textMessageReceived(NULL);
   emit textMessageCountChanged(0);
}

86
void UASMessageHandler::clearMessages()
87 88 89 90 91 92
{
    _mutex.lock();
    while(_messages.count()) {
        delete _messages.last();
        _messages.pop_back();
    }
dogmaphobic's avatar
dogmaphobic committed
93 94 95
    _errorCount   = 0;
    _warningCount = 0;
    _normalCount  = 0;
96
    _mutex.unlock();
dogmaphobic's avatar
dogmaphobic committed
97
    emit textMessageCountChanged(0);
98 99
}

100
void UASMessageHandler::_activeVehicleChanged(Vehicle* vehicle)
101 102
{
    // If we were already attached to an autopilot, disconnect it.
103
    if (_activeUAS)
104
    {
105
        disconnect(_activeUAS, &UASInterface::textMessageReceived, this, &UASMessageHandler::handleTextMessage);
106
        _activeUAS = NULL;
107
        clearMessages();
108 109 110
        emit textMessageReceived(NULL);
    }
    // And now if there's an autopilot to follow, set up the UI.
111
    if (vehicle)
112
    {
113
        UAS* uas = vehicle->uas();
114

115
        // Connect to the new UAS.
116
        clearMessages();
117
        _activeUAS = uas;
118
        connect(uas, &UASInterface::textMessageReceived, this, &UASMessageHandler::handleTextMessage);
119 120 121
    }
}

dogmaphobic's avatar
dogmaphobic committed
122
void UASMessageHandler::handleTextMessage(int, int compId, int severity, QString text)
123 124 125 126 127 128 129
{

    // Color the output depending on the message severity. We have 3 distinct cases:
    // 1: If we have an ERROR or worse, make it bigger, bolder, and highlight it red.
    // 2: If we have a warning or notice, just make it bold and color it orange.
    // 3: Otherwise color it the standard color, white.

dogmaphobic's avatar
dogmaphobic committed
130 131
    _mutex.lock();

Lorenz Meier's avatar
Lorenz Meier committed
132 133 134 135 136 137 138 139
    if (_activeComponent < 0) {
        _activeComponent = compId;
    }

    if (compId != _activeComponent) {
        _multiComp = true;
    }

140 141 142 143 144 145 146 147
    // So first determine the styling based on the severity.
    QString style;
    switch (severity)
    {
    case MAV_SEVERITY_EMERGENCY:
    case MAV_SEVERITY_ALERT:
    case MAV_SEVERITY_CRITICAL:
    case MAV_SEVERITY_ERROR:
148
        style = QString("<#E>");
dogmaphobic's avatar
dogmaphobic committed
149
        _errorCount++;
150
        _errorCountTotal++;
151 152 153
        break;
    case MAV_SEVERITY_NOTICE:
    case MAV_SEVERITY_WARNING:
154
        style = QString("<#I>");
dogmaphobic's avatar
dogmaphobic committed
155
        _warningCount++;
156 157
        break;
    default:
158
        style = QString("<#N>");
dogmaphobic's avatar
dogmaphobic committed
159
        _normalCount++;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        break;
    }

    // And determine the text for the severitie
    QString severityText("");
    switch (severity)
    {
    case MAV_SEVERITY_EMERGENCY:
        severityText = QString(tr(" EMERGENCY:"));
        break;
    case MAV_SEVERITY_ALERT:
        severityText = QString(tr(" ALERT:"));
        break;
    case MAV_SEVERITY_CRITICAL:
        severityText = QString(tr(" Critical:"));
        break;
    case MAV_SEVERITY_ERROR:
        severityText = QString(tr(" Error:"));
        break;
    case MAV_SEVERITY_WARNING:
        severityText = QString(tr(" Warning:"));
        break;
    case MAV_SEVERITY_NOTICE:
        severityText = QString(tr(" Notice:"));
        break;
    case MAV_SEVERITY_INFO:
        severityText = QString(tr(" Info:"));
        break;
    case MAV_SEVERITY_DEBUG:
        severityText = QString(tr(" Debug:"));
        break;
    default:
        severityText = QString(tr(""));
        break;
    }

    // Finally preppend the properly-styled text with a timestamp.
    QString dateString = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
198
    UASMessage* message = new UASMessage(compId, severity, text);
Lorenz Meier's avatar
Lorenz Meier committed
199 200 201 202
    QString compString("");
    if (_multiComp) {
        compString = QString(" COMP:%1").arg(compId);
    }
203
    message->_setFormatedText(QString("<font style=\"%1\">[%2%3]%4 %5</font><br/>").arg(style).arg(dateString).arg(compString).arg(severityText).arg(text));
Lorenz Meier's avatar
Lorenz Meier committed
204

205
    if (message->severityIsError()) {
206 207
        _latestError = severityText + " " + text;
    }
Lorenz Meier's avatar
Lorenz Meier committed
208

209
    _mutex.unlock();
Lorenz Meier's avatar
Lorenz Meier committed
210

211
    emit textMessageReceived(message);
Lorenz Meier's avatar
Lorenz Meier committed
212 213 214

    _messages.append(message);
    int count = _messages.count();
dogmaphobic's avatar
dogmaphobic committed
215
    emit textMessageCountChanged(count);
216

217
    if (_showErrorsInToolbar && message->severityIsError()) {
218
        _app->showMessage(message->getText());
219
    }
220
}
dogmaphobic's avatar
dogmaphobic committed
221

222 223 224 225 226 227 228
int UASMessageHandler::getErrorCountTotal() {
    _mutex.lock();
    int c = _errorCountTotal;
    _mutex.unlock();
    return c;
}

dogmaphobic's avatar
dogmaphobic committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
int UASMessageHandler::getErrorCount() {
    _mutex.lock();
    int c = _errorCount;
    _errorCount = 0;
    _mutex.unlock();
    return c;
}

int UASMessageHandler::getWarningCount() {
    _mutex.lock();
    int c = _warningCount;
    _warningCount = 0;
    _mutex.unlock();
    return c;
}

int UASMessageHandler::getNormalCount() {
    _mutex.lock();
    int c = _normalCount;
    _normalCount = 0;
    _mutex.unlock();
    return c;
}