devices to things

This commit is contained in:
bernhard.trinnes 2020-10-01 23:47:33 +02:00 committed by Boernsman
parent c78d3663d9
commit 66505b7fd4
10 changed files with 567 additions and 523 deletions

View File

@ -1,355 +0,0 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "devicepluginyeelight.h"
#include "devices/device.h"
#include "types/param.h"
#include "plugininfo.h"
#include "ssdp.h"
#include <QDebug>
#include <QColor>
#include <QRgb>
DevicePluginYeelight::DevicePluginYeelight()
{
}
void DevicePluginYeelight::init()
{
m_connectedStateTypeIds.insert(colorBulbDeviceClassId, colorBulbConnectedStateTypeId);
m_connectedStateTypeIds.insert(dimmableBulbDeviceClassId, dimmableBulbConnectedStateTypeId);
m_powerStateTypeIds.insert(colorBulbDeviceClassId, colorBulbPowerStateTypeId);
m_powerStateTypeIds.insert(dimmableBulbDeviceClassId, dimmableBulbPowerStateTypeId);
m_brightnessStateTypeIds.insert(colorBulbDeviceClassId, colorBulbBrightnessStateTypeId);
m_brightnessStateTypeIds.insert(dimmableBulbDeviceClassId, dimmableBulbBrightnessStateTypeId);
m_ssdp = new Ssdp(this);
m_ssdp->enable();
m_ssdp->discover();
}
void DevicePluginYeelight::discoverDevices(DeviceDiscoveryInfo *info)
{
if (info->deviceClassId() == colorBulbDeviceClassId) {
qCDebug(dcYeelight()) << "Starting UPnP discovery...";
/*
connect(ssdp, &Ssdp::discovered, this,[info, this](const QString &address, int port, int id, const QString &model) {
DeviceDescriptor descriptor(colorBulbDeviceClassId, "Yeelight"+ model + "Bulb", address);
ParamList params;
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(colorBulbDeviceIdParamTypeId).toString() == id) {
descriptor.setDeviceId(existingDevice->id());
break;
}
}
params.append(Param(colorBulbDeviceHostParamTypeId, address));
params.append(Param(colorBulbDeviceIdParamTypeId, id));
params.append(Param(colorBulbDevicePortParamTypeId, port));
descriptor.setParams(params);
qCDebug(dcYeelight()) << "UPnP: Found Yeelight device:" << id;
info->finish(Device::DeviceErrorNoError);
});*/
}
}
void DevicePluginYeelight::setupDevice(DeviceSetupInfo *info)
{
Device *device = info->device();
if (device->deviceClassId() == colorBulbDeviceClassId) {
QHostAddress address = QHostAddress(device->paramValue(colorBulbDeviceHostParamTypeId).toString());
quint16 port = device->paramValue(colorBulbDevicePortParamTypeId).toUInt();
Yeelight *yeelight = new Yeelight(hardwareManager()->networkManager(), address, port, this);
m_yeelightConnections.insert(device->id(), yeelight);
connect(yeelight, &Yeelight::connectionChanged, this, &DevicePluginYeelight::onConnectionChanged);
connect(yeelight, &Yeelight::requestExecuted, this, &DevicePluginYeelight::onRequestExecuted);
connect(yeelight, &Yeelight::colorModeNotificationReceived, this, &DevicePluginYeelight::onColorModeNotificationReceived);
connect(yeelight, &Yeelight::powerNotificationReceived, this, &DevicePluginYeelight::onPowerNotificationReceived);
connect(yeelight, &Yeelight::brightnessNotificationReceived, this, &DevicePluginYeelight::onBrightnessNotificationReceived);
connect(yeelight, &Yeelight::colorTemperatureNotificationReceived, this, &DevicePluginYeelight::onColorTemperatureNotificationReceived);
connect(yeelight, &Yeelight::rgbNotificationReceived, this, &DevicePluginYeelight::onRgbNotificationReceived);
connect(yeelight, &Yeelight::nameNotificationReceived, this, &DevicePluginYeelight::onNameNotificationReceived);
info->finish(Device::DeviceErrorNoError);
}
}
void DevicePluginYeelight::postSetupDevice(Device *device)
{
connect(device, &Device::nameChanged, this, &DevicePluginYeelight::onDeviceNameChanged);
if (!m_pluginTimer) {
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() {
foreach (DeviceId id, m_yeelightConnections.keys()) {
Device *device = myDevices().findById(id);
Yeelight *yeelight = m_yeelightConnections.value(id);
if (yeelight->isConnected()) {
QList<Yeelight::YeelightProperty> properties;
if (device->deviceClassId() == colorBulbDeviceClassId) {
properties << Yeelight::YeelightProperty::Ct;
properties << Yeelight::YeelightProperty::Rgb;
properties << Yeelight::YeelightProperty::Power;
properties << Yeelight::YeelightProperty::Bright;
properties << Yeelight::YeelightProperty::ColorMode;
} else if (device->deviceClassId() == colorBulbDeviceClassId) {
properties << Yeelight::YeelightProperty::Power;
properties << Yeelight::YeelightProperty::Bright;
}
yeelight->getParam(properties);
}
}
});
}
m_pluginTimer->timeout();
}
void DevicePluginYeelight::executeAction(DeviceActionInfo *info)
{
Device *device = info->device();
Action action = info->action();
Yeelight *yeelight = m_yeelightConnections.value(device->id());
if (!yeelight)
return info->finish(Device::DeviceErrorHardwareFailure);
if (device->deviceClassId() == colorBulbDeviceClassId) {
if (action.actionTypeId() == colorBulbPowerActionTypeId) {
bool power = action.param(colorBulbPowerActionPowerParamTypeId).value().toBool();
int requestId = yeelight->setPower(power);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbBrightnessActionTypeId) {
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (m_pendingBrightnessAction.contains(device->id())) {
//Scrap old info and insert new one
m_pendingBrightnessAction.insert(device->id(), info);
} else {
m_pendingBrightnessAction.insert(device->id(), info);
QTimer::singleShot(500, this, [yeelight, info, this]{
int brightness = info->action().param(colorBulbBrightnessActionBrightnessParamTypeId).value().toInt();
m_pendingBrightnessAction.remove(info->device()->id());
int requestId = yeelight->setBrightness(brightness);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
});
}
} else if (action.actionTypeId() == colorBulbColorActionColorParamTypeId) {
QRgb color = QColor(action.param(colorBulbColorActionColorParamTypeId).value().toString()).rgba();
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setRgb(color);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbColorTemperatureActionTypeId) {
int colorTemperature = 6500 - (action.param(colorBulbColorTemperatureActionColorTemperatureParamTypeId).value().toUInt() * -11.12);
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setColorTemperature(colorTemperature);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbEffectActionTypeId) {
QString effect = action.param(colorBulbEffectActionEffectParamTypeId).value().toString();
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (effect.contains("non")) {
int requestId = yeelight->stopColorFlow();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->startColorFlow();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == colorBulbAlertActionTypeId) {
QString alertType = action.param(colorBulbAlertActionAlertParamTypeId).value().toString();
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (alertType.contains("15")) { //Flash 15 sec
int requestId = yeelight->flash15s();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->flash();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == colorBulbDefaultActionTypeId) {
int requestId = yeelight->setDefault();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
qCWarning(dcYeelight()) << "Unhandled action";
}
} else if (device->deviceClassId() == dimmableBulbDeviceClassId) {
if (action.actionTypeId() == dimmableBulbPowerActionTypeId) {
bool power = action.param(dimmableBulbPowerActionPowerParamTypeId).value().toBool();
int requestId = yeelight->setPower(power);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
} else if (action.actionTypeId() == dimmableBulbBrightnessActionTypeId) {
int brightness = action.param(dimmableBulbBrightnessActionBrightnessParamTypeId).value().toInt();
if (!device->stateValue(dimmableBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setBrightness(brightness);
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == dimmableBulbAlertActionTypeId) {
QString alertType = action.param(dimmableBulbAlertActionAlertParamTypeId).value().toString();
if (!device->stateValue(dimmableBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (alertType.contains("15")) { //Flash 15 sec
int requestId = yeelight->flash15s();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->flash();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == dimmableBulbDefaultActionTypeId) {
int requestId = yeelight->setDefault();
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
qCWarning(dcYeelight()) << "Unhandled action";
}
} else {
qCWarning(dcYeelight()) << "Unhandled device class";
}
}
void DevicePluginYeelight::deviceRemoved(Device *device)
{
if ((device->deviceClassId() == colorBulbDeviceClassId) ||
(device->deviceClassId() == dimmableBulbDeviceClassId)){
Yeelight *yeelight = m_yeelightConnections.take(device->id());
yeelight->deleteLater();
}
}
void DevicePluginYeelight::onDeviceNameChanged()
{
Device *device = static_cast<Device*>(sender());
Yeelight *yeelight = m_yeelightConnections.value(device->id());
yeelight->setName(device->name());
}
void DevicePluginYeelight::onConnectionChanged(bool connected)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(m_connectedStateTypeIds.value(device->deviceClassId()), connected);
}
void DevicePluginYeelight::onRequestExecuted(int requestId, bool success)
{
if (m_asyncActions.contains(requestId)) {
DeviceActionInfo *info = m_asyncActions.take(requestId);
if (success) {
info->finish(Device::DeviceErrorNoError);
} else {
info->finish(Device::DeviceErrorHardwareFailure);
}
}
}
void DevicePluginYeelight::onPowerNotificationReceived(bool status)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(m_powerStateTypeIds.value(device->deviceClassId()), status);
}
void DevicePluginYeelight::onBrightnessNotificationReceived(int percentage)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(m_brightnessStateTypeIds.value(device->deviceClassId()), percentage);
}
void DevicePluginYeelight::onColorTemperatureNotificationReceived(int kelvin)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(colorBulbColorTemperatureStateTypeId, kelvin/11.12); //TODO needs proper conversion
}
void DevicePluginYeelight::onRgbNotificationReceived(QRgb rgbColor)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(colorBulbColorStateTypeId, rgbColor);
}
void DevicePluginYeelight::onNameNotificationReceived(const QString &name)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setName(name);
}
void DevicePluginYeelight::onColorModeNotificationReceived(Yeelight::YeelightColorMode colorMode)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
switch (colorMode) {
case Yeelight::RGB:
device->setStateValue(colorBulbColorModeStateTypeId, "RGB");
break;
case Yeelight::HSV:
device->setStateValue(colorBulbColorModeStateTypeId, "HSV");
break;
case Yeelight::ColorTemperature:
device->setStateValue(colorBulbColorModeStateTypeId, "Color temperature");
break;
}
}

View File

@ -1,77 +0,0 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DEVICEPLUGINYEELIGHT_H
#define DEVICEPLUGINYEELIGHT_H
#include "devices/deviceplugin.h"
#include "plugintimer.h"
#include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h"
#include "yeelight.h"
#include "ssdp.h"
#include <QTimer>
class DevicePluginYeelight : public DevicePlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "io.nymea.DevicePlugin" FILE "devicepluginyeelight.json")
Q_INTERFACES(DevicePlugin)
public:
explicit DevicePluginYeelight();
void init() override;
void discoverDevices(DeviceDiscoveryInfo *info) override;
void setupDevice(DeviceSetupInfo *info) override;
void postSetupDevice(Device *device) override;
void executeAction(DeviceActionInfo *info) override;
void deviceRemoved(Device *device) override;
private:
Ssdp *m_ssdp = nullptr;
PluginTimer *m_pluginTimer = nullptr;
QHash<int, DeviceActionInfo *> m_asyncActions;
QHash<DeviceId, Yeelight *> m_yeelightConnections;
QHash<DeviceClassId, StateTypeId> m_connectedStateTypeIds;
QHash<DeviceClassId, StateTypeId> m_powerStateTypeIds;
QHash<DeviceClassId, StateTypeId> m_brightnessStateTypeIds;
QHash<DeviceId, DeviceActionInfo *> m_pendingBrightnessAction;
private slots:
void onDeviceNameChanged();
void onConnectionChanged(bool connected);
void onRequestExecuted(int requestId, bool success);
void onPowerNotificationReceived(bool status);
void onBrightnessNotificationReceived(int percentage);
void onColorTemperatureNotificationReceived(int kelvin);
void onRgbNotificationReceived(QRgb rgbColor);
void onNameNotificationReceived(const QString &name);
void onColorModeNotificationReceived(Yeelight::YeelightColorMode colorMode);
};
#endif // DEVICEPLUGINYEELIGHT_H

View File

@ -0,0 +1,362 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "integrationpluginyeelight.h"
#include "types/param.h"
#include "plugininfo.h"
#include "ssdp.h"
#include <QDebug>
#include <QColor>
#include <QRgb>
IntegrationPluginYeelight::IntegrationPluginYeelight()
{
}
void IntegrationPluginYeelight::init()
{
m_connectedStateTypeIds.insert(colorBulbThingClassId, colorBulbConnectedStateTypeId);
m_connectedStateTypeIds.insert(dimmableBulbThingClassId, dimmableBulbConnectedStateTypeId);
m_powerStateTypeIds.insert(colorBulbThingClassId, colorBulbPowerStateTypeId);
m_powerStateTypeIds.insert(dimmableBulbThingClassId, dimmableBulbPowerStateTypeId);
m_brightnessStateTypeIds.insert(colorBulbThingClassId, colorBulbBrightnessStateTypeId);
m_brightnessStateTypeIds.insert(dimmableBulbThingClassId, dimmableBulbBrightnessStateTypeId);
m_ssdp = new Ssdp(this);
m_ssdp->enable();
m_ssdp->discover();
}
void IntegrationPluginYeelight::discoverThings(ThingDiscoveryInfo *info)
{
if (info->thingClassId() == colorBulbThingClassId) {
qCDebug(dcYeelight()) << "Starting UPnP discovery...";
/*
connect(ssdp, &Ssdp::discovered, this,[info, this](const QString &address, int port, int id, const QString &model) {
ThingDescriptor descriptor(colorBulbThingClassId, "Yeelight"+ model + "Bulb", address);
ParamList params;
foreach (Thing *existingThing, myThings()) {
if (existingThing->paramValue(colorBulbThingIdParamTypeId).toString() == id) {
descriptor.setThingId(existingThing->id());
break;
}
}
params.append(Param(colorBulbThingHostParamTypeId, address));
params.append(Param(colorBulbThingIdParamTypeId, id));
params.append(Param(colorBulbThingPortParamTypeId, port));
descriptor.setParams(params);
qCDebug(dcYeelight()) << "UPnP: Found Yeelight thing:" << id;
info->finish(Thing::ThingErrorNoError);
});*/
}
}
void IntegrationPluginYeelight::setupThing(ThingSetupInfo *info)
{
Thing *thing = info->thing();
if (thing->thingClassId() == colorBulbThingClassId) {
QHostAddress address = QHostAddress(thing->paramValue(colorBulbThingHostParamTypeId).toString());
quint16 port = thing->paramValue(colorBulbThingPortParamTypeId).toUInt();
Yeelight *yeelight = new Yeelight(hardwareManager()->networkManager(), address, port, this);
m_yeelightConnections.insert(thing->id(), yeelight);
connect(yeelight, &Yeelight::connectionChanged, this, &IntegrationPluginYeelight::onConnectionChanged);
connect(yeelight, &Yeelight::requestExecuted, this, &IntegrationPluginYeelight::onRequestExecuted);
connect(yeelight, &Yeelight::colorModeNotificationReceived, this, &IntegrationPluginYeelight::onColorModeNotificationReceived);
connect(yeelight, &Yeelight::powerNotificationReceived, this, &IntegrationPluginYeelight::onPowerNotificationReceived);
connect(yeelight, &Yeelight::brightnessNotificationReceived, this, &IntegrationPluginYeelight::onBrightnessNotificationReceived);
connect(yeelight, &Yeelight::colorTemperatureNotificationReceived, this, &IntegrationPluginYeelight::onColorTemperatureNotificationReceived);
connect(yeelight, &Yeelight::rgbNotificationReceived, this, &IntegrationPluginYeelight::onRgbNotificationReceived);
connect(yeelight, &Yeelight::nameNotificationReceived, this, &IntegrationPluginYeelight::onNameNotificationReceived);
info->finish(Thing::ThingErrorNoError);
}
}
void IntegrationPluginYeelight::postSetupThing(Thing *thing)
{
connect(thing, &Thing::nameChanged, this, &IntegrationPluginYeelight::onDeviceNameChanged);
if (!m_pluginTimer) {
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() {
foreach (ThingId id, m_yeelightConnections.keys()) {
Thing *thing = myThings().findById(id);
Yeelight *yeelight = m_yeelightConnections.value(id);
if (yeelight->isConnected()) {
QList<Yeelight::YeelightProperty> properties;
if (thing->thingClassId() == colorBulbThingClassId) {
properties << Yeelight::YeelightProperty::Ct;
properties << Yeelight::YeelightProperty::Rgb;
properties << Yeelight::YeelightProperty::Power;
properties << Yeelight::YeelightProperty::Bright;
properties << Yeelight::YeelightProperty::ColorMode;
} else if (thing->thingClassId() == colorBulbThingClassId) {
properties << Yeelight::YeelightProperty::Power;
properties << Yeelight::YeelightProperty::Bright;
}
yeelight->getParam(properties);
}
}
});
}
m_pluginTimer->timeout();
}
void IntegrationPluginYeelight::executeAction(ThingActionInfo *info)
{
Thing *thing = info->thing();
Action action = info->action();
Yeelight *yeelight = m_yeelightConnections.value(thing->id());
if (!yeelight)
return info->finish(Thing::ThingErrorHardwareFailure);
if (thing->thingClassId() == colorBulbThingClassId) {
if (action.actionTypeId() == colorBulbPowerActionTypeId) {
bool power = action.param(colorBulbPowerActionPowerParamTypeId).value().toBool();
int requestId = yeelight->setPower(power);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbBrightnessActionTypeId) {
if (!thing->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (m_pendingBrightnessAction.contains(thing->id())) {
//Scrap old info and insert new one
m_pendingBrightnessAction.insert(thing->id(), info);
} else {
m_pendingBrightnessAction.insert(thing->id(), info);
QTimer::singleShot(500, this, [yeelight, info, this]{
int brightness = info->action().param(colorBulbBrightnessActionBrightnessParamTypeId).value().toInt();
m_pendingBrightnessAction.remove(info->thing()->id());
int requestId = yeelight->setBrightness(brightness);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
});
}
} else if (action.actionTypeId() == colorBulbColorActionColorParamTypeId) {
QRgb color = QColor(action.param(colorBulbColorActionColorParamTypeId).value().toString()).rgba();
if (!thing->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setRgb(color);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbColorTemperatureActionTypeId) {
int colorTemperature = 6500 - (action.param(colorBulbColorTemperatureActionColorTemperatureParamTypeId).value().toUInt() * -11.12);
if (!thing->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setColorTemperature(colorTemperature);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbEffectActionTypeId) {
QString effect = action.param(colorBulbEffectActionEffectParamTypeId).value().toString();
if (!thing->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (effect.contains("non")) {
int requestId = yeelight->stopColorFlow();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->startColorFlow();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == colorBulbAlertActionTypeId) {
QString alertType = action.param(colorBulbAlertActionAlertParamTypeId).value().toString();
if (!thing->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (alertType.contains("15")) { //Flash 15 sec
int requestId = yeelight->flash15s();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->flash();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == colorBulbDefaultActionTypeId) {
int requestId = yeelight->setDefault();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
qCWarning(dcYeelight()) << "Unhandled action";
}
} else if (thing->thingClassId() == dimmableBulbThingClassId) {
if (action.actionTypeId() == dimmableBulbPowerActionTypeId) {
bool power = action.param(dimmableBulbPowerActionPowerParamTypeId).value().toBool();
int requestId = yeelight->setPower(power);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
} else if (action.actionTypeId() == dimmableBulbBrightnessActionTypeId) {
int brightness = action.param(dimmableBulbBrightnessActionBrightnessParamTypeId).value().toInt();
if (!thing->stateValue(dimmableBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setBrightness(brightness);
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == dimmableBulbAlertActionTypeId) {
QString alertType = action.param(dimmableBulbAlertActionAlertParamTypeId).value().toString();
if (!thing->stateValue(dimmableBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (alertType.contains("15")) { //Flash 15 sec
int requestId = yeelight->flash15s();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->flash();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
}
} else if (action.actionTypeId() == dimmableBulbDefaultActionTypeId) {
int requestId = yeelight->setDefault();
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
qCWarning(dcYeelight()) << "Unhandled action";
}
} else {
qCWarning(dcYeelight()) << "Unhandled thing class";
}
}
void IntegrationPluginYeelight::thingRemoved(Thing *thing)
{
if ((thing->thingClassId() == colorBulbThingClassId) ||
(thing->thingClassId() == dimmableBulbThingClassId)){
Yeelight *yeelight = m_yeelightConnections.take(thing->id());
yeelight->deleteLater();
}
}
void IntegrationPluginYeelight::onDeviceNameChanged()
{
Thing *thing = static_cast<Thing*>(sender());
Yeelight *yeelight = m_yeelightConnections.value(thing->id());
yeelight->setName(thing->name());
}
void IntegrationPluginYeelight::onConnectionChanged(bool connected)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setStateValue(m_connectedStateTypeIds.value(thing->thingClassId()), connected);
}
void IntegrationPluginYeelight::onRequestExecuted(int requestId, bool success)
{
if (m_asyncActions.contains(requestId)) {
ThingActionInfo *info = m_asyncActions.take(requestId);
if (success) {
info->finish(Thing::ThingErrorNoError);
} else {
info->finish(Thing::ThingErrorHardwareFailure);
}
}
}
void IntegrationPluginYeelight::onPowerNotificationReceived(bool status)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setStateValue(m_powerStateTypeIds.value(thing->thingClassId()), status);
}
void IntegrationPluginYeelight::onBrightnessNotificationReceived(int percentage)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setStateValue(m_brightnessStateTypeIds.value(thing->thingClassId()), percentage);
}
void IntegrationPluginYeelight::onColorTemperatureNotificationReceived(int kelvin)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setStateValue(colorBulbColorTemperatureStateTypeId, kelvin/11.12); //TODO needs proper conversion
}
void IntegrationPluginYeelight::onRgbNotificationReceived(QRgb rgbColor)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setStateValue(colorBulbColorStateTypeId, rgbColor);
}
void IntegrationPluginYeelight::onNameNotificationReceived(const QString &name)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
thing->setName(name);
}
void IntegrationPluginYeelight::onColorModeNotificationReceived(Yeelight::YeelightColorMode colorMode)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Thing * thing = myThings().findById(m_yeelightConnections.key(yeelight));
if(!thing)
return;
switch (colorMode) {
case Yeelight::RGB:
thing->setStateValue(colorBulbColorModeStateTypeId, "RGB");
break;
case Yeelight::HSV:
thing->setStateValue(colorBulbColorModeStateTypeId, "HSV");
break;
case Yeelight::ColorTemperature:
thing->setStateValue(colorBulbColorModeStateTypeId, "Color temperature");
break;
}
}

View File

@ -0,0 +1,85 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef INTEGRATIONPLUGINYEELIGHT_H
#define INTEGRATIONPLUGINYEELIGHT_H
#include "integrations/integrationplugin.h"
#include "plugintimer.h"
#include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h"
#include "yeelight.h"
#include "ssdp.h"
#include <QTimer>
class IntegrationPluginYeelight : public IntegrationPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "io.nymea.IntegrationPlugin" FILE "integrationpluginyeelight.json")
Q_INTERFACES(IntegrationPlugin)
public:
explicit IntegrationPluginYeelight();
void init() override;
void discoverThings(ThingDiscoveryInfo *info) override;
void setupThing(ThingSetupInfo *info) override;
void postSetupThing(Thing *thing) override;
void executeAction(ThingActionInfo *info) override;
void thingRemoved(Thing *thing) override;
private:
Ssdp *m_ssdp = nullptr;
PluginTimer *m_pluginTimer = nullptr;
QHash<int, ThingActionInfo *> m_asyncActions;
QHash<ThingId, Yeelight *> m_yeelightConnections;
QHash<ThingClassId, StateTypeId> m_connectedStateTypeIds;
QHash<ThingClassId, StateTypeId> m_powerStateTypeIds;
QHash<ThingClassId, StateTypeId> m_brightnessStateTypeIds;
QHash<ThingId, ThingActionInfo *> m_pendingBrightnessAction;
private slots:
void onDeviceNameChanged();
void onConnectionChanged(bool connected);
void onRequestExecuted(int requestId, bool success);
void onPowerNotificationReceived(bool status);
void onBrightnessNotificationReceived(int percentage);
void onColorTemperatureNotificationReceived(int kelvin);
void onRgbNotificationReceived(QRgb rgbColor);
void onNameNotificationReceived(const QString &name);
void onColorModeNotificationReceived(Yeelight::YeelightColorMode colorMode);
};
#endif // INTEGRATIONPLUGINYEELIGHT_H

View File

@ -7,7 +7,7 @@
"name": "Yeelight",
"displayName": "Yeelight",
"id": "74011456-0a0f-4e43-bd94-bf4bb69593a4",
"deviceClasses": [
"thingClasses": [
{
"id": "7d801b71-0aee-4b5d-ae62-a950473b20f5",
"name": "colorBulb",

View File

@ -1,24 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ssdp.h"
#include "extern-plugininfo.h"

View File

@ -1,24 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef SSDP_H
#define SSDP_H

View File

@ -1,25 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "yeelight.h"
#include "extern-plugininfo.h"

View File

@ -1,24 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2020 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* *
* This file is part of nymea. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef YEELIGHT_H
#define YEELIGHT_H
@ -29,8 +37,6 @@
#include <QTcpSocket>
#include "network/networkaccessmanager.h"
#include "devices/device.h"
#include <QRgb>
class Yeelight : public QObject

View File

@ -2,14 +2,14 @@ include(../plugins.pri)
QT += network
TARGET = $$qtLibraryTarget(nymea_devicepluginyeelight)
TARGET = $$qtLibraryTarget(nymea_integrationpluginyeelight)
SOURCES += \
devicepluginyeelight.cpp \
integrationpluginyeelight.cpp \
yeelight.cpp \
ssdp.cpp \
HEADERS += \
devicepluginyeelight.h \
integrationpluginyeelight.h \
yeelight.h \
ssdp.h \