added state update on device notification

This commit is contained in:
Boernsman 2019-12-27 13:41:23 +01:00
parent c076034ad5
commit cda658f0a2
4 changed files with 101 additions and 25 deletions

View File

@ -79,6 +79,11 @@ void DevicePluginYeelight::setupDevice(DeviceSetupInfo *info)
connect(yeelight, &Yeelight::connectionChanged, this, &DevicePluginYeelight::onConnectionChanged);
connect(yeelight, &Yeelight::requestExecuted, this, &DevicePluginYeelight::onRequestExecuted);
connect(yeelight, &Yeelight::propertyListReceived, this, &DevicePluginYeelight::onPropertyListReceived);
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);
}
}
@ -87,21 +92,22 @@ void DevicePluginYeelight::postSetupDevice(Device *device)
{
connect(device, &Device::nameChanged, this, &DevicePluginYeelight::onDeviceNameChanged);
if (device->deviceClassId() == colorBulbDeviceClassId) {
}
if (!m_pluginTimer) {
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() {
foreach (Yeelight *yeelight, m_yeelightConnections.values()) {
foreach (DeviceId id, m_yeelightConnections.keys()) {
Device *device = myDevices().findById(id);
Yeelight *yeelight = m_yeelightConnections.value(id);
if (yeelight->isConnected()) {
QList<Yeelight::Property> properties;
if (device->deviceClassId() == colorBulbDeviceClassId) {
properties << Yeelight::Property::Name;
properties << Yeelight::Property::Ct;
properties << Yeelight::Property::Rgb;
properties << Yeelight::Property::Power;
properties << Yeelight::Property::Bright;
properties << Yeelight::Property::ColorMode;
} //TODO add white color bulb with other property requests
yeelight->getParam(properties);
} else {
yeelight->connectDevice();
@ -126,49 +132,56 @@ void DevicePluginYeelight::executeAction(DeviceActionInfo *info)
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbBrightnessActionTypeId) {
int brightness = action.param(colorBulbBrightnessActionBrightnessParamTypeId).value().toInt();
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setBrightness(brightness);
//TODO turn on if off
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbColorActionColorParamTypeId) {
QRgb color = action.param(colorBulbColorActionColorParamTypeId).value().toUInt();
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setRgb(color);
//TODO turn on if off
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else if (action.actionTypeId() == colorBulbColorTemperatureActionTypeId) {
int colorTemperature = action.param(colorBulbColorTemperatureActionColorTemperatureParamTypeId).value().toUInt() * 11.12;
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
int requestId = yeelight->setColorTemperature(colorTemperature);
//TODO turn on if off
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();
//TODO turn on if off
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->startColorFlow();
//TODO turn on if off
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 (alertType.contains("15s")) {
if (!device->stateValue(colorBulbPowerStateTypeId).toBool())
yeelight->setPower(true);
if (alertType.contains("15")) { //Flash 15 sec
int requestId = yeelight->flash15s();
//TODO turn on if off
connect(info, &DeviceActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
m_asyncActions.insert(requestId, info);
} else {
int requestId = yeelight->flash();
//TODO turn on if off
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";
}
}
@ -235,3 +248,52 @@ void DevicePluginYeelight::onPropertyListReceived(QVariantList value)
device->setStateValue(colorBulbBrightnessStateTypeId, value[4].toInt());
device->setStateValue(colorBulbColorModeStateTypeId, value[5].toString());
}
void DevicePluginYeelight::onPowerNotificationReceived(bool status)
{
Yeelight *yeelight = static_cast<Yeelight *>(sender());
Device * device = myDevices().findById(m_yeelightConnections.key(yeelight));
if(!device)
return;
device->setStateValue(colorBulbPowerStateTypeId, 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(colorBulbBrightnessStateTypeId, 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(int 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);
}

View File

@ -66,6 +66,12 @@ private slots:
void onConnectionChanged(bool connected);
void onRequestExecuted(int requestId, bool success);
void onPropertyListReceived(QVariantList value);
void onPowerNotificationReceived(bool status);
void onBrightnessNotificationReceived(int percentage);
void onColorTemperatureNotificationReceived(int kelvin);
void onRgbNotificationReceived(int rgbColor);
void onNameNotificationReceived(const QString &name);
};
#endif // DEVICEPLUGINYEELIGHT_H

View File

@ -173,7 +173,7 @@ int Yeelight::setColorTemperature(int mirad, int msFadeTime)
params.append(msFadeTime);
obj["params"] = params;
doc.setObject(obj);
qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
//qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
m_socket->write(doc.toJson() + "\r\n");
return requestId;
}
@ -209,7 +209,7 @@ int Yeelight::setBrightness(int percentage, int msFadeTime)
params.append(msFadeTime);
obj["params"] = params;
doc.setObject(obj);
qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
//qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
m_socket->write(doc.toJson() + "\r\n");
return requestId;
}
@ -231,7 +231,7 @@ int Yeelight::setPower(bool power, int msFadeTime)
params.append(msFadeTime);
obj["params"] = params;
doc.setObject(obj);
qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
//qCDebug(dcYeelight()) << "Sending request" << doc.toJson();
m_socket->write(doc.toJson() + "\r\n");
return requestId;
}
@ -348,28 +348,28 @@ void Yeelight::onReadyRead()
if (map["method"] == "props") {
QVariantMap params = map["params"].toMap();
if (params.contains("power")) {
emit notificationReveiced(Property::Power, params["power"]);
emit powerNotificationReceived((params["power"].toString() == "on"));
}
if (params.contains("bright")) {
emit notificationReveiced(Property::Bright, params["bright"]);
emit brightnessNotificationReceived(params["bright"].toInt());
}
if (params.contains("ct")) {
emit notificationReveiced(Property::Ct, params["ct"]);
emit colorTemperatureNotificationReceived(params["ct"].toInt());
}
if (params.contains("rgb")) {
emit notificationReveiced(Property::Rgb, params["rgb"]);
emit rgbNotificationReceived(params["rgb"].toInt());
}
if (params.contains("hue")) {
emit notificationReveiced(Property::Hue, params["hue"]);
emit hueNotificationReceived(params["hue"].toInt());
}
if (params.contains("name")) {
emit notificationReveiced(Property::Name, params["name"]);
emit nameNotificationReceived(params["name"].toString());
}
if (params.contains("color_mode")) {
emit notificationReveiced(Property::ColorMode, params["color_mode"]);
//emit colorModeNotificationReceived(static_cast<ColorMode>((params["color_mode"].toInt())));
}
if (params.contains("sat")) {
emit notificationReveiced(Property::Sat, params["sat"]);
emit saturationNotificationReceived(params["sat"].toInt());
}
}
} else {
@ -379,7 +379,7 @@ void Yeelight::onReadyRead()
if (result.first().toString() == "ok") {
emit requestExecuted(id, true);
} else {
//TODO parse error
//TODO parse error, status code and error string
//emit errorReceived()
}
} else {

View File

@ -106,6 +106,14 @@ signals:
* will get the latest state of the smart LED in time without having to poll the status
* from time to time.
*/
void notificationReveiced(Property property, QVariant value);
void notificationReceived(Property property, QVariant value);
void powerNotificationReceived(bool status);
void brightnessNotificationReceived(int percentage);
void colorTemperatureNotificationReceived(int kelvin);
void rgbNotificationReceived(int rgbColor);
void hueNotificationReceived(int hueColor);
void nameNotificationReceived(const QString &name);
void saturationNotificationReceived(int percentage);
//void colorModeNotificationReceived(ColorMode colorMode);
};
#endif // YEELIGHT_H