Update Boblight to new API

This commit is contained in:
Michael Zanetti 2019-09-17 17:37:00 +02:00
parent 3d50fabe6d
commit 58476b4592
3 changed files with 37 additions and 26 deletions

View File

@ -84,7 +84,8 @@ void BobClient::setPriority(int priority)
{ {
m_priority = priority; m_priority = priority;
if (connected()) { if (connected()) {
qCDebug(dcBoblight) << "setting priority to" << priority << boblight_setpriority(m_boblight, priority); qCDebug(dcBoblight) << "setting priority to" << priority;
boblight_setpriority(m_boblight, priority);
} }
emit priorityChanged(priority); emit priorityChanged(priority);
} }

View File

@ -43,8 +43,8 @@ void DevicePluginBoblight::init()
void DevicePluginBoblight::deviceRemoved(Device *device) void DevicePluginBoblight::deviceRemoved(Device *device)
{ {
BobClient *client = m_bobClients.take(device->id());
if (device->deviceClassId() == boblightServerDeviceClassId) { if (device->deviceClassId() == boblightServerDeviceClassId) {
BobClient *client = m_bobClients.take(device->id());
client->deleteLater(); client->deleteLater();
} }
} }
@ -67,7 +67,6 @@ void DevicePluginBoblight::startMonitoringAutoDevices()
parentDevices[device->parentId()] += 1; parentDevices[device->parentId()] += 1;
} }
} }
qWarning() << "have bridge devices:" << parentDevices.count();
QList<DeviceDescriptor> descriptors; QList<DeviceDescriptor> descriptors;
foreach (const DeviceId &id, parentDevices.keys()) { foreach (const DeviceId &id, parentDevices.keys()) {
if (parentDevices.value(id) < deviceIds.value(id)->paramValue(boblightServerDeviceChannelsParamTypeId).toInt()) { if (parentDevices.value(id) < deviceIds.value(id)->paramValue(boblightServerDeviceChannelsParamTypeId).toInt()) {
@ -80,7 +79,7 @@ void DevicePluginBoblight::startMonitoringAutoDevices()
} }
} }
if (!descriptors.isEmpty()) { if (!descriptors.isEmpty()) {
emit autoDevicesAppeared(boblightDeviceClassId, descriptors); emit autoDevicesAppeared(descriptors);
} }
} }
@ -99,7 +98,7 @@ void DevicePluginBoblight::onPowerChanged(int channel, bool power)
BobClient *sndr = dynamic_cast<BobClient*>(sender()); BobClient *sndr = dynamic_cast<BobClient*>(sender());
foreach (Device* device, myDevices()) { foreach (Device* device, myDevices()) {
if (m_bobClients.value(device->parentId()) == sndr && device->paramValue(boblightDeviceChannelParamTypeId).toInt() == channel) { if (m_bobClients.value(device->parentId()) == sndr && device->paramValue(boblightDeviceChannelParamTypeId).toInt() == channel) {
qCWarning(dcBoblight()) << "setting state power" << power; qCDebug(dcBoblight()) << "setting state power" << power;
device->setStateValue(boblightPowerStateTypeId, power); device->setStateValue(boblightPowerStateTypeId, power);
} }
} }
@ -129,7 +128,7 @@ void DevicePluginBoblight::onPriorityChanged(int priority)
{ {
BobClient *sndr = dynamic_cast<BobClient*>(sender()); BobClient *sndr = dynamic_cast<BobClient*>(sender());
foreach (Device* device, myDevices()) { foreach (Device* device, myDevices()) {
if (m_bobClients.value(device->id()) == sndr) { if (device->deviceClassId() == boblightServerDeviceClassId && m_bobClients.value(device->id()) == sndr) {
device->setStateValue(boblightServerPriorityStateTypeId, priority); device->setStateValue(boblightServerPriorityStateTypeId, priority);
} }
} }
@ -163,14 +162,16 @@ QColor DevicePluginBoblight::tempToRgb(int temp)
return QColor(red, green, blue); return QColor(red, green, blue);
} }
Device::DeviceSetupStatus DevicePluginBoblight::setupDevice(Device *device) void DevicePluginBoblight::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
if (device->deviceClassId() == boblightServerDeviceClassId) { if (device->deviceClassId() == boblightServerDeviceClassId) {
BobClient *bobClient = new BobClient(device->paramValue(boblightServerDeviceHostAddressParamTypeId).toString(), device->paramValue(boblightServerDevicePortParamTypeId).toInt(), this); BobClient *bobClient = new BobClient(device->paramValue(boblightServerDeviceHostAddressParamTypeId).toString(), device->paramValue(boblightServerDevicePortParamTypeId).toInt(), this);
bool connected = bobClient->connectToBoblight(); bool connected = bobClient->connectToBoblight();
if (!connected) { if (!connected) {
qCWarning(dcBoblight()) << "Error connecting to boblight..."; qCWarning(dcBoblight()) << "Error connecting to boblight on" << device->paramValue(boblightServerDeviceHostAddressParamTypeId).toString();
} else { } else {
qCDebug(dcBoblight()) << "Connected to boblight"; qCDebug(dcBoblight()) << "Connected to boblight";
} }
@ -188,7 +189,7 @@ Device::DeviceSetupStatus DevicePluginBoblight::setupDevice(Device *device)
m_bobClients.insert(device->id(), bobClient); m_bobClients.insert(device->id(), bobClient);
} }
return Device::DeviceSetupStatusSuccess; info->finish(Device::DeviceErrorNoError);
} }
void DevicePluginBoblight::postSetupDevice(Device *device) void DevicePluginBoblight::postSetupDevice(Device *device)
@ -213,53 +214,62 @@ void DevicePluginBoblight::postSetupDevice(Device *device)
} }
} }
Device::DeviceError DevicePluginBoblight::executeAction(Device *device, const Action &action) void DevicePluginBoblight::executeAction(DeviceActionInfo *info)
{ {
if (!device->setupComplete()) { Device *device = info->device();
return Device::DeviceErrorHardwareNotAvailable; Action action = info->action();
}
qCDebug(dcBoblight()) << "Execute action for boblight" << action.params(); qCDebug(dcBoblight()) << "Execute action for boblight" << action.params();
if (device->deviceClassId() == boblightServerDeviceClassId) { if (device->deviceClassId() == boblightServerDeviceClassId) {
BobClient *bobClient = m_bobClients.value(device->id()); BobClient *bobClient = m_bobClients.value(device->id());
if (!bobClient || !bobClient->connected()) { if (!bobClient || !bobClient->connected()) {
qCWarning(dcBoblight()) << "Boblight on" << device->paramValue(boblightServerDeviceHostAddressParamTypeId).toString() << "not connected"; qCWarning(dcBoblight()) << "Boblight on" << device->paramValue(boblightServerDeviceHostAddressParamTypeId).toString() << "not connected";
return Device::DeviceErrorHardwareNotAvailable; info->finish(Device::DeviceErrorHardwareNotAvailable);
return;
} }
if (action.actionTypeId() == boblightServerPriorityActionTypeId) { if (action.actionTypeId() == boblightServerPriorityActionTypeId) {
bobClient->setPriority(action.param(boblightServerPriorityActionPriorityParamTypeId).value().toInt()); bobClient->setPriority(action.param(boblightServerPriorityActionPriorityParamTypeId).value().toInt());
return Device::DeviceErrorNoError; info->finish(Device::DeviceErrorNoError);
return;
} }
qCWarning(dcBoblight()) << "Unhandled action" << action.actionTypeId() << "for BoblightServer device" << device; qCWarning(dcBoblight()) << "Unhandled action" << action.actionTypeId() << "for BoblightServer device" << info;
return Device::DeviceErrorActionTypeNotFound; info->finish(Device::DeviceErrorActionTypeNotFound);
return;
} }
if (device->deviceClassId() == boblightDeviceClassId) { if (device->deviceClassId() == boblightDeviceClassId) {
BobClient *bobClient = m_bobClients.value(device->parentId()); BobClient *bobClient = m_bobClients.value(device->parentId());
if (!bobClient || !bobClient->connected()) { if (!bobClient || !bobClient->connected()) {
qCWarning(dcBoblight()) << "Boblight not connected"; qCWarning(dcBoblight()) << "Boblight not connected";
return Device::DeviceErrorHardwareNotAvailable; info->finish(Device::DeviceErrorHardwareNotAvailable);
return;
} }
if (action.actionTypeId() == boblightPowerActionTypeId) { if (action.actionTypeId() == boblightPowerActionTypeId) {
bobClient->setPower(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightPowerActionPowerParamTypeId).value().toBool()); bobClient->setPower(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightPowerActionPowerParamTypeId).value().toBool());
return Device::DeviceErrorNoError; info->finish(Device::DeviceErrorNoError);
return;
} }
if (action.actionTypeId() == boblightColorActionTypeId) { if (action.actionTypeId() == boblightColorActionTypeId) {
bobClient->setColor(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightColorActionColorParamTypeId).value().value<QColor>()); bobClient->setColor(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightColorActionColorParamTypeId).value().value<QColor>());
return Device::DeviceErrorNoError; info->finish(Device::DeviceErrorNoError);
return;
} }
if (action.actionTypeId() == boblightBrightnessActionTypeId) { if (action.actionTypeId() == boblightBrightnessActionTypeId) {
bobClient->setBrightness(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightBrightnessActionBrightnessParamTypeId).value().toInt()); bobClient->setBrightness(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), action.param(boblightBrightnessActionBrightnessParamTypeId).value().toInt());
return Device::DeviceErrorNoError; info->finish(Device::DeviceErrorNoError);
return;
} }
if (action.actionTypeId() == boblightColorTemperatureActionTypeId) { if (action.actionTypeId() == boblightColorTemperatureActionTypeId) {
bobClient->setColor(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), tempToRgb(action.param(boblightColorTemperatureActionColorTemperatureParamTypeId).value().toInt())); bobClient->setColor(device->paramValue(boblightDeviceChannelParamTypeId).toInt(), tempToRgb(action.param(boblightColorTemperatureActionColorTemperatureParamTypeId).value().toInt()));
return Device::DeviceErrorNoError; info->finish(Device::DeviceErrorNoError);
return;
} }
return Device::DeviceErrorActionTypeNotFound; info->finish(Device::DeviceErrorActionTypeNotFound);
return;
} }
return Device::DeviceErrorDeviceClassNotFound; info->finish(Device::DeviceErrorDeviceClassNotFound);
} }
void DevicePluginBoblight::onConnectionChanged() void DevicePluginBoblight::onConnectionChanged()

View File

@ -39,13 +39,13 @@ public:
void init() override; void init() override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
void postSetupDevice(Device *device) override; void postSetupDevice(Device *device) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
void startMonitoringAutoDevices() override; void startMonitoringAutoDevices() override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
private slots: private slots:
void onConnectionChanged(); void onConnectionChanged();