2013-08-17 15:39:29 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// stl includes
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
|
|
|
// jsoncpp includes
|
|
|
|
#include <json/json.h>
|
|
|
|
|
2013-08-18 12:21:07 +02:00
|
|
|
// Hyperion includes
|
|
|
|
#include <hyperion/Hyperion.h>
|
|
|
|
|
2013-08-17 19:20:19 +02:00
|
|
|
// util includes
|
|
|
|
#include <utils/jsonschema/JsonSchemaChecker.h>
|
|
|
|
|
2013-08-18 20:55:59 +02:00
|
|
|
class ImageProcessor;
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// The Connection object created by \a JsonServer when a new connection is establshed
|
2013-08-31 14:36:54 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
class JsonClientConnection : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Constructor
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param socket The Socket object for this connection
|
|
|
|
/// @param hyperion The Hyperion server
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:21:07 +02:00
|
|
|
JsonClientConnection(QTcpSocket * socket, Hyperion * hyperion);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Destructor
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
~JsonClientConnection();
|
|
|
|
|
|
|
|
signals:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Signal which is emitted when the connection is being closed
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param connection This connection object
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void connectionClosed(JsonClientConnection * connection);
|
|
|
|
|
|
|
|
private slots:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Slot called when new data has arrived
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void readData();
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Slot called when this connection is being closed
|
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void socketClosed();
|
|
|
|
|
|
|
|
private:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message as string
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void handleMessage(const std::string & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Color message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleColorCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Image message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleImageCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Effect message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleEffectCommand(const Json::Value & message);
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Server info message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleServerInfoCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Clear message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleClearCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Clearall message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleClearallCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Transform message
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message the incoming message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleTransformCommand(const Json::Value & message);
|
2016-03-11 12:32:48 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Temperature message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleTemperatureCommand(const Json::Value & message);
|
2016-04-02 00:04:11 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Handle an incoming JSON Adjustment message
|
|
|
|
///
|
|
|
|
/// @param message the incoming message
|
|
|
|
///
|
|
|
|
void handleAdjustmentCommand(const Json::Value & message);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Handle an incoming JSON message of unknown type
|
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
void handleNotImplemented();
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send a message to the connected client
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message The JSON message to send
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void sendMessage(const Json::Value & message);
|
2016-02-15 18:25:18 +01:00
|
|
|
void sendMessage(const Json::Value & message, QTcpSocket * socket);
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send a standard reply indicating success
|
|
|
|
///
|
2013-08-18 12:21:07 +02:00
|
|
|
void sendSuccessReply();
|
2013-08-31 14:36:54 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Send an error message back to the client
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param error String describing the error
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-17 15:39:29 +02:00
|
|
|
void sendErrorReply(const std::string & error);
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Do handshake for a websocket connection
|
|
|
|
///
|
|
|
|
void doWebSocketHandshake();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Handle incoming websocket data frame
|
|
|
|
///
|
|
|
|
void handleWebSocketFrame();
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
///
|
|
|
|
/// forward json message
|
|
|
|
///
|
|
|
|
void forwardJsonMessage(const Json::Value & message);
|
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
private:
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Check if a JSON messag is valid according to a given JSON schema
|
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @param message JSON message which need to be checked
|
|
|
|
/// @param schemaResource Qt esource identifier with the JSON schema
|
|
|
|
/// @param errors Output error message
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-31 14:36:54 +02:00
|
|
|
/// @return true if message conforms the given JSON schema
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-08-18 12:02:17 +02:00
|
|
|
bool checkJson(const Json::Value & message, const QString &schemaResource, std::string & errors);
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-08-18 12:02:17 +02:00
|
|
|
private:
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The TCP-Socket that is connected tot the Json-client
|
2013-08-18 12:02:17 +02:00
|
|
|
QTcpSocket * _socket;
|
2013-08-17 19:20:19 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The processor for translating images to led-values
|
2013-08-18 20:55:59 +02:00
|
|
|
ImageProcessor * _imageProcessor;
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// Link to Hyperion for writing led-values to a priority channel
|
2013-08-18 12:21:07 +02:00
|
|
|
Hyperion * _hyperion;
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The buffer used for reading data from the socket
|
2013-08-17 15:39:29 +02:00
|
|
|
QByteArray _receiveBuffer;
|
2014-10-25 22:35:53 +02:00
|
|
|
|
|
|
|
/// used for WebSocket detection and connection handling
|
|
|
|
bool _webSocketHandshakeDone;
|
2013-08-17 15:39:29 +02:00
|
|
|
};
|