2018-12-28 18:12:45 +01:00
# pragma once
2020-03-26 17:59:41 +01:00
// parent class
# include <api/API.h>
2018-12-28 18:12:45 +01:00
// hyperion includes
# include <utils/Components.h>
# include <hyperion/Hyperion.h>
2019-08-24 22:53:30 +02:00
# include <hyperion/HyperionIManager.h>
2018-12-28 18:12:45 +01:00
2019-07-02 19:06:36 +02:00
// qt includes
2018-12-28 18:12:45 +01:00
# include <QJsonObject>
# include <QString>
2019-08-24 22:53:30 +02:00
class QTimer ;
2018-12-28 18:12:45 +01:00
class JsonCB ;
2019-07-12 16:54:26 +02:00
class AuthManager ;
2018-12-28 18:12:45 +01:00
2020-03-26 17:59:41 +01:00
class JsonAPI : public API
2018-12-28 18:12:45 +01:00
{
Q_OBJECT
public :
///
/// Constructor
///
/// @param peerAddress provide the Address of the peer
/// @param log The Logger class of the creator
/// @param parent Parent QObject
2019-07-12 16:54:26 +02:00
/// @param localConnection True when the sender has origin home network
2018-12-28 18:12:45 +01:00
/// @param noListener if true, this instance won't listen for hyperion push events
///
2020-03-26 17:59:41 +01:00
JsonAPI ( QString peerAddress , Logger * log , const bool & localConnection , QObject * parent , bool noListener = false ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON message
///
/// @param message the incoming message as string
///
2020-03-26 17:59:41 +01:00
void handleMessage ( const QString & message , const QString & httpAuthHeader = " " ) ;
2018-12-28 18:12:45 +01:00
2019-09-17 21:33:46 +02:00
///
/// @brief Initialization steps
///
void initialize ( void ) ;
2018-12-28 18:12:45 +01:00
public slots :
2018-12-31 15:48:29 +01:00
///
2019-08-24 22:53:30 +02:00
/// @brief Is called whenever the current Hyperion instance pushes new led raw values (if enabled)
/// @param ledColors The current led colors
2018-12-31 15:48:29 +01:00
///
2020-03-26 17:59:41 +01:00
void streamLedcolorsUpdate ( const std : : vector < ColorRgb > & ledColors ) ;
2018-12-28 18:12:45 +01:00
2019-08-24 22:53:30 +02:00
///
/// @brief Push images whenever hyperion emits (if enabled)
/// @param image The current image
///
2020-03-26 17:59:41 +01:00
void setImage ( const Image < ColorRgb > & image ) ;
2018-12-28 18:12:45 +01:00
2019-08-24 22:53:30 +02:00
///
/// @brief Process and push new log messages from logger (if enabled)
///
2020-03-26 17:59:41 +01:00
void incommingLogMessage ( const Logger : : T_LOG_MESSAGE & ) ;
2018-12-28 18:12:45 +01:00
2019-07-12 16:54:26 +02:00
private slots :
///
2020-03-26 17:59:41 +01:00
/// @brief Handle emits from API of a new Token request.
2019-07-12 16:54:26 +02:00
/// @param id The id of the request
/// @param The comment which needs to be accepted
///
2020-03-26 17:59:41 +01:00
void newPendingTokenRequest ( const QString & id , const QString & comment ) ;
2019-07-12 16:54:26 +02:00
///
/// @brief Handle emits from AuthManager of accepted/denied/timeouts token request, just if QObject matches with this instance we are allowed to send response.
/// @param success If true the request was accepted else false and no token was created
/// @param token The new token that is now valid
/// @param comment The comment that was part of the request
/// @param id The id that was part of the request
///
2020-03-26 17:59:41 +01:00
void handleTokenResponse ( const bool & success , const QString & token , const QString & comment , const QString & id ) ;
2019-07-12 16:54:26 +02:00
2019-07-14 22:43:22 +02:00
///
/// @brief Handle whenever the state of a instance (HyperionIManager) changes according to enum instanceState
/// @param instaneState A state from enum
/// @param instance The index of instance
/// @param name The name of the instance, just available with H_CREATED
///
2020-03-26 17:59:41 +01:00
void handleInstanceStateChange ( const instanceState & state , const quint8 & instance , const QString & name = QString ( ) ) ;
2019-07-14 22:43:22 +02:00
2018-12-28 18:12:45 +01:00
signals :
///
/// Signal emits with the reply message provided with handleMessage()
///
void callbackMessage ( QJsonObject ) ;
///
/// Signal emits whenever a jsonmessage should be forwarded
///
void forwardJsonMessage ( QJsonObject ) ;
private :
// true if further callbacks are forbidden (http)
bool _noListener ;
2018-12-31 15:48:29 +01:00
2019-07-02 19:06:36 +02:00
/// The peer address of the client
QString _peerAddress ;
2018-12-28 18:12:45 +01:00
2018-12-31 15:48:29 +01:00
// The JsonCB instance which handles data subscription/notifications
2020-03-26 17:59:41 +01:00
JsonCB * _jsonCB ;
2018-12-28 18:12:45 +01:00
// streaming buffers
QJsonObject _streaming_leds_reply ;
QJsonObject _streaming_image_reply ;
QJsonObject _streaming_logging_reply ;
/// flag to determine state of log streaming
bool _streaming_logging_activated ;
2019-08-24 22:53:30 +02:00
/// timer for led color refresh
2020-03-26 17:59:41 +01:00
QTimer * _ledStreamTimer ;
2018-12-31 15:48:29 +01:00
2019-08-24 22:53:30 +02:00
/// led stream connection handle
QMetaObject : : Connection _ledStreamConnection ;
2018-12-28 18:12:45 +01:00
2019-08-24 22:53:30 +02:00
/// the current streaming led values
std : : vector < ColorRgb > _currentLedValues ;
2018-12-31 15:48:29 +01:00
2019-07-14 22:43:22 +02:00
///
/// @brief Handle the switches of Hyperion instances
/// @param instance the instance to switch
/// @param forced indicate if it was a forced switch by system
/// @return true on success. false if not found
///
2020-03-26 17:59:41 +01:00
bool handleInstanceSwitch ( const quint8 & instance = 0 , const bool & forced = false ) ;
2019-07-14 22:43:22 +02:00
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Color message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleColorCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Image message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleImageCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Effect message
///
/// @param message the incoming message
///
2019-01-06 19:49:56 +01:00
void handleEffectCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Effect message (Write JSON Effect)
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleCreateEffectCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Effect message (Delete JSON Effect)
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleDeleteEffectCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON System info message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleSysInfoCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Server info message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleServerInfoCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Clear message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleClearCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
2019-07-14 22:43:22 +02:00
///
/// Handle an incoming JSON Clearall message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleClearallCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2019-07-14 22:43:22 +02:00
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Adjustment message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleAdjustmentCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON SourceSelect message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleSourceSelectCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON GetConfig message and check subcommand
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleConfigCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
2020-04-17 16:59:20 +02:00
/// Handle an incoming JSON GetSchema message from handleConfigCommand()
2018-12-28 18:12:45 +01:00
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleSchemaGetCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON SetConfig message from handleConfigCommand()
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleConfigSetCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON Component State message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleComponentStateCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON Led Colors message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleLedColorsCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON Logging message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleLoggingCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON Proccessing message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleProcessingCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
/// Handle an incoming JSON VideoMode message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleVideoModeCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2018-12-28 18:12:45 +01:00
2019-07-12 16:54:26 +02:00
/// Handle an incoming JSON plugin message
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleAuthorizeCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2019-07-12 16:54:26 +02:00
2019-07-14 22:43:22 +02:00
/// Handle an incoming JSON instance message
2019-01-27 13:41:21 +01:00
///
/// @param message the incoming message
///
2020-03-26 17:59:41 +01:00
void handleInstanceCommand ( const QJsonObject & message , const QString & command , const int tan ) ;
2019-01-27 13:41:21 +01:00
2018-12-28 18:12:45 +01:00
///
/// Handle an incoming JSON message of unknown type
///
void handleNotImplemented ( ) ;
///
/// Send a standard reply indicating success
///
2020-03-26 17:59:41 +01:00
void sendSuccessReply ( const QString & command = " " , const int tan = 0 ) ;
2018-12-28 18:12:45 +01:00
///
/// Send a standard reply indicating success with data
///
2020-03-26 17:59:41 +01:00
void sendSuccessDataReply ( const QJsonDocument & doc , const QString & command = " " , const int & tan = 0 ) ;
2018-12-28 18:12:45 +01:00
///
/// Send an error message back to the client
///
/// @param error String describing the error
///
2020-03-26 17:59:41 +01:00
void sendErrorReply ( const QString & error , const QString & command = " " , const int tan = 0 ) ;
2019-09-17 21:33:46 +02:00
///
/// @brief Kill all signal/slot connections to stop possible data emitter
///
void stopDataConnections ( void ) ;
2020-04-17 16:59:20 +02:00
} ;