mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Razor Chroma Support - Initial version
This commit is contained in:
parent
9d2e442d42
commit
8476553448
@ -492,6 +492,7 @@
|
|||||||
"edt_dev_spec_printTimeStamp_title": "Add timestamp",
|
"edt_dev_spec_printTimeStamp_title": "Add timestamp",
|
||||||
"edt_dev_spec_pwmChannel_title": "PWM channel",
|
"edt_dev_spec_pwmChannel_title": "PWM channel",
|
||||||
"edt_dev_spec_restoreOriginalState_title": "Restore lights' original state when disabled",
|
"edt_dev_spec_restoreOriginalState_title": "Restore lights' original state when disabled",
|
||||||
|
"edt_dev_spec_razor_device_title": "Razor Chroma Device",
|
||||||
"edt_dev_spec_serial_title": "Serial number",
|
"edt_dev_spec_serial_title": "Serial number",
|
||||||
"edt_dev_spec_spipath_title": "SPI path",
|
"edt_dev_spec_spipath_title": "SPI path",
|
||||||
"edt_dev_spec_sslHSTimeoutMax_title": "Streamer handshake timeout maximum",
|
"edt_dev_spec_sslHSTimeoutMax_title": "Streamer handshake timeout maximum",
|
||||||
|
@ -571,8 +571,8 @@ $(document).ready(function() {
|
|||||||
var devRPiSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'sk9822', 'ws2812spi'];
|
var devRPiSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'sk9822', 'ws2812spi'];
|
||||||
var devRPiPWM = ['ws281x'];
|
var devRPiPWM = ['ws281x'];
|
||||||
var devRPiGPIO = ['piblaster'];
|
var devRPiGPIO = ['piblaster'];
|
||||||
var devNET = ['atmoorb', 'fadecandy', 'philipshue', 'nanoleaf', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udph801', 'udpraw', 'wled', 'yeelight'];
|
var devNET = ['atmoorb', 'fadecandy', 'philipshue', 'nanoleaf', 'razer', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udph801', 'udpraw', 'wled', 'yeelight'];
|
||||||
var devUSB = ['adalight', 'dmx', 'atmo', 'hyperionusbasp', 'lightpack', 'multilightpack', 'paintpack', 'rawhid', 'sedu', 'tpm2', 'karate'];
|
var devUSB = ['adalight', 'dmx', 'atmo', 'hyperionusbasp', 'lightpack', 'paintpack', 'rawhid', 'sedu', 'tpm2', 'karate'];
|
||||||
|
|
||||||
var optArr = [[]];
|
var optArr = [[]];
|
||||||
optArr[1]=[];
|
optArr[1]=[];
|
||||||
|
@ -414,7 +414,7 @@ protected slots:
|
|||||||
///
|
///
|
||||||
/// @return Zero on success else negative
|
/// @return Zero on success else negative
|
||||||
///
|
///
|
||||||
int rewriteLEDs();
|
virtual int rewriteLEDs();
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Set device in error state
|
/// @brief Set device in error state
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
<file alias="schema-lightpack">schemas/schema-lightpack.json</file>
|
<file alias="schema-lightpack">schemas/schema-lightpack.json</file>
|
||||||
<file alias="schema-lpd6803">schemas/schema-lpd6803.json</file>
|
<file alias="schema-lpd6803">schemas/schema-lpd6803.json</file>
|
||||||
<file alias="schema-lpd8806">schemas/schema-lpd8806.json</file>
|
<file alias="schema-lpd8806">schemas/schema-lpd8806.json</file>
|
||||||
<file alias="schema-multilightpack">schemas/schema-multilightpack.json</file>
|
|
||||||
<file alias="schema-p9813">schemas/schema-p9813.json</file>
|
<file alias="schema-p9813">schemas/schema-p9813.json</file>
|
||||||
<file alias="schema-paintpack">schemas/schema-paintpack.json</file>
|
<file alias="schema-paintpack">schemas/schema-paintpack.json</file>
|
||||||
<file alias="schema-philipshue">schemas/schema-philipshue.json</file>
|
<file alias="schema-philipshue">schemas/schema-philipshue.json</file>
|
||||||
@ -36,5 +35,6 @@
|
|||||||
<file alias="schema-nanoleaf">schemas/schema-nanoleaf.json</file>
|
<file alias="schema-nanoleaf">schemas/schema-nanoleaf.json</file>
|
||||||
<file alias="schema-wled">schemas/schema-wled.json</file>
|
<file alias="schema-wled">schemas/schema-wled.json</file>
|
||||||
<file alias="schema-yeelight">schemas/schema-yeelight.json</file>
|
<file alias="schema-yeelight">schemas/schema-yeelight.json</file>
|
||||||
|
<file alias="schema-razer">schemas/schema-razer.json</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
254
libsrc/leddevice/dev_net/LedDeviceRazer.cpp
Normal file
254
libsrc/leddevice/dev_net/LedDeviceRazer.cpp
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
#include "LedDeviceRazer.h"
|
||||||
|
#include <utils/QStringUtils.h>
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
// Constants
|
||||||
|
namespace {
|
||||||
|
bool verbose = false;
|
||||||
|
|
||||||
|
// Configuration settings
|
||||||
|
const char CONFIG_ADDRESS[] = "host";
|
||||||
|
const char RAZOR_DEVICE_TYPE[] = "razorDevice";
|
||||||
|
|
||||||
|
// WLED JSON-API elements
|
||||||
|
const char API_DEFAULT_HOST[] = "localhost";
|
||||||
|
const int API_DEFAULT_PORT = 54235;
|
||||||
|
|
||||||
|
const char API_BASE_PATH[] = "/razer/chromasdk";
|
||||||
|
const char API_PATH_INFO[] = "info";
|
||||||
|
const char API_PATH_STATE[] = "state";
|
||||||
|
|
||||||
|
const char API_RESULT[] = "result";
|
||||||
|
|
||||||
|
// List of State Information
|
||||||
|
const char STATE_ON[] = "on";
|
||||||
|
const char STATE_VALUE_TRUE[] = "true";
|
||||||
|
const char STATE_VALUE_FALSE[] = "false";
|
||||||
|
} //End of constants
|
||||||
|
|
||||||
|
LedDeviceRazer::LedDeviceRazer(const QJsonObject& deviceConfig)
|
||||||
|
: LedDevice()
|
||||||
|
, _restApi(nullptr)
|
||||||
|
, _apiPort(API_DEFAULT_PORT)
|
||||||
|
{
|
||||||
|
_devConfig = deviceConfig;
|
||||||
|
_isDeviceReady = false;
|
||||||
|
|
||||||
|
_activeDeviceType = deviceConfig["type"].toString("UNSPECIFIED").toLower();
|
||||||
|
}
|
||||||
|
|
||||||
|
LedDevice* LedDeviceRazer::construct(const QJsonObject& deviceConfig)
|
||||||
|
{
|
||||||
|
return new LedDeviceRazer(deviceConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LedDeviceRazer::init(const QJsonObject& deviceConfig)
|
||||||
|
{
|
||||||
|
bool isInitOK = false;
|
||||||
|
setRewriteTime(1000);
|
||||||
|
connect(_refreshTimer, &QTimer::timeout, this, &LedDeviceRazer::rewriteLEDs);
|
||||||
|
|
||||||
|
// Initialise sub-class
|
||||||
|
if (LedDevice::init(deviceConfig))
|
||||||
|
{
|
||||||
|
// Initialise LedDevice configuration and execution environment
|
||||||
|
uint configuredLedCount = this->getLedCount();
|
||||||
|
Debug(_log, "DeviceType : %s", QSTRING_CSTR(this->getActiveDeviceType()));
|
||||||
|
Debug(_log, "LedCount : %u", configuredLedCount);
|
||||||
|
Debug(_log, "ColorOrder : %s", QSTRING_CSTR(this->getColorOrder()));
|
||||||
|
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||||
|
Debug(_log, "RefreshTime : %d", _refreshTimerInterval_ms);
|
||||||
|
|
||||||
|
//Razor Chroma SDK allows localhost connection only
|
||||||
|
_hostname = API_DEFAULT_HOST;
|
||||||
|
_apiPort = API_DEFAULT_PORT;
|
||||||
|
|
||||||
|
Debug(_log, "Hostname : %s", QSTRING_CSTR(_hostname));
|
||||||
|
Debug(_log, "Port : %d", _apiPort);
|
||||||
|
|
||||||
|
_razerDeviceType = deviceConfig[RAZOR_DEVICE_TYPE].toString("keyboard");
|
||||||
|
|
||||||
|
Debug(_log, "Razer device : %s", QSTRING_CSTR(_razerDeviceType));
|
||||||
|
|
||||||
|
if (initRestAPI(_hostname, _apiPort))
|
||||||
|
{
|
||||||
|
isInitOK = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isInitOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LedDeviceRazer::initRestAPI(const QString& hostname, int port)
|
||||||
|
{
|
||||||
|
bool isInitOK = false;
|
||||||
|
|
||||||
|
if (_restApi == nullptr)
|
||||||
|
{
|
||||||
|
_restApi = new ProviderRestApi(hostname, port);
|
||||||
|
_restApi->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
|
||||||
|
isInitOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isInitOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LedDeviceRazer::checkApiError(const httpResponse& response)
|
||||||
|
{
|
||||||
|
bool apiError = false;
|
||||||
|
|
||||||
|
if (response.error())
|
||||||
|
{
|
||||||
|
this->setInError(response.getErrorReason());
|
||||||
|
apiError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QString errorReason;
|
||||||
|
|
||||||
|
QString strJson(response.getBody().toJson(QJsonDocument::Compact));
|
||||||
|
DebugIf(verbose, _log, "Reply: [%s]", strJson.toUtf8().constData());
|
||||||
|
|
||||||
|
QJsonObject jsonObj = response.getBody().object();
|
||||||
|
|
||||||
|
if (!jsonObj[API_RESULT].isNull())
|
||||||
|
{
|
||||||
|
int resultCode = jsonObj[API_RESULT].toInt();
|
||||||
|
|
||||||
|
if (resultCode != 0)
|
||||||
|
{
|
||||||
|
errorReason = QString("Chroma SDK error (%1)").arg(resultCode);
|
||||||
|
this->setInError(errorReason);
|
||||||
|
apiError = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return apiError;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LedDeviceRazer::open()
|
||||||
|
{
|
||||||
|
int retval = -1;
|
||||||
|
QString errortext;
|
||||||
|
_isDeviceReady = false;
|
||||||
|
|
||||||
|
// Try to open the LedDevice
|
||||||
|
|
||||||
|
QJsonObject obj;
|
||||||
|
|
||||||
|
obj.insert("title", "Hyperion - Razer Chroma");
|
||||||
|
obj.insert("description", "Hyperion to Razer Chroma interface");
|
||||||
|
|
||||||
|
QJsonObject authorDetails;
|
||||||
|
authorDetails.insert("name", "Hyperion Team");
|
||||||
|
authorDetails.insert("contact", "https://github.com/hyperion-project/hyperion.ng");
|
||||||
|
|
||||||
|
obj.insert("author", authorDetails);
|
||||||
|
|
||||||
|
QJsonArray deviceList = { "keyboard","mouse","headset","mousepad","keypad","chromalink" };
|
||||||
|
obj.insert("device_supported", deviceList);
|
||||||
|
|
||||||
|
obj.insert("category", "application");
|
||||||
|
|
||||||
|
QJsonDocument data = QJsonDocument(obj);
|
||||||
|
|
||||||
|
_restApi->setPort(API_DEFAULT_PORT);
|
||||||
|
_restApi->setBasePath(API_BASE_PATH);
|
||||||
|
|
||||||
|
httpResponse response = _restApi->post(data.toJson(QJsonDocument::Compact));
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
QJsonObject jsonObj = response.getBody().object();
|
||||||
|
if (jsonObj["uri"].isNull())
|
||||||
|
{
|
||||||
|
this->setInError("Chroma SDK error. No 'uri' received");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_uri = jsonObj.value("uri").toString();
|
||||||
|
_restApi->setUrl(_uri);
|
||||||
|
|
||||||
|
DebugIf(verbose, _log, "Session-ID: %d, uri [%s]", jsonObj.value("sessionid").toInt(), QSTRING_CSTR(_uri.toString()));
|
||||||
|
|
||||||
|
QJsonObject effectObj;
|
||||||
|
effectObj.insert("effect", "CHROMA_STATIC");
|
||||||
|
QJsonObject param;
|
||||||
|
param.insert("color", 255);
|
||||||
|
effectObj.insert("param", param);
|
||||||
|
data = QJsonDocument(effectObj);
|
||||||
|
|
||||||
|
_restApi->setPath(_razerDeviceType);
|
||||||
|
response = _restApi->put(data.toJson(QJsonDocument::Compact));
|
||||||
|
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
_restApi->setPath(_razerDeviceType);
|
||||||
|
response = _restApi->put(data.toJson(QJsonDocument::Compact));
|
||||||
|
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
// Everything is OK, device is ready
|
||||||
|
_isDeviceReady = true;
|
||||||
|
retval = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LedDeviceRazer::close()
|
||||||
|
{
|
||||||
|
int retval = -1;
|
||||||
|
_isDeviceReady = false;
|
||||||
|
|
||||||
|
if (!_uri.isEmpty())
|
||||||
|
{
|
||||||
|
httpResponse response = _restApi->deleteResource(_uri);
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
// Everything is OK -> device is closed
|
||||||
|
retval = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LedDeviceRazer::write(const std::vector<ColorRgb>& ledValues)
|
||||||
|
{
|
||||||
|
int retval = -1;
|
||||||
|
|
||||||
|
ColorRgb color = ledValues[0];
|
||||||
|
int colorParam = (color.red * 65536) + (color.green * 256) + color.blue;
|
||||||
|
|
||||||
|
QJsonObject effectObj;
|
||||||
|
effectObj.insert("effect", "CHROMA_STATIC");
|
||||||
|
QJsonObject param;
|
||||||
|
param.insert("color", colorParam);
|
||||||
|
effectObj.insert("param", param);
|
||||||
|
QJsonDocument data = QJsonDocument(effectObj);
|
||||||
|
|
||||||
|
_restApi->setPath(_razerDeviceType);
|
||||||
|
httpResponse response = _restApi->put(data.toJson(QJsonDocument::Compact));
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
retval = 0;
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LedDeviceRazer::rewriteLEDs()
|
||||||
|
{
|
||||||
|
int retval = -1;
|
||||||
|
|
||||||
|
_restApi->setPath("heartbeat");
|
||||||
|
httpResponse response = _restApi->put();
|
||||||
|
if (!checkApiError(response))
|
||||||
|
{
|
||||||
|
retval = 0;
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
101
libsrc/leddevice/dev_net/LedDeviceRazer.h
Normal file
101
libsrc/leddevice/dev_net/LedDeviceRazer.h
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#ifndef LEDEVICERAZER_H
|
||||||
|
#define LEDEVICERAZER_H
|
||||||
|
|
||||||
|
// LedDevice includes
|
||||||
|
#include <leddevice/LedDevice.h>
|
||||||
|
#include "ProviderRestApi.h"
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Implementation of a Razer Chroma LedDevice
|
||||||
|
/// Supported Razer Chroma device types: Keyboard, Mouse, Headset, Mousepad, Keypad, Chromalink
|
||||||
|
///
|
||||||
|
class LedDeviceRazer : public LedDevice
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Constructs a specific LED-device
|
||||||
|
///
|
||||||
|
/// @param deviceConfig Device's configuration as JSON-Object
|
||||||
|
///
|
||||||
|
explicit LedDeviceRazer(const QJsonObject& deviceConfig);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Constructs the LED-device
|
||||||
|
///
|
||||||
|
/// @param[in] deviceConfig Device's configuration as JSON-Object
|
||||||
|
/// @return LedDevice constructed
|
||||||
|
///
|
||||||
|
static LedDevice* construct(const QJsonObject& deviceConfig);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Initialise the device's configuration
|
||||||
|
///
|
||||||
|
/// @param[in] deviceConfig the JSON device configuration
|
||||||
|
/// @return True, if success
|
||||||
|
///
|
||||||
|
bool init(const QJsonObject& deviceConfig) override;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Opens the output device.
|
||||||
|
///
|
||||||
|
/// @return Zero on success (i.e. device is ready), else negative
|
||||||
|
///
|
||||||
|
int open() override;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Closes the output device.
|
||||||
|
///
|
||||||
|
/// @return Zero on success (i.e. device is closed), else negative
|
||||||
|
///
|
||||||
|
int close() override;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Writes the RGB-Color values to the LEDs.
|
||||||
|
///
|
||||||
|
/// @param[in] ledValues The RGB-color per LED
|
||||||
|
/// @return Zero on success, else negative
|
||||||
|
///
|
||||||
|
int write(const std::vector<ColorRgb>& ledValues) override;
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Write the last data to the LEDs again.
|
||||||
|
///
|
||||||
|
/// @return Zero on success else negative
|
||||||
|
///
|
||||||
|
int rewriteLEDs() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Initialise the access to the REST-API wrapper
|
||||||
|
///
|
||||||
|
/// @param[in] host
|
||||||
|
/// @param[in] port
|
||||||
|
/// @return True, if success
|
||||||
|
///
|
||||||
|
bool initRestAPI(const QString& hostname, int port);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Check, if Chroma SDK API response failed
|
||||||
|
///
|
||||||
|
/// @param[in] http response, incl. the response by Chroma SDK in JSON-format
|
||||||
|
/// return True, API call failed
|
||||||
|
///
|
||||||
|
bool checkApiError(const httpResponse& response);
|
||||||
|
|
||||||
|
///REST-API wrapper
|
||||||
|
ProviderRestApi* _restApi;
|
||||||
|
|
||||||
|
QString _hostname;
|
||||||
|
int _apiPort;
|
||||||
|
QUrl _uri;
|
||||||
|
|
||||||
|
QString _razerDeviceType;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // LEDEVICERAZER_H
|
@ -12,6 +12,8 @@
|
|||||||
// Constants
|
// Constants
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
bool verbose = false;
|
||||||
|
|
||||||
const QChar ONE_SLASH = '/';
|
const QChar ONE_SLASH = '/';
|
||||||
|
|
||||||
} //End of constants
|
} //End of constants
|
||||||
@ -19,13 +21,10 @@ const QChar ONE_SLASH = '/';
|
|||||||
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
||||||
:_log(Logger::getInstance("LEDDEVICE"))
|
:_log(Logger::getInstance("LEDDEVICE"))
|
||||||
, _networkManager(nullptr)
|
, _networkManager(nullptr)
|
||||||
,_scheme("http")
|
|
||||||
,_hostname(host)
|
|
||||||
,_port(port)
|
|
||||||
{
|
{
|
||||||
_networkManager = new QNetworkAccessManager();
|
_networkManager = new QNetworkAccessManager();
|
||||||
|
|
||||||
_apiUrl.setScheme(_scheme);
|
_apiUrl.setScheme("http");
|
||||||
_apiUrl.setHost(host);
|
_apiUrl.setHost(host);
|
||||||
_apiUrl.setPort(port);
|
_apiUrl.setPort(port);
|
||||||
_basePath = basePath;
|
_basePath = basePath;
|
||||||
@ -45,6 +44,12 @@ ProviderRestApi::~ProviderRestApi()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProviderRestApi::setUrl(const QUrl& url)
|
||||||
|
{
|
||||||
|
_apiUrl = url;
|
||||||
|
_basePath = url.path();
|
||||||
|
}
|
||||||
|
|
||||||
void ProviderRestApi::setBasePath(const QString& basePath)
|
void ProviderRestApi::setBasePath(const QString& basePath)
|
||||||
{
|
{
|
||||||
_basePath.clear();
|
_basePath.clear();
|
||||||
@ -121,7 +126,7 @@ httpResponse ProviderRestApi::get()
|
|||||||
|
|
||||||
httpResponse ProviderRestApi::get(const QUrl& url)
|
httpResponse ProviderRestApi::get(const QUrl& url)
|
||||||
{
|
{
|
||||||
Debug(_log, "GET: [%s]", QSTRING_CSTR( url.toString() ));
|
DebugIf(verbose,_log, "GET: [%s]", QSTRING_CSTR(url.toString()));
|
||||||
|
|
||||||
// Perform request
|
// Perform request
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
@ -150,9 +155,11 @@ httpResponse ProviderRestApi::put(const QString &body)
|
|||||||
|
|
||||||
httpResponse ProviderRestApi::put(const QUrl& url, const QString& body)
|
httpResponse ProviderRestApi::put(const QUrl& url, const QString& body)
|
||||||
{
|
{
|
||||||
Debug(_log, "PUT: [%s] [%s]", QSTRING_CSTR( url.toString() ), QSTRING_CSTR( body ) );
|
DebugIf(verbose, _log, "PUT: [%s] [%s]", QSTRING_CSTR(url.toString()), QSTRING_CSTR(body));
|
||||||
// Perform request
|
// Perform request
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(_networkRequestHeaders);
|
||||||
|
request.setUrl(url);
|
||||||
|
|
||||||
QNetworkReply* reply = _networkManager->put(request, body.toUtf8());
|
QNetworkReply* reply = _networkManager->put(request, body.toUtf8());
|
||||||
// Connect requestFinished signal to quit slot of the loop.
|
// Connect requestFinished signal to quit slot of the loop.
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
@ -172,6 +179,63 @@ httpResponse ProviderRestApi::put(const QUrl &url, const QString &body)
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
httpResponse ProviderRestApi::post(const QString& body)
|
||||||
|
{
|
||||||
|
return post(getUrl(), body);
|
||||||
|
}
|
||||||
|
|
||||||
|
httpResponse ProviderRestApi::post(const QUrl& url, const QString& body)
|
||||||
|
{
|
||||||
|
DebugIf(verbose, _log, "POST: [%s] [%s]", QSTRING_CSTR(url.toString()), QSTRING_CSTR(body));
|
||||||
|
// Perform request
|
||||||
|
QNetworkRequest request(_networkRequestHeaders);
|
||||||
|
request.setUrl(url);
|
||||||
|
|
||||||
|
QNetworkReply* reply = _networkManager->post(request, body.toUtf8());
|
||||||
|
// Connect requestFinished signal to quit slot of the loop.
|
||||||
|
QEventLoop loop;
|
||||||
|
loop.connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||||
|
// Go into the loop until the request is finished.
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
httpResponse response;
|
||||||
|
if (reply->operation() == QNetworkAccessManager::PostOperation)
|
||||||
|
{
|
||||||
|
response = getResponse(reply);
|
||||||
|
}
|
||||||
|
// Free space.
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
// Return response
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
httpResponse ProviderRestApi::deleteResource(const QUrl& url)
|
||||||
|
{
|
||||||
|
DebugIf(verbose, _log, "DELETE: [%s]", QSTRING_CSTR(url.toString()));
|
||||||
|
// Perform request
|
||||||
|
QNetworkRequest request(_networkRequestHeaders);
|
||||||
|
request.setUrl(url);
|
||||||
|
|
||||||
|
QNetworkReply* reply = _networkManager->deleteResource(request);
|
||||||
|
// Connect requestFinished signal to quit slot of the loop.
|
||||||
|
QEventLoop loop;
|
||||||
|
loop.connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||||
|
// Go into the loop until the request is finished.
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
httpResponse response;
|
||||||
|
if (reply->operation() == QNetworkAccessManager::DeleteOperation)
|
||||||
|
{
|
||||||
|
response = getResponse(reply);
|
||||||
|
}
|
||||||
|
// Free space.
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
// Return response
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
|
httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
|
||||||
{
|
{
|
||||||
httpResponse response;
|
httpResponse response;
|
||||||
@ -179,7 +243,7 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
|||||||
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
response.setHttpStatusCode(httpStatusCode);
|
response.setHttpStatusCode(httpStatusCode);
|
||||||
|
|
||||||
Debug(_log, "Reply.httpStatusCode [%d]", httpStatusCode );
|
DebugIf(verbose, _log, "Reply.error [%d], Reply.httpStatusCode [%d]", reply->error(), httpStatusCode);
|
||||||
|
|
||||||
response.setNetworkReplyError(reply->error());
|
response.setNetworkReplyError(reply->error());
|
||||||
|
|
||||||
@ -245,3 +309,18 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProviderRestApi::setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value)
|
||||||
|
{
|
||||||
|
QVariant headerValue = _networkRequestHeaders.header(header);
|
||||||
|
if (headerValue.isNull())
|
||||||
|
{
|
||||||
|
_networkRequestHeaders.setHeader(header, value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!headerValue.toString().contains(value.toString()))
|
||||||
|
{
|
||||||
|
_networkRequestHeaders.setHeader(header, headerValue.toString() + "," + value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -93,6 +93,27 @@ public:
|
|||||||
///
|
///
|
||||||
virtual ~ProviderRestApi();
|
virtual ~ProviderRestApi();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Set an API's host
|
||||||
|
///
|
||||||
|
/// @param[in] host
|
||||||
|
///
|
||||||
|
void setHost(const QString& host) { _apiUrl.setHost(host); };
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Set an API's port
|
||||||
|
///
|
||||||
|
/// @param[in] port
|
||||||
|
///
|
||||||
|
void setPort(const int port) { _apiUrl.setPort(port); };
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Set an API's url
|
||||||
|
///
|
||||||
|
/// @param[in] url, e.g. "http://locahost:60351/chromalink/"
|
||||||
|
///
|
||||||
|
void setUrl(const QUrl& url);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Get the URL as defined using scheme, host, port, API-basepath, path, query, fragment
|
/// @brief Get the URL as defined using scheme, host, port, API-basepath, path, query, fragment
|
||||||
///
|
///
|
||||||
@ -173,7 +194,24 @@ public:
|
|||||||
/// @param[in] body The body of the request in JSON
|
/// @param[in] body The body of the request in JSON
|
||||||
/// @return Response The body of the response in JSON
|
/// @return Response The body of the response in JSON
|
||||||
///
|
///
|
||||||
httpResponse post(QString body = "");
|
httpResponse post(const QString& body = "");
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Execute POST request
|
||||||
|
///
|
||||||
|
/// @param[in] URL for POST request
|
||||||
|
/// @param[in] body The body of the request in JSON
|
||||||
|
/// @return Response The body of the response in JSON
|
||||||
|
///
|
||||||
|
httpResponse post(const QUrl& url, const QString& body = "");
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Execute DELETE request
|
||||||
|
///
|
||||||
|
/// @param[in] URL (Resource) for DELETE request
|
||||||
|
/// @return Response The body of the response in JSON
|
||||||
|
///
|
||||||
|
httpResponse deleteResource(const QUrl& url);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @brief Handle responses for REST requests
|
/// @brief Handle responses for REST requests
|
||||||
@ -183,6 +221,20 @@ public:
|
|||||||
///
|
///
|
||||||
httpResponse getResponse(QNetworkReply* const& reply);
|
httpResponse getResponse(QNetworkReply* const& reply);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Adds a header field.
|
||||||
|
///
|
||||||
|
/// @param[in] The type of the header field.
|
||||||
|
/// @param[in] The value of the header field.
|
||||||
|
/// If the header field exists, the value will be combined as comma separated string.
|
||||||
|
|
||||||
|
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Remove all header fields.
|
||||||
|
///
|
||||||
|
void removeAllHeaders() { _networkRequestHeaders = QNetworkRequest(); };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -200,16 +252,13 @@ private:
|
|||||||
|
|
||||||
QUrl _apiUrl;
|
QUrl _apiUrl;
|
||||||
|
|
||||||
QString _scheme;
|
|
||||||
QString _hostname;
|
|
||||||
int _port;
|
|
||||||
|
|
||||||
QString _basePath;
|
QString _basePath;
|
||||||
QString _path;
|
QString _path;
|
||||||
|
|
||||||
QString _fragment;
|
QString _fragment;
|
||||||
QUrlQuery _query;
|
QUrlQuery _query;
|
||||||
|
|
||||||
|
QNetworkRequest _networkRequestHeaders;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PROVIDERRESTKAPI_H
|
#endif // PROVIDERRESTKAPI_H
|
||||||
|
28
libsrc/leddevice/schemas/schema-razer.json
Normal file
28
libsrc/leddevice/schemas/schema-razer.json
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": true,
|
||||||
|
"properties": {
|
||||||
|
"razorDevice": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "edt_dev_spec_razor_device_title",
|
||||||
|
"enum": [ "keyboard", "mouse", "headset", "mousepad", "keypad", "chromalink" ],
|
||||||
|
"default": "keyboard",
|
||||||
|
"options": {
|
||||||
|
"enum_titles": [ "Keyboard", "Mouse", "Headset", "Mousepad", "Keypad", "Chromalink" ]
|
||||||
|
},
|
||||||
|
"propertyOrder": 1
|
||||||
|
},
|
||||||
|
"latchTime": {
|
||||||
|
"type": "integer",
|
||||||
|
"title": "edt_dev_spec_latchtime_title",
|
||||||
|
"default": 0,
|
||||||
|
"append": "edt_append_ms",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 1000,
|
||||||
|
"access": "expert",
|
||||||
|
"propertyOrder": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user