updated to new device api

This commit is contained in:
nymea 2019-06-25 22:49:16 +02:00
parent 0ba20c9c4e
commit 1fa68fc3c3
2 changed files with 57 additions and 56 deletions

View File

@ -44,7 +44,7 @@ DevicePluginWs2812fx ::DevicePluginWs2812fx ()
{ {
} }
DeviceManager::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *device) Device::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *device)
{ {
if(!m_reconnectTimer) { if(!m_reconnectTimer) {
m_reconnectTimer = new QTimer(this); m_reconnectTimer = new QTimer(this);
@ -61,7 +61,7 @@ DeviceManager::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *devic
QSerialPort *serialPort = new QSerialPort(interface, this); QSerialPort *serialPort = new QSerialPort(interface, this);
if(!serialPort) if(!serialPort)
return DeviceManager::DeviceSetupStatusFailure; return Device::DeviceSetupStatusFailure;
serialPort->setBaudRate(115200); serialPort->setBaudRate(115200);
serialPort->setDataBits(QSerialPort::DataBits::Data8); serialPort->setDataBits(QSerialPort::DataBits::Data8);
@ -71,7 +71,8 @@ DeviceManager::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *devic
if (!serialPort->open(QIODevice::ReadWrite)) { if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcWs2812fx()) << "Could not open serial port" << interface << serialPort->errorString(); qCWarning(dcWs2812fx()) << "Could not open serial port" << interface << serialPort->errorString();
return DeviceManager::DeviceSetupStatusFailure; serialPort->deleteLater();
return Device::DeviceSetupStatusFailure;
} }
connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError)));
@ -82,15 +83,15 @@ DeviceManager::DeviceSetupStatus DevicePluginWs2812fx::setupDevice(Device *devic
m_usedInterfaces.append(interface); m_usedInterfaces.append(interface);
m_serialPorts.insert(device, serialPort); m_serialPorts.insert(device, serialPort);
} else { } else {
return DeviceManager::DeviceSetupStatusFailure; return Device::DeviceSetupStatusFailure;
} }
return DeviceManager::DeviceSetupStatusSuccess; return Device::DeviceSetupStatusSuccess;
} }
return DeviceManager::DeviceSetupStatusFailure; return Device::DeviceSetupStatusFailure;
} }
DeviceManager::DeviceError DevicePluginWs2812fx::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) Device::DeviceError DevicePluginWs2812fx::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
Q_UNUSED(params) Q_UNUSED(params)
// Create the list of available serial interfaces // Create the list of available serial interfaces
@ -101,25 +102,23 @@ DeviceManager::DeviceError DevicePluginWs2812fx::discoverDevices(const DeviceCla
qCDebug(dcWs2812fx()) << "Found serial port:" << port.portName(); qCDebug(dcWs2812fx()) << "Found serial port:" << port.portName();
QString description = port.manufacturer() + " " + port.description(); QString description = port.manufacturer() + " " + port.description();
DeviceDescriptor descriptor(deviceClassId, port.portName(), description); DeviceDescriptor descriptor(deviceClassId, port.portName(), description);
foreach (Device *existingDevice, myDevices().filterByParam(ws2812fxDeviceSerialPortParamTypeId, port.portName())) {
descriptor.setDeviceId(existingDevice->id());
}
ParamList parameters; ParamList parameters;
parameters.append(Param(ws2812fxDeviceSerialPortParamTypeId, port.portName())); parameters.append(Param(ws2812fxDeviceSerialPortParamTypeId, port.portName()));
descriptor.setParams(parameters); descriptor.setParams(parameters);
deviceDescriptors.append(descriptor); deviceDescriptors.append(descriptor);
} }
emit devicesDiscovered(deviceClassId, deviceDescriptors); emit devicesDiscovered(deviceClassId, deviceDescriptors);
return DeviceManager::DeviceErrorAsync; return Device::DeviceErrorAsync;
} }
DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Action &action) Device::DeviceError DevicePluginWs2812fx::executeAction(Device *device, const Action &action)
{ {
qCDebug(dcWs2812fx) << "Execute action" << action.actionTypeId() << action.params();
if (device->deviceClassId() == ws2812fxDeviceClassId ) { if (device->deviceClassId() == ws2812fxDeviceClassId ) {
QSerialPort *serialPort = m_serialPorts.value(device);
if (!serialPort)
return DeviceManager::DeviceErrorDeviceNotFound;
QByteArray command; QByteArray command;
if (action.actionTypeId() == ws2812fxPowerActionTypeId) { if (action.actionTypeId() == ws2812fxPowerActionTypeId) {
command.append("b "); command.append("b ");
@ -129,10 +128,7 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append("0"); command.append("0");
} }
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Brightness);
serialPort->write(command);
m_pendingActions.insert("brightness", action.id());
return DeviceManager::DeviceErrorAsync;
} }
if (action.actionTypeId() == ws2812fxBrightnessActionTypeId) { if (action.actionTypeId() == ws2812fxBrightnessActionTypeId) {
@ -140,10 +136,7 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append("b "); command.append("b ");
command.append(action.param(ws2812fxBrightnessActionBrightnessParamTypeId).value().toString()); command.append(action.param(ws2812fxBrightnessActionBrightnessParamTypeId).value().toString());
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Brightness);
serialPort->write(command);
m_pendingActions.insert("brightness", action.id());
return DeviceManager::DeviceErrorAsync;
} }
if (action.actionTypeId() == ws2812fxSpeedActionTypeId) { if (action.actionTypeId() == ws2812fxSpeedActionTypeId) {
@ -151,10 +144,7 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append("s "); command.append("s ");
command.append(action.param(ws2812fxSpeedActionSpeedParamTypeId).value().toString()); command.append(action.param(ws2812fxSpeedActionSpeedParamTypeId).value().toString());
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Speed);
serialPort->write(command);
m_pendingActions.insert("speed", action.id());
return DeviceManager::DeviceErrorAsync;
} }
if (action.actionTypeId() == ws2812fxColorActionTypeId) { if (action.actionTypeId() == ws2812fxColorActionTypeId) {
@ -164,11 +154,7 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append("c "); command.append("c ");
command.append(QString(color.name()).remove("#")); command.append(QString(color.name()).remove("#"));
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Color);
serialPort->write(command);
m_pendingActions.insert("color", action.id());
return DeviceManager::DeviceErrorAsync;
} }
if (action.actionTypeId() == ws2812fxColorTemperatureActionTypeId) { if (action.actionTypeId() == ws2812fxColorTemperatureActionTypeId) {
@ -180,10 +166,7 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append("c "); command.append("c ");
command.append(QString(color.name()).remove("#")); command.append(QString(color.name()).remove("#"));
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Color);
serialPort->write(command);
m_pendingActions.insert("color", action.id());
return DeviceManager::DeviceErrorAsync;
} }
if (action.actionTypeId() == ws2812fxEffectModeActionTypeId) { if (action.actionTypeId() == ws2812fxEffectModeActionTypeId) {
@ -308,14 +291,11 @@ DeviceManager::DeviceError DevicePluginWs2812fx::executeAction(Device *device, c
command.append(QString::number(FX_MODE_CUSTOM_3)); command.append(QString::number(FX_MODE_CUSTOM_3));
} }
command.append("\r\n"); command.append("\r\n");
qDebug(dcWs2812fx()) << "Sending command" << command; return sendCommand(device, action.id(), command, CommandType::Mode);
serialPort->write(command);
m_pendingActions.insert("mode", action.id());
return DeviceManager::DeviceErrorAsync;
} }
return DeviceManager::DeviceErrorActionTypeNotFound; return Device::DeviceErrorActionTypeNotFound;
} }
return DeviceManager::DeviceErrorDeviceClassNotFound; return Device::DeviceErrorDeviceClassNotFound;
} }
@ -347,8 +327,8 @@ void DevicePluginWs2812fx::onReadyRead()
qDebug(dcWs2812fx()) << "Message received" << data; qDebug(dcWs2812fx()) << "Message received" << data;
if (data.contains("mode")) { if (data.contains("mode")) {
if (m_pendingActions.contains("mode")) { if (m_pendingActions.contains(CommandType::Mode)) {
emit actionExecutionFinished(m_pendingActions.value("mode"), DeviceManager::DeviceErrorNoError); emit actionExecutionFinished(m_pendingActions.value(CommandType::Mode), Device::DeviceErrorNoError);
} }
QString mode = data.split('-').at(1); QString mode = data.split('-').at(1);
mode.remove(0, 1); mode.remove(0, 1);
@ -357,8 +337,8 @@ void DevicePluginWs2812fx::onReadyRead()
device->setStateValue(ws2812fxEffectModeStateTypeId, mode); device->setStateValue(ws2812fxEffectModeStateTypeId, mode);
} }
if (data.contains("brightness")) { if (data.contains("brightness")) {
if (m_pendingActions.contains("brightness")) { if (m_pendingActions.contains(CommandType::Brightness)) {
emit actionExecutionFinished(m_pendingActions.value("brightness"), DeviceManager::DeviceErrorNoError); emit actionExecutionFinished(m_pendingActions.value(CommandType::Brightness), Device::DeviceErrorNoError);
} }
QString rawBrightness = data.split(':').at(1); QString rawBrightness = data.split(':').at(1);
rawBrightness.remove(" "); rawBrightness.remove(" ");
@ -374,8 +354,8 @@ void DevicePluginWs2812fx::onReadyRead()
} }
} }
if (data.contains("speed")) { if (data.contains("speed")) {
if (m_pendingActions.contains("speed")) { if (m_pendingActions.contains(CommandType::Speed)) {
emit actionExecutionFinished(m_pendingActions.value("speed"), DeviceManager::DeviceErrorNoError); emit actionExecutionFinished(m_pendingActions.value(CommandType::Speed), Device::DeviceErrorNoError);
} }
QString rawSpeed = data.split(':').at(1); QString rawSpeed = data.split(':').at(1);
rawSpeed.remove(" "); rawSpeed.remove(" ");
@ -386,8 +366,8 @@ void DevicePluginWs2812fx::onReadyRead()
device->setStateValue(ws2812fxSpeedStateTypeId, speed); device->setStateValue(ws2812fxSpeedStateTypeId, speed);
} }
if (data.contains("color")) { if (data.contains("color")) {
if (m_pendingActions.contains("color")) { if (m_pendingActions.contains(CommandType::Color)) {
emit actionExecutionFinished(m_pendingActions.value("color"), DeviceManager::DeviceErrorNoError); emit actionExecutionFinished(m_pendingActions.value(CommandType::Color), Device::DeviceErrorNoError);
} }
QString rawColor = data.split(':').at(1); QString rawColor = data.split(':').at(1);
rawColor.remove(" "); rawColor.remove(" ");
@ -430,3 +410,17 @@ void DevicePluginWs2812fx::onSerialError(QSerialPort::SerialPortError error)
device->setStateValue(ws2812fxConnectedStateTypeId, false); device->setStateValue(ws2812fxConnectedStateTypeId, false);
} }
} }
Device::DeviceError DevicePluginWs2812fx::sendCommand(Device* device, ActionId actionId, const QByteArray &command, CommandType commandType)
{
qDebug(dcWs2812fx()) << "Sending command" << command;
QSerialPort *serialPort = m_serialPorts.value(device);
if (!serialPort)
return Device::DeviceErrorDeviceNotFound;
if (serialPort->write(command) != command.length()) {
qCWarning(dcWs2812fx) << "Error writing to serial port";
return Device::DeviceErrorHardwareFailure;
}
m_pendingActions.insert(commandType, actionId);
return Device::DeviceErrorAsync;
}

View File

@ -85,8 +85,7 @@
#define FX_MODE_CUSTOM_2 58 #define FX_MODE_CUSTOM_2 58
#define FX_MODE_CUSTOM_3 59 #define FX_MODE_CUSTOM_3 59
#include "plugin/deviceplugin.h" #include "devices/deviceplugin.h"
#include "devicemanager.h"
#include <QTimer> #include <QTimer>
#include <QSerialPort> #include <QSerialPort>
@ -102,21 +101,29 @@ class DevicePluginWs2812fx : public DevicePlugin
public: public:
explicit DevicePluginWs2812fx(); explicit DevicePluginWs2812fx();
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; Device::DeviceSetupStatus setupDevice(Device *device) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; Device::DeviceError executeAction(Device *device, const Action &action) override;
private: private:
enum CommandType {
Color,
Speed,
Brightness,
Mode
};
QHash<Device *, QSerialPort *> m_serialPorts; QHash<Device *, QSerialPort *> m_serialPorts;
QList<QString> m_usedInterfaces; QList<QString> m_usedInterfaces;
QHash<QString, ActionId> m_pendingActions; QHash<CommandType, ActionId> m_pendingActions;
QTimer *m_reconnectTimer = nullptr; QTimer *m_reconnectTimer = nullptr;
Device::DeviceError sendCommand(Device *device, ActionId actionId, const QByteArray &command, CommandType commandType);
private slots: private slots:
void onReadyRead(); void onReadyRead();
void onReconnectTimer(); void onReconnectTimer();
void onSerialError(QSerialPort::SerialPortError error); void onSerialError(QSerialPort::SerialPortError error);
signals: signals: