From ff477cd2c7d53a2bee1606992f570b0d43185fe6 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Sun, 5 Jan 2020 11:49:00 +0100 Subject: [PATCH] added effects --- dynatrace/deviceplugindynatrace.cpp | 127 ++++++++++++++++++++++++--- dynatrace/deviceplugindynatrace.json | 75 ++++++++++------ dynatrace/ufo.cpp | 96 ++++++++++++++++++-- dynatrace/ufo.h | 6 +- 4 files changed, 257 insertions(+), 47 deletions(-) diff --git a/dynatrace/deviceplugindynatrace.cpp b/dynatrace/deviceplugindynatrace.cpp index 3f9edb64..e9a61b63 100644 --- a/dynatrace/deviceplugindynatrace.cpp +++ b/dynatrace/deviceplugindynatrace.cpp @@ -92,13 +92,12 @@ void DevicePluginDynatrace::setupDevice(DeviceSetupInfo *info) if (error.error != QJsonParseError::NoError) { info->finish(Device::DeviceErrorSetupFailed, error.errorString()); } - QString id = data.toVariant().toMap().value("ufoid").toString(); info->device()->setParamValue(ufoDeviceIdParamTypeId, id); info->finish(Device::DeviceErrorNoError); }); } else { - // Discovery device setup + // Discovery device setup or devices setup caused by nymea restart info->finish(Device::DeviceErrorNoError); } } @@ -107,10 +106,18 @@ void DevicePluginDynatrace::setupDevice(DeviceSetupInfo *info) void DevicePluginDynatrace::postSetupDevice(Device *device) { if (device->deviceClassId() == ufoDeviceClassId) { + device->setStateValue(ufoConnectedStateTypeId, true); //FIXME + device->setStateValue(ufoPowerStateTypeId, false); + device->setStateValue(ufoLogoStateTypeId, false); + device->setStateValue(ufoEffectTopStateTypeId, "None"); + device->setStateValue(ufoEffectBottomStateTypeId, "None"); + QHostAddress address = QHostAddress(device->paramValue(ufoDeviceHostParamTypeId).toString()); Ufo *ufo = new Ufo(hardwareManager()->networkManager(), address, this); m_ufoConnections.insert(device->id(), ufo); - ufo->resetLogo(); + // Set all off + ufo->setLogo(QColor(Qt::black), QColor(Qt::black), QColor(Qt::black), QColor(Qt::black)); + ufo->setBackgroundColor(true, true, true, true, QColor(Qt::black)); } if(!m_pluginTimer) { @@ -134,11 +141,12 @@ void DevicePluginDynatrace::executeAction(DeviceActionInfo *info) bool power = action.param(ufoLogoActionLogoParamTypeId).value().toBool(); device->setStateValue(ufoLogoStateTypeId, power); if (power) { - ufo->resetLogo(); - } else { - QColor color; - color.setRgb(0, 0, 0); + int brightness = device->stateValue(ufoBrightnessActionBrightnessParamTypeId).toInt(); + QColor color = QColor(device->stateValue(ufoLogoColorStateTypeId).toString()); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); ufo->setLogo(color, color, color, color); + } else { + ufo->setLogo(QColor(Qt::black), QColor(Qt::black), QColor(Qt::black), QColor(Qt::black)); } info->finish(Device::DeviceErrorNoError); @@ -146,30 +154,123 @@ void DevicePluginDynatrace::executeAction(DeviceActionInfo *info) bool power = action.param(ufoPowerActionPowerParamTypeId).value().toBool(); device->setStateValue(ufoPowerStateTypeId, power); if (power) { - ufo->setBackgroundColor(true, true, QColor(Qt::white)); //#ffffff + int brightness = device->stateValue(ufoBrightnessActionBrightnessParamTypeId).toInt(); + QColor color = QColor(device->stateValue(ufoColorStateTypeId).toString()); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + device->setStateValue(ufoLogoStateTypeId, true); + ufo->setLogo(color, color, color, color); + ufo->setBackgroundColor(true, true, true, true, color); + device->setStateValue(ufoEffectTopStateTypeId, "None"); + device->setStateValue(ufoEffectBottomStateTypeId, "None"); + device->setStateValue(ufoLogoColorStateTypeId, color); + device->setStateValue(ufoTopColorStateTypeId, color); + device->setStateValue(ufoBottomColorStateTypeId, color); } else { - ufo->setBackgroundColor(true, true, QColor(Qt::black)); //#000000 + ufo->setLogo(QColor(Qt::black), QColor(Qt::black), QColor(Qt::black), QColor(Qt::black)); + device->setStateValue(ufoLogoStateTypeId, false); + ufo->setBackgroundColor(true, true, true, true, QColor(Qt::black)); + device->setStateValue(ufoEffectTopStateTypeId, "None"); + device->setStateValue(ufoEffectBottomStateTypeId, "None"); } info->finish(Device::DeviceErrorNoError); } else if (action.actionTypeId() == ufoBrightnessActionTypeId) { int brightness = action.param(ufoBrightnessActionBrightnessParamTypeId).value().toInt(); + device->setStateValue(ufoBrightnessStateTypeId, brightness); QColor color = QColor(device->stateValue(ufoColorStateTypeId).toString()); - color.setHsv(color.hue(), color.saturation(), brightness); - ufo->setBackgroundColor(true, true, color); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + if (device->stateValue(ufoLogoStateTypeId).toBool()) { + ufo->setLogo(color, color, color, color); + } + ufo->setBackgroundColor(true, false, true, false, color); info->finish(Device::DeviceErrorNoError); } else if (action.actionTypeId() == ufoColorActionTypeId) { QColor color = QColor(action.param(ufoColorActionColorParamTypeId).value().toString()); int brightness = device->stateValue(ufoBrightnessStateTypeId).toInt(); device->setStateValue(ufoColorStateTypeId, color); - color.setHsv(color.hue(), color.saturation(), brightness); - ufo->setBackgroundColor(true, true, color); + device->setStateValue(ufoLogoColorStateTypeId, color); + device->setStateValue(ufoTopColorStateTypeId, color); + device->setStateValue(ufoBottomColorStateTypeId, color); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + if (device->stateValue(ufoLogoStateTypeId).toBool()) { + ufo->setLogo(color, color, color, color); + } + ufo->setBackgroundColor(true, false, true, false, color); + info->finish(Device::DeviceErrorNoError); + } else if (action.actionTypeId() == ufoColorTemperatureActionTypeId) { + int mired= device->stateValue(ufoColorTemperatureActionColorTemperatureParamTypeId).toInt(); + device->setStateValue(ufoColorTemperatureStateTypeId, mired); + int brightness = device->stateValue(ufoBrightnessActionBrightnessParamTypeId).toInt(); + QColor color(Qt::white); + color.setBlue(static_cast((mired-153)*0.73)); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + if (device->stateValue(ufoLogoStateTypeId).toBool()) { + ufo->setLogo(color, color, color, color); + } + device->setStateValue(ufoColorStateTypeId, color); + device->setStateValue(ufoLogoColorStateTypeId, color); + device->setStateValue(ufoTopColorStateTypeId, color); + device->setStateValue(ufoBottomColorStateTypeId, color); + ufo->setBackgroundColor(true, false, true, false, color); + info->finish(Device::DeviceErrorNoError); + + } else if (action.actionTypeId() == ufoEffectTopActionTypeId) { + QString effect = action.param(ufoEffectTopActionEffectTopParamTypeId).value().toString(); + int brightness = device->stateValue(ufoBrightnessActionBrightnessParamTypeId).toInt(); + QColor color = QColor(device->stateValue(ufoColorStateTypeId).toString()); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + if (effect == "None") { + ufo->setBackgroundColor(true, true, false, false, color); + } else if (effect == "Whirl") { + ufo->startWhirl(true, false, color, 500, true); + } else if (effect == "Morph") { + ufo->startMorph(true, false, color, 250, 8); + } + info->finish(Device::DeviceErrorNoError); + } else if (action.actionTypeId() == ufoEffectBottomActionTypeId) { + QString effect = action.param(ufoEffectBottomActionEffectBottomParamTypeId).value().toString(); + int brightness = device->stateValue(ufoBrightnessActionBrightnessParamTypeId).toInt(); + QColor color = QColor(device->stateValue(ufoColorStateTypeId).toString()); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + if (effect == "None") { + ufo->setBackgroundColor(false, false, true, true, color); + } else if (effect == "Whirl") { + ufo->startWhirl(false, true, color, 500, true); + } else if (effect == "Morph") { + ufo->startMorph(false, true, color, 250, 8); + } + info->finish(Device::DeviceErrorNoError); + } else if (action.actionTypeId() == ufoLogoColorActionTypeId) { + QColor color = QColor(action.param(ufoLogoColorActionLogoColorParamTypeId).value().toString()); + int brightness = device->stateValue(ufoBrightnessStateTypeId).toInt(); + device->setStateValue(ufoLogoColorStateTypeId, color); + device->setStateValue(ufoLogoStateTypeId, true); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + ufo->setLogo(color, color, color, color); + info->finish(Device::DeviceErrorNoError); + } else if (action.actionTypeId() == ufoTopColorActionTypeId) { + QColor color = QColor(action.param(ufoTopColorActionTopColorParamTypeId).value().toString()); + int brightness = device->stateValue(ufoBrightnessStateTypeId).toInt(); + device->setStateValue(ufoTopColorStateTypeId, color); + device->setStateValue(ufoPowerStateTypeId, true); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + ufo->setBackgroundColor(true, false, false, false, color); + info->finish(Device::DeviceErrorNoError); + } else if (action.actionTypeId() == ufoBottomColorActionTypeId) { + QColor color = QColor(action.param(ufoBottomColorActionBottomColorParamTypeId).value().toString()); + int brightness = device->stateValue(ufoBrightnessStateTypeId).toInt(); + device->setStateValue(ufoBottomColorStateTypeId, color); + device->setStateValue(ufoPowerStateTypeId, true); + color.setHsv(color.hue(), color.saturation(), brightness*2.55); + ufo->setBackgroundColor(false, false, true, false, color); info->finish(Device::DeviceErrorNoError); } else { qCWarning(dcDynatrace()) << "Execute action: Unhandled actionTypeId"; + info->finish(Device::DeviceErrorHardwareFailure); } } else { qCWarning(dcDynatrace()) << "Execute action: Unhandled deviceClass"; + info->finish(Device::DeviceErrorHardwareFailure); } } diff --git a/dynatrace/deviceplugindynatrace.json b/dynatrace/deviceplugindynatrace.json index e2eac5e2..69347cd9 100644 --- a/dynatrace/deviceplugindynatrace.json +++ b/dynatrace/deviceplugindynatrace.json @@ -13,7 +13,7 @@ "name": "ufo", "displayName": "ufo", "createMethods": ["user", "discovery"], - "interfaces": ["colorlight", "alert", "connectable"], + "interfaces": ["colorlight", "connectable"], "paramTypes": [ { "id": "3e14968b-954e-4e85-ae79-3de9ae4fe951", @@ -31,26 +31,6 @@ "readOnly": true } ], - "actionTypes": [ - { - "id": "6efa52d9-527b-4ac7-8885-50f2b1786561", - "name": "alert", - "displayName": "flash", - "paramTypes": [ - { - "id": "e8d854ce-02d0-4ddd-a2bd-0c464cbb9182", - "name": "alert", - "displayName": "alert", - "type": "QString", - "defaultValue": "flash", - "allowedValues": [ - "flash", - "flash 15 [s]" - ] - } - ] - } - ], "stateTypes": [ { "id": "1a439907-e810-4dea-b357-dd32281896e7", @@ -118,10 +98,10 @@ }, { "id": "53da0021-974a-434a-94ac-3f187aad1480", - "name": "effect", - "displayName": "Effect", - "displayNameEvent": "Effect changed", - "displayNameAction": "Set effect", + "name": "effectTop", + "displayName": "Effect top", + "displayNameEvent": "Effect top changed", + "displayNameAction": "Set top effect", "type": "QString", "defaultValue": "None", "possibleValues": [ @@ -130,6 +110,51 @@ "Whirl" ], "writable": true + }, + { + "id": "0fab896b-9900-4375-96c0-0d38460cee65", + "name": "effectBottom", + "displayName": "Effect bottom", + "displayNameEvent": "Effect bottom changed", + "displayNameAction": "Set bottom effect", + "type": "QString", + "defaultValue": "None", + "possibleValues": [ + "None", + "Morph", + "Whirl" + ], + "writable": true + }, + { + "id": "d3eb4a99-20cc-4d47-af7d-33453aca2087", + "name": "logoColor", + "displayName": "Logo color", + "displayNameEvent": "Logo color changed", + "displayNameAction": "Set logo color", + "type": "QColor", + "defaultValue": "#000000", + "writable": true + }, + { + "id": "b378290a-468f-45ef-9d65-00546737ce9b", + "name": "topColor", + "displayName": "Top color", + "displayNameEvent": "Top color changed", + "displayNameAction": "Set top color", + "type": "QColor", + "defaultValue": "#000000", + "writable": true + }, + { + "id": "ef2a8d5b-82d4-43d5-b9ec-82ef5662b971", + "name": "bottomColor", + "displayName": "Bottom color", + "displayNameEvent": "Bottom color changed", + "displayNameAction": "Set bottom color", + "type": "QColor", + "defaultValue": "#000000", + "writable": true } ] } diff --git a/dynatrace/ufo.cpp b/dynatrace/ufo.cpp index ff9232d6..b558d694 100644 --- a/dynatrace/ufo.cpp +++ b/dynatrace/ufo.cpp @@ -98,15 +98,16 @@ void Ufo::initBackgroundColor(bool top, bool bottom) url.setScheme("http"); url.setHost(m_address.toString()); url.setPath("/api"); + QUrlQuery query; if (top) { - url.setQuery("top_init"); + query.addQueryItem("top_init", "0"); } if (bottom) { - url.setQuery("bottom_init"); + query.addQueryItem("bottom_init", "0"); } + url.setQuery(query); QNetworkRequest request; request.setUrl(url); - qCDebug(dcDynatrace()) << "Sending request" << url; QNetworkReply *reply = m_networkManager->get(request); connect(reply, &QNetworkReply::finished, this, [reply, this] { @@ -123,19 +124,23 @@ void Ufo::initBackgroundColor(bool top, bool bottom) }); } -void Ufo::setBackgroundColor(bool top, bool bottom, QColor color) +void Ufo::setBackgroundColor(bool top, bool initTop, bool bottom, bool initBottom, QColor color) { QUrl url; url.setScheme("http"); url.setHost(m_address.toString()); url.setPath("/api"); QUrlQuery query; - if (top){ + if (initTop){ query.addQueryItem("top_init", "0"); + } + if (initBottom){ + query.addQueryItem("bottom_init", "0"); + } + if (top){ query.addQueryItem("top_bg", color.name().remove(0,1)); } if (bottom) { - query.addQueryItem("bottom_init", "0"); query.addQueryItem("bottom_bg", color.name().remove(0,1)); } url.setQuery(query); @@ -157,3 +162,82 @@ void Ufo::setBackgroundColor(bool top, bool bottom, QColor color) emit connectionChanged(true); }); } + +void Ufo::startWhirl(bool top, bool bottom, QColor color, int speed, bool clockwise) +{ + Q_UNUSED(clockwise) + QUrl url; + url.setScheme("http"); + url.setHost(m_address.toString()); + url.setPath("/api"); + QUrlQuery query; + if (top){ + query.addQueryItem("top_init", "0"); + query.addQueryItem("top_bg", color.name().remove(0,1)); + query.addQueryItem("top", "0|8|000000"); + query.addQueryItem("top_whirl", QString::number(speed)); + } + if (bottom) { + query.addQueryItem("bottom_init", "0"); + query.addQueryItem("bottom_bg", color.name().remove(0,1)); + query.addQueryItem("bottom", "0|8|000000"); + query.addQueryItem("bottom_whirl", QString::number(speed)); + } + url.setQuery(query); + QNetworkRequest request; + request.setUrl(url); + + qCDebug(dcDynatrace()) << "Sending request" << url; + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + // Check HTTP status code + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcDynatrace()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + emit connectionChanged(true); + }); +} + +void Ufo::startMorph(bool top, bool bottom, QColor color, int time, int speed) +{ + QUrl url; + url.setScheme("http"); + url.setHost(m_address.toString()); + url.setPath("/api"); + QUrlQuery query; + if (top){ + query.addQueryItem("top_init", "0"); + query.addQueryItem("top_bg", color.name().remove(0,1)); + query.addQueryItem("top", "0|16|000000"); + query.addQueryItem("top_morph", QString::number(time)+"|"+QString::number(speed)); + } + if (bottom) { + query.addQueryItem("bottom_init", "0"); + query.addQueryItem("bottom_bg", color.name().remove(0,1)); + query.addQueryItem("bottom", "0|16|000000"); + query.addQueryItem("bottom_morph", QString::number(time)+"|"+QString::number(speed)); + } + url.setQuery(query); + QNetworkRequest request; + request.setUrl(url); + + qCDebug(dcDynatrace()) << "Sending request" << url; + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + // Check HTTP status code + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcDynatrace()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + emit connectionChanged(true); + }); +} diff --git a/dynatrace/ufo.h b/dynatrace/ufo.h index a2e1ebdc..b1427876 100644 --- a/dynatrace/ufo.h +++ b/dynatrace/ufo.h @@ -46,11 +46,11 @@ public: void resetLogo(); void setLogo(QColor led1, QColor led2, QColor led3, QColor led4); void initBackgroundColor(bool top, bool bottom); - void setBackgroundColor(bool top, bool bottom, QColor color); + void setBackgroundColor(bool top, bool initTop, bool bottom, bool initBottom, QColor color); //top and bottom flags are to select the ring, init is to reset a effect void setLeds(bool top, int ledIndex, int numOfLeds, QColor color); - void startWhirl(bool top, bool bottom, int speed, bool clockwise); + void startWhirl(bool top, bool bottom, QColor color, int speed, bool clockwise); //Speed: 0 (no movement) to about 510 (very fast) void stopWhirl(bool top, bool bottom); - void startMorph(bool top, bool bottom, int speed, bool time); + void startMorph(bool top, bool bottom, QColor color, int time, int speed); //time in ms, speed 0-10 void stopMorph(bool top, bool bottom);