mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Merge branch 'master' into Razer_Chroma_Support
This commit is contained in:
@@ -55,7 +55,7 @@ bool LedDeviceAtmoOrb::init(const QJsonObject &deviceConfig)
|
||||
QStringList orbIds = QStringUtils::split(deviceConfig["orbIds"].toString().simplified().remove(" "),",", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR( this->getActiveDeviceType() ));
|
||||
Debug(_log, "LedCount : %u", this->getLedCount());
|
||||
Debug(_log, "LedCount : %d", this->getLedCount());
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
|
||||
Debug(_log, "RefreshTime : %d", _refreshTimerInterval_ms);
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
@@ -89,8 +89,8 @@ bool LedDeviceAtmoOrb::init(const QJsonObject &deviceConfig)
|
||||
}
|
||||
}
|
||||
|
||||
uint numberOrbs = _orbIds.size();
|
||||
uint configuredLedCount = this->getLedCount();
|
||||
int numberOrbs = _orbIds.size();
|
||||
int configuredLedCount = this->getLedCount();
|
||||
|
||||
if ( _orbIds.empty() )
|
||||
{
|
||||
@@ -111,7 +111,7 @@ bool LedDeviceAtmoOrb::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
if ( numberOrbs > configuredLedCount )
|
||||
{
|
||||
Info(_log, "%s: More Orbs [%u] than configured LEDs [%u].", QSTRING_CSTR(this->getActiveDeviceType()), numberOrbs, configuredLedCount );
|
||||
Info(_log, "%s: More Orbs [%d] than configured LEDs [%d].", QSTRING_CSTR(this->getActiveDeviceType()), numberOrbs, configuredLedCount );
|
||||
}
|
||||
|
||||
isInitOK = true;
|
||||
@@ -276,16 +276,21 @@ void LedDeviceAtmoOrb::sendCommand(const QByteArray &bytes)
|
||||
_udpSocket->writeDatagram(bytes.data(), bytes.size(), _groupAddress, _multiCastGroupPort);
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceAtmoOrb::discover()
|
||||
QJsonObject LedDeviceAtmoOrb::discover(const QJsonObject& params)
|
||||
{
|
||||
//Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
|
||||
QJsonArray deviceList;
|
||||
|
||||
_multicastGroup = params["multiCastGroup"].toString(MULTICAST_GROUP_DEFAULT_ADDRESS);
|
||||
_multiCastGroupPort = static_cast<quint16>(params["multiCastPort"].toInt(MULTICAST_GROUP_DEFAULT_PORT));
|
||||
|
||||
if ( open() == 0 )
|
||||
{
|
||||
Debug ( _log, "Send discovery requests to all AtmoOrbs" );
|
||||
Debug ( _log, "Send discovery requests to all AtmoOrbs listening to %s:%d", QSTRING_CSTR(_multicastGroup),_multiCastGroupPort );
|
||||
setColor(0, ColorRgb::BLACK, 8);
|
||||
|
||||
if ( _udpSocket->waitForReadyRead(DEFAULT_DISCOVERY_TIMEOUT.count()) )
|
||||
|
@@ -43,9 +43,11 @@ public:
|
||||
///
|
||||
/// @brief Discover AtmoOrb devices available (for configuration).
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
virtual QJsonObject discover() override;
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Send an update to the AtmoOrb device to identify it.
|
||||
|
725
libsrc/leddevice/dev_net/LedDeviceCololight.cpp
Normal file
725
libsrc/leddevice/dev_net/LedDeviceCololight.cpp
Normal file
@@ -0,0 +1,725 @@
|
||||
#include "LedDeviceCololight.h"
|
||||
|
||||
#include <utils/QStringUtils.h>
|
||||
#include <QUdpSocket>
|
||||
#include <QHostInfo>
|
||||
#include <QtEndian>
|
||||
#include <QEventLoop>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
const bool verbose = false;
|
||||
const bool verbose3 = false;
|
||||
|
||||
// Configuration settings
|
||||
|
||||
const char CONFIG_HW_LED_COUNT[] = "hardwareLedCount";
|
||||
|
||||
// Cololight discovery service
|
||||
|
||||
const int API_DEFAULT_PORT = 8900;
|
||||
|
||||
const char DISCOVERY_ADDRESS[] = "255.255.255.255";
|
||||
const quint16 DISCOVERY_PORT = 12345;
|
||||
const char DISCOVERY_MESSAGE[] = "Z-SEARCH * \r\n";
|
||||
constexpr std::chrono::milliseconds DEFAULT_DISCOVERY_TIMEOUT{ 5000 };
|
||||
constexpr std::chrono::milliseconds DEFAULT_READ_TIMEOUT{ 1000 };
|
||||
constexpr std::chrono::milliseconds DEFAULT_IDENTIFY_TIME{ 2000 };
|
||||
|
||||
const char COLOLIGHT_MODEL[] = "mod";
|
||||
const char COLOLIGHT_MODEL_TYPE[] = "subkey";
|
||||
const char COLOLIGHT_MAC[] = "sn";
|
||||
const char COLOLIGHT_NAME[] = "name";
|
||||
|
||||
const char COLOLIGHT_MODEL_IDENTIFIER[] = "OD_WE_QUAN";
|
||||
|
||||
const int COLOLIGHT_BEADS_PER_MODULE = 19;
|
||||
const int COLOLIGHT_MIN_STRIP_SEGMENT_SIZE = 30;
|
||||
|
||||
} //End of constants
|
||||
|
||||
LedDeviceCololight::LedDeviceCololight(const QJsonObject& deviceConfig)
|
||||
: ProviderUdp(deviceConfig)
|
||||
, _modelType(-1)
|
||||
, _ledLayoutType(STRIP_LAYOUT)
|
||||
, _ledBeadCount(0)
|
||||
, _distance(0)
|
||||
, _sequenceNumber(1)
|
||||
{
|
||||
_packetFixPart.append(reinterpret_cast<const char*>(PACKET_HEADER), sizeof(PACKET_HEADER));
|
||||
_packetFixPart.append(reinterpret_cast<const char*>(PACKET_SECU), sizeof(PACKET_SECU));
|
||||
}
|
||||
|
||||
LedDevice* LedDeviceCololight::construct(const QJsonObject& deviceConfig)
|
||||
{
|
||||
return new LedDeviceCololight(deviceConfig);
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::init(const QJsonObject& deviceConfig)
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
_port = API_DEFAULT_PORT;
|
||||
|
||||
if (ProviderUdp::init(deviceConfig))
|
||||
{
|
||||
// Initialise LedDevice configuration and execution environment
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR(this->getActiveDeviceType()));
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR(this->getColorOrder()));
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
|
||||
if (initLedsConfiguration())
|
||||
{
|
||||
initDirectColorCmdTemplate();
|
||||
isInitOK = true;
|
||||
}
|
||||
}
|
||||
return isInitOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::initLedsConfiguration()
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
if (!getInfo())
|
||||
{
|
||||
QString errorReason = QString("Cololight device (%1) not accessible to get additional properties!")
|
||||
.arg(getAddress().toString());
|
||||
setInError(errorReason);
|
||||
}
|
||||
else
|
||||
{
|
||||
QString modelTypeText;
|
||||
|
||||
switch (_modelType) {
|
||||
case 0:
|
||||
modelTypeText = "Strip";
|
||||
_ledLayoutType = STRIP_LAYOUT;
|
||||
break;
|
||||
case 1:
|
||||
_ledLayoutType = MODLUE_LAYOUT;
|
||||
modelTypeText = "Plus";
|
||||
break;
|
||||
default:
|
||||
_modelType = STRIP;
|
||||
modelTypeText = "Strip";
|
||||
_ledLayoutType = STRIP_LAYOUT;
|
||||
Info(_log, "Model not identified, assuming Cololight %s", QSTRING_CSTR(modelTypeText));
|
||||
break;
|
||||
}
|
||||
Debug(_log, "Model type : %s", QSTRING_CSTR(modelTypeText));
|
||||
|
||||
if (getLedCount() == 0)
|
||||
{
|
||||
setLedCount(_devConfig[CONFIG_HW_LED_COUNT].toInt(0));
|
||||
}
|
||||
|
||||
if (_modelType == STRIP && (getLedCount() % COLOLIGHT_MIN_STRIP_SEGMENT_SIZE != 0))
|
||||
{
|
||||
QString errorReason = QString("Hardware LED count must be multiple of %1 for Cololight Strip!")
|
||||
.arg(COLOLIGHT_MIN_STRIP_SEGMENT_SIZE);
|
||||
this->setInError(errorReason);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log, "LedCount : %d", getLedCount());
|
||||
|
||||
int configuredLedCount = _devConfig["currentLedCount"].toInt(1);
|
||||
|
||||
if (getLedCount() < configuredLedCount)
|
||||
{
|
||||
QString errorReason = QString("Not enough LEDs [%1] for configured LEDs in layout [%2] found!")
|
||||
.arg(getLedCount())
|
||||
.arg(configuredLedCount);
|
||||
this->setInError(errorReason);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (getLedCount() > configuredLedCount)
|
||||
{
|
||||
Info(_log, "%s: More LEDs [%d] than configured LEDs in layout [%d].", QSTRING_CSTR(this->getActiveDeviceType()), getLedCount(), configuredLedCount);
|
||||
}
|
||||
isInitOK = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isInitOK;
|
||||
}
|
||||
|
||||
void LedDeviceCololight::initDirectColorCmdTemplate()
|
||||
{
|
||||
int ledNumber = static_cast<int>(this->getLedCount());
|
||||
|
||||
_directColorCommandTemplate.clear();
|
||||
|
||||
//Packet
|
||||
_directColorCommandTemplate.append(static_cast<char>(bufferMode::LIGHTBEAD)); // idx
|
||||
|
||||
int beads = 1;
|
||||
if (_ledLayoutType == MODLUE_LAYOUT)
|
||||
{
|
||||
beads = COLOLIGHT_BEADS_PER_MODULE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ledNumber; ++i)
|
||||
{
|
||||
_directColorCommandTemplate.append(static_cast<char>(i * beads + 1));
|
||||
_directColorCommandTemplate.append(static_cast<char>(i * beads + beads));
|
||||
_directColorCommandTemplate.append(3, static_cast<char>(0x00));
|
||||
}
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::getInfo()
|
||||
{
|
||||
bool isCmdOK = false;
|
||||
|
||||
QByteArray command;
|
||||
|
||||
const quint8 packetSize = 2;
|
||||
int fixPartsize = sizeof(TL1_CMD_FIXED_PART);
|
||||
|
||||
command.resize(sizeof(TL1_CMD_FIXED_PART) + packetSize);
|
||||
command.fill('\0');
|
||||
|
||||
command[fixPartsize - 3] = static_cast<char>(SETVAR); // verb
|
||||
command[fixPartsize - 2] = static_cast<char>(_sequenceNumber); // ctag
|
||||
command[fixPartsize - 1] = static_cast<char>(packetSize); // length
|
||||
|
||||
//Packet
|
||||
command[fixPartsize] = static_cast<char>(READ_INFO_FROM_STORAGE); // idx
|
||||
command[fixPartsize + 1] = static_cast<char>(0x01); // idx
|
||||
|
||||
if (sendRequest(TL1_CMD, command))
|
||||
{
|
||||
QByteArray response;
|
||||
if (readResponse(response))
|
||||
{
|
||||
DebugIf(verbose, _log, "#[0x%x], Data returned: [%s]", _sequenceNumber, QSTRING_CSTR(toHex(response)));
|
||||
|
||||
quint16 ledNum = qFromBigEndian<quint16>(response.data() + 1);
|
||||
|
||||
if (ledNum != 0xFFFF)
|
||||
{
|
||||
_ledBeadCount = ledNum;
|
||||
if (ledNum % COLOLIGHT_BEADS_PER_MODULE == 0)
|
||||
{
|
||||
_modelType = MODLUE_LAYOUT;
|
||||
_distance = ledNum / COLOLIGHT_BEADS_PER_MODULE;
|
||||
setLedCount(_distance);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_modelType = STRIP;
|
||||
setLedCount(0);
|
||||
}
|
||||
|
||||
Debug(_log, "#LEDs found [0x%x], [%u], distance [%d]", _ledBeadCount, _ledBeadCount, _distance);
|
||||
|
||||
isCmdOK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setEffect(const effect effect)
|
||||
{
|
||||
return setColor(static_cast<uint32_t>(effect));
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setColor(const ColorRgb colorRgb)
|
||||
{
|
||||
uint32_t color = colorRgb.blue | (colorRgb.green << 8) | (colorRgb.red << 16) | (0x00 << 24);
|
||||
|
||||
return setColor(color);
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setColor(const uint32_t color)
|
||||
{
|
||||
bool isCmdOK = false;
|
||||
|
||||
QByteArray command;
|
||||
|
||||
const quint8 packetSize = 6;
|
||||
int fixPartsize = sizeof(TL1_CMD_FIXED_PART);
|
||||
|
||||
command.resize(sizeof(TL1_CMD_FIXED_PART) + packetSize);
|
||||
command.fill('\0');
|
||||
|
||||
command[fixPartsize - 3] = static_cast<char>(SET); // verb
|
||||
command[fixPartsize - 2] = static_cast<char>(_sequenceNumber); // ctag
|
||||
command[fixPartsize - 1] = static_cast<char>(packetSize); // length
|
||||
|
||||
//Packet
|
||||
command[fixPartsize] = static_cast<char>(0x02); // idx
|
||||
command[fixPartsize + 1] = static_cast<char>(0xff); // set color or dynamic effect
|
||||
|
||||
qToBigEndian<quint32>(color, command.data() + fixPartsize + 2);
|
||||
|
||||
if (sendRequest(TL1_CMD, command))
|
||||
{
|
||||
QByteArray response;
|
||||
if (readResponse(response))
|
||||
{
|
||||
DebugIf(verbose, _log, "#[0x%x], Data returned: [%s]", _sequenceNumber, QSTRING_CSTR(toHex(response)));
|
||||
isCmdOK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setState(bool isOn)
|
||||
{
|
||||
bool isCmdOK = false;
|
||||
|
||||
quint8 type = isOn ? STATE_ON : STATE_OFF;
|
||||
|
||||
QByteArray command;
|
||||
|
||||
const quint8 packetSize = 3;
|
||||
int fixPartsize = sizeof(TL1_CMD_FIXED_PART);
|
||||
|
||||
command.resize(sizeof(TL1_CMD_FIXED_PART) + packetSize);
|
||||
command.fill('\0');
|
||||
|
||||
command[fixPartsize - 3] = static_cast<char>(SET); // verb
|
||||
command[fixPartsize - 2] = static_cast<char>(_sequenceNumber); // ctag
|
||||
command[fixPartsize - 1] = static_cast<char>(packetSize); // length
|
||||
|
||||
//Packet
|
||||
command[fixPartsize] = static_cast<char>(BRIGTHNESS_CONTROL); // idx
|
||||
command[fixPartsize + 1] = static_cast<char>(type); // type
|
||||
command[fixPartsize + 2] = static_cast<char>(isOn); // value
|
||||
|
||||
if (sendRequest(TL1_CMD, command))
|
||||
{
|
||||
QByteArray response;
|
||||
if (readResponse(response))
|
||||
{
|
||||
DebugIf(verbose, _log, "#[0x%x], Data returned: [%s]", _sequenceNumber, QSTRING_CSTR(toHex(response)));
|
||||
isCmdOK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setStateDirect(bool isOn)
|
||||
{
|
||||
bool isCmdOK = false;
|
||||
|
||||
QByteArray command;
|
||||
|
||||
//Packet
|
||||
command.append(static_cast<char>(0x04)); // idx
|
||||
command.append(static_cast<char>(isOn)); // idx
|
||||
command.append(static_cast<char>(0xd7)); // idx
|
||||
|
||||
if (sendRequest(DIRECT_CONTROL, command))
|
||||
{
|
||||
QByteArray response;
|
||||
if (readResponse(response))
|
||||
{
|
||||
DebugIf(verbose, _log, "#[0x%x], Data returned: [%s]", _sequenceNumber, QSTRING_CSTR(toHex(response)));
|
||||
isCmdOK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setColor(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
int ledNumber = static_cast<int>(ledValues.size());
|
||||
|
||||
QByteArray command = _directColorCommandTemplate;
|
||||
|
||||
//Update LED values, start from offset (mode + first start/stop pair) = 3
|
||||
for (int i = 0; i < ledNumber; ++i)
|
||||
{
|
||||
command[3 + i * 5] = static_cast<char>(ledValues[i].red);
|
||||
command[3 + i * 5 + 1] = static_cast<char>(ledValues[i].green);
|
||||
command[3 + i * 5 + 2] = static_cast<char>(ledValues[i].blue);
|
||||
}
|
||||
|
||||
bool isCmdOK = sendRequest(DIRECT_CONTROL, command);
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::setTL1CommandMode(bool isOn)
|
||||
{
|
||||
bool isCmdOK = false;
|
||||
|
||||
quint8 type = isOn ? STATE_ON : STATE_OFF;
|
||||
|
||||
QByteArray command;
|
||||
|
||||
const quint8 packetSize = 2;
|
||||
int fixPartsize = sizeof(TL1_CMD_FIXED_PART);
|
||||
|
||||
command.resize(sizeof(TL1_CMD_FIXED_PART) + packetSize);
|
||||
command.fill('\0');
|
||||
|
||||
command[fixPartsize - 3] = static_cast<char>(SETEEPROM); // verb
|
||||
command[fixPartsize - 2] = static_cast<char>(_sequenceNumber); // ctag
|
||||
command[fixPartsize - 1] = static_cast<char>(packetSize); // length
|
||||
|
||||
//Packet
|
||||
command[fixPartsize] = static_cast<char>(COLOR_CONTROL); // idx
|
||||
command[fixPartsize + 1] = static_cast<char>(type); // type
|
||||
|
||||
if (sendRequest(TL1_CMD, command))
|
||||
{
|
||||
QByteArray response;
|
||||
if (readResponse(response))
|
||||
{
|
||||
DebugIf(verbose, _log, "#[0x%x], Data returned: [%s]", _sequenceNumber, QSTRING_CSTR(toHex(response)));
|
||||
isCmdOK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isCmdOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::sendRequest(const appID appID, const QByteArray& command)
|
||||
{
|
||||
bool isSendOK = true;
|
||||
QByteArray packet(_packetFixPart);
|
||||
packet.append(static_cast<char>(_sequenceNumber));
|
||||
packet.append(command);
|
||||
|
||||
quint32 size = static_cast<quint32>(static_cast<int>(sizeof(PACKET_SECU)) + 1 + command.size());
|
||||
|
||||
qToBigEndian<quint16>(appID, packet.data() + 4);
|
||||
|
||||
qToBigEndian<quint32>(size, packet.data() + 6);
|
||||
|
||||
++_sequenceNumber;
|
||||
|
||||
DebugIf(verbose3, _log, "packet: ([0x%x], [%u])[%s]", size, size, QSTRING_CSTR(toHex(packet, 64)));
|
||||
|
||||
if (writeBytes(packet) < 0)
|
||||
{
|
||||
isSendOK = false;
|
||||
}
|
||||
|
||||
return isSendOK;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::readResponse()
|
||||
{
|
||||
QByteArray response;
|
||||
return readResponse(response);
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::readResponse(QByteArray& response)
|
||||
{
|
||||
bool isRequestOK = false;
|
||||
if (_udpSocket->waitForReadyRead(DEFAULT_READ_TIMEOUT.count()))
|
||||
{
|
||||
while (_udpSocket->waitForReadyRead(200))
|
||||
{
|
||||
QByteArray datagram;
|
||||
|
||||
while (_udpSocket->hasPendingDatagrams())
|
||||
{
|
||||
datagram.resize(static_cast<int>(_udpSocket->pendingDatagramSize()));
|
||||
QHostAddress senderIP;
|
||||
quint16 senderPort;
|
||||
|
||||
_udpSocket->readDatagram(datagram.data(), datagram.size(), &senderIP, &senderPort);
|
||||
|
||||
if (datagram.size() >= 10)
|
||||
{
|
||||
DebugIf(verbose3, _log, "response: [%s]", QSTRING_CSTR(toHex(datagram, 64)));
|
||||
|
||||
quint16 appID = qFromBigEndian<quint16>(datagram.mid(4, sizeof(appID)));
|
||||
|
||||
if (verbose && appID == 0x8000)
|
||||
{
|
||||
QString tagVersion = datagram.left(2);
|
||||
quint32 packetSize = qFromBigEndian<quint32>(datagram.mid(sizeof(PACKET_HEADER) - sizeof(packetSize)));
|
||||
|
||||
Debug(_log, "Response HEADER: tagVersion [%s], appID: [0x%.2x][%u], packet size: [0x%.4x][%u]", QSTRING_CSTR(tagVersion), appID, appID, packetSize, packetSize);
|
||||
|
||||
quint32 dictionary = qFromBigEndian<quint32>(datagram.mid(sizeof(PACKET_HEADER)));
|
||||
quint32 checkSum = qFromBigEndian<quint32>(datagram.mid(sizeof(PACKET_HEADER) + sizeof(dictionary)));
|
||||
quint32 salt = qFromBigEndian<quint32>(datagram.mid(sizeof(PACKET_HEADER) + sizeof(dictionary) + sizeof(checkSum), sizeof(salt)));
|
||||
quint32 sequenceNumber = qFromBigEndian<quint32>(datagram.mid(sizeof(PACKET_HEADER) + sizeof(dictionary) + sizeof(checkSum) + sizeof(salt)));
|
||||
|
||||
Debug(_log, "Response SECU : Dict: [0x%.4x][%u], Sum: [0x%.4x][%u], Salt: [0x%.4x][%u], SN: [0x%.4x][%u]", dictionary, dictionary, checkSum, checkSum, salt, salt, sequenceNumber, sequenceNumber);
|
||||
|
||||
quint8 packetSN = static_cast<quint8>(datagram.at(sizeof(PACKET_HEADER) + sizeof(PACKET_SECU)));
|
||||
Debug(_log, "Response packSN: [0x%.4x][%u]", packetSN, packetSN);
|
||||
}
|
||||
|
||||
quint8 errorCode = static_cast<quint8>(datagram.at(sizeof(PACKET_HEADER) + sizeof(PACKET_SECU) + 1));
|
||||
|
||||
int dataPartStart = sizeof(PACKET_HEADER) + sizeof(PACKET_SECU) + sizeof(TL1_CMD_FIXED_PART);
|
||||
|
||||
if (errorCode != 0)
|
||||
{
|
||||
quint8 originalVerb = static_cast<quint8>(datagram.at(dataPartStart - 2) - 0x80);
|
||||
quint8 originalRequestPacketSN = static_cast<quint8>(datagram.at(dataPartStart - 1));
|
||||
|
||||
if (errorCode == 16)
|
||||
{
|
||||
//TL1 Command failure
|
||||
Error(_log, "Request [0x%x] failed =with error [%u], appID [%u], originalVerb [0x%x]", originalRequestPacketSN, errorCode, appID, originalVerb);
|
||||
}
|
||||
else
|
||||
{
|
||||
Error(_log, "Request [0x%x] failed with error [%u], appID [%u]", originalRequestPacketSN, errorCode, appID);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TL1 Protocol
|
||||
if (appID == 0x8000)
|
||||
{
|
||||
if (dataPartStart < datagram.size())
|
||||
{
|
||||
quint8 dataLength = static_cast<quint8>(datagram.at(dataPartStart));
|
||||
|
||||
response = datagram.mid(dataPartStart + 1, dataLength);
|
||||
if (verbose)
|
||||
{
|
||||
quint8 originalVerb = static_cast<quint8>(datagram.at(dataPartStart - 2) - 0x80);
|
||||
Debug(_log, "Cmd [0x%x], Data returned: [%s]", originalVerb, QSTRING_CSTR(toHex(response)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugIf(verbose, _log, "No additional data returned");
|
||||
}
|
||||
}
|
||||
isRequestOK = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isRequestOK;
|
||||
}
|
||||
|
||||
int LedDeviceCololight::write(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
int rc = -1;
|
||||
|
||||
if (setColor(ledValues))
|
||||
{
|
||||
rc = 0;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::powerOn()
|
||||
{
|
||||
bool on = true;
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
if (!setState(false) || !setTL1CommandMode(false))
|
||||
{
|
||||
on = false;
|
||||
}
|
||||
}
|
||||
return on;
|
||||
}
|
||||
|
||||
bool LedDeviceCololight::powerOff()
|
||||
{
|
||||
bool off = true;
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
writeBlack();
|
||||
off = setStateDirect(false);
|
||||
setTL1CommandMode(false);
|
||||
}
|
||||
return off;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceCololight::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
||||
|
||||
QJsonArray deviceList;
|
||||
|
||||
QUdpSocket udpSocket;
|
||||
|
||||
udpSocket.writeDatagram(QString(DISCOVERY_MESSAGE).toUtf8(), QHostAddress(DISCOVERY_ADDRESS), DISCOVERY_PORT);
|
||||
|
||||
if (udpSocket.waitForReadyRead(DEFAULT_DISCOVERY_TIMEOUT.count()))
|
||||
{
|
||||
while (udpSocket.waitForReadyRead(500))
|
||||
{
|
||||
QByteArray datagram;
|
||||
|
||||
while (udpSocket.hasPendingDatagrams())
|
||||
{
|
||||
datagram.resize(static_cast<int>(udpSocket.pendingDatagramSize()));
|
||||
QHostAddress senderIP;
|
||||
quint16 senderPort;
|
||||
|
||||
udpSocket.readDatagram(datagram.data(), datagram.size(), &senderIP, &senderPort);
|
||||
|
||||
QString data(datagram);
|
||||
|
||||
QMap<QString, QString> headers;
|
||||
// parse request
|
||||
QStringList entries = QStringUtils::split(data, "\n", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
for (auto entry : entries)
|
||||
{
|
||||
// split into key=value, be aware that value field may contain also a "="
|
||||
entry = entry.simplified();
|
||||
int pos = entry.indexOf("=");
|
||||
if (pos == -1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const QString key = entry.left(pos).trimmed().toLower();
|
||||
const QString value = entry.mid(pos + 1).trimmed();
|
||||
headers[key] = value;
|
||||
}
|
||||
|
||||
if (headers.value("mod") == COLOLIGHT_MODEL_IDENTIFIER)
|
||||
{
|
||||
QString ipAddress = QHostAddress(senderIP.toIPv4Address()).toString();
|
||||
_services.insert(ipAddress, headers);
|
||||
|
||||
Debug(_log, "Cololight discovered at [%s]", QSTRING_CSTR(ipAddress));
|
||||
DebugIf(verbose3, _log, "_data: [%s]", QSTRING_CSTR(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMap<QString, QMap <QString, QString>>::iterator i;
|
||||
for (i = _services.begin(); i != _services.end(); ++i)
|
||||
{
|
||||
QJsonObject obj;
|
||||
|
||||
QString ipAddress = i.key();
|
||||
obj.insert("ip", ipAddress);
|
||||
obj.insert("model", i.value().value(COLOLIGHT_MODEL));
|
||||
obj.insert("type", i.value().value(COLOLIGHT_MODEL_TYPE));
|
||||
obj.insert("mac", i.value().value(COLOLIGHT_MAC));
|
||||
obj.insert("name", i.value().value(COLOLIGHT_NAME));
|
||||
|
||||
QHostInfo hostInfo = QHostInfo::fromName(i.key());
|
||||
if (hostInfo.error() == QHostInfo::NoError)
|
||||
{
|
||||
QString hostname = hostInfo.hostName();
|
||||
if (!QHostInfo::localDomainName().isEmpty())
|
||||
{
|
||||
obj.insert("hostname", hostname.remove("." + QHostInfo::localDomainName()));
|
||||
obj.insert("domain", QHostInfo::localDomainName());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hostname.startsWith(ipAddress))
|
||||
{
|
||||
obj.insert("hostname", ipAddress);
|
||||
|
||||
QString domain = hostname.remove(ipAddress);
|
||||
if (domain.at(0) == '.')
|
||||
{
|
||||
domain.remove(0, 1);
|
||||
}
|
||||
obj.insert("domain", domain);
|
||||
}
|
||||
else
|
||||
{
|
||||
int domainPos = hostname.indexOf('.');
|
||||
obj.insert("hostname", hostname.left(domainPos));
|
||||
obj.insert("domain", hostname.mid(domainPos + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deviceList << obj;
|
||||
}
|
||||
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
DebugIf(verbose, _log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceCololight::getProperties(const QJsonObject& params)
|
||||
{
|
||||
DebugIf(verbose, _log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
QJsonObject properties;
|
||||
|
||||
QString apiHostname = params["host"].toString("");
|
||||
quint16 apiPort = static_cast<quint16>(params["port"].toInt(API_DEFAULT_PORT));
|
||||
|
||||
if (!apiHostname.isEmpty())
|
||||
{
|
||||
QJsonObject deviceConfig;
|
||||
|
||||
deviceConfig.insert("host", apiHostname);
|
||||
deviceConfig.insert("port", apiPort);
|
||||
if (ProviderUdp::init(deviceConfig))
|
||||
{
|
||||
if (getInfo())
|
||||
{
|
||||
QString modelTypeText;
|
||||
|
||||
switch (_modelType) {
|
||||
case 1:
|
||||
modelTypeText = "Plus";
|
||||
break;
|
||||
default:
|
||||
modelTypeText = "Strip";
|
||||
break;
|
||||
}
|
||||
properties.insert("modelType", modelTypeText);
|
||||
properties.insert("ledCount", static_cast<int>(getLedCount()));
|
||||
properties.insert("ledBeadCount", _ledBeadCount);
|
||||
properties.insert("distance", _distance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DebugIf(verbose, _log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
void LedDeviceCololight::identify(const QJsonObject& params)
|
||||
{
|
||||
DebugIf(verbose, _log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
QString apiHostname = params["host"].toString("");
|
||||
quint16 apiPort = static_cast<quint16>(params["port"].toInt(API_DEFAULT_PORT));
|
||||
|
||||
if (!apiHostname.isEmpty())
|
||||
{
|
||||
QJsonObject deviceConfig;
|
||||
|
||||
deviceConfig.insert("host", apiHostname);
|
||||
deviceConfig.insert("port", apiPort);
|
||||
if (ProviderUdp::init(deviceConfig))
|
||||
{
|
||||
if (setStateDirect(false) && setState(true))
|
||||
{
|
||||
setEffect(THE_CIRCUS);
|
||||
|
||||
QEventLoop loop;
|
||||
QTimer::singleShot(DEFAULT_IDENTIFY_TIME.count(), &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
|
||||
setColor(ColorRgb::BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
310
libsrc/leddevice/dev_net/LedDeviceCololight.h
Normal file
310
libsrc/leddevice/dev_net/LedDeviceCololight.h
Normal file
@@ -0,0 +1,310 @@
|
||||
#ifndef LEDEVICECOLOLIGHT_H
|
||||
#define LEDEVICECOLOLIGHT_H
|
||||
|
||||
// LedDevice includes
|
||||
#include <leddevice/LedDevice.h>
|
||||
#include "ProviderUdp.h"
|
||||
|
||||
enum appID {
|
||||
TL1_CMD = 0x00,
|
||||
DIRECT_CONTROL = 0x01,
|
||||
TRANSMIT_FILE = 0x02,
|
||||
CLEAR_FILES = 0x03,
|
||||
WRITE_FILE = 0x04,
|
||||
READ_FILE = 0x05,
|
||||
MODIFY_SECU = 0x06
|
||||
};
|
||||
|
||||
enum effect : uint32_t {
|
||||
SAVANNA = 0x04970400,
|
||||
SUNRISE = 0x01c10a00,
|
||||
UNICORNS = 0x049a0e00,
|
||||
PENSIEVE = 0x04c40600,
|
||||
THE_CIRCUS = 0x04810130,
|
||||
INSTASHARE = 0x03bc0190,
|
||||
EIGTHIES = 0x049a0000,
|
||||
CHERRY_BLOS = 0x04940800,
|
||||
RAINBOW = 0x05bd0690,
|
||||
TEST = 0x03af0af0,
|
||||
CHRISTMAS = 0x068b0900
|
||||
};
|
||||
|
||||
enum verbs {
|
||||
GET = 0x03,
|
||||
SET = 0x04,
|
||||
SETEEPROM = 0x07,
|
||||
SETVAR = 0x0b
|
||||
};
|
||||
|
||||
enum commandTypes {
|
||||
STATE_OFF = 0x80,
|
||||
STATE_ON = 0x81,
|
||||
BRIGTHNESS = 0xCF,
|
||||
SETCOLOR = 0xFF
|
||||
};
|
||||
|
||||
enum idxTypes {
|
||||
BRIGTHNESS_CONTROL = 0x01,
|
||||
COLOR_CONTROL = 0x02,
|
||||
COLOR_DIRECT_CONTROL = 0x81,
|
||||
READ_INFO_FROM_STORAGE = 0x86
|
||||
};
|
||||
|
||||
enum bufferMode {
|
||||
MONOCROME = 0x01,
|
||||
LIGHTBEAD = 0x02,
|
||||
};
|
||||
|
||||
enum ledLayout {
|
||||
STRIP_LAYOUT,
|
||||
MODLUE_LAYOUT
|
||||
};
|
||||
|
||||
enum modelType {
|
||||
STRIP,
|
||||
PLUS
|
||||
};
|
||||
|
||||
const uint8_t PACKET_HEADER[] =
|
||||
{
|
||||
'S', 'Z', // Tag "SZ"
|
||||
0x30, 0x30, // Version "00"
|
||||
0x00, 0x00, // AppID, 0x0000 = TL1 command mode
|
||||
0x00, 0x00, 0x00, 0x00 // Size
|
||||
};
|
||||
|
||||
const uint8_t PACKET_SECU[] =
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, // Dict
|
||||
0x00, 0x00, 0x00, 0x00, // Sum
|
||||
0x00, 0x00, 0x00, 0x00, // Salt
|
||||
0x00, 0x00, 0x00, 0x00 // SN
|
||||
};
|
||||
|
||||
const uint8_t TL1_CMD_FIXED_PART[] =
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, // DISTID
|
||||
0x00, 0x00, 0x00, 0x00, // SRCID
|
||||
0x00, // SECU
|
||||
0x00, // VERB
|
||||
0x00, // CTAG
|
||||
0x00 // LENGTH
|
||||
};
|
||||
|
||||
///
|
||||
/// Implementation of a Cololight LedDevice
|
||||
///
|
||||
class LedDeviceCololight : public ProviderUdp
|
||||
{
|
||||
public:
|
||||
|
||||
///
|
||||
/// @brief Constructs a Cololight LED-device
|
||||
///
|
||||
/// @param deviceConfig Device's configuration as JSON-Object
|
||||
///
|
||||
explicit LedDeviceCololight(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);
|
||||
|
||||
///
|
||||
/// @brief Discover Cololight devices available (for configuration).
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Get a Cololight device's resource properties
|
||||
///
|
||||
/// Following parameters are required
|
||||
/// @code
|
||||
/// {
|
||||
/// "host" : "hostname or IP",
|
||||
/// }
|
||||
///@endcode
|
||||
///
|
||||
/// @param[in] params Parameters to query device
|
||||
/// @return A JSON structure holding the device's properties
|
||||
///
|
||||
QJsonObject getProperties(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Send an update to the Cololight device to identify it.
|
||||
///
|
||||
/// Following parameters are required
|
||||
/// @code
|
||||
/// {
|
||||
/// "host" : "hostname or IP",
|
||||
/// }
|
||||
///@endcode
|
||||
///
|
||||
/// @param[in] params Parameters to address device
|
||||
///
|
||||
void identify(const QJsonObject& params) override;
|
||||
|
||||
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 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;
|
||||
|
||||
///
|
||||
/// @brief Power-/turn on the Cololight device.
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool powerOn() override;
|
||||
|
||||
///
|
||||
/// @brief Power-/turn off the Cololight device.
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool powerOff() override;
|
||||
|
||||
private:
|
||||
|
||||
bool initLedsConfiguration();
|
||||
void initDirectColorCmdTemplate();
|
||||
|
||||
///
|
||||
/// @brief Read additional information from Cololight
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool getInfo();
|
||||
|
||||
///
|
||||
/// @brief Set a Cololight effect
|
||||
///
|
||||
/// @param[in] effect from effect list
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setEffect(const effect effect);
|
||||
|
||||
///
|
||||
/// @brief Set a color
|
||||
///
|
||||
/// @param[in] color in RGB
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setColor(const ColorRgb colorRgb);
|
||||
|
||||
///
|
||||
/// @brief Set a color (or effect)
|
||||
///
|
||||
/// @param[in] color in four bytes (red, green, blue, mode)
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setColor(const uint32_t color);
|
||||
|
||||
///
|
||||
/// @brief Set colors per LED as per given list
|
||||
///
|
||||
/// @param[in] list of color per LED
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setColor(const std::vector<ColorRgb>& ledValues);
|
||||
|
||||
///
|
||||
/// @brief Set the Cololight device in TL1 command mode
|
||||
///
|
||||
/// @param[in] isOn, Enable TL1 command mode = true
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setTL1CommandMode(bool isOn);
|
||||
|
||||
///
|
||||
/// @brief Set the Cololight device's state (on/off) in TL1 mode
|
||||
///
|
||||
/// @param[in] isOn, on=true
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setState(bool isOn);
|
||||
|
||||
///
|
||||
/// @brief Set the Cololight device's state (on/off) in Direct Mode
|
||||
///
|
||||
/// @param[in] isOn, on=true
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool setStateDirect(bool isOn);
|
||||
|
||||
///
|
||||
/// @brief Send a request to the Cololight device for execution
|
||||
///
|
||||
/// @param[in] appID
|
||||
/// @param[in] command
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool sendRequest(const appID appID, const QByteArray& command);
|
||||
|
||||
///
|
||||
/// @brief Read response for a send request
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool readResponse();
|
||||
|
||||
///
|
||||
/// @brief Read response for a send request
|
||||
///
|
||||
/// @param[out] response
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
bool readResponse(QByteArray& response);
|
||||
|
||||
// Cololight model, e.g. CololightPlus, CololightStrip
|
||||
int _modelType;
|
||||
|
||||
// Defines how Cololight LED are organised (multiple light beads in a module or individual lights on a strip
|
||||
int _ledLayoutType;
|
||||
|
||||
// Count of overall LEDs across all modules
|
||||
int _ledBeadCount;
|
||||
|
||||
// Distance (in #modules) of the module farest away from the main controller
|
||||
int _distance;
|
||||
|
||||
QByteArray _packetFixPart;
|
||||
QByteArray _DataPart;
|
||||
|
||||
QByteArray _directColorCommandTemplate;
|
||||
|
||||
quint32 _sequenceNumber;
|
||||
|
||||
//Cololights discovered and their response message details
|
||||
QMultiMap<QString, QMap <QString, QString>> _services;
|
||||
};
|
||||
|
||||
#endif // LEDEVICECOLOLIGHT_H
|
@@ -1,5 +1,9 @@
|
||||
#include "LedDeviceFadeCandy.h"
|
||||
|
||||
#include <QtEndian>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#ssize-t
|
||||
#if defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
@@ -8,22 +12,22 @@ typedef SSIZE_T ssize_t;
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
constexpr std::chrono::milliseconds CONNECT_TIMEOUT{1000};
|
||||
|
||||
const signed MAX_NUM_LEDS = 10000; // OPC can handle 21845 LEDs - in theory, fadecandy device should handle 10000 LEDs
|
||||
const unsigned OPC_SET_PIXELS = 0; // OPC command codes
|
||||
const unsigned OPC_SYS_EX = 255; // OPC command codes
|
||||
const unsigned OPC_HEADER_SIZE = 4; // OPC header size
|
||||
|
||||
const int MAX_NUM_LEDS = 10000; // OPC can handle 21845 LEDs - in theory, fadecandy device should handle 10000 LEDs
|
||||
const int OPC_SET_PIXELS = 0; // OPC command codes
|
||||
const int OPC_SYS_EX = 255; // OPC command codes
|
||||
const int OPC_HEADER_SIZE = 4; // OPC header size
|
||||
} //End of constants
|
||||
|
||||
// TCP elements
|
||||
const quint16 STREAM_DEFAULT_PORT = 7890;
|
||||
const int STREAM_DEFAULT_PORT = 7890;
|
||||
|
||||
LedDeviceFadeCandy::LedDeviceFadeCandy(const QJsonObject &deviceConfig)
|
||||
LedDeviceFadeCandy::LedDeviceFadeCandy(const QJsonObject& deviceConfig)
|
||||
: LedDevice(deviceConfig)
|
||||
, _client(nullptr)
|
||||
,_host()
|
||||
,_port(STREAM_DEFAULT_PORT)
|
||||
, _host()
|
||||
, _port(STREAM_DEFAULT_PORT)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -32,20 +36,20 @@ LedDeviceFadeCandy::~LedDeviceFadeCandy()
|
||||
delete _client;
|
||||
}
|
||||
|
||||
LedDevice* LedDeviceFadeCandy::construct(const QJsonObject &deviceConfig)
|
||||
LedDevice* LedDeviceFadeCandy::construct(const QJsonObject& deviceConfig)
|
||||
{
|
||||
return new LedDeviceFadeCandy(deviceConfig);
|
||||
}
|
||||
|
||||
bool LedDeviceFadeCandy::init(const QJsonObject &deviceConfig)
|
||||
bool LedDeviceFadeCandy::init(const QJsonObject& deviceConfig)
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
if (LedDevice::init(deviceConfig))
|
||||
{
|
||||
if (getLedCount() > MAX_NUM_LEDS)
|
||||
{
|
||||
QString errortext = QString ("More LED configured than allowed (%1)").arg(MAX_NUM_LEDS);
|
||||
QString errortext = QString("More LED configured than allowed (%1)").arg(MAX_NUM_LEDS);
|
||||
this->setInError(errortext);
|
||||
isInitOK = false;
|
||||
}
|
||||
@@ -55,18 +59,18 @@ bool LedDeviceFadeCandy::init(const QJsonObject &deviceConfig)
|
||||
_port = deviceConfig["port"].toInt(STREAM_DEFAULT_PORT);
|
||||
|
||||
//If host not configured the init fails
|
||||
if ( _host.isEmpty() )
|
||||
if (_host.isEmpty())
|
||||
{
|
||||
this->setInError("No target hostname nor IP defined");
|
||||
}
|
||||
else
|
||||
{
|
||||
_channel = deviceConfig["channel"].toInt(0);
|
||||
_gamma = deviceConfig["gamma"].toDouble(1.0);
|
||||
_noDither = ! deviceConfig["dither"].toBool(false);
|
||||
_noInterp = ! deviceConfig["interpolation"].toBool(false);
|
||||
_manualLED = deviceConfig["manualLed"].toBool(false);
|
||||
_ledOnOff = deviceConfig["ledOn"].toBool(false);
|
||||
_channel = deviceConfig["channel"].toInt(0);
|
||||
_gamma = deviceConfig["gamma"].toDouble(1.0);
|
||||
_noDither = !deviceConfig["dither"].toBool(false);
|
||||
_noInterp = !deviceConfig["interpolation"].toBool(false);
|
||||
_manualLED = deviceConfig["manualLed"].toBool(false);
|
||||
_ledOnOff = deviceConfig["ledOn"].toBool(false);
|
||||
_setFcConfig = deviceConfig["setFcConfig"].toBool(false);
|
||||
|
||||
_whitePoint_r = 1.0;
|
||||
@@ -74,20 +78,19 @@ bool LedDeviceFadeCandy::init(const QJsonObject &deviceConfig)
|
||||
_whitePoint_b = 1.0;
|
||||
|
||||
const QJsonArray whitePointConfig = deviceConfig["whitePoint"].toArray();
|
||||
if ( !whitePointConfig.isEmpty() && whitePointConfig.size() == 3 )
|
||||
if (!whitePointConfig.isEmpty() && whitePointConfig.size() == 3)
|
||||
{
|
||||
_whitePoint_r = whitePointConfig[0].toDouble() / 255.0;
|
||||
_whitePoint_g = whitePointConfig[1].toDouble() / 255.0;
|
||||
_whitePoint_b = whitePointConfig[2].toDouble() / 255.0;
|
||||
}
|
||||
|
||||
_opc_data.resize( _ledRGBCount + OPC_HEADER_SIZE );
|
||||
_opc_data[0] = _channel;
|
||||
_opc_data.resize(static_cast<int>(_ledRGBCount) + OPC_HEADER_SIZE);
|
||||
_opc_data[0] = static_cast<char>(_channel);
|
||||
_opc_data[1] = OPC_SET_PIXELS;
|
||||
_opc_data[2] = _ledRGBCount >> 8;
|
||||
_opc_data[3] = _ledRGBCount & 0xff;
|
||||
qToBigEndian<quint16>(static_cast<quint16>(_ledRGBCount), _opc_data.data() + 2);
|
||||
|
||||
if ( initNetwork() )
|
||||
if (initNetwork())
|
||||
{
|
||||
isInitOK = true;
|
||||
}
|
||||
@@ -101,7 +104,7 @@ bool LedDeviceFadeCandy::initNetwork()
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
if ( _client == nullptr )
|
||||
if (_client == nullptr)
|
||||
{
|
||||
_client = new QTcpSocket(this);
|
||||
isInitOK = true;
|
||||
@@ -116,10 +119,10 @@ int LedDeviceFadeCandy::open()
|
||||
_isDeviceReady = false;
|
||||
|
||||
// Try to open the LedDevice
|
||||
if ( !tryConnect() )
|
||||
if (!tryConnect())
|
||||
{
|
||||
errortext = QString ("Failed to open device.");
|
||||
this->setInError( errortext );
|
||||
errortext = QString("Failed to open device.");
|
||||
this->setInError(errortext);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -136,7 +139,7 @@ int LedDeviceFadeCandy::close()
|
||||
_isDeviceReady = false;
|
||||
|
||||
// LedDevice specific closing activities
|
||||
if ( _client != nullptr )
|
||||
if (_client != nullptr)
|
||||
{
|
||||
_client->close();
|
||||
// Everything is OK -> device is closed
|
||||
@@ -147,7 +150,7 @@ int LedDeviceFadeCandy::close()
|
||||
bool LedDeviceFadeCandy::isConnected() const
|
||||
{
|
||||
bool connected = false;
|
||||
if ( _client != nullptr )
|
||||
if (_client != nullptr)
|
||||
{
|
||||
connected = _client->state() == QAbstractSocket::ConnectedState;
|
||||
}
|
||||
@@ -156,13 +159,13 @@ bool LedDeviceFadeCandy::isConnected() const
|
||||
|
||||
bool LedDeviceFadeCandy::tryConnect()
|
||||
{
|
||||
if ( _client != nullptr )
|
||||
if (_client != nullptr)
|
||||
{
|
||||
if ( _client->state() == QAbstractSocket::UnconnectedState ) {
|
||||
_client->connectToHost( _host, _port);
|
||||
if ( _client->waitForConnected(1000) )
|
||||
if (_client->state() == QAbstractSocket::UnconnectedState) {
|
||||
_client->connectToHost(_host, static_cast<quint16>(_port));
|
||||
if (_client->waitForConnected(CONNECT_TIMEOUT.count()))
|
||||
{
|
||||
Info(_log,"fadecandy/opc: connected to %s:%i on channel %i", QSTRING_CSTR(_host), _port, _channel);
|
||||
Info(_log, "fadecandy/opc: connected to %s:%d on channel %d", QSTRING_CSTR(_host), _port, _channel);
|
||||
if (_setFcConfig)
|
||||
{
|
||||
sendFadeCandyConfiguration();
|
||||
@@ -173,50 +176,48 @@ bool LedDeviceFadeCandy::tryConnect()
|
||||
return isConnected();
|
||||
}
|
||||
|
||||
int LedDeviceFadeCandy::write( const std::vector<ColorRgb> & ledValues )
|
||||
int LedDeviceFadeCandy::write(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
uint idx = OPC_HEADER_SIZE;
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
_opc_data[idx ] = unsigned( color.red );
|
||||
_opc_data[idx+1] = unsigned( color.green );
|
||||
_opc_data[idx+2] = unsigned( color.blue );
|
||||
_opc_data[idx] = static_cast<char>(color.red);
|
||||
_opc_data[idx + 1] = static_cast<char>(color.green);
|
||||
_opc_data[idx + 2] = static_cast<char>(color.blue);
|
||||
idx += 3;
|
||||
}
|
||||
|
||||
int retval = transferData()<0 ? -1 : 0;
|
||||
int retval = transferData() < 0 ? -1 : 0;
|
||||
return retval;
|
||||
}
|
||||
|
||||
int LedDeviceFadeCandy::transferData()
|
||||
qint64 LedDeviceFadeCandy::transferData()
|
||||
{
|
||||
if ( isConnected() || tryConnect() )
|
||||
if (isConnected() || tryConnect())
|
||||
{
|
||||
return _client->write( _opc_data, _opc_data.size() );
|
||||
return _client->write(_opc_data);
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
int LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, const QByteArray& msg)
|
||||
qint64 LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, const QByteArray& msg)
|
||||
{
|
||||
if ( isConnected() )
|
||||
if (isConnected())
|
||||
{
|
||||
QByteArray sysExData;
|
||||
uint data_size = msg.size() + 4;
|
||||
sysExData.resize( 4 + OPC_HEADER_SIZE );
|
||||
int data_size = msg.size() + 4;
|
||||
sysExData.resize(4 + OPC_HEADER_SIZE);
|
||||
|
||||
sysExData[0] = 0;
|
||||
sysExData[1] = OPC_SYS_EX;
|
||||
sysExData[2] = data_size >>8;
|
||||
sysExData[3] = data_size &0xff;
|
||||
sysExData[4] = systemId >>8;
|
||||
sysExData[5] = systemId &0xff;
|
||||
sysExData[6] = commandId >>8;
|
||||
sysExData[7] = commandId &0xff;
|
||||
sysExData[1] = static_cast<char>(OPC_SYS_EX);
|
||||
|
||||
qToBigEndian<quint16>(static_cast<quint16>(data_size), sysExData.data() + 2);
|
||||
qToBigEndian<quint16>(static_cast<quint16>(systemId), sysExData.data() + 4);
|
||||
qToBigEndian<quint16>(static_cast<quint16>(commandId), sysExData.data() + 6);
|
||||
|
||||
sysExData += msg;
|
||||
|
||||
return _client->write( sysExData, sysExData.size() );
|
||||
return _client->write(sysExData, sysExData.size());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -224,9 +225,9 @@ int LedDeviceFadeCandy::sendSysEx(uint8_t systemId, uint8_t commandId, const QBy
|
||||
void LedDeviceFadeCandy::sendFadeCandyConfiguration()
|
||||
{
|
||||
Debug(_log, "send configuration to fadecandy");
|
||||
QString data = "{\"gamma\": "+QString::number(_gamma,'g',4)+", \"whitepoint\": ["+QString::number(_whitePoint_r,'g',4)+", "+QString::number(_whitePoint_g,'g',4)+", "+QString::number(_whitePoint_b,'g',4)+"]}";
|
||||
sendSysEx(1, 1, data.toLocal8Bit() );
|
||||
QString data = "{\"gamma\": " + QString::number(_gamma, 'g', 4) + ", \"whitepoint\": [" + QString::number(_whitePoint_r, 'g', 4) + ", " + QString::number(_whitePoint_g, 'g', 4) + ", " + QString::number(_whitePoint_b, 'g', 4) + "]}";
|
||||
sendSysEx(1, 1, data.toLocal8Bit());
|
||||
|
||||
char firmware_data = ((uint8_t)_noDither | ((uint8_t)_noInterp << 1) | ((uint8_t)_manualLED << 2) | ((uint8_t)_ledOnOff << 3) );
|
||||
sendSysEx(1, 2, QByteArray(1,firmware_data) );
|
||||
char firmware_data = static_cast<char>(static_cast<uint8_t>(_noDither) | (static_cast<uint8_t>(_noInterp) << 1) | (static_cast<uint8_t>(_manualLED) << 2) | (static_cast<uint8_t>(_ledOnOff) << 3));
|
||||
sendSysEx(1, 2, QByteArray(1, firmware_data));
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ public:
|
||||
///
|
||||
/// @param deviceConfig Device's configuration as JSON-Object
|
||||
///
|
||||
explicit LedDeviceFadeCandy(const QJsonObject &deviceConfig);
|
||||
explicit LedDeviceFadeCandy(const QJsonObject& deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Destructor of the LedDevice
|
||||
@@ -52,7 +52,7 @@ public:
|
||||
///
|
||||
/// @param[in] deviceConfig Device's configuration as JSON-Object
|
||||
/// @return LedDevice constructed
|
||||
static LedDevice* construct(const QJsonObject &deviceConfig);
|
||||
static LedDevice* construct(const QJsonObject& deviceConfig);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -62,7 +62,7 @@ protected:
|
||||
/// @param[in] deviceConfig the JSON device configuration
|
||||
/// @return True, if success
|
||||
///
|
||||
bool init(const QJsonObject &deviceConfig) override;
|
||||
bool init(const QJsonObject& deviceConfig) override;
|
||||
|
||||
///
|
||||
/// @brief Opens the output device.
|
||||
@@ -84,7 +84,7 @@ protected:
|
||||
/// @param[in] ledValues The RGB-color per LED
|
||||
/// @return Zero on success, else negative
|
||||
///
|
||||
int write(const std::vector<ColorRgb> & ledValues) override;
|
||||
int write(const std::vector<ColorRgb>& ledValues) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -113,7 +113,7 @@ private:
|
||||
///
|
||||
/// @return amount of transferred bytes. -1 error while transferring, -2 error while connecting
|
||||
///
|
||||
int transferData();
|
||||
qint64 transferData();
|
||||
|
||||
///
|
||||
/// @brief Send system exclusive commands
|
||||
@@ -122,7 +122,7 @@ private:
|
||||
/// @param[in] commandId id of command
|
||||
/// @param[in] msg the sysEx message
|
||||
/// @return amount bytes written, -1 if failed
|
||||
int sendSysEx(uint8_t systemId, uint8_t commandId, const QByteArray& msg);
|
||||
qint64 sendSysEx(uint8_t systemId, uint8_t commandId, const QByteArray& msg);
|
||||
|
||||
///
|
||||
/// @brief Sends the configuration to fadecandy cserver
|
||||
@@ -131,8 +131,8 @@ private:
|
||||
|
||||
QTcpSocket* _client;
|
||||
QString _host;
|
||||
uint16_t _port;
|
||||
unsigned _channel;
|
||||
int _port;
|
||||
int _channel;
|
||||
QByteArray _opc_data;
|
||||
|
||||
// fadecandy sysEx
|
||||
@@ -145,7 +145,6 @@ private:
|
||||
bool _noInterp;
|
||||
bool _manualLED;
|
||||
bool _ledOnOff;
|
||||
|
||||
};
|
||||
|
||||
#endif // LEDEVICEFADECANDY_H
|
||||
|
@@ -7,6 +7,7 @@
|
||||
// Qt includes
|
||||
#include <QEventLoop>
|
||||
#include <QNetworkReply>
|
||||
#include <QtEndian>
|
||||
|
||||
//std includes
|
||||
#include <sstream>
|
||||
@@ -14,18 +15,17 @@
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
|
||||
const bool verbose = false;
|
||||
const bool verbose = false;
|
||||
const bool verbose3 = false;
|
||||
|
||||
// Configuration settings
|
||||
const char CONFIG_ADDRESS[] = "host";
|
||||
//const char CONFIG_PORT[] = "port";
|
||||
const char CONFIG_AUTH_TOKEN[] ="token";
|
||||
const char CONFIG_AUTH_TOKEN[] = "token";
|
||||
|
||||
const char CONFIG_PANEL_ORDER_TOP_DOWN[] ="panelOrderTopDown";
|
||||
const char CONFIG_PANEL_ORDER_LEFT_RIGHT[] ="panelOrderLeftRight";
|
||||
const char CONFIG_PANEL_START_POS[] ="panelStartPos";
|
||||
const char CONFIG_PANEL_ORDER_TOP_DOWN[] = "panelOrderTopDown";
|
||||
const char CONFIG_PANEL_ORDER_LEFT_RIGHT[] = "panelOrderLeftRight";
|
||||
const char CONFIG_PANEL_START_POS[] = "panelStartPos";
|
||||
|
||||
// Panel configuration settings
|
||||
const char PANEL_LAYOUT[] = "layout";
|
||||
@@ -61,16 +61,19 @@ const char API_BASE_PATH[] = "/api/v1/%1/";
|
||||
const char API_ROOT[] = "";
|
||||
//const char API_EXT_MODE_STRING_V1[] = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\"}}";
|
||||
const char API_EXT_MODE_STRING_V2[] = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\", \"extControlVersion\" : \"v2\"}}";
|
||||
const char API_STATE[] ="state";
|
||||
const char API_STATE[] = "state";
|
||||
const char API_PANELLAYOUT[] = "panelLayout";
|
||||
const char API_EFFECT[] = "effects";
|
||||
|
||||
//Nanoleaf Control data stream
|
||||
const int STREAM_FRAME_PANEL_NUM_SIZE = 2;
|
||||
const int STREAM_FRAME_PANEL_INFO_SIZE = 8;
|
||||
|
||||
// Nanoleaf ssdp services
|
||||
const char SSDP_ID[] = "ssdp:all";
|
||||
const char SSDP_FILTER_HEADER[] = "ST";
|
||||
const char SSDP_CANVAS[] = "nanoleaf:nl29";
|
||||
const char SSDP_LIGHTPANELS[] = "nanoleaf_aurora:light";
|
||||
|
||||
} //End of constants
|
||||
|
||||
// Nanoleaf Panel Shapetypes
|
||||
@@ -81,7 +84,7 @@ enum SHAPETYPES {
|
||||
CONTROL_SQUARE_PRIMARY,
|
||||
CONTROL_SQUARE_PASSIVE,
|
||||
POWER_SUPPLY,
|
||||
};
|
||||
};
|
||||
|
||||
// Nanoleaf external control versions
|
||||
enum EXTCONTROLVERSIONS {
|
||||
@@ -89,20 +92,20 @@ enum EXTCONTROLVERSIONS {
|
||||
EXTCTRLVER_V2
|
||||
};
|
||||
|
||||
LedDeviceNanoleaf::LedDeviceNanoleaf(const QJsonObject &deviceConfig)
|
||||
LedDeviceNanoleaf::LedDeviceNanoleaf(const QJsonObject& deviceConfig)
|
||||
: ProviderUdp(deviceConfig)
|
||||
,_restApi(nullptr)
|
||||
,_apiPort(API_DEFAULT_PORT)
|
||||
,_topDown(true)
|
||||
,_leftRight(true)
|
||||
,_startPos(0)
|
||||
,_endPos(0)
|
||||
,_extControlVersion (EXTCTRLVER_V2),
|
||||
, _restApi(nullptr)
|
||||
, _apiPort(API_DEFAULT_PORT)
|
||||
, _topDown(true)
|
||||
, _leftRight(true)
|
||||
, _startPos(0)
|
||||
, _endPos(0)
|
||||
, _extControlVersion(EXTCTRLVER_V2),
|
||||
_panelLedCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
LedDevice* LedDeviceNanoleaf::construct(const QJsonObject &deviceConfig)
|
||||
LedDevice* LedDeviceNanoleaf::construct(const QJsonObject& deviceConfig)
|
||||
{
|
||||
return new LedDeviceNanoleaf(deviceConfig);
|
||||
}
|
||||
@@ -113,7 +116,7 @@ LedDeviceNanoleaf::~LedDeviceNanoleaf()
|
||||
_restApi = nullptr;
|
||||
}
|
||||
|
||||
bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig)
|
||||
bool LedDeviceNanoleaf::init(const QJsonObject& deviceConfig)
|
||||
{
|
||||
// Overwrite non supported/required features
|
||||
setLatchTime(0);
|
||||
@@ -121,69 +124,69 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig)
|
||||
|
||||
if (deviceConfig["rewriteTime"].toInt(0) > 0)
|
||||
{
|
||||
Info (_log, "Device Nanoleaf does not require rewrites. Refresh time is ignored.");
|
||||
Info(_log, "Device Nanoleaf does not require rewrites. Refresh time is ignored.");
|
||||
}
|
||||
|
||||
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
bool isInitOK = false;
|
||||
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
if (LedDevice::init(deviceConfig))
|
||||
{
|
||||
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() ));
|
||||
int configuredLedCount = this->getLedCount();
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR(this->getActiveDeviceType()));
|
||||
Debug(_log, "LedCount : %d", configuredLedCount);
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR(this->getColorOrder()));
|
||||
Debug(_log, "RewriteTime : %d", this->getRewriteTime());
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
|
||||
// Read panel organisation configuration
|
||||
if ( deviceConfig[ CONFIG_PANEL_ORDER_TOP_DOWN ].isString() )
|
||||
if (deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].isString())
|
||||
{
|
||||
_topDown = deviceConfig[ CONFIG_PANEL_ORDER_TOP_DOWN ].toString().toInt() == 0;
|
||||
_topDown = deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].toString().toInt() == 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_topDown = deviceConfig[ CONFIG_PANEL_ORDER_TOP_DOWN ].toInt() == 0;
|
||||
_topDown = deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].toInt() == 0;
|
||||
}
|
||||
|
||||
if ( deviceConfig[ CONFIG_PANEL_ORDER_LEFT_RIGHT ].isString() )
|
||||
if (deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].isString())
|
||||
{
|
||||
_leftRight = deviceConfig[ CONFIG_PANEL_ORDER_LEFT_RIGHT ].toString().toInt() == 0;
|
||||
_leftRight = deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].toString().toInt() == 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_leftRight = deviceConfig[ CONFIG_PANEL_ORDER_LEFT_RIGHT ].toInt() == 0;
|
||||
_leftRight = deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].toInt() == 0;
|
||||
}
|
||||
|
||||
_startPos = static_cast<uint>( deviceConfig[ CONFIG_PANEL_START_POS ].toInt(0) );
|
||||
_startPos = deviceConfig[CONFIG_PANEL_START_POS].toInt(0);
|
||||
|
||||
// TODO: Allow to handle port dynamically
|
||||
|
||||
//Set hostname as per configuration and_defaultHost default port
|
||||
_hostname = deviceConfig[ CONFIG_ADDRESS ].toString();
|
||||
_apiPort = API_DEFAULT_PORT;
|
||||
_authToken = deviceConfig[ CONFIG_AUTH_TOKEN ].toString();
|
||||
_hostname = deviceConfig[CONFIG_ADDRESS].toString();
|
||||
_apiPort = API_DEFAULT_PORT;
|
||||
_authToken = deviceConfig[CONFIG_AUTH_TOKEN].toString();
|
||||
|
||||
//If host not configured the init failed
|
||||
if ( _hostname.isEmpty() )
|
||||
if (_hostname.isEmpty())
|
||||
{
|
||||
this->setInError("No target hostname nor IP defined");
|
||||
isInitOK = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( initRestAPI( _hostname, _apiPort, _authToken ) )
|
||||
if (initRestAPI(_hostname, _apiPort, _authToken))
|
||||
{
|
||||
// Read LedDevice configuration and validate against device configuration
|
||||
if ( initLedsConfiguration() )
|
||||
if (initLedsConfiguration())
|
||||
{
|
||||
// Set UDP streaming host and port
|
||||
_devConfig["host"] = _hostname;
|
||||
_devConfig["port"] = STREAM_CONTROL_DEFAULT_PORT;
|
||||
|
||||
isInitOK = ProviderUdp::init(_devConfig);
|
||||
Debug(_log, "Hostname/IP : %s", QSTRING_CSTR( _hostname ));
|
||||
Debug(_log, "Hostname/IP : %s", QSTRING_CSTR(_hostname));
|
||||
Debug(_log, "Port : %d", _port);
|
||||
}
|
||||
}
|
||||
@@ -201,9 +204,9 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
// Read Panel count and panel Ids
|
||||
_restApi->setPath(API_ROOT);
|
||||
httpResponse response = _restApi->get();
|
||||
if ( response.error() )
|
||||
if (response.error())
|
||||
{
|
||||
this->setInError ( response.getErrorReason() );
|
||||
this->setInError(response.getErrorReason());
|
||||
isInitOK = false;
|
||||
}
|
||||
else
|
||||
@@ -215,35 +218,35 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
QString deviceManufacturer = jsonAllPanelInfo[DEV_DATA_MANUFACTURER].toString();
|
||||
_deviceFirmwareVersion = jsonAllPanelInfo[DEV_DATA_FIRMWAREVERSION].toString();
|
||||
|
||||
Debug(_log, "Name : %s", QSTRING_CSTR( deviceName ));
|
||||
Debug(_log, "Model : %s", QSTRING_CSTR( _deviceModel ));
|
||||
Debug(_log, "Manufacturer : %s", QSTRING_CSTR( deviceManufacturer ));
|
||||
Debug(_log, "FirmwareVersion: %s", QSTRING_CSTR( _deviceFirmwareVersion));
|
||||
Debug(_log, "Name : %s", QSTRING_CSTR(deviceName));
|
||||
Debug(_log, "Model : %s", QSTRING_CSTR(_deviceModel));
|
||||
Debug(_log, "Manufacturer : %s", QSTRING_CSTR(deviceManufacturer));
|
||||
Debug(_log, "FirmwareVersion: %s", QSTRING_CSTR(_deviceFirmwareVersion));
|
||||
|
||||
// Get panel details from /panelLayout/layout
|
||||
QJsonObject jsonPanelLayout = jsonAllPanelInfo[API_PANELLAYOUT].toObject();
|
||||
QJsonObject jsonLayout = jsonPanelLayout[PANEL_LAYOUT].toObject();
|
||||
|
||||
uint panelNum = static_cast<uint>(jsonLayout[PANEL_NUM].toInt());
|
||||
int panelNum = jsonLayout[PANEL_NUM].toInt();
|
||||
QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray();
|
||||
|
||||
std::map<uint, std::map<uint, uint>> panelMap;
|
||||
std::map<int, std::map<int, int>> panelMap;
|
||||
|
||||
// Loop over all children.
|
||||
for (const QJsonValue value : positionData)
|
||||
foreach(const QJsonValue & value, positionData)
|
||||
{
|
||||
QJsonObject panelObj = value.toObject();
|
||||
|
||||
uint panelId = static_cast<uint>(panelObj[PANEL_ID].toInt());
|
||||
uint panelX = static_cast<uint>(panelObj[PANEL_POS_X].toInt());
|
||||
uint panelY = static_cast<uint>(panelObj[PANEL_POS_Y].toInt());
|
||||
uint panelshapeType = static_cast<uint>(panelObj[PANEL_SHAPE_TYPE].toInt());
|
||||
//uint panelOrientation = static_cast<uint>(panelObj[PANEL_ORIENTATION].toInt());
|
||||
int panelId = panelObj[PANEL_ID].toInt();
|
||||
int panelX = panelObj[PANEL_POS_X].toInt();
|
||||
int panelY = panelObj[PANEL_POS_Y].toInt();
|
||||
int panelshapeType = panelObj[PANEL_SHAPE_TYPE].toInt();
|
||||
//int panelOrientation = panelObj[PANEL_ORIENTATION].toInt();
|
||||
|
||||
DebugIf(verbose, _log, "Panel [%u] (%u,%u) - Type: [%u]", panelId, panelX, panelY, panelshapeType );
|
||||
DebugIf(verbose, _log, "Panel [%d] (%d,%d) - Type: [%d]", panelId, panelX, panelY, panelshapeType);
|
||||
|
||||
// Skip Rhythm panels
|
||||
if ( panelshapeType != RHYTM )
|
||||
if (panelshapeType != RHYTM)
|
||||
{
|
||||
panelMap[panelY][panelX] = panelId;
|
||||
}
|
||||
@@ -254,16 +257,16 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
}
|
||||
|
||||
// Travers panels top down
|
||||
for(auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY)
|
||||
for (auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY)
|
||||
{
|
||||
// Sort panels left to right
|
||||
if ( _leftRight )
|
||||
if (_leftRight)
|
||||
{
|
||||
for( auto posX = posY->second.cbegin(); posX != posY->second.cend(); ++posX)
|
||||
for (auto posX = posY->second.cbegin(); posX != posY->second.cend(); ++posX)
|
||||
{
|
||||
DebugIf(verbose3, _log, "panelMap[%u][%u]=%u", posY->first, posX->first, posX->second );
|
||||
DebugIf(verbose3, _log, "panelMap[%d][%d]=%d", posY->first, posX->first, posX->second);
|
||||
|
||||
if ( _topDown )
|
||||
if (_topDown)
|
||||
{
|
||||
_panelIds.push_back(posX->second);
|
||||
}
|
||||
@@ -276,11 +279,11 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
else
|
||||
{
|
||||
// Sort panels right to left
|
||||
for( auto posX = posY->second.crbegin(); posX != posY->second.crend(); ++posX)
|
||||
for (auto posX = posY->second.crbegin(); posX != posY->second.crend(); ++posX)
|
||||
{
|
||||
DebugIf(verbose3, _log, "panelMap[%u][%u]=%u", posY->first, posX->first, posX->second );
|
||||
DebugIf(verbose3, _log, "panelMap[%d][%d]=%d", posY->first, posX->first, posX->second);
|
||||
|
||||
if ( _topDown )
|
||||
if (_topDown)
|
||||
{
|
||||
_panelIds.push_back(posX->second);
|
||||
}
|
||||
@@ -292,22 +295,22 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
}
|
||||
}
|
||||
|
||||
this->_panelLedCount = static_cast<uint>(_panelIds.size());
|
||||
_devConfig["hardwareLedCount"] = static_cast<int>(_panelLedCount);
|
||||
this->_panelLedCount = _panelIds.size();
|
||||
_devConfig["hardwareLedCount"] = _panelLedCount;
|
||||
|
||||
Debug(_log, "PanelsNum : %u", panelNum);
|
||||
Debug(_log, "PanelLedCount : %u", _panelLedCount);
|
||||
Debug(_log, "PanelsNum : %d", panelNum);
|
||||
Debug(_log, "PanelLedCount : %d", _panelLedCount);
|
||||
|
||||
// Check. if enough panels were found.
|
||||
uint configuredLedCount = this->getLedCount();
|
||||
int configuredLedCount = this->getLedCount();
|
||||
_endPos = _startPos + configuredLedCount - 1;
|
||||
|
||||
Debug(_log, "Sort Top>Down : %d", _topDown);
|
||||
Debug(_log, "Sort Left>Right: %d", _leftRight);
|
||||
Debug(_log, "Start Panel Pos: %u", _startPos);
|
||||
Debug(_log, "End Panel Pos : %u", _endPos);
|
||||
Debug(_log, "Start Panel Pos: %d", _startPos);
|
||||
Debug(_log, "End Panel Pos : %d", _endPos);
|
||||
|
||||
if (_panelLedCount < configuredLedCount )
|
||||
if (_panelLedCount < configuredLedCount)
|
||||
{
|
||||
QString errorReason = QString("Not enough panels [%1] for configured LEDs [%2] found!")
|
||||
.arg(_panelLedCount)
|
||||
@@ -317,16 +320,16 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _panelLedCount > this->getLedCount() )
|
||||
if (_panelLedCount > this->getLedCount())
|
||||
{
|
||||
Info(_log, "%s: More panels [%u] than configured LEDs [%u].", QSTRING_CSTR(this->getActiveDeviceType()), _panelLedCount, configuredLedCount );
|
||||
Info(_log, "%s: More panels [%d] than configured LEDs [%d].", QSTRING_CSTR(this->getActiveDeviceType()), _panelLedCount, configuredLedCount);
|
||||
}
|
||||
|
||||
// Check, if start position + number of configured LEDs is greater than number of panels available
|
||||
if ( _endPos >= _panelLedCount )
|
||||
if (_endPos >= _panelLedCount)
|
||||
{
|
||||
QString errorReason = QString("Start panel [%1] out of range. Start panel position can be max [%2] given [%3] panel available!")
|
||||
.arg(_startPos).arg(_panelLedCount-configuredLedCount).arg(_panelLedCount);
|
||||
.arg(_startPos).arg(_panelLedCount - configuredLedCount).arg(_panelLedCount);
|
||||
|
||||
this->setInError(errorReason);
|
||||
isInitOK = false;
|
||||
@@ -336,16 +339,16 @@ bool LedDeviceNanoleaf::initLedsConfiguration()
|
||||
return isInitOK;
|
||||
}
|
||||
|
||||
bool LedDeviceNanoleaf::initRestAPI(const QString &hostname, int port, const QString &token )
|
||||
bool LedDeviceNanoleaf::initRestAPI(const QString& hostname, int port, const QString& token)
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
if ( _restApi == nullptr )
|
||||
if (_restApi == nullptr)
|
||||
{
|
||||
_restApi = new ProviderRestApi(hostname, port );
|
||||
_restApi = new ProviderRestApi(hostname, port);
|
||||
|
||||
//Base-path is api-path + authentication token
|
||||
_restApi->setBasePath( QString(API_BASE_PATH).arg(token) );
|
||||
_restApi->setBasePath(QString(API_BASE_PATH).arg(token));
|
||||
|
||||
isInitOK = true;
|
||||
}
|
||||
@@ -360,13 +363,13 @@ int LedDeviceNanoleaf::open()
|
||||
QJsonDocument responseDoc = changeToExternalControlMode();
|
||||
// Resolve port for Light Panels
|
||||
QJsonObject jsonStreamControllInfo = responseDoc.object();
|
||||
if ( ! jsonStreamControllInfo.isEmpty() )
|
||||
if (!jsonStreamControllInfo.isEmpty())
|
||||
{
|
||||
//Set default streaming port
|
||||
_port = static_cast<uchar>(jsonStreamControllInfo[STREAM_CONTROL_PORT].toInt());
|
||||
}
|
||||
|
||||
if ( ProviderUdp::open() == 0 )
|
||||
if (ProviderUdp::open() == 0)
|
||||
{
|
||||
// Everything is OK, device is ready
|
||||
_isDeviceReady = true;
|
||||
@@ -375,10 +378,10 @@ int LedDeviceNanoleaf::open()
|
||||
return retval;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceNanoleaf::discover()
|
||||
QJsonObject LedDeviceNanoleaf::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
||||
|
||||
QJsonArray deviceList;
|
||||
|
||||
@@ -391,41 +394,41 @@ QJsonObject LedDeviceNanoleaf::discover()
|
||||
discover.setSearchFilter(searchTargetFilter, SSDP_FILTER_HEADER);
|
||||
QString searchTarget = SSDP_ID;
|
||||
|
||||
if ( discover.discoverServices(searchTarget) > 0 )
|
||||
if (discover.discoverServices(searchTarget) > 0)
|
||||
{
|
||||
deviceList = discover.getServicesDiscoveredJson();
|
||||
}
|
||||
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
Debug(_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceNanoleaf::getProperties(const QJsonObject& params)
|
||||
{
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
QJsonObject properties;
|
||||
|
||||
// Get Nanoleaf device properties
|
||||
QString host = params["host"].toString("");
|
||||
if ( !host.isEmpty() )
|
||||
if (!host.isEmpty())
|
||||
{
|
||||
QString authToken = params["token"].toString("");
|
||||
QString filter = params["filter"].toString("");
|
||||
|
||||
// Resolve hostname and port (or use default API port)
|
||||
QStringList addressparts = QStringUtils::split(host,":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
QStringList addressparts = QStringUtils::split(host, ":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
QString apiHost = addressparts[0];
|
||||
int apiPort;
|
||||
|
||||
if ( addressparts.size() > 1)
|
||||
if (addressparts.size() > 1)
|
||||
{
|
||||
apiPort = addressparts[1].toInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
apiPort = API_DEFAULT_PORT;
|
||||
apiPort = API_DEFAULT_PORT;
|
||||
}
|
||||
|
||||
initRestAPI(apiHost, apiPort, authToken);
|
||||
@@ -433,40 +436,39 @@ QJsonObject LedDeviceNanoleaf::getProperties(const QJsonObject& params)
|
||||
|
||||
// Perform request
|
||||
httpResponse response = _restApi->get();
|
||||
if ( response.error() )
|
||||
if (response.error())
|
||||
{
|
||||
Warning (_log, "%s get properties failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
||||
Warning(_log, "%s get properties failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
||||
}
|
||||
|
||||
properties.insert("properties", response.getBody().object());
|
||||
|
||||
Debug(_log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
|
||||
Debug(_log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
void LedDeviceNanoleaf::identify(const QJsonObject& params)
|
||||
{
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
QString host = params["host"].toString("");
|
||||
if ( !host.isEmpty() )
|
||||
if (!host.isEmpty())
|
||||
{
|
||||
QString authToken = params["token"].toString("");
|
||||
|
||||
// Resolve hostname and port (or use default API port)
|
||||
QStringList addressparts = QStringUtils::split(host,":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
QStringList addressparts = QStringUtils::split(host, ":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
||||
QString apiHost = addressparts[0];
|
||||
int apiPort;
|
||||
|
||||
if ( addressparts.size() > 1)
|
||||
if (addressparts.size() > 1)
|
||||
{
|
||||
apiPort = addressparts[1].toInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
apiPort = API_DEFAULT_PORT;
|
||||
apiPort = API_DEFAULT_PORT;
|
||||
}
|
||||
|
||||
initRestAPI(apiHost, apiPort, authToken);
|
||||
@@ -474,33 +476,33 @@ void LedDeviceNanoleaf::identify(const QJsonObject& params)
|
||||
|
||||
// Perform request
|
||||
httpResponse response = _restApi->put();
|
||||
if ( response.error() )
|
||||
if (response.error())
|
||||
{
|
||||
Warning (_log, "%s identification failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
||||
Warning(_log, "%s identification failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool LedDeviceNanoleaf::powerOn()
|
||||
{
|
||||
if ( _isDeviceReady)
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
changeToExternalControlMode();
|
||||
|
||||
//Power-on Nanoleaf device
|
||||
_restApi->setPath(API_STATE);
|
||||
_restApi->put( getOnOffRequest(true) );
|
||||
_restApi->put(getOnOffRequest(true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LedDeviceNanoleaf::powerOff()
|
||||
{
|
||||
if ( _isDeviceReady)
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
//Power-off the Nanoleaf device physically
|
||||
_restApi->setPath(API_STATE);
|
||||
_restApi->put( getOnOffRequest(false) );
|
||||
_restApi->put(getOnOffRequest(false));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -508,7 +510,7 @@ bool LedDeviceNanoleaf::powerOff()
|
||||
QString LedDeviceNanoleaf::getOnOffRequest(bool isOn) const
|
||||
{
|
||||
QString state = isOn ? STATE_VALUE_TRUE : STATE_VALUE_FALSE;
|
||||
return QString( "{\"%1\":{\"%2\":%3}}" ).arg(STATE_ON, STATE_ONOFF_VALUE, state);
|
||||
return QString("{\"%1\":{\"%2\":%3}}").arg(STATE_ON, STATE_ONOFF_VALUE, state);
|
||||
}
|
||||
|
||||
QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode()
|
||||
@@ -518,15 +520,14 @@ QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode()
|
||||
//Enable UDP Mode v2
|
||||
|
||||
_restApi->setPath(API_EFFECT);
|
||||
httpResponse response =_restApi->put(API_EXT_MODE_STRING_V2);
|
||||
httpResponse response = _restApi->put(API_EXT_MODE_STRING_V2);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
|
||||
int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
|
||||
int LedDeviceNanoleaf::write(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
int retVal = 0;
|
||||
uint udpBufferSize;
|
||||
|
||||
//
|
||||
// nPanels 2B
|
||||
@@ -537,35 +538,27 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
|
||||
//
|
||||
// Note: Nanoleaf Light Panels (Aurora) now support External Control V2 (tested with FW 3.2.0)
|
||||
|
||||
udpBufferSize = _panelLedCount * 8 + 2;
|
||||
std::vector<uint8_t> udpbuffer;
|
||||
int udpBufferSize = STREAM_FRAME_PANEL_NUM_SIZE + _panelLedCount * STREAM_FRAME_PANEL_INFO_SIZE;
|
||||
|
||||
QByteArray udpbuffer;
|
||||
udpbuffer.resize(udpBufferSize);
|
||||
|
||||
uchar lowByte; // lower byte
|
||||
uchar highByte; // upper byte
|
||||
|
||||
uint i=0;
|
||||
int i = 0;
|
||||
|
||||
// Set number of panels
|
||||
highByte = static_cast<uchar>(_panelLedCount >>8 );
|
||||
lowByte = static_cast<uchar>(_panelLedCount & 0xFF);
|
||||
|
||||
udpbuffer[i++] = highByte;
|
||||
udpbuffer[i++] = lowByte;
|
||||
qToBigEndian<quint16>(static_cast<quint16>(_panelLedCount), udpbuffer.data() + i);
|
||||
i += 2;
|
||||
|
||||
ColorRgb color;
|
||||
|
||||
//Maintain LED counter independent from PanelCounter
|
||||
uint ledCounter = 0;
|
||||
for ( uint panelCounter=0; panelCounter < _panelLedCount; panelCounter++ )
|
||||
int ledCounter = 0;
|
||||
for (int panelCounter = 0; panelCounter < _panelLedCount; panelCounter++)
|
||||
{
|
||||
uint panelID = _panelIds[panelCounter];
|
||||
|
||||
highByte = static_cast<uchar>(panelID >>8 );
|
||||
lowByte = static_cast<uchar>(panelID & 0xFF);
|
||||
int panelID = _panelIds[panelCounter];
|
||||
|
||||
// Set panels configured
|
||||
if( panelCounter >= _startPos && panelCounter <= _endPos ) {
|
||||
if (panelCounter >= _startPos && panelCounter <= _endPos) {
|
||||
color = static_cast<ColorRgb>(ledValues.at(ledCounter));
|
||||
++ledCounter;
|
||||
}
|
||||
@@ -573,49 +566,35 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
|
||||
{
|
||||
// Set panels not configured to black;
|
||||
color = ColorRgb::BLACK;
|
||||
DebugIf(verbose3, _log, "[%u] >= panelLedCount [%u] => Set to BLACK", panelCounter, _panelLedCount );
|
||||
DebugIf(verbose3, _log, "[%d] >= panelLedCount [%d] => Set to BLACK", panelCounter, _panelLedCount);
|
||||
}
|
||||
|
||||
// Set panelID
|
||||
udpbuffer[i++] = highByte;
|
||||
udpbuffer[i++] = lowByte;
|
||||
qToBigEndian<quint16>(static_cast<quint16>(panelID), udpbuffer.data() + i);
|
||||
i += 2;
|
||||
|
||||
// Set panel's color LEDs
|
||||
udpbuffer[i++] = color.red;
|
||||
udpbuffer[i++] = color.green;
|
||||
udpbuffer[i++] = color.blue;
|
||||
udpbuffer[i++] = static_cast<char>(color.red);
|
||||
udpbuffer[i++] = static_cast<char>(color.green);
|
||||
udpbuffer[i++] = static_cast<char>(color.blue);
|
||||
|
||||
// Set white LED
|
||||
udpbuffer[i++] = 0; // W not set manually
|
||||
|
||||
// Set transition time
|
||||
unsigned char tranitionTime = 1; // currently fixed at value 1 which corresponds to 100ms
|
||||
qToBigEndian<quint16>(static_cast<quint16>(tranitionTime), udpbuffer.data() + i);
|
||||
i += 2;
|
||||
|
||||
highByte = static_cast<uchar>(tranitionTime >>8 );
|
||||
lowByte = static_cast<uchar>(tranitionTime & 0xFF);
|
||||
|
||||
udpbuffer[i++] = highByte;
|
||||
udpbuffer[i++] = lowByte;
|
||||
DebugIf(verbose3, _log, "[%u] Color: {%u,%u,%u}", panelCounter, color.red, color.green, color.blue );
|
||||
|
||||
DebugIf(verbose3, _log, "[%u] Color: {%u,%u,%u}", panelCounter, color.red, color.green, color.blue);
|
||||
}
|
||||
DebugIf(verbose3, _log, "UDP-Address [%s], UDP-Port [%u], udpBufferSize[%u], Bytes to send [%u]", QSTRING_CSTR(_address.toString()), _port, udpBufferSize, i);
|
||||
DebugIf(verbose3, _log, "[%s]", uint8_vector_to_hex_string(udpbuffer).c_str() );
|
||||
|
||||
retVal &= writeBytes( i , udpbuffer.data());
|
||||
DebugIf(verbose3, _log, "writeBytes(): [%d]",retVal);
|
||||
if (verbose3)
|
||||
{
|
||||
Debug(_log, "UDP-Address [%s], UDP-Port [%u], udpBufferSize[%d], Bytes to send [%d]", QSTRING_CSTR(_address.toString()), _port, udpBufferSize, i);
|
||||
Debug( _log, "packet: [%s]", QSTRING_CSTR(toHex(udpbuffer, 64)));
|
||||
}
|
||||
|
||||
retVal = writeBytes(udpbuffer);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
std::string LedDeviceNanoleaf::uint8_vector_to_hex_string(const std::vector<uint8_t>& buffer) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::setfill('0');
|
||||
std::vector<uint8_t>::const_iterator it;
|
||||
|
||||
for (it = buffer.begin(); it != buffer.end(); ++it)
|
||||
{
|
||||
ss << " " << std::setw(2) << static_cast<unsigned>(*it);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ public:
|
||||
///
|
||||
/// @param deviceConfig Device's configuration as JSON-Object
|
||||
///
|
||||
explicit LedDeviceNanoleaf(const QJsonObject &deviceConfig);
|
||||
explicit LedDeviceNanoleaf(const QJsonObject& deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Destructor of the LED-device
|
||||
@@ -44,14 +44,16 @@ public:
|
||||
///
|
||||
/// @param[in] deviceConfig Device's configuration as JSON-Object
|
||||
/// @return LedDevice constructed
|
||||
static LedDevice* construct(const QJsonObject &deviceConfig);
|
||||
static LedDevice* construct(const QJsonObject& deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Discover Nanoleaf devices available (for configuration).
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover() override;
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Get the Nanoleaf device's resource properties
|
||||
@@ -93,7 +95,7 @@ protected:
|
||||
/// @param[in] deviceConfig the JSON device configuration
|
||||
/// @return True, if success
|
||||
///
|
||||
bool init(const QJsonObject &deviceConfig) override;
|
||||
bool init(const QJsonObject& deviceConfig) override;
|
||||
|
||||
///
|
||||
/// @brief Opens the output device.
|
||||
@@ -108,7 +110,7 @@ protected:
|
||||
/// @param[in] ledValues The RGB-color per LED
|
||||
/// @return Zero on success, else negative
|
||||
//////
|
||||
int write(const std::vector<ColorRgb> & ledValues) override;
|
||||
int write(const std::vector<ColorRgb>& ledValues) override;
|
||||
|
||||
///
|
||||
/// @brief Power-/turn on the Nanoleaf device.
|
||||
@@ -135,7 +137,7 @@ private:
|
||||
///
|
||||
/// @return True, if success
|
||||
///
|
||||
bool initRestAPI(const QString &hostname, int port, const QString &token );
|
||||
bool initRestAPI(const QString& hostname, int port, const QString& token);
|
||||
|
||||
///
|
||||
/// @brief Get Nanoleaf device details and configuration
|
||||
@@ -157,14 +159,7 @@ private:
|
||||
/// @param isOn True, if to switch on device
|
||||
/// @return Command to switch device on/off
|
||||
///
|
||||
QString getOnOffRequest (bool isOn ) const;
|
||||
|
||||
///
|
||||
/// @brief Convert vector to hex string
|
||||
///
|
||||
/// @param uint8_t vector
|
||||
/// @return vector as string of hex values
|
||||
std::string uint8_vector_to_hex_string( const std::vector<uint8_t>& buffer ) const;
|
||||
QString getOnOffRequest(bool isOn) const;
|
||||
|
||||
///REST-API wrapper
|
||||
ProviderRestApi* _restApi;
|
||||
@@ -175,8 +170,8 @@ private:
|
||||
|
||||
bool _topDown;
|
||||
bool _leftRight;
|
||||
uint _startPos;
|
||||
uint _endPos;
|
||||
int _startPos;
|
||||
int _endPos;
|
||||
|
||||
//Nanoleaf device details
|
||||
QString _deviceModel;
|
||||
@@ -184,11 +179,10 @@ private:
|
||||
ushort _extControlVersion;
|
||||
|
||||
/// The number of panels with LEDs
|
||||
uint _panelLedCount;
|
||||
int _panelLedCount;
|
||||
|
||||
/// Array of the panel ids.
|
||||
QVector<uint> _panelIds;
|
||||
|
||||
QVector<int> _panelIds;
|
||||
};
|
||||
|
||||
#endif // LEDEVICENANOLEAF_H
|
||||
|
@@ -6,11 +6,11 @@
|
||||
|
||||
#include <chrono>
|
||||
|
||||
bool verbose = false;
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
|
||||
bool verbose = false;
|
||||
|
||||
// Configuration settings
|
||||
const char CONFIG_ADDRESS[] = "output";
|
||||
//const char CONFIG_PORT[] = "port";
|
||||
@@ -97,31 +97,6 @@ const int STREAM_SSL_HANDSHAKE_ATTEMPTS = 5;
|
||||
constexpr std::chrono::milliseconds STREAM_REWRITE_TIME{20};
|
||||
const int SSL_CIPHERSUITES[2] = { MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 0 };
|
||||
|
||||
//Streaming message header and payload definition
|
||||
const uint8_t HEADER[] =
|
||||
{
|
||||
'H', 'u', 'e', 'S', 't', 'r', 'e', 'a', 'm', //protocol
|
||||
0x01, 0x00, //version 1.0
|
||||
0x01, //sequence number 1
|
||||
0x00, 0x00, //Reserved write 0’s
|
||||
0x01, //xy Brightness
|
||||
0x00, // Reserved, write 0’s
|
||||
};
|
||||
|
||||
const uint8_t PAYLOAD_PER_LIGHT[] =
|
||||
{
|
||||
0x01, 0x00, 0x06, //light ID
|
||||
//color: 16 bpc
|
||||
0xff, 0xff,
|
||||
0xff, 0xff,
|
||||
0xff, 0xff,
|
||||
/*
|
||||
(message.R >> 8) & 0xff, message.R & 0xff,
|
||||
(message.G >> 8) & 0xff, message.G & 0xff,
|
||||
(message.B >> 8) & 0xff, message.B & 0xff
|
||||
*/
|
||||
};
|
||||
|
||||
} //End of constants
|
||||
|
||||
bool operator ==(const CiColor& p1, const CiColor& p2)
|
||||
@@ -301,7 +276,7 @@ bool LedDevicePhilipsHueBridge::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
|
||||
log( "DeviceType", "%s", QSTRING_CSTR( this->getActiveDeviceType() ) );
|
||||
log( "LedCount", "%u", this->getLedCount() );
|
||||
log( "LedCount", "%d", this->getLedCount() );
|
||||
log( "ColorOrder", "%s", QSTRING_CSTR( this->getColorOrder() ) );
|
||||
log( "RefreshTime", "%d", _refreshTimerInterval_ms );
|
||||
log( "LatchTime", "%d", this->getLatchTime() );
|
||||
@@ -313,7 +288,7 @@ bool LedDevicePhilipsHueBridge::init(const QJsonObject &deviceConfig)
|
||||
if ( address.isEmpty() )
|
||||
{
|
||||
this->setInError("No target hostname nor IP defined");
|
||||
return false;
|
||||
isInitOK = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -419,13 +394,7 @@ void LedDevicePhilipsHueBridge::log(const char* msg, const char* type, ...) cons
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::getAllBridgeInfos()
|
||||
{
|
||||
// Read Groups/ Lights and Light-Ids
|
||||
_restApi->setPath(API_ROOT);
|
||||
|
||||
httpResponse response = _restApi->get();
|
||||
checkApiError(response.getBody());
|
||||
|
||||
return response.getBody();
|
||||
return get(API_ROOT);
|
||||
}
|
||||
|
||||
bool LedDevicePhilipsHueBridge::initMaps()
|
||||
@@ -491,7 +460,7 @@ void LedDevicePhilipsHueBridge::setBridgeConfig(const QJsonDocument &doc)
|
||||
log( "Bridge-ID", "%s", QSTRING_CSTR( deviceBridgeID ));
|
||||
log( "SoftwareVersion", "%s", QSTRING_CSTR( _deviceFirmwareVersion ));
|
||||
log( "API-Version", "%u.%u.%u", _api_major, _api_minor, _api_patch );
|
||||
log( "EntertainmentReady", "%d", _isHueEntertainmentReady );
|
||||
log( "EntertainmentReady", "%d", static_cast<int>(_isHueEntertainmentReady) );
|
||||
}
|
||||
|
||||
void LedDevicePhilipsHueBridge::setLightsMap(const QJsonDocument &doc)
|
||||
@@ -517,7 +486,7 @@ void LedDevicePhilipsHueBridge::setLightsMap(const QJsonDocument &doc)
|
||||
}
|
||||
else
|
||||
{
|
||||
log( "Lights in Bridge found", "%u", getLedCount() );
|
||||
log( "Lights in Bridge found", "%d", getLedCount() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -628,6 +597,15 @@ bool LedDevicePhilipsHueBridge::checkApiError(const QJsonDocument &response)
|
||||
return apiError;
|
||||
}
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::get(const QString& route)
|
||||
{
|
||||
_restApi->setPath(route);
|
||||
|
||||
httpResponse response = _restApi->get();
|
||||
checkApiError(response.getBody());
|
||||
return response.getBody();
|
||||
}
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::post(const QString& route, const QString& content)
|
||||
{
|
||||
_restApi->setPath(route);
|
||||
@@ -637,6 +615,12 @@ QJsonDocument LedDevicePhilipsHueBridge::post(const QString& route, const QStrin
|
||||
return response.getBody();
|
||||
}
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::getLightState(unsigned int lightId)
|
||||
{
|
||||
DebugIf( verbose, _log, "GetLightState [%u]", lightId );
|
||||
return get( QString("%1/%2").arg( API_LIGHTS ).arg( lightId ) );
|
||||
}
|
||||
|
||||
void LedDevicePhilipsHueBridge::setLightState(unsigned int lightId, const QString &state)
|
||||
{
|
||||
DebugIf( verbose, _log, "SetLightState [%u]: %s", lightId, QSTRING_CSTR(state) );
|
||||
@@ -645,10 +629,8 @@ void LedDevicePhilipsHueBridge::setLightState(unsigned int lightId, const QStrin
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::getGroupState(unsigned int groupId)
|
||||
{
|
||||
_restApi->setPath( QString("%1/%2").arg( API_GROUPS ).arg( groupId ) );
|
||||
httpResponse response = _restApi->get();
|
||||
checkApiError(response.getBody());
|
||||
return response.getBody();
|
||||
DebugIf( verbose, _log, "GetGroupState [%u]", groupId );
|
||||
return get( QString("%1/%2").arg( API_GROUPS ).arg( groupId ) );
|
||||
}
|
||||
|
||||
QJsonDocument LedDevicePhilipsHueBridge::setGroupState(unsigned int groupId, bool state)
|
||||
@@ -712,8 +694,6 @@ PhilipsHueLight::PhilipsHueLight(Logger* log, unsigned int id, QJsonObject value
|
||||
_colorBlack = {0.0, 0.0, 0.0};
|
||||
}
|
||||
|
||||
saveOriginalState(values);
|
||||
|
||||
_lightname = values["name"].toString().trimmed().replace("\"", "");
|
||||
Info(_log, "Light ID %d (\"%s\", LED index \"%d\") created", id, QSTRING_CSTR(_lightname), ledidx );
|
||||
}
|
||||
@@ -806,7 +786,6 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const QJsonObject& deviceConfig)
|
||||
, _switchOffOnBlack(false)
|
||||
, _brightnessFactor(1.0)
|
||||
, _transitionTime(1)
|
||||
, _lightStatesRestored(false)
|
||||
, _isInitLeds(false)
|
||||
, _lightsCount(0)
|
||||
, _groupId(0)
|
||||
@@ -918,7 +897,7 @@ bool LedDevicePhilipsHue::setLights()
|
||||
|
||||
if( !lArray.empty() )
|
||||
{
|
||||
for (const auto id : lArray)
|
||||
for (const QJsonValueRef id : lArray)
|
||||
{
|
||||
unsigned int lightId = id.toString().toUInt();
|
||||
if( lightId > 0 )
|
||||
@@ -1249,7 +1228,7 @@ QByteArray LedDevicePhilipsHue::prepareStreamData() const
|
||||
{
|
||||
QByteArray msg;
|
||||
msg.reserve(static_cast<int>(sizeof(HEADER) + sizeof(PAYLOAD_PER_LIGHT) * _lights.size()));
|
||||
msg.append((const char*)HEADER, sizeof(HEADER));
|
||||
msg.append(reinterpret_cast<const char*>(HEADER), sizeof(HEADER));
|
||||
|
||||
for (const PhilipsHueLight& light : _lights)
|
||||
{
|
||||
@@ -1264,7 +1243,7 @@ QByteArray LedDevicePhilipsHue::prepareStreamData() const
|
||||
static_cast<uint8_t>((G >> 8) & 0xff), static_cast<uint8_t>(G & 0xff),
|
||||
static_cast<uint8_t>((B >> 8) & 0xff), static_cast<uint8_t>(B & 0xff)
|
||||
};
|
||||
msg.append((char*)payload, sizeof(payload));
|
||||
msg.append(reinterpret_cast<const char *>(payload), sizeof(payload));
|
||||
}
|
||||
|
||||
return msg;
|
||||
@@ -1278,30 +1257,8 @@ void LedDevicePhilipsHue::stop()
|
||||
|
||||
int LedDevicePhilipsHue::open()
|
||||
{
|
||||
int retval = -1;
|
||||
_isDeviceReady = false;
|
||||
|
||||
if( _useHueEntertainmentAPI )
|
||||
{
|
||||
if ( openStream() )
|
||||
{
|
||||
// Everything is OK, device is ready
|
||||
_isDeviceReady = true;
|
||||
retval = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Stop device (or fallback to classic mode) - suggest to stop device to meet user expectation
|
||||
//_useHueEntertainmentAPI = false; -to be removed, if 1
|
||||
// Everything is OK, device is ready
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Classic mode, everything is OK, device is ready
|
||||
_isDeviceReady = true;
|
||||
retval = 0;
|
||||
}
|
||||
int retval = 0;
|
||||
_isDeviceReady = true;
|
||||
|
||||
return retval;
|
||||
}
|
||||
@@ -1315,6 +1272,40 @@ int LedDevicePhilipsHue::close()
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool LedDevicePhilipsHue::switchOn()
|
||||
{
|
||||
Debug(_log, "");
|
||||
|
||||
bool rc = false;
|
||||
|
||||
if ( _isOn )
|
||||
{
|
||||
rc = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _isEnabled && _isDeviceInitialised )
|
||||
{
|
||||
storeState();
|
||||
|
||||
if ( _useHueEntertainmentAPI)
|
||||
{
|
||||
if ( openStream() )
|
||||
{
|
||||
_isOn = true;
|
||||
rc = true;
|
||||
}
|
||||
}
|
||||
else if ( powerOn() )
|
||||
{
|
||||
_isOn = true;
|
||||
rc = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool LedDevicePhilipsHue::switchOff()
|
||||
{
|
||||
Debug(_log, "");
|
||||
@@ -1322,7 +1313,10 @@ bool LedDevicePhilipsHue::switchOff()
|
||||
this->stopBlackTimeoutTimer();
|
||||
|
||||
stop_retry_left = 3;
|
||||
if (_useHueEntertainmentAPI)
|
||||
{
|
||||
stopStream();
|
||||
}
|
||||
|
||||
return LedDevicePhilipsHueBridge::switchOff();
|
||||
}
|
||||
@@ -1369,7 +1363,7 @@ void LedDevicePhilipsHue::stopBlackTimeoutTimer()
|
||||
|
||||
bool LedDevicePhilipsHue::noSignalDetection()
|
||||
{
|
||||
if( _allLightsBlack )
|
||||
if( _allLightsBlack && _switchOffOnBlack)
|
||||
{
|
||||
if( !_stopConnection && _isInitLeds )
|
||||
{
|
||||
@@ -1563,11 +1557,14 @@ bool LedDevicePhilipsHue::storeState()
|
||||
|
||||
if ( _isRestoreOrigState )
|
||||
{
|
||||
// Save device's original state
|
||||
//_orignalStateValues = get device's state;
|
||||
|
||||
// TODO: Move saveOriginalState out of the HueLight constructor,
|
||||
// as the light state may have change since last close and needs to be stored again before reopen
|
||||
if( !_lightIds.empty() )
|
||||
{
|
||||
for ( PhilipsHueLight& light : _lights )
|
||||
{
|
||||
QJsonObject values = getLightState(light.getId()).object();
|
||||
light.saveOriginalState(values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
@@ -1577,11 +1574,9 @@ bool LedDevicePhilipsHue::restoreState()
|
||||
{
|
||||
bool rc = true;
|
||||
|
||||
if ( _isRestoreOrigState && !_lightStatesRestored )
|
||||
if ( _isRestoreOrigState )
|
||||
{
|
||||
// Restore device's original state
|
||||
_lightStatesRestored = true;
|
||||
|
||||
if( !_lightIds.empty() )
|
||||
{
|
||||
for ( PhilipsHueLight& light : _lights )
|
||||
@@ -1594,7 +1589,7 @@ bool LedDevicePhilipsHue::restoreState()
|
||||
return rc;
|
||||
}
|
||||
|
||||
QJsonObject LedDevicePhilipsHue::discover()
|
||||
QJsonObject LedDevicePhilipsHue::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
|
@@ -17,6 +17,31 @@
|
||||
#include "ProviderRestApi.h"
|
||||
#include "ProviderUdpSSL.h"
|
||||
|
||||
//Streaming message header and payload definition
|
||||
const uint8_t HEADER[] =
|
||||
{
|
||||
'H', 'u', 'e', 'S', 't', 'r', 'e', 'a', 'm', //protocol
|
||||
0x01, 0x00, //version 1.0
|
||||
0x01, //sequence number 1
|
||||
0x00, 0x00, //Reserved write 0’s
|
||||
0x01, //xy Brightness
|
||||
0x00, // Reserved, write 0’s
|
||||
};
|
||||
|
||||
const uint8_t PAYLOAD_PER_LIGHT[] =
|
||||
{
|
||||
0x01, 0x00, 0x06, //light ID
|
||||
//color: 16 bpc
|
||||
0xff, 0xff,
|
||||
0xff, 0xff,
|
||||
0xff, 0xff,
|
||||
/*
|
||||
(message.R >> 8) & 0xff, message.R & 0xff,
|
||||
(message.G >> 8) & 0xff, message.G & 0xff,
|
||||
(message.B >> 8) & 0xff, message.B & 0xff
|
||||
*/
|
||||
};
|
||||
|
||||
/**
|
||||
* A XY color point in the color space of the hue system without brightness.
|
||||
*/
|
||||
@@ -152,12 +177,11 @@ public:
|
||||
/// @return the color space of the light determined by the model id reported by the bridge.
|
||||
CiColorTriangle getColorSpace() const;
|
||||
|
||||
void saveOriginalState(const QJsonObject& values);
|
||||
QString getOriginalState() const;
|
||||
|
||||
private:
|
||||
|
||||
void saveOriginalState(const QJsonObject& values);
|
||||
|
||||
Logger* _log;
|
||||
/// light id
|
||||
unsigned int _id;
|
||||
@@ -200,12 +224,23 @@ public:
|
||||
bool initRestAPI(const QString &hostname, int port, const QString &token );
|
||||
|
||||
///
|
||||
/// @param route the route of the POST request.
|
||||
/// @brief Perform a REST-API GET
|
||||
///
|
||||
/// @param route the route of the GET request.
|
||||
///
|
||||
/// @return the content of the GET request.
|
||||
///
|
||||
QJsonDocument get(const QString& route);
|
||||
|
||||
///
|
||||
/// @brief Perform a REST-API POST
|
||||
///
|
||||
/// @param route the route of the POST request.
|
||||
/// @param content the content of the POST request.
|
||||
///
|
||||
QJsonDocument post(const QString& route, const QString& content);
|
||||
|
||||
QJsonDocument getLightState(unsigned int lightId);
|
||||
void setLightState(unsigned int lightId = 0, const QString &state = "");
|
||||
|
||||
QMap<quint16,QJsonObject> getLightMap() const;
|
||||
@@ -316,7 +351,7 @@ public:
|
||||
///
|
||||
/// @brief Destructor of the LED-device
|
||||
///
|
||||
~LedDevicePhilipsHue();
|
||||
~LedDevicePhilipsHue() override;
|
||||
|
||||
///
|
||||
/// @brief Constructs the LED-device
|
||||
@@ -329,9 +364,11 @@ public:
|
||||
/// @brief Discover devices of this type available (for configuration).
|
||||
/// @note Mainly used for network devices. Allows to find devices, e.g. via ssdp, mDNS or cloud ways.
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover() override;
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Get the Hue Bridge device's resource properties
|
||||
@@ -421,7 +458,7 @@ protected:
|
||||
///
|
||||
/// @return True if success
|
||||
///
|
||||
//bool switchOn() override;
|
||||
bool switchOn() override;
|
||||
|
||||
///
|
||||
/// @brief Switch the LEDs off.
|
||||
@@ -525,7 +562,6 @@ private:
|
||||
/// The default of the Hue lights is 400 ms, but we may want it snappier.
|
||||
int _transitionTime;
|
||||
|
||||
bool _lightStatesRestored;
|
||||
bool _isInitLeds;
|
||||
|
||||
/// Array of the light ids.
|
||||
|
@@ -17,7 +17,7 @@ const quint16 STREAM_DEFAULT_PORT = 19446;
|
||||
const int API_DEFAULT_PORT = -1; //Use default port per communication scheme
|
||||
|
||||
const char API_BASE_PATH[] = "/json/";
|
||||
const char API_PATH_INFO[] = "info";
|
||||
//const char API_PATH_INFO[] = "info";
|
||||
const char API_PATH_STATE[] = "state";
|
||||
|
||||
// List of State Information
|
||||
@@ -60,9 +60,9 @@ bool LedDeviceWled::init(const QJsonObject &deviceConfig)
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
{
|
||||
// Initialise LedDevice configuration and execution environment
|
||||
uint configuredLedCount = this->getLedCount();
|
||||
int configuredLedCount = this->getLedCount();
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR( this->getActiveDeviceType() ));
|
||||
Debug(_log, "LedCount : %u", configuredLedCount);
|
||||
Debug(_log, "LedCount : %d", configuredLedCount);
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
|
||||
@@ -166,7 +166,7 @@ bool LedDeviceWled::powerOff()
|
||||
return off;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceWled::discover()
|
||||
QJsonObject LedDeviceWled::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
|
@@ -37,9 +37,11 @@ public:
|
||||
///
|
||||
/// @brief Discover WLED devices available (for configuration).
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover() override;
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Get the WLED device's resource properties
|
||||
|
@@ -247,7 +247,7 @@ int YeelightLight::writeCommand( const QJsonDocument &command, QJsonArray &resul
|
||||
if ( elapsedTime < _waitTimeQuota )
|
||||
{
|
||||
int waitTime = _waitTimeQuota;
|
||||
log ( 1, "writeCommand():", "Wait %dms, elapsedTime: %dms < quotaTime: %dms", waitTime, elapsedTime, _waitTimeQuota);
|
||||
log ( 1, "writeCommand():", "Wait %dms, elapsedTime: %dms < quotaTime: %dms", waitTime, static_cast<int>(elapsedTime), _waitTimeQuota);
|
||||
|
||||
// Wait time (in ms) before doing next write to not overrun Yeelight command quota
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(_waitTimeQuota));
|
||||
@@ -452,7 +452,7 @@ YeelightResponse YeelightLight::handleResponse(int correlationID, QByteArray con
|
||||
// Debug output
|
||||
if(!yeeResponse.getResult().empty())
|
||||
{
|
||||
for(const auto item : yeeResponse.getResult())
|
||||
for(const QJsonValueRef item : yeeResponse.getResult())
|
||||
{
|
||||
log ( 3, "Result:", "%s", QSTRING_CSTR( item.toString() ));
|
||||
}
|
||||
@@ -524,7 +524,7 @@ QJsonObject YeelightLight::getProperties()
|
||||
if( !result.empty())
|
||||
{
|
||||
int i = 0;
|
||||
for(const auto item : result)
|
||||
for(const QJsonValueRef item : result)
|
||||
{
|
||||
log (1,"Property:", "%s = %s", QSTRING_CSTR( propertyList.at(i).toString() ), QSTRING_CSTR( item.toString() ));
|
||||
properties.insert( propertyList.at(i).toString(), item );
|
||||
@@ -1008,7 +1008,7 @@ bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
{
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR( this->getActiveDeviceType() ));
|
||||
Debug(_log, "LedCount : %u", this->getLedCount());
|
||||
Debug(_log, "LedCount : %d", this->getLedCount());
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
|
||||
Debug(_log, "RewriteTime : %d", this->getRewriteTime());
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
@@ -1073,8 +1073,8 @@ bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
Debug(_log, "Debuglevel : %d", _debuglevel);
|
||||
|
||||
QJsonArray configuredYeelightLights = _devConfig[CONFIG_LIGHTS].toArray();
|
||||
uint configuredYeelightsCount = 0;
|
||||
for (const QJsonValue light : configuredYeelightLights)
|
||||
int configuredYeelightsCount = 0;
|
||||
for (const QJsonValueRef light : configuredYeelightLights)
|
||||
{
|
||||
QString host = light.toObject().value("host").toString();
|
||||
int port = light.toObject().value("port").toInt(API_DEFAULT_PORT);
|
||||
@@ -1085,9 +1085,9 @@ bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
++configuredYeelightsCount;
|
||||
}
|
||||
}
|
||||
Debug(_log, "Light configured : %u", configuredYeelightsCount );
|
||||
Debug(_log, "Light configured : %d", configuredYeelightsCount );
|
||||
|
||||
uint configuredLedCount = this->getLedCount();
|
||||
int configuredLedCount = this->getLedCount();
|
||||
if (configuredYeelightsCount < configuredLedCount )
|
||||
{
|
||||
QString errorReason = QString("Not enough Yeelights [%1] for configured LEDs [%2] found!")
|
||||
@@ -1101,7 +1101,7 @@ bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
|
||||
if ( configuredYeelightsCount > configuredLedCount )
|
||||
{
|
||||
Warning(_log, "More Yeelights defined [%u] than configured LEDs [%u].", configuredYeelightsCount, configuredLedCount );
|
||||
Warning(_log, "More Yeelights defined [%d] than configured LEDs [%d].", configuredYeelightsCount, configuredLedCount );
|
||||
}
|
||||
|
||||
_lightsAddressList.clear();
|
||||
@@ -1347,7 +1347,7 @@ bool LedDeviceYeelight::restoreState()
|
||||
return rc;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceYeelight::discover()
|
||||
QJsonObject LedDeviceYeelight::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
|
@@ -435,12 +435,11 @@ public:
|
||||
///
|
||||
static LedDevice* construct(const QJsonObject &deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Discover Yeelight devices available (for configuration).
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover() override;
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
///
|
||||
/// @brief Get a Yeelight device's resource properties
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
// STL includes
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
@@ -16,13 +15,13 @@
|
||||
|
||||
const ushort MAX_PORT = 65535;
|
||||
|
||||
ProviderUdp::ProviderUdp(const QJsonObject &deviceConfig)
|
||||
ProviderUdp::ProviderUdp(const QJsonObject& deviceConfig)
|
||||
: LedDevice(deviceConfig)
|
||||
, _udpSocket (nullptr)
|
||||
, _udpSocket(nullptr)
|
||||
, _port(1)
|
||||
, _defaultHost("127.0.0.1")
|
||||
{
|
||||
_latchTime_ms = 1;
|
||||
_latchTime_ms = 0;
|
||||
}
|
||||
|
||||
ProviderUdp::~ProviderUdp()
|
||||
@@ -30,48 +29,48 @@ ProviderUdp::~ProviderUdp()
|
||||
delete _udpSocket;
|
||||
}
|
||||
|
||||
bool ProviderUdp::init(const QJsonObject &deviceConfig)
|
||||
bool ProviderUdp::init(const QJsonObject& deviceConfig)
|
||||
{
|
||||
bool isInitOK = false;
|
||||
|
||||
// Initialise sub-class
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
if (LedDevice::init(deviceConfig))
|
||||
{
|
||||
QString host = deviceConfig["host"].toString(_defaultHost);
|
||||
|
||||
if (_address.setAddress(host) )
|
||||
if (_address.setAddress(host))
|
||||
{
|
||||
Debug( _log, "Successfully parsed %s as an IP-address.", QSTRING_CSTR(_address.toString()));
|
||||
Debug(_log, "Successfully parsed %s as an IP-address.", QSTRING_CSTR(_address.toString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
QHostInfo hostInfo = QHostInfo::fromName(host);
|
||||
if ( hostInfo.error() == QHostInfo::NoError )
|
||||
if (hostInfo.error() == QHostInfo::NoError)
|
||||
{
|
||||
_address = hostInfo.addresses().first();
|
||||
Debug( _log, "Successfully resolved IP-address (%s) for hostname (%s).", QSTRING_CSTR(_address.toString()), QSTRING_CSTR(host));
|
||||
Debug(_log, "Successfully resolved IP-address (%s) for hostname (%s).", QSTRING_CSTR(_address.toString()), QSTRING_CSTR(host));
|
||||
}
|
||||
else
|
||||
{
|
||||
QString errortext = QString ("Failed resolving IP-address for [%1], (%2) %3").arg(host).arg(hostInfo.error()).arg(hostInfo.errorString());
|
||||
this->setInError ( errortext );
|
||||
QString errortext = QString("Failed resolving IP-address for [%1], (%2) %3").arg(host).arg(hostInfo.error()).arg(hostInfo.errorString());
|
||||
this->setInError(errortext);
|
||||
isInitOK = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !_isDeviceInError )
|
||||
if (!_isDeviceInError)
|
||||
{
|
||||
int config_port = deviceConfig["port"].toInt(_port);
|
||||
if ( config_port <= 0 || config_port > MAX_PORT )
|
||||
if (config_port <= 0 || config_port > MAX_PORT)
|
||||
{
|
||||
QString errortext = QString ("Invalid target port [%1]!").arg(config_port);
|
||||
this->setInError ( errortext );
|
||||
QString errortext = QString("Invalid target port [%1]!").arg(config_port);
|
||||
this->setInError(errortext);
|
||||
isInitOK = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_port = static_cast<int>(config_port);
|
||||
Debug( _log, "UDP socket will write to %s:%u", QSTRING_CSTR(_address.toString()) , _port );
|
||||
_port = static_cast<quint16>(config_port);
|
||||
Debug(_log, "UDP socket will write to %s:%u", QSTRING_CSTR(_address.toString()), _port);
|
||||
|
||||
_udpSocket = new QUdpSocket(this);
|
||||
|
||||
@@ -88,16 +87,16 @@ int ProviderUdp::open()
|
||||
_isDeviceReady = false;
|
||||
|
||||
// Try to bind the UDP-Socket
|
||||
if ( _udpSocket != nullptr )
|
||||
if (_udpSocket != nullptr)
|
||||
{
|
||||
if ( _udpSocket->state() != QAbstractSocket::BoundState )
|
||||
if (_udpSocket->state() != QAbstractSocket::BoundState)
|
||||
{
|
||||
QHostAddress localAddress = QHostAddress::Any;
|
||||
quint16 localPort = 0;
|
||||
if ( !_udpSocket->bind(localAddress, localPort) )
|
||||
if (!_udpSocket->bind(localAddress, localPort))
|
||||
{
|
||||
QString warntext = QString ("Could not bind local address: %1, (%2) %3").arg(localAddress.toString()).arg(_udpSocket->error()).arg(_udpSocket->errorString());
|
||||
Warning ( _log, "%s", QSTRING_CSTR(warntext));
|
||||
QString warntext = QString("Could not bind local address: %1, (%2) %3").arg(localAddress.toString()).arg(_udpSocket->error()).arg(_udpSocket->errorString());
|
||||
Warning(_log, "%s", QSTRING_CSTR(warntext));
|
||||
}
|
||||
}
|
||||
// Everything is OK, device is ready
|
||||
@@ -106,7 +105,7 @@ int ProviderUdp::open()
|
||||
}
|
||||
else
|
||||
{
|
||||
this->setInError( " Open error. UDP Socket not initialised!" );
|
||||
this->setInError(" Open error. UDP Socket not initialised!");
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
@@ -116,12 +115,12 @@ int ProviderUdp::close()
|
||||
int retval = 0;
|
||||
_isDeviceReady = false;
|
||||
|
||||
if ( _udpSocket != nullptr )
|
||||
if (_udpSocket != nullptr)
|
||||
{
|
||||
// Test, if device requires closing
|
||||
if ( _udpSocket->isOpen() )
|
||||
if (_udpSocket->isOpen())
|
||||
{
|
||||
Debug(_log,"Close UDP-device: %s", QSTRING_CSTR( this->getActiveDeviceType() ) );
|
||||
Debug(_log, "Close UDP-device: %s", QSTRING_CSTR(this->getActiveDeviceType()));
|
||||
_udpSocket->close();
|
||||
// Everything is OK -> device is closed
|
||||
}
|
||||
@@ -129,12 +128,28 @@ int ProviderUdp::close()
|
||||
return retval;
|
||||
}
|
||||
|
||||
int ProviderUdp::writeBytes(const unsigned size, const uint8_t * data)
|
||||
int ProviderUdp::writeBytes(const unsigned size, const uint8_t* data)
|
||||
{
|
||||
qint64 retVal = _udpSocket->writeDatagram((const char *)data,size,_address,_port);
|
||||
int rc = 0;
|
||||
qint64 bytesWritten = _udpSocket->writeDatagram(reinterpret_cast<const char*>(data), size, _address, _port);
|
||||
|
||||
WarningIf((retVal<0), _log, "&s", QSTRING_CSTR(QString
|
||||
("(%1:%2) Write Error: (%3) %4").arg(_address.toString()).arg(_port).arg(_udpSocket->error()).arg(_udpSocket->errorString())));
|
||||
|
||||
return retVal;
|
||||
if (bytesWritten == -1 || bytesWritten != size)
|
||||
{
|
||||
Warning(_log, "%s", QSTRING_CSTR(QString("(%1:%2) Write Error: (%3) %4").arg(_address.toString()).arg(_port).arg(_udpSocket->error()).arg(_udpSocket->errorString())));
|
||||
rc = -1;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int ProviderUdp::writeBytes(const QByteArray& bytes)
|
||||
{
|
||||
int rc = 0;
|
||||
qint64 bytesWritten = _udpSocket->writeDatagram(bytes, _address, _port);
|
||||
|
||||
if (bytesWritten == -1 || bytesWritten != bytes.size())
|
||||
{
|
||||
Warning(_log, "%s", QSTRING_CSTR(QString("(%1:%2) Write Error: (%3) %4").arg(_address.toString()).arg(_port).arg(_udpSocket->error()).arg(_udpSocket->errorString())));
|
||||
rc = -1;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
@@ -21,13 +21,15 @@ public:
|
||||
///
|
||||
/// @brief Constructs an UDP LED-device
|
||||
///
|
||||
ProviderUdp(const QJsonObject &deviceConfig);
|
||||
ProviderUdp(const QJsonObject& deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Destructor of the UDP LED-device
|
||||
///
|
||||
~ProviderUdp() override;
|
||||
|
||||
QHostAddress getAddress() const { return _address; }
|
||||
|
||||
protected:
|
||||
|
||||
///
|
||||
@@ -36,7 +38,7 @@ protected:
|
||||
/// @param[in] deviceConfig the JSON device configuration
|
||||
/// @return True, if success
|
||||
///
|
||||
bool init(const QJsonObject &deviceConfig) override;
|
||||
bool init(const QJsonObject& deviceConfig) override;
|
||||
|
||||
///
|
||||
/// @brief Opens the output device.
|
||||
@@ -53,18 +55,26 @@ protected:
|
||||
int close() override;
|
||||
|
||||
///
|
||||
/// @brief Writes the given bytes/bits to the UDP-device and sleeps the latch time to ensure that the
|
||||
/// values are latched.
|
||||
/// @brief Writes the given bytes to the UDP-device
|
||||
///
|
||||
/// @param[in] size The length of the data
|
||||
/// @param[in] data The data
|
||||
///
|
||||
/// @return Zero on success, else negative
|
||||
///
|
||||
int writeBytes(const unsigned size, const uint8_t *data);
|
||||
int writeBytes(const unsigned size, const uint8_t* data);
|
||||
|
||||
///
|
||||
QUdpSocket * _udpSocket;
|
||||
/// @brief Writes the given bytes to the UDP-device
|
||||
///
|
||||
/// @param[in] data The data
|
||||
///
|
||||
/// @return Zero on success, else negative
|
||||
///
|
||||
int writeBytes(const QByteArray& bytes);
|
||||
|
||||
///
|
||||
QUdpSocket* _udpSocket;
|
||||
QHostAddress _address;
|
||||
quint16 _port;
|
||||
QString _defaultHost;
|
||||
|
Reference in New Issue
Block a user