UASMessageHandler.h 4.62 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 31 32 33 34 35 36 37
/*=====================================================================

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>
 */

#ifndef QGCMESSAGEHANDLER_H
#define QGCMESSAGEHANDLER_H

#include <QObject>
#include <QVector>
#include <QMutex>

#include "QGCSingleton.h"
38
#include "Vehicle.h"
39 40

class UASInterface;
41
class UASMessageHandler;
42 43

/*!
44
 * @class UASMessage
45 46
 * @brief Message element
 */
47
class UASMessage
48
{
49
    friend class UASMessageHandler;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
public:
    /**
     * @brief Get message source component ID
     */
    int getComponentID()        { return _compId; }
    /**
     * @brief Get message severity (from MAV_SEVERITY_XXX enum)
     */
    int getSeverity()           { return _severity; }
    /**
     * @brief Get message text (e.g. "[pm] sending list")
     */
    QString getText()           { return _text; }
    /**
     * @brief Get (html) formatted text (in the form: "[11:44:21.137 - COMP:50] Info: [pm] sending list")
     */
    QString getFormatedText()   { return _formatedText; }
67 68 69 70 71
    /**
     * @return true: This message is a of a severity which is considered an error
     */
    bool severityIsError();
    
72
private:
73
    UASMessage(int componentid, int severity, QString text);
74 75 76 77 78 79 80
    void _setFormatedText(const QString formatedText) { _formatedText = formatedText; }
    int _compId;
    int _severity;
    QString _text;
    QString _formatedText;
};

81
class UASMessageHandler : public QGCSingleton
82 83
{
    Q_OBJECT
84
    DECLARE_QGC_SINGLETON(UASMessageHandler, UASMessageHandler)
85
    
86
public:
87 88
    explicit UASMessageHandler(QObject *parent = 0);
    ~UASMessageHandler();
89 90 91 92 93 94 95 96 97 98 99
    /**
     * @brief Locks access to the message list
     */
    void lockAccess()   {_mutex.lock(); }
    /**
     * @brief Unlocks access to the message list
     */
    void unlockAccess() {_mutex.unlock(); }
    /**
     * @brief Access to the message list
     */
100
    const QVector<UASMessage*>& messages() { return _messages; }
101 102 103 104
    /**
     * @brief Clear messages
     */
    void clearMessages();
dogmaphobic's avatar
dogmaphobic committed
105 106 107 108
    /**
     * @brief Get error message count (Resets count once read)
     */
    int getErrorCount();
109 110 111 112
    /**
     * @brief Get error message count (never reset)
     */
    int getErrorCountTotal();
dogmaphobic's avatar
dogmaphobic committed
113 114 115 116 117 118 119 120
    /**
     * @brief Get warning message count (Resets count once read)
     */
    int getWarningCount();
    /**
     * @brief Get normal message count (Resets count once read)
     */
    int getNormalCount();
121 122 123 124
    /**
     * @brief Get latest error message
     */
    QString getLatestError()   { return _latestError; }
125 126 127 128
    
    /// Begin to show message which are errors in the toolbar
    void showErrorsInToolbar(void) { _showErrorsInToolbar = true; }
    
129 130 131 132 133 134 135 136 137
public slots:
    /**
     * @brief Handle text message from current active UAS
     * @param uasid UAS Id
     * @param componentid Component Id
     * @param severity Message severity
     * @param text Message Text
     */
    void handleTextMessage(int uasid, int componentid, int severity, QString text);
138
    
139 140 141 142 143
signals:
    /**
     * @brief Sent out when new message arrives
     * @param message A pointer to the message. NULL if resetting (new UAS assigned)
     */
144
    void textMessageReceived(UASMessage* message);
dogmaphobic's avatar
dogmaphobic committed
145 146 147 148 149
    /**
     * @brief Sent out when the message count changes
     * @param count The new message count
     */
    void textMessageCountChanged(int count);
150
    
151 152 153
private slots:
    void _activeVehicleChanged(Vehicle* vehicle);
    
154 155 156
private:
    // Stores the UAS that we're currently receiving messages from.
    UASInterface* _activeUAS;
157
    QVector<UASMessage*> _messages;
158 159 160 161 162
    QMutex  _mutex;
    int     _errorCount;
    int     _errorCountTotal;
    int     _warningCount;
    int     _normalCount;
163
    QString _latestError;
164
    bool    _showErrorsInToolbar;
165 166 167
};

#endif // QGCMESSAGEHANDLER_H