2019-04-08 23:13:11 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDeviceNanoleaf.h"
|
|
|
|
|
|
|
|
#include <ssdp/SSDPDiscover.h>
|
2020-07-12 20:27:56 +02:00
|
|
|
#include <utils/QStringUtils.h>
|
2019-04-08 23:13:11 +02:00
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QNetworkReply>
|
2020-11-14 17:58:56 +01:00
|
|
|
#include <QtEndian>
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
//std includes
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Constants
|
|
|
|
namespace {
|
2020-11-14 17:58:56 +01:00
|
|
|
const bool verbose = false;
|
2020-07-12 20:27:56 +02:00
|
|
|
const bool verbose3 = false;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Configuration settings
|
|
|
|
const char CONFIG_ADDRESS[] = "host";
|
|
|
|
//const char CONFIG_PORT[] = "port";
|
2020-11-14 17:58:56 +01:00
|
|
|
const char CONFIG_AUTH_TOKEN[] = "token";
|
2020-07-12 20:27:56 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
const char CONFIG_PANEL_ORDER_TOP_DOWN[] = "panelOrderTopDown";
|
|
|
|
const char CONFIG_PANEL_ORDER_LEFT_RIGHT[] = "panelOrderLeftRight";
|
|
|
|
const char CONFIG_PANEL_START_POS[] = "panelStartPos";
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2019-04-08 23:13:11 +02:00
|
|
|
// Panel configuration settings
|
2020-07-12 20:27:56 +02:00
|
|
|
const char PANEL_LAYOUT[] = "layout";
|
|
|
|
const char PANEL_NUM[] = "numPanels";
|
|
|
|
const char PANEL_ID[] = "panelId";
|
|
|
|
const char PANEL_POSITIONDATA[] = "positionData";
|
|
|
|
const char PANEL_SHAPE_TYPE[] = "shapeType";
|
|
|
|
//const char PANEL_ORIENTATION[] = "0";
|
|
|
|
const char PANEL_POS_X[] = "x";
|
|
|
|
const char PANEL_POS_Y[] = "y";
|
2019-04-08 23:13:11 +02:00
|
|
|
|
|
|
|
// List of State Information
|
2020-07-12 20:27:56 +02:00
|
|
|
const char STATE_ON[] = "on";
|
|
|
|
const char STATE_ONOFF_VALUE[] = "value";
|
|
|
|
const char STATE_VALUE_TRUE[] = "true";
|
|
|
|
const char STATE_VALUE_FALSE[] = "false";
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-03-26 18:49:44 +01:00
|
|
|
// Device Data elements
|
2020-07-12 20:27:56 +02:00
|
|
|
const char DEV_DATA_NAME[] = "name";
|
|
|
|
const char DEV_DATA_MODEL[] = "model";
|
|
|
|
const char DEV_DATA_MANUFACTURER[] = "manufacturer";
|
|
|
|
const char DEV_DATA_FIRMWAREVERSION[] = "firmwareVersion";
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-03-26 18:49:44 +01:00
|
|
|
// Nanoleaf Stream Control elements
|
2020-07-12 20:27:56 +02:00
|
|
|
//const char STREAM_CONTROL_IP[] = "streamControlIpAddr";
|
|
|
|
const char STREAM_CONTROL_PORT[] = "streamControlPort";
|
|
|
|
//const char STREAM_CONTROL_PROTOCOL[] = "streamControlProtocol";
|
2019-04-08 23:13:11 +02:00
|
|
|
const quint16 STREAM_CONTROL_DEFAULT_PORT = 60222; //Fixed port for Canvas;
|
|
|
|
|
|
|
|
// Nanoleaf OpenAPI URLs
|
2020-07-12 20:27:56 +02:00
|
|
|
const int API_DEFAULT_PORT = 16021;
|
|
|
|
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\"}}";
|
2020-11-14 17:58:56 +01:00
|
|
|
const char API_STATE[] = "state";
|
2020-07-12 20:27:56 +02:00
|
|
|
const char API_PANELLAYOUT[] = "panelLayout";
|
|
|
|
const char API_EFFECT[] = "effects";
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
//Nanoleaf Control data stream
|
|
|
|
const int STREAM_FRAME_PANEL_NUM_SIZE = 2;
|
|
|
|
const int STREAM_FRAME_PANEL_INFO_SIZE = 8;
|
|
|
|
|
2020-03-26 18:49:44 +01:00
|
|
|
// Nanoleaf ssdp services
|
2020-07-12 20:27:56 +02:00
|
|
|
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
|
2019-04-08 23:13:11 +02:00
|
|
|
|
|
|
|
// Nanoleaf Panel Shapetypes
|
|
|
|
enum SHAPETYPES {
|
2019-12-08 13:12:01 +01:00
|
|
|
TRIANGLE,
|
|
|
|
RHYTM,
|
|
|
|
SQUARE,
|
|
|
|
CONTROL_SQUARE_PRIMARY,
|
|
|
|
CONTROL_SQUARE_PASSIVE,
|
|
|
|
POWER_SUPPLY,
|
2020-11-14 17:58:56 +01:00
|
|
|
};
|
2019-04-08 23:13:11 +02:00
|
|
|
|
|
|
|
// Nanoleaf external control versions
|
|
|
|
enum EXTCONTROLVERSIONS {
|
2019-12-08 13:12:01 +01:00
|
|
|
EXTCTRLVER_V1 = 1,
|
|
|
|
EXTCTRLVER_V2
|
2019-04-08 23:13:11 +02:00
|
|
|
};
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
LedDeviceNanoleaf::LedDeviceNanoleaf(const QJsonObject& deviceConfig)
|
2020-08-08 00:21:19 +02:00
|
|
|
: ProviderUdp(deviceConfig)
|
2020-11-14 17:58:56 +01:00
|
|
|
, _restApi(nullptr)
|
|
|
|
, _apiPort(API_DEFAULT_PORT)
|
|
|
|
, _topDown(true)
|
|
|
|
, _leftRight(true)
|
|
|
|
, _startPos(0)
|
|
|
|
, _endPos(0)
|
|
|
|
, _extControlVersion(EXTCTRLVER_V2),
|
2020-07-12 20:27:56 +02:00
|
|
|
_panelLedCount(0)
|
2019-04-08 23:13:11 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
LedDevice* LedDeviceNanoleaf::construct(const QJsonObject& deviceConfig)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
return new LedDeviceNanoleaf(deviceConfig);
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
LedDeviceNanoleaf::~LedDeviceNanoleaf()
|
2019-04-08 23:13:11 +02:00
|
|
|
{
|
2020-08-08 00:21:19 +02:00
|
|
|
delete _restApi;
|
|
|
|
_restApi = nullptr;
|
2019-04-08 23:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
bool LedDeviceNanoleaf::init(const QJsonObject& deviceConfig)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
// Overwrite non supported/required features
|
2020-09-14 17:19:14 +02:00
|
|
|
setLatchTime(0);
|
|
|
|
setRewriteTime(0);
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
if (deviceConfig["rewriteTime"].toInt(0) > 0)
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Info(_log, "Device Nanoleaf does not require rewrites. Refresh time is ignored.");
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
bool isInitOK = false;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (LedDevice::init(deviceConfig))
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
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()));
|
2020-09-14 17:19:14 +02:00
|
|
|
Debug(_log, "RewriteTime : %d", this->getRewriteTime());
|
2020-02-10 15:21:58 +01:00
|
|
|
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
|
|
|
|
2020-06-12 11:16:39 +02:00
|
|
|
// Read panel organisation configuration
|
2020-11-14 17:58:56 +01:00
|
|
|
if (deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].isString())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
_topDown = deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].toString().toInt() == 0;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
_topDown = deviceConfig[CONFIG_PANEL_ORDER_TOP_DOWN].toInt() == 0;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].isString())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
_leftRight = deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].toString().toInt() == 0;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
_leftRight = deviceConfig[CONFIG_PANEL_ORDER_LEFT_RIGHT].toInt() == 0;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
_startPos = deviceConfig[CONFIG_PANEL_START_POS].toInt(0);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
// TODO: Allow to handle port dynamically
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//Set hostname as per configuration and_defaultHost default port
|
2020-11-14 17:58:56 +01:00
|
|
|
_hostname = deviceConfig[CONFIG_ADDRESS].toString();
|
|
|
|
_apiPort = API_DEFAULT_PORT;
|
|
|
|
_authToken = deviceConfig[CONFIG_AUTH_TOKEN].toString();
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
//If host not configured the init failed
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_hostname.isEmpty())
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
this->setInError("No target hostname nor IP defined");
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
if (initRestAPI(_hostname, _apiPort, _authToken))
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// Read LedDevice configuration and validate against device configuration
|
2020-11-14 17:58:56 +01:00
|
|
|
if (initLedsConfiguration())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
// Set UDP streaming host and port
|
|
|
|
_devConfig["host"] = _hostname;
|
|
|
|
_devConfig["port"] = STREAM_CONTROL_DEFAULT_PORT;
|
|
|
|
|
|
|
|
isInitOK = ProviderUdp::init(_devConfig);
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "Hostname/IP : %s", QSTRING_CSTR(_hostname));
|
2020-07-12 20:27:56 +02:00
|
|
|
Debug(_log, "Port : %d", _port);
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
return isInitOK;
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
bool LedDeviceNanoleaf::initLedsConfiguration()
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
bool isInitOK = true;
|
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
//Get Nanoleaf device details and configuration
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
// Read Panel count and panel Ids
|
2020-07-12 20:27:56 +02:00
|
|
|
_restApi->setPath(API_ROOT);
|
|
|
|
httpResponse response = _restApi->get();
|
2020-11-14 17:58:56 +01:00
|
|
|
if (response.error())
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
this->setInError(response.getErrorReason());
|
2020-02-10 15:21:58 +01:00
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QJsonObject jsonAllPanelInfo = response.getBody().object();
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
QString deviceName = jsonAllPanelInfo[DEV_DATA_NAME].toString();
|
|
|
|
_deviceModel = jsonAllPanelInfo[DEV_DATA_MODEL].toString();
|
|
|
|
QString deviceManufacturer = jsonAllPanelInfo[DEV_DATA_MANUFACTURER].toString();
|
|
|
|
_deviceFirmwareVersion = jsonAllPanelInfo[DEV_DATA_FIRMWAREVERSION].toString();
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
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));
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
// Get panel details from /panelLayout/layout
|
|
|
|
QJsonObject jsonPanelLayout = jsonAllPanelInfo[API_PANELLAYOUT].toObject();
|
|
|
|
QJsonObject jsonLayout = jsonPanelLayout[PANEL_LAYOUT].toObject();
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
int panelNum = jsonLayout[PANEL_NUM].toInt();
|
2020-02-10 15:21:58 +01:00
|
|
|
QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray();
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
std::map<int, std::map<int, int>> panelMap;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
// Loop over all children.
|
2020-11-14 17:58:56 +01:00
|
|
|
foreach(const QJsonValue & value, positionData)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
QJsonObject panelObj = value.toObject();
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
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();
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose, _log, "Panel [%d] (%d,%d) - Type: [%d]", panelId, panelX, panelY, panelshapeType);
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
// Skip Rhythm panels
|
2020-11-14 17:58:56 +01:00
|
|
|
if (panelshapeType != RHYTM)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
panelMap[panelY][panelX] = panelId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Reset non support/required features
|
|
|
|
Info(_log, "Rhythm panel skipped.");
|
|
|
|
}
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:16:39 +02:00
|
|
|
// Travers panels top down
|
2020-11-14 17:58:56 +01:00
|
|
|
for (auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
// Sort panels left to right
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_leftRight)
|
2020-06-12 11:16:39 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
for (auto posX = posY->second.cbegin(); posX != posY->second.cend(); ++posX)
|
2020-06-12 11:16:39 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose3, _log, "panelMap[%d][%d]=%d", posY->first, posX->first, posX->second);
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_topDown)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
_panelIds.push_back(posX->second);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
_panelIds.push_front(posX->second);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
// Sort panels right to left
|
2020-11-14 17:58:56 +01:00
|
|
|
for (auto posX = posY->second.crbegin(); posX != posY->second.crend(); ++posX)
|
2020-06-12 11:16:39 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose3, _log, "panelMap[%d][%d]=%d", posY->first, posX->first, posX->second);
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_topDown)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
_panelIds.push_back(posX->second);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
else
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
_panelIds.push_front(posX->second);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
this->_panelLedCount = _panelIds.size();
|
|
|
|
_devConfig["hardwareLedCount"] = _panelLedCount;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "PanelsNum : %d", panelNum);
|
|
|
|
Debug(_log, "PanelLedCount : %d", _panelLedCount);
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-06-12 11:16:39 +02:00
|
|
|
// Check. if enough panels were found.
|
2020-11-14 17:58:56 +01:00
|
|
|
int configuredLedCount = this->getLedCount();
|
2020-06-12 11:16:39 +02:00
|
|
|
_endPos = _startPos + configuredLedCount - 1;
|
|
|
|
|
|
|
|
Debug(_log, "Sort Top>Down : %d", _topDown);
|
|
|
|
Debug(_log, "Sort Left>Right: %d", _leftRight);
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "Start Panel Pos: %d", _startPos);
|
|
|
|
Debug(_log, "End Panel Pos : %d", _endPos);
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_panelLedCount < configuredLedCount)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
|
|
|
QString errorReason = QString("Not enough panels [%1] for configured LEDs [%2] found!")
|
2020-06-12 11:16:39 +02:00
|
|
|
.arg(_panelLedCount)
|
|
|
|
.arg(configuredLedCount);
|
2020-02-10 15:21:58 +01:00
|
|
|
this->setInError(errorReason);
|
|
|
|
isInitOK = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_panelLedCount > this->getLedCount())
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Info(_log, "%s: More panels [%d] than configured LEDs [%d].", QSTRING_CSTR(this->getActiveDeviceType()), _panelLedCount, configuredLedCount);
|
2020-06-12 11:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check, if start position + number of configured LEDs is greater than number of panels available
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_endPos >= _panelLedCount)
|
2020-06-12 11:16:39 +02:00
|
|
|
{
|
|
|
|
QString errorReason = QString("Start panel [%1] out of range. Start panel position can be max [%2] given [%3] panel available!")
|
2020-11-14 17:58:56 +01:00
|
|
|
.arg(_startPos).arg(_panelLedCount - configuredLedCount).arg(_panelLedCount);
|
2020-06-12 11:16:39 +02:00
|
|
|
|
|
|
|
this->setInError(errorReason);
|
|
|
|
isInitOK = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return isInitOK;
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
bool LedDeviceNanoleaf::initRestAPI(const QString& hostname, int port, const QString& token)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
bool isInitOK = false;
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_restApi == nullptr)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
_restApi = new ProviderRestApi(hostname, port);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
//Base-path is api-path + authentication token
|
2020-11-14 17:58:56 +01:00
|
|
|
_restApi->setBasePath(QString(API_BASE_PATH).arg(token));
|
2020-06-12 11:16:39 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
isInitOK = true;
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
return isInitOK;
|
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
int LedDeviceNanoleaf::open()
|
|
|
|
{
|
|
|
|
int retval = -1;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
|
|
|
|
|
|
|
QJsonDocument responseDoc = changeToExternalControlMode();
|
|
|
|
// Resolve port for Light Panels
|
|
|
|
QJsonObject jsonStreamControllInfo = responseDoc.object();
|
2020-11-14 17:58:56 +01:00
|
|
|
if (!jsonStreamControllInfo.isEmpty())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
//Set default streaming port
|
|
|
|
_port = static_cast<uchar>(jsonStreamControllInfo[STREAM_CONTROL_PORT].toInt());
|
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (ProviderUdp::open() == 0)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// Everything is OK, device is ready
|
|
|
|
_isDeviceReady = true;
|
|
|
|
retval = 0;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
return retval;
|
2019-04-08 23:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QJsonObject LedDeviceNanoleaf::discover(const QJsonObject& /*params*/)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QJsonObject devicesDiscovered;
|
2020-11-14 17:58:56 +01:00
|
|
|
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
QJsonArray deviceList;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Discover Nanoleaf Devices
|
2019-12-08 13:12:01 +01:00
|
|
|
SSDPDiscover discover;
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Search for Canvas and Light-Panels
|
|
|
|
QString searchTargetFilter = QString("%1|%2").arg(SSDP_CANVAS, SSDP_LIGHTPANELS);
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
discover.setSearchFilter(searchTargetFilter, SSDP_FILTER_HEADER);
|
|
|
|
QString searchTarget = SSDP_ID;
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (discover.discoverServices(searchTarget) > 0)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
deviceList = discover.getServicesDiscoveredJson();
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
devicesDiscovered.insert("devices", deviceList);
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
return devicesDiscovered;
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
QJsonObject LedDeviceNanoleaf::getProperties(const QJsonObject& params)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
2020-07-12 20:27:56 +02:00
|
|
|
QJsonObject properties;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Get Nanoleaf device properties
|
|
|
|
QString host = params["host"].toString("");
|
2020-11-14 17:58:56 +01:00
|
|
|
if (!host.isEmpty())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
QString authToken = params["token"].toString("");
|
|
|
|
QString filter = params["filter"].toString("");
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Resolve hostname and port (or use default API port)
|
2020-11-14 17:58:56 +01:00
|
|
|
QStringList addressparts = QStringUtils::split(host, ":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
2020-07-12 20:27:56 +02:00
|
|
|
QString apiHost = addressparts[0];
|
|
|
|
int apiPort;
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (addressparts.size() > 1)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
apiPort = addressparts[1].toInt();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
apiPort = API_DEFAULT_PORT;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
initRestAPI(apiHost, apiPort, authToken);
|
|
|
|
_restApi->setPath(filter);
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Perform request
|
|
|
|
httpResponse response = _restApi->get();
|
2020-11-14 17:58:56 +01:00
|
|
|
if (response.error())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Warning(_log, "%s get properties failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
properties.insert("properties", response.getBody().object());
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return properties;
|
2019-04-08 23:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
void LedDeviceNanoleaf::identify(const QJsonObject& params)
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
QString host = params["host"].toString("");
|
2020-11-14 17:58:56 +01:00
|
|
|
if (!host.isEmpty())
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString authToken = params["token"].toString("");
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Resolve hostname and port (or use default API port)
|
2020-11-14 17:58:56 +01:00
|
|
|
QStringList addressparts = QStringUtils::split(host, ":", QStringUtils::SplitBehavior::SkipEmptyParts);
|
2020-07-12 20:27:56 +02:00
|
|
|
QString apiHost = addressparts[0];
|
|
|
|
int apiPort;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (addressparts.size() > 1)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
apiPort = addressparts[1].toInt();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
apiPort = API_DEFAULT_PORT;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
initRestAPI(apiHost, apiPort, authToken);
|
|
|
|
_restApi->setPath("identify");
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// Perform request
|
|
|
|
httpResponse response = _restApi->put();
|
2020-11-14 17:58:56 +01:00
|
|
|
if (response.error())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
Warning(_log, "%s identification failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
bool LedDeviceNanoleaf::powerOn()
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_isDeviceReady)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-09-14 17:19:14 +02:00
|
|
|
changeToExternalControlMode();
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
//Power-on Nanoleaf device
|
|
|
|
_restApi->setPath(API_STATE);
|
2020-11-14 17:58:56 +01:00
|
|
|
_restApi->put(getOnOffRequest(true));
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LedDeviceNanoleaf::powerOff()
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
if (_isDeviceReady)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
//Power-off the Nanoleaf device physically
|
|
|
|
_restApi->setPath(API_STATE);
|
2020-11-14 17:58:56 +01:00
|
|
|
_restApi->put(getOnOffRequest(false));
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return true;
|
2019-04-08 23:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QString LedDeviceNanoleaf::getOnOffRequest(bool isOn) const
|
2019-04-08 23:13:11 +02:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString state = isOn ? STATE_VALUE_TRUE : STATE_VALUE_FALSE;
|
2020-11-14 17:58:56 +01:00
|
|
|
return QString("{\"%1\":{\"%2\":%3}}").arg(STATE_ON, STATE_ONOFF_VALUE, state);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode()
|
|
|
|
{
|
2020-09-14 17:19:14 +02:00
|
|
|
Debug(_log, "Set Nanoleaf to External Control (UDP) streaming mode");
|
2020-07-12 20:27:56 +02:00
|
|
|
_extControlVersion = EXTCTRLVER_V2;
|
|
|
|
//Enable UDP Mode v2
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
_restApi->setPath(API_EFFECT);
|
2020-11-14 17:58:56 +01:00
|
|
|
httpResponse response = _restApi->put(API_EXT_MODE_STRING_V2);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
return response.getBody();
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
int LedDeviceNanoleaf::write(const std::vector<ColorRgb>& ledValues)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2019-12-08 13:12:01 +01:00
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// nPanels 2B
|
|
|
|
// panelID 2B
|
|
|
|
// <R> <G> <B> 3B
|
|
|
|
// <W> 1B
|
|
|
|
// tranitionTime 2B
|
|
|
|
//
|
|
|
|
// Note: Nanoleaf Light Panels (Aurora) now support External Control V2 (tested with FW 3.2.0)
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
int udpBufferSize = STREAM_FRAME_PANEL_NUM_SIZE + _panelLedCount * STREAM_FRAME_PANEL_INFO_SIZE;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QByteArray udpbuffer;
|
|
|
|
udpbuffer.resize(udpBufferSize);
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
int i = 0;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
// Set number of panels
|
2020-11-14 17:58:56 +01:00
|
|
|
qToBigEndian<quint16>(static_cast<quint16>(_panelLedCount), udpbuffer.data() + i);
|
|
|
|
i += 2;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
ColorRgb color;
|
2020-06-12 11:16:39 +02:00
|
|
|
|
|
|
|
//Maintain LED counter independent from PanelCounter
|
2020-11-14 17:58:56 +01:00
|
|
|
int ledCounter = 0;
|
|
|
|
for (int panelCounter = 0; panelCounter < _panelLedCount; panelCounter++)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
int panelID = _panelIds[panelCounter];
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
// Set panels configured
|
2020-11-14 17:58:56 +01:00
|
|
|
if (panelCounter >= _startPos && panelCounter <= _endPos) {
|
2020-06-12 11:16:39 +02:00
|
|
|
color = static_cast<ColorRgb>(ledValues.at(ledCounter));
|
|
|
|
++ledCounter;
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-12 11:16:39 +02:00
|
|
|
// Set panels not configured to black;
|
2019-12-08 13:12:01 +01:00
|
|
|
color = ColorRgb::BLACK;
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose3, _log, "[%d] >= panelLedCount [%d] => Set to BLACK", panelCounter, _panelLedCount);
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set panelID
|
2020-11-14 17:58:56 +01:00
|
|
|
qToBigEndian<quint16>(static_cast<quint16>(panelID), udpbuffer.data() + i);
|
|
|
|
i += 2;
|
2019-12-08 13:12:01 +01:00
|
|
|
|
|
|
|
// Set panel's color LEDs
|
2020-11-14 17:58:56 +01:00
|
|
|
udpbuffer[i++] = static_cast<char>(color.red);
|
|
|
|
udpbuffer[i++] = static_cast<char>(color.green);
|
|
|
|
udpbuffer[i++] = static_cast<char>(color.blue);
|
2019-12-08 13:12:01 +01:00
|
|
|
|
|
|
|
// 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
|
2020-11-14 17:58:56 +01:00
|
|
|
qToBigEndian<quint16>(static_cast<quint16>(tranitionTime), udpbuffer.data() + i);
|
|
|
|
i += 2;
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
DebugIf(verbose3, _log, "[%u] Color: {%u,%u,%u}", panelCounter, color.red, color.green, color.blue);
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2019-04-08 23:13:11 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
if (verbose3)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
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)));
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-11-14 17:58:56 +01:00
|
|
|
|
|
|
|
retVal = writeBytes(udpbuffer);
|
|
|
|
return retVal;
|
2019-04-08 23:13:11 +02:00
|
|
|
}
|