Pull changes from other branch

This commit is contained in:
Simon Stürz 2021-02-05 17:07:29 +01:00 committed by Boernsman
parent 7f6ceb4171
commit 17dc0f7186
3 changed files with 214 additions and 323 deletions

View File

@ -41,13 +41,15 @@
and \l{Vendor}{Vendors} of this \l{DevicePlugin}. and \l{Vendor}{Vendors} of this \l{DevicePlugin}.
For more details how to read this JSON file please check out the documentation for \l{The plugin JSON File}. For more details how to read this JSON file please check out the documentation for \l{The plugin JSON File}.
s
\quotefile plugins/deviceplugins/ws2812fx/devicepluginws2812fx.json \quotefile plugins/deviceplugins/ws2812fx/devicepluginws2812fx.json
*/ */
#include <QColor> #include <QColor>
#include "integrationpluginws2812fx.h" #include "integrationpluginws2812fx.h"
#include "plugininfo.h" #include "plugininfo.h"
#include "nymealightserialinterface.h"
IntegrationPluginWs2812fx ::IntegrationPluginWs2812fx () IntegrationPluginWs2812fx ::IntegrationPluginWs2812fx ()
{ {
} }
@ -63,36 +65,26 @@ void IntegrationPluginWs2812fx::setupThing(ThingSetupInfo *info)
return; return;
} }
QSerialPort *serialPort = new QSerialPort(interface, this); NymeaLightSerialInterface *lightInterface = new NymeaLightSerialInterface(interface, thing);
NymeaLight *light = new NymeaLight(lightInterface, this);
lightInterface->setParent(light);
serialPort->setBaudRate(115200); if (!lightInterface->open()) {
serialPort->setDataBits(QSerialPort::DataBits::Data8); qCWarning(dcWs2812fx()) << "Could not open interface" << interface;
serialPort->setParity(QSerialPort::Parity::NoParity); light->deleteLater();
serialPort->setStopBits(QSerialPort::StopBits::OneStop); info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error opening serial port."));
serialPort->setFlowControl(QSerialPort::FlowControl::NoFlowControl); return;
if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcWs2812fx()) << "Could not open serial port" << interface << serialPort->errorString();
serialPort->deleteLater();
return info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error opening serial port."));
} }
connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); connect(light, &NymeaLight::availableChanged, thing, [=](bool available){
connect(serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); qCDebug(dcWs2812fx()) << thing << "available changed" << available;
thing->setStateValue(ws2812fxConnectedStateTypeId, available);
});
qCDebug(dcWs2812fx()) << "Setup successfully serial port" << interface; qCDebug(dcWs2812fx()) << "Setup successfully serial port" << interface;
thing->setStateValue(ws2812fxConnectedStateTypeId, true); thing->setStateValue(ws2812fxConnectedStateTypeId, true);
m_usedInterfaces.append(interface); m_usedInterfaces.append(interface);
m_serialPorts.insert(thing, serialPort); m_lights.insert(thing, light);
if(!m_reconnectTimer) {
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setSingleShot(true);
m_reconnectTimer->setInterval(5000);
connect(m_reconnectTimer, &QTimer::timeout, this, &IntegrationPluginWs2812fx::onReconnectTimer);
}
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} }
@ -122,182 +114,202 @@ void IntegrationPluginWs2812fx::executeAction(ThingActionInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
Action action = info->action(); Action action = info->action();
NymeaLight *light = m_lights.value(thing);
if (!light || !light->available()) {
QByteArray command; info->finish(Thing::ThingErrorHardwareNotAvailable);
if (action.actionTypeId() == ws2812fxPowerActionTypeId) { return;
command.append("b ");
if (action.param(ws2812fxPowerActionPowerParamTypeId).value().toBool()) {
command.append("30");
} else {
command.append("0");
}
command.append("\r\n");
return sendCommand(info, command, CommandType::Brightness);
}
if (action.actionTypeId() == ws2812fxBrightnessActionTypeId) {
command.append("b ");
command.append(action.param(ws2812fxBrightnessActionBrightnessParamTypeId).value().toString());
command.append("\r\n");
return sendCommand(info, command, CommandType::Brightness);
}
if (action.actionTypeId() == ws2812fxSpeedActionTypeId) {
command.append("s ");
command.append(action.param(ws2812fxSpeedActionSpeedParamTypeId).value().toString());
command.append("\r\n");
return sendCommand(info, command, CommandType::Speed);
} }
if (action.actionTypeId() == ws2812fxColorActionTypeId) { if (action.actionTypeId() == ws2812fxColorActionTypeId) {
QColor color = action.param(ws2812fxColorActionColorParamTypeId).value().value<QColor>();
QColor color; qCDebug(dcWs2812fx()) << "Set color to" << color.name(QColor::HexRgb);
color= action.param(ws2812fxColorActionColorParamTypeId).value().value<QColor>(); NymeaLightInterfaceReply *reply = light->setColor(color);
command.append("c "); connect(info, &ThingActionInfo::aborted, reply, &NymeaLightInterfaceReply::finished);
command.append(QString(color.name()).remove("#")); connect(reply, &NymeaLightInterfaceReply::finished, this, [=](){
command.append("\r\n"); if (reply->status() != NymeaLightInterface::StatusSuccess) {
return sendCommand(info, command, CommandType::Color); info->finish(Thing::ThingErrorHardwareFailure);
return;
}
qCDebug(dcWs2812fx()) << "Set color finished successfully" << color.name(QColor::HexRgb);
thing->setStateValue(ws2812fxColorStateTypeId, color);
info->finish(Thing::ThingErrorNoError);
});
} }
if (action.actionTypeId() == ws2812fxColorTemperatureActionTypeId) { // QByteArray command;
// if (action.actionTypeId() == ws2812fxPowerActionTypeId) {
// command.append("b ");
// if (action.param(ws2812fxPowerActionPowerParamTypeId).value().toBool()) {
// command.append("30");
// } else {
// command.append("0");
// }
// command.append("\r\n");
// return sendCommand(info, command, CommandType::Brightness);
// }
// minValue 153, maxValue 500 // if (action.actionTypeId() == ws2812fxBrightnessActionTypeId) {
QColor color;
color.setRgb(255, 255, static_cast<int>((255.00-(((action.param(ws2812fxColorTemperatureActionColorTemperatureParamTypeId).value().toDouble()-153.00)/347.00))*255.00)));
thing->setStateValue(ws2812fxColorTemperatureStateTypeId, action.param(ws2812fxColorTemperatureActionColorTemperatureParamTypeId).value());
command.append("c ");
command.append(QString(color.name()).remove("#"));
command.append("\r\n");
return sendCommand(info, command, CommandType::Color);
}
if (action.actionTypeId() == ws2812fxEffectModeActionTypeId) { // command.append("b ");
// command.append(action.param(ws2812fxBrightnessActionBrightnessParamTypeId).value().toString());
// command.append("\r\n");
// return sendCommand(info, command, CommandType::Brightness);
// }
QString effectMode = action.param(ws2812fxEffectModeActionEffectModeParamTypeId).value().toString(); // if (action.actionTypeId() == ws2812fxSpeedActionTypeId) {
command.append("m ");
if (effectMode == "Static") { // command.append("s ");
command.append(QString::number(FX_MODE_STATIC)); // command.append(action.param(ws2812fxSpeedActionSpeedParamTypeId).value().toString());
} else if (effectMode == "Blink") { // command.append("\r\n");
command.append(QString::number(FX_MODE_BLINK)); // return sendCommand(info, command, CommandType::Speed);
} else if (effectMode == "Color Wipe") { // }
command.append(QString::number(FX_MODE_COLOR_WIPE));
} else if (effectMode == "Color Wipe Inverse") { // if (action.actionTypeId() == ws2812fxColorActionTypeId) {
command.append(QString::number(FX_MODE_COLOR_WIPE_INV));
} else if (effectMode == "Color Wipe Reverse") { // QColor color;
command.append(QString::number(FX_MODE_COLOR_WIPE_REV)); // color= action.param(ws2812fxColorActionColorParamTypeId).value().value<QColor>();
} else if (effectMode == "Color Wipe Reverse Inverse") { // command.append("c ");
command.append(QString::number(FX_MODE_COLOR_WIPE_REV_INV)); // command.append(QString(color.name()).remove("#"));
} else if (effectMode == "Color Wipe Random") { // command.append("\r\n");
command.append(QString::number(FX_MODE_COLOR_WIPE_RANDOM)); // return sendCommand(info, command, CommandType::Color);
} else if (effectMode == "Random Color") { // }
command.append(QString::number(FX_MODE_RANDOM_COLOR));
} else if (effectMode == "Single Dynamic") { // if (action.actionTypeId() == ws2812fxColorTemperatureActionTypeId) {
command.append(QString::number(FX_MODE_SINGLE_DYNAMIC));
} else if (effectMode == "Multi Dynamic") { // // minValue 153, maxValue 500
command.append(QString::number(FX_MODE_MULTI_DYNAMIC)); // QColor color;
} else if (effectMode == "Rainbow") { // color.setRgb(255, 255, static_cast<int>((255.00-(((action.param(ws2812fxColorTemperatureActionColorTemperatureParamTypeId).value().toDouble()-153.00)/347.00))*255.00)));
command.append(QString::number(FX_MODE_RAINBOW)); // thing->setStateValue(ws2812fxColorTemperatureStateTypeId, action.param(ws2812fxColorTemperatureActionColorTemperatureParamTypeId).value());
} else if (effectMode == "Rainbow Cycle") { // command.append("c ");
command.append(QString::number(FX_MODE_RAINBOW_CYCLE)); // command.append(QString(color.name()).remove("#"));
} else if (effectMode == "Scan") { // command.append("\r\n");
command.append(QString::number(FX_MODE_SCAN)); // return sendCommand(info, command, CommandType::Color);
} else if (effectMode == "Dual Scan") { // }
command.append(QString::number(FX_MODE_DUAL_SCAN));
} else if (effectMode == "Fade") { // if (action.actionTypeId() == ws2812fxEffectModeActionTypeId) {
command.append(QString::number(FX_MODE_FADE));
} else if (effectMode == "Theater Chase") { // QString effectMode = action.param(ws2812fxEffectModeActionEffectModeParamTypeId).value().toString();
command.append(QString::number(FX_MODE_THEATER_CHASE)); // command.append("m ");
} else if (effectMode == "Theater Chase Rainbow") { // if (effectMode == "Static") {
command.append(QString::number(FX_MODE_THEATER_CHASE_RAINBOW)); // command.append(QString::number(FX_MODE_STATIC));
} else if (effectMode == "Running Lights") { // } else if (effectMode == "Blink") {
command.append(QString::number(FX_MODE_RUNNING_LIGHTS)); // command.append(QString::number(FX_MODE_BLINK));
} else if (effectMode == "Twinkle") { // } else if (effectMode == "Color Wipe") {
command.append(QString::number(FX_MODE_TWINKLE)); // command.append(QString::number(FX_MODE_COLOR_WIPE));
} else if (effectMode == "Twinkle Random") { // } else if (effectMode == "Color Wipe Inverse") {
command.append(QString::number(FX_MODE_TWINKLE_RANDOM)); // command.append(QString::number(FX_MODE_COLOR_WIPE_INV));
} else if (effectMode == "Twinkle Fade") { // } else if (effectMode == "Color Wipe Reverse") {
command.append(QString::number(FX_MODE_TWINKLE_FADE)); // command.append(QString::number(FX_MODE_COLOR_WIPE_REV));
} else if (effectMode == "Twinkle Fade Random") { // } else if (effectMode == "Color Wipe Reverse Inverse") {
command.append(QString::number(FX_MODE_TWINKLE_FADE_RANDOM)); // command.append(QString::number(FX_MODE_COLOR_WIPE_REV_INV));
} else if (effectMode == "Sparkle") { // } else if (effectMode == "Color Wipe Random") {
command.append(QString::number(FX_MODE_SPARKLE)); // command.append(QString::number(FX_MODE_COLOR_WIPE_RANDOM));
} else if (effectMode == "Flash Sparkle") { // } else if (effectMode == "Random Color") {
command.append(QString::number(FX_MODE_FLASH_SPARKLE)); // command.append(QString::number(FX_MODE_RANDOM_COLOR));
} else if (effectMode == "Hyper Sparkle") { // } else if (effectMode == "Single Dynamic") {
command.append(QString::number(FX_MODE_HYPER_SPARKLE)); // command.append(QString::number(FX_MODE_SINGLE_DYNAMIC));
} else if (effectMode == "Strobe") { // } else if (effectMode == "Multi Dynamic") {
command.append(QString::number(FX_MODE_STROBE)); // command.append(QString::number(FX_MODE_MULTI_DYNAMIC));
} else if (effectMode == "Strobe Rainbow") { // } else if (effectMode == "Rainbow") {
command.append(QString::number(FX_MODE_STROBE_RAINBOW)); // command.append(QString::number(FX_MODE_RAINBOW));
} else if (effectMode == "Multi Strobe") { // } else if (effectMode == "Rainbow Cycle") {
command.append(QString::number(FX_MODE_MULTI_STROBE)); // command.append(QString::number(FX_MODE_RAINBOW_CYCLE));
} else if (effectMode == "Blink Rainbow") { // } else if (effectMode == "Scan") {
command.append(QString::number(FX_MODE_BLINK_RAINBOW)); // command.append(QString::number(FX_MODE_SCAN));
} else if (effectMode == "Chase White") { // } else if (effectMode == "Dual Scan") {
command.append(QString::number(FX_MODE_CHASE_WHITE)); // command.append(QString::number(FX_MODE_DUAL_SCAN));
} else if (effectMode == "Chase Color") { // } else if (effectMode == "Fade") {
command.append(QString::number(FX_MODE_CHASE_COLOR)); // command.append(QString::number(FX_MODE_FADE));
} else if (effectMode == "Chase Random") { // } else if (effectMode == "Theater Chase") {
command.append(QString::number(FX_MODE_CHASE_RANDOM)); // command.append(QString::number(FX_MODE_THEATER_CHASE));
} else if (effectMode == "Chase Flash") { // } else if (effectMode == "Theater Chase Rainbow") {
command.append(QString::number(FX_MODE_CHASE_FLASH)); // command.append(QString::number(FX_MODE_THEATER_CHASE_RAINBOW));
} else if (effectMode == "Chase Flash Random") { // } else if (effectMode == "Running Lights") {
command.append(QString::number(FX_MODE_CHASE_FLASH_RANDOM)); // command.append(QString::number(FX_MODE_RUNNING_LIGHTS));
} else if (effectMode == "Chase Rainbow White") { // } else if (effectMode == "Twinkle") {
command.append(QString::number(FX_MODE_CHASE_RAINBOW_WHITE)); // command.append(QString::number(FX_MODE_TWINKLE));
} else if (effectMode == "Chase Blackout") { // } else if (effectMode == "Twinkle Random") {
command.append(QString::number(FX_MODE_CHASE_BLACKOUT)); // command.append(QString::number(FX_MODE_TWINKLE_RANDOM));
} else if (effectMode == "Chase Blackout Rainbow") { // } else if (effectMode == "Twinkle Fade") {
command.append(QString::number(FX_MODE_CHASE_BLACKOUT_RAINBOW)); // command.append(QString::number(FX_MODE_TWINKLE_FADE));
} else if (effectMode == "Color Sweep Random") { // } else if (effectMode == "Twinkle Fade Random") {
command.append(QString::number(FX_MODE_COLOR_SWEEP_RANDOM)); // command.append(QString::number(FX_MODE_TWINKLE_FADE_RANDOM));
} else if (effectMode == "Running Color") { // } else if (effectMode == "Sparkle") {
command.append(QString::number(FX_MODE_RUNNING_COLOR)); // command.append(QString::number(FX_MODE_SPARKLE));
} else if (effectMode == "Running Red Blue") { // } else if (effectMode == "Flash Sparkle") {
command.append(QString::number(FX_MODE_RUNNING_RED_BLUE)); // command.append(QString::number(FX_MODE_FLASH_SPARKLE));
} else if (effectMode == "Running Random") { // } else if (effectMode == "Hyper Sparkle") {
command.append(QString::number(FX_MODE_RUNNING_RANDOM)); // command.append(QString::number(FX_MODE_HYPER_SPARKLE));
}else if (effectMode == "Larson Scanner") { // } else if (effectMode == "Strobe") {
command.append(QString::number(FX_MODE_LARSON_SCANNER)); // command.append(QString::number(FX_MODE_STROBE));
}else if (effectMode == "Comet") { // } else if (effectMode == "Strobe Rainbow") {
command.append(QString::number(FX_MODE_COMET)); // command.append(QString::number(FX_MODE_STROBE_RAINBOW));
}else if (effectMode == "Fireworks") { // } else if (effectMode == "Multi Strobe") {
command.append(QString::number(FX_MODE_FIREWORKS)); // command.append(QString::number(FX_MODE_MULTI_STROBE));
}else if (effectMode == "Fireworks Random") { // } else if (effectMode == "Blink Rainbow") {
command.append(QString::number(FX_MODE_FIREWORKS_RANDOM)); // command.append(QString::number(FX_MODE_BLINK_RAINBOW));
}else if (effectMode == "Merry Christmas") { // } else if (effectMode == "Chase White") {
command.append(QString::number(FX_MODE_MERRY_CHRISTMAS)); // command.append(QString::number(FX_MODE_CHASE_WHITE));
}else if (effectMode == "Fire Flicker") { // } else if (effectMode == "Chase Color") {
command.append(QString::number(FX_MODE_FIRE_FLICKER)); // command.append(QString::number(FX_MODE_CHASE_COLOR));
}else if (effectMode == "Fire Flicker (soft)") { // } else if (effectMode == "Chase Random") {
command.append(QString::number(FX_MODE_FIRE_FLICKER_SOFT)); // command.append(QString::number(FX_MODE_CHASE_RANDOM));
}else if (effectMode == "Fire Flicker (intense)") { // } else if (effectMode == "Chase Flash") {
command.append(QString::number(FX_MODE_FIRE_FLICKER_INTENSE)); // command.append(QString::number(FX_MODE_CHASE_FLASH));
}else if (effectMode == "Circus Combustus") { // } else if (effectMode == "Chase Flash Random") {
command.append(QString::number(FX_MODE_CIRCUS_COMBUSTUS)); // command.append(QString::number(FX_MODE_CHASE_FLASH_RANDOM));
}else if (effectMode == "Halloween") { // } else if (effectMode == "Chase Rainbow White") {
command.append(QString::number(FX_MODE_HALLOWEEN)); // command.append(QString::number(FX_MODE_CHASE_RAINBOW_WHITE));
}else if (effectMode == "Bicolor Chase") { // } else if (effectMode == "Chase Blackout") {
command.append(QString::number(FX_MODE_BICOLOR_CHASE)); // command.append(QString::number(FX_MODE_CHASE_BLACKOUT));
}else if (effectMode == "Tricolor Chase") { // } else if (effectMode == "Chase Blackout Rainbow") {
command.append(QString::number(FX_MODE_TRICOLOR_CHASE)); // command.append(QString::number(FX_MODE_CHASE_BLACKOUT_RAINBOW));
}else if (effectMode == "ICU") { // } else if (effectMode == "Color Sweep Random") {
command.append(QString::number(FX_MODE_ICU)); // command.append(QString::number(FX_MODE_COLOR_SWEEP_RANDOM));
}else if (effectMode == "Custom 0") { // } else if (effectMode == "Running Color") {
command.append(QString::number(FX_MODE_CUSTOM_0)); // command.append(QString::number(FX_MODE_RUNNING_COLOR));
}else if (effectMode == "Custom 1") { // } else if (effectMode == "Running Red Blue") {
command.append(QString::number(FX_MODE_CUSTOM_1)); // command.append(QString::number(FX_MODE_RUNNING_RED_BLUE));
}else if (effectMode == "Custom 2") { // } else if (effectMode == "Running Random") {
command.append(QString::number(FX_MODE_CUSTOM_2)); // command.append(QString::number(FX_MODE_RUNNING_RANDOM));
}else if (effectMode == "Custom 3") { // }else if (effectMode == "Larson Scanner") {
command.append(QString::number(FX_MODE_CUSTOM_3)); // command.append(QString::number(FX_MODE_LARSON_SCANNER));
} // }else if (effectMode == "Comet") {
command.append("\r\n"); // command.append(QString::number(FX_MODE_COMET));
return sendCommand(info, command, CommandType::Mode); // }else if (effectMode == "Fireworks") {
} // command.append(QString::number(FX_MODE_FIREWORKS));
// }else if (effectMode == "Fireworks Random") {
// command.append(QString::number(FX_MODE_FIREWORKS_RANDOM));
// }else if (effectMode == "Merry Christmas") {
// command.append(QString::number(FX_MODE_MERRY_CHRISTMAS));
// }else if (effectMode == "Fire Flicker") {
// command.append(QString::number(FX_MODE_FIRE_FLICKER));
// }else if (effectMode == "Fire Flicker (soft)") {
// command.append(QString::number(FX_MODE_FIRE_FLICKER_SOFT));
// }else if (effectMode == "Fire Flicker (intense)") {
// command.append(QString::number(FX_MODE_FIRE_FLICKER_INTENSE));
// }else if (effectMode == "Circus Combustus") {
// command.append(QString::number(FX_MODE_CIRCUS_COMBUSTUS));
// }else if (effectMode == "Halloween") {
// command.append(QString::number(FX_MODE_HALLOWEEN));
// }else if (effectMode == "Bicolor Chase") {
// command.append(QString::number(FX_MODE_BICOLOR_CHASE));
// }else if (effectMode == "Tricolor Chase") {
// command.append(QString::number(FX_MODE_TRICOLOR_CHASE));
// }else if (effectMode == "ICU") {
// command.append(QString::number(FX_MODE_ICU));
// }else if (effectMode == "Custom 0") {
// command.append(QString::number(FX_MODE_CUSTOM_0));
// }else if (effectMode == "Custom 1") {
// command.append(QString::number(FX_MODE_CUSTOM_1));
// }else if (effectMode == "Custom 2") {
// command.append(QString::number(FX_MODE_CUSTOM_2));
// }else if (effectMode == "Custom 3") {
// command.append(QString::number(FX_MODE_CUSTOM_3));
// }
// command.append("\r\n");
// return sendCommand(info, command, CommandType::Mode);
// }
} }
@ -306,122 +318,8 @@ void IntegrationPluginWs2812fx::thingRemoved(Thing *thing)
if (thing->thingClassId() == ws2812fxThingClassId) { if (thing->thingClassId() == ws2812fxThingClassId) {
m_usedInterfaces.removeAll(thing->paramValue(ws2812fxThingSerialPortParamTypeId).toString()); m_usedInterfaces.removeAll(thing->paramValue(ws2812fxThingSerialPortParamTypeId).toString());
QSerialPort *serialPort = m_serialPorts.take(thing); NymeaLight *light = m_lights.take(thing);
serialPort->flush(); light->deleteLater();
serialPort->close();
serialPort->deleteLater();
}
if (myThings().empty()) {
m_reconnectTimer->stop();
m_reconnectTimer->deleteLater();
} }
} }
void IntegrationPluginWs2812fx::onReadyRead()
{
QSerialPort *serialPort = static_cast<QSerialPort*>(sender());
Thing *thing = m_serialPorts.key(serialPort);
QByteArray data;
while (serialPort->canReadLine()) {
data = serialPort->readLine();
qDebug(dcWs2812fx()) << "Message received" << data;
if (data.contains("mode")) {
if (m_pendingActions.contains(CommandType::Mode)) {
m_pendingActions.take(CommandType::Mode)->finish(Thing::ThingErrorNoError);
}
QString mode = data.split('-').at(1);
mode.remove(0, 1);
mode.remove("\r\n");
qDebug(dcWs2812fx()) << "set mode to:" << mode;
thing->setStateValue(ws2812fxEffectModeStateTypeId, mode);
}
if (data.contains("brightness")) {
if (m_pendingActions.contains(CommandType::Brightness)) {
m_pendingActions.take(CommandType::Brightness)->finish(Thing::ThingErrorNoError);
}
QString rawBrightness = data.split(':').at(1);
rawBrightness.remove(" ");
rawBrightness.remove("\r\n");
int brightness = rawBrightness.toInt();
qDebug(dcWs2812fx()) << "set brightness to:" << brightness;
thing->setStateValue(ws2812fxBrightnessStateTypeId, brightness);
if (brightness == 0) {
thing->setStateValue(ws2812fxPowerStateTypeId, false);
} else {
thing->setStateValue(ws2812fxPowerStateTypeId, true);
}
}
if (data.contains("speed")) {
if (m_pendingActions.contains(CommandType::Speed)) {
m_pendingActions.take(CommandType::Speed)->finish(Thing::ThingErrorNoError);
}
QString rawSpeed = data.split(':').at(1);
rawSpeed.remove(" ");
rawSpeed.remove("\r\n");
int speed = data.split(':').at(1).toInt();
qDebug(dcWs2812fx()) << "set speed to:" << speed;
thing->setStateValue(ws2812fxSpeedStateTypeId, speed);
}
if (data.contains("color")) {
if (m_pendingActions.contains(CommandType::Color)) {
m_pendingActions.take(CommandType::Color)->finish(Thing::ThingErrorNoError);
}
QString rawColor = data.split(':').at(1);
rawColor.remove(" ");
rawColor.remove("0x");
rawColor.remove("\r\n");
rawColor.prepend("#");
qDebug(dcWs2812fx()) << "set color to:" << rawColor;
thing->setStateValue(ws2812fxColorStateTypeId, rawColor);
}
}
}
void IntegrationPluginWs2812fx::onReconnectTimer()
{
foreach(Thing *thing, myThings()) {
if (!thing->stateValue(ws2812fxConnectedStateTypeId).toBool()) {
QSerialPort *serialPort = m_serialPorts.value(thing);
if (serialPort) {
if (serialPort->open(QSerialPort::ReadWrite)) {
thing->setStateValue(ws2812fxConnectedStateTypeId, true);
} else {
thing->setStateValue(ws2812fxConnectedStateTypeId, false);
m_reconnectTimer->start();
}
}
}
}
}
void IntegrationPluginWs2812fx::onSerialError(QSerialPort::SerialPortError error)
{
QSerialPort *serialPort = static_cast<QSerialPort*>(sender());
Thing *thing = m_serialPorts.key(serialPort);
if (error != QSerialPort::NoError && serialPort->isOpen()) {
qCCritical(dcWs2812fx()) << "Serial port error:" << error << serialPort->errorString();
m_reconnectTimer->start();
serialPort->close();
thing->setStateValue(ws2812fxConnectedStateTypeId, false);
}
}
void IntegrationPluginWs2812fx::sendCommand(ThingActionInfo *info, const QByteArray &command, CommandType commandType)
{
qDebug(dcWs2812fx()) << "Sending command" << command;
QSerialPort *serialPort = m_serialPorts.value(info->thing());
if (!serialPort)
return info->finish(Thing::ThingErrorThingNotFound);
if (serialPort->write(command) != command.length()) {
qCWarning(dcWs2812fx) << "Error writing to serial port";
return info->finish(Thing::ThingErrorHardwareFailure);
}
m_pendingActions.insert(commandType, info);
}

View File

@ -94,6 +94,7 @@
#define FX_MODE_CUSTOM_3 59 #define FX_MODE_CUSTOM_3 59
#include "integrations/integrationplugin.h" #include "integrations/integrationplugin.h"
#include "nymealight.h"
#include <QTimer> #include <QTimer>
#include <QSerialPort> #include <QSerialPort>
@ -115,24 +116,10 @@ public:
void executeAction(ThingActionInfo *info) override; void executeAction(ThingActionInfo *info) override;
private: private:
enum CommandType { QHash<Thing *, NymeaLight *> m_lights;
Color,
Speed,
Brightness,
Mode
};
QHash<Thing *, QSerialPort *> m_serialPorts;
QList<QString> m_usedInterfaces; QList<QString> m_usedInterfaces;
QHash<CommandType, ThingActionInfo*> m_pendingActions;
QTimer *m_reconnectTimer = nullptr;
void sendCommand(ThingActionInfo *info, const QByteArray &command, CommandType commandType);
private slots: private slots:
void onReadyRead();
void onReconnectTimer();
void onSerialError(QSerialPort::SerialPortError error);
signals: signals:

View File

@ -6,7 +6,13 @@ TARGET = $$qtLibraryTarget(nymea_integrationpluginws2812fx)
SOURCES += \ SOURCES += \
integrationpluginws2812fx.cpp \ integrationpluginws2812fx.cpp \
nymealight.cpp \
nymealightinterface.cpp \
nymealightserialinterface.cpp
HEADERS += \ HEADERS += \
integrationpluginws2812fx.h \ integrationpluginws2812fx.h \
nymealight.h \
nymealightinterface.h \
nymealightserialinterface.h