From 7050a6657791bafb2c3ec659462ced238974aee6 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 13 Jun 2018 22:22:09 +0200 Subject: [PATCH 01/15] added serial port commander --- nymea-plugins.pro | 1 + .../devicepluginserialportcommander.cpp | 211 +++++++++++++++++ .../devicepluginserialportcommander.h | 59 +++++ .../devicepluginserialportcommander.json | 214 ++++++++++++++++++ serialportcommander/serialportcommander.cpp | 133 +++++++++++ serialportcommander/serialportcommander.h | 68 ++++++ serialportcommander/serialportcommander.pro | 14 ++ 7 files changed, 700 insertions(+) create mode 100644 serialportcommander/devicepluginserialportcommander.cpp create mode 100644 serialportcommander/devicepluginserialportcommander.h create mode 100644 serialportcommander/devicepluginserialportcommander.json create mode 100644 serialportcommander/serialportcommander.cpp create mode 100644 serialportcommander/serialportcommander.h create mode 100644 serialportcommander/serialportcommander.pro diff --git a/nymea-plugins.pro b/nymea-plugins.pro index 7e7689f6..6e31b8c3 100644 --- a/nymea-plugins.pro +++ b/nymea-plugins.pro @@ -36,6 +36,7 @@ PLUGIN_DIRS = \ pushbullet \ remotessh \ senic \ + serialportcommander \ simulation \ snapd \ tasmota \ diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp new file mode 100644 index 00000000..462b0755 --- /dev/null +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -0,0 +1,211 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Bernhard Trinnes * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "devicepluginserialportcommander.h" +#include "plugininfo.h" + +DevicePluginSerialPortCommander::DevicePluginSerialPortCommander() +{ +} + +void DevicePluginSerialPortCommander::init() +{ +} + + +DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) +{ + + if (device->deviceClassId() == serialPortOutputDeviceClassId) { + QString interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); + + if (!m_serialPortCommanders.contains(interface)) { + + QSerialPort *serialPort = new QSerialPort(interface, this); + if(!serialPort) + return DeviceManager::DeviceSetupStatusFailure; + + serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); + serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); + serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt())); + serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); + + if (!serialPort->open(QIODevice::ReadWrite)) { + qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); + return DeviceManager::DeviceSetupStatusFailure; + } + + qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; + + SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); + connect(serialPortCommander, &SerialPortCommander::commandReceived, this, &DevicePluginSerialPortCommander::onCommandReceived); + m_serialPortCommanders.insert(interface, serialPortCommander); + + } else { + SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); + if (serialPortCommander->hasOutputDevice()) + return DeviceManager::DeviceSetupStatusFailure; + serialPortCommander->addOutputDevice(device); + + } + return DeviceManager::DeviceSetupStatusSuccess; + + } else if (device->deviceClassId() == serialPortInputDeviceClassId) { + QString interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); + + if (!m_serialPortCommanders.contains(interface)) { + + QSerialPort *serialPort = new QSerialPort(interface, this); + if(!serialPort) + return DeviceManager::DeviceSetupStatusFailure; + + serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); + serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); + serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt())); + serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); + + if (!serialPort->open(QIODevice::ReadWrite)) { + qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); + return DeviceManager::DeviceSetupStatusFailure; + } + + qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; + connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); + connect(serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + + SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); + m_serialPortCommanders.insert(interface, serialPortCommander); + + } else { + SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); + //connect(serialPortCommander, SIGNAL(commandReceived(Device *), this, SLOT(onCommandReceived(Device *)); + serialPortCommander->addInputDevice(device); + } + return DeviceManager::DeviceSetupStatusSuccess; + } + return DeviceManager::DeviceSetupStatusFailure; +} + + +DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +{ + Q_UNUSED(params) + + // Create the list of available serial interfaces + QList deviceDescriptors; + + Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { + qCDebug(dcSerialPortCommander()) << "Found Serial interface:" << port.systemLocation() << port.portName(); + QString description = port.manufacturer() + " | " + port.description(); + DeviceDescriptor descriptor(deviceClassId, port.systemLocation(), description); + ParamList parameters; + + if (deviceClassId == serialPortInputDeviceClassId) { + //TODO add all currenly used devices + parameters.append(Param(serialPortInputSerialPortParamTypeId, port.systemLocation())); + } + + if (deviceClassId == serialPortOutputDeviceClassId) { + //TODO add currently only as input used devices + parameters.append(Param(serialPortOutputSerialPortParamTypeId, port.systemLocation())); + } + descriptor.setParams(parameters); + deviceDescriptors.append(descriptor); + } + + Q_FOREACH(SerialPortCommander *serialPortCommander, m_serialPortCommanders.values()) { + + QSerialPort *serialPort = serialPortCommander->serialPort(); + QString description = "also used by another device"; + DeviceDescriptor descriptor(deviceClassId, serialPort->portName(), description); + ParamList parameters; + + if (deviceClassId == serialPortInputDeviceClassId) { + parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); + } + + if (deviceClassId == serialPortOutputDeviceClassId) { + if (serialPortCommander->hasOutputDevice()){ + continue; + } + + parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); + parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); + parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); + parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); + parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); + parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); + } + descriptor.setParams(parameters); + deviceDescriptors.append(descriptor); + } + + + + emit devicesDiscovered(deviceClassId, deviceDescriptors); + return DeviceManager::DeviceErrorAsync; +} + + +DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device *device, const Action &action) +{ + if (device->deviceClassId() == serialPortOutputDeviceClassId ) { + + if (action.actionTypeId() == serialPortOutputTriggerActionTypeId) { + + QString interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); + SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); + serialPortCommander->sendCommand(action.param(serialPortOutputOutputDataAreaParamTypeId).value().toByteArray()); + + return DeviceManager::DeviceErrorNoError; + } + return DeviceManager::DeviceErrorActionTypeNotFound; + } + return DeviceManager::DeviceErrorDeviceClassNotFound; +} + + +void DevicePluginSerialPortCommander::deviceRemoved(Device *device) +{ + QString interface; + SerialPortCommander *serialPortCommander; + + if (device->deviceClassId() == serialPortInputDeviceClassId) { + + interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); + serialPortCommander = m_serialPortCommanders.value(interface); + serialPortCommander->removeInputDevice(device); + } + + if (device->deviceClassId() == serialPortOutputDeviceClassId) { + interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); + serialPortCommander = m_serialPortCommanders.value(interface); + serialPortCommander->removeOutputDevice(); + } + + if (serialPortCommander->isEmpty()) + m_serialPortCommanders.remove(interface); +} + + +void DevicePluginSerialPortCommander::onCommandReceived(Device *device) +{ + emitEvent(Event(serialPortInputTriggeredEventTypeId, device->id())); +} diff --git a/serialportcommander/devicepluginserialportcommander.h b/serialportcommander/devicepluginserialportcommander.h new file mode 100644 index 00000000..90d26ef8 --- /dev/null +++ b/serialportcommander/devicepluginserialportcommander.h @@ -0,0 +1,59 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Bernhard Trinnes * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef DEVICEPLUGINSERIALPORTCOMMANDER_H +#define DEVICEPLUGINSERIALPORTCOMMANDER_H + +#include "plugin/deviceplugin.h" +#include "devicemanager.h" +#include "serialportcommander.h" +#include +#include + +class DevicePluginSerialPortCommander : public DevicePlugin +{ + Q_OBJECT + + Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "devicepluginserialportcommander.json") + Q_INTERFACES(DevicePlugin) + +public: + explicit DevicePluginSerialPortCommander(); + + DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; + void deviceRemoved(Device *device) override; + DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); + + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; + void init() override; + +private: + //QHash m_outputSerialPorts; + //QHash m_inputSerialPorts; + QHash m_serialPortCommanders; + +private slots: + void onCommandReceived(Device *device); + +signals: + +}; + +#endif // DEVICEPLUGINSERIALPORTCOMMANDER_H diff --git a/serialportcommander/devicepluginserialportcommander.json b/serialportcommander/devicepluginserialportcommander.json new file mode 100644 index 00000000..686d7f70 --- /dev/null +++ b/serialportcommander/devicepluginserialportcommander.json @@ -0,0 +1,214 @@ +{ + "displayName": "Serial port commander", + "name": "SerialPortCommander", + "id": "fe93a12e-36f4-4015-8019-26b659817773", + "vendors": [ + { + "name": "guh", + "displayName": "guh GmbH", + "id": "2062d64d-3232-433c-88bc-0d33c0ba2ba6", + "deviceClasses": [ + + { + "id": "540566d8-a2a6-4ce2-9a1e-a66a989e6199", + "name": "serialPortOutput", + "displayName": "Serial port output", + "deviceIcon": "Network", + "createMethods": ["user", "discovery"], + "interfaces": ["outputtrigger"], + "basicTags": [ + "Device" + ], + "paramTypes": [ + { + "id": "ed49f7d8-ab18-4c37-9b80-1004b75dcb91", + "name": "serialPort", + "displayName": "Serial port", + "type": "QString", + "inputType": "TextLine", + "defaultValue": "/dev/ttyAMA0" + }, + { + "id": "45dfc828-f238-4263-89a3-9b35cf5dea39", + "name": "baudRate", + "displayName": "Baud rate", + "type": "int", + "defaultValue": 9600 + }, + { + "id": "add4f7fb-1be9-4944-a420-3355b20174f9", + "name": "dataBits", + "displayName": "Data bits", + "type": "int", + "defaultValue": 8 + }, + { + "id": "4ea8bcdf-d4c5-45a4-a54f-f10ac3f08a78", + "name": "stopBits", + "displayName": "Stop bits", + "type": "int", + "defaultValue": 0 + }, + { + "id": "7e5d197f-0224-4c6f-8e86-0e7c867da5f1", + "name": "flowControl", + "displayName": "Flow control", + "type": "QString", + "inputType": "TextLine", + "allowedValues": [ + "No Flow Control", + "Hardware Control", + "Software Control", + "Unknown Flow Control" + ], + "defaultValue": "No Flow Control" + }, + { + "id": "72de1b08-2a27-49c5-90e0-8788c3ea1da3", + "name": "parity", + "displayName": "Parity", + "type": "QString", + "inputType": "TextLine", + "allowedValues": [ + "No Parity", + "Even Parity", + "Odd Parity", + "Space Parity", + "Mark Parity", + "Unknown Parity" + ], + "defaultValue": "No Parity" + } + ], + "actionTypes": [ + { + "id": "0b22c4d1-f5f6-4a93-aa93-660d27bf8f71", + "name": "trigger", + "displayName": "Trigger", + "paramTypes": [ + { + "id": "a27ecedc-424e-49ce-8956-9dbca2feac02", + "name": "outputDataArea", + "displayName": "Data", + "type": "QString", + "inputType": "TextArea" + } + ] + } + ] + }, + { + "id": "b4862936-e6a1-4720-b386-7292ca4d0f6d", + "name": "serialPortInput", + "displayName": "Serial Port Input", + "deviceIcon": "Network", + "createMethods": ["user", "discovery"], + "interfaces": ["inputtrigger"], + "basicTags": [ + "Device" + ], + "paramTypes": [ + { + "id": "ed49f7d8-ab18-4c37-9b80-1004b75dcb91", + "name": "serialPort", + "displayName": "Serial Port", + "type": "QString", + "inputType": "TextLine", + "defaultValue": "/dev/ttyAMA0" + }, + { + "id": "45dfc828-f238-4263-89a3-9b35cf5dea39", + "name": "baudRate", + "displayName": "Baud Rate", + "type": "int", + "defaultValue": 9600 + }, + { + "id": "add4f7fb-1be9-4944-a420-3355b20174f9", + "name": "dataBits", + "displayName": "Data Bits", + "type": "int", + "defaultValue": 8 + }, + { + "id": "4ea8bcdf-d4c5-45a4-a54f-f10ac3f08a78", + "name": "stopBits", + "displayName": "Stop Bits", + "type": "int", + "defaultValue": 0 + }, + { + "id": "7e5d197f-0224-4c6f-8e86-0e7c867da5f1", + "name": "flowControl", + "displayName": "Flow Control", + "type": "QString", + "inputType": "TextLine", + "allowedValues": [ + "No Flow Control", + "Hardware Control", + "Software Control", + "Unknown Flow Control" + ], + "defaultValue": "No Flow Control" + }, + { + "id": "71de1b08-2a27-49c5-90e0-8788c3ea1da3", + "name": "parity", + "displayName": "Parity", + "type": "QString", + "inputType": "TextLine", + "allowedValues": [ + "No Parity", + "Even Parity", + "Odd Parity", + "Space Parity", + "Mark Parity", + "Unknown Parity" + ], + "defaultValue": "No Parity" + }, + { + "id": "e99f55c7-0e14-45ee-b0f0-33f1d1d2e674", + "name": "comparisonType", + "displayName": "Comparison Type", + "type": "QString", + "allowedValues": [ + "Is exactly", + "Contains", + "Contains not", + "Starts with", + "Ends with" + ], + "defaultValue": "Exactly" + }, + { + "id": "13051bdf-3f50-41fa-abde-bc4fe0bcc4fc", + "name": "inputCommand", + "displayName": "Data", + "type": "QString", + "inputType": "TextArea", + "defaultValue": "" + } + ], + "stateTypes": [ + { + "id": "b98fdacc-59d7-41c4-b790-1fdca50dfb22", + "name": "inputData", + "displayName": "Received Data", + "displayNameEvent": "received data changed", + "type": "QString", + "defaultValue": "" + } + ], + "eventTypes": [ + { + "id": "6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b", + "name": "triggered", + "displayName": "Command received" + } + ] + } + ] + } + ] +} diff --git a/serialportcommander/serialportcommander.cpp b/serialportcommander/serialportcommander.cpp new file mode 100644 index 00000000..7ebde253 --- /dev/null +++ b/serialportcommander/serialportcommander.cpp @@ -0,0 +1,133 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Bernhard Trinnes * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "serialportcommander.h" + +SerialPortCommander::SerialPortCommander(QSerialPort *serialPort, QObject *parent) : + QObject(parent), + m_serialPort(serialPort) +{ + connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); + connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); +} + + +SerialPortCommander::~SerialPortCommander() +{ + m_serialPort->close(); + m_serialPort->deleteLater(); +} + + +void SerialPortCommander::addOutputDevice(Device* device) +{ + m_outputDevice = device; + return; +} + + +void SerialPortCommander::removeOutputDevice() +{ + m_outputDevice = NULL; +} + + +void SerialPortCommander::addInputDevice(Device* device) +{ + m_inputDevices.append(device); +} + + +void SerialPortCommander::removeInputDevice(Device* device) +{ + m_inputDevices.removeOne(device); +} + + +bool SerialPortCommander::isEmpty() +{ + return(!hasOutputDevice() || m_inputDevices.empty()); +} + + +bool SerialPortCommander::hasOutputDevice() +{ + if (m_outputDevice == NULL) { + return false; + } else { + return true; + } +} + + +QSerialPort * SerialPortCommander::serialPort() +{ + return m_serialPort; +} + + +Device * SerialPortCommander::outputDevice() +{ + return m_outputDevice; +} + + +void SerialPortCommander::onReadyRead() +{ + QByteArray data; + while (!m_serialPort->atEnd()) { + data = m_serialPort->read(100); + } + qDebug(dcSerialPortCommander()) << "Message received" << data; + + foreach (Device *device, m_inputDevices) { + if (device->paramValue(serialPortInputComparisonTypeParamTypeId).toString() == "Is exactly") { + if (data == device->paramValue(serialPortInputInputCommandParamTypeId)) { + emit commandReceived(device); + } + } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId).toString() == "Contains") { + if (data.contains(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { + emit commandReceived(device); + } + } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Contains not") { + if (!data.contains(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { + emit commandReceived(device); + } + } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Starts with") { + if (data.startsWith(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { + emit commandReceived(device); + } + } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Ends with") { + if (data.endsWith(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { + emit commandReceived(device); + } + } + } +} + +void SerialPortCommander::onSerialError(QSerialPort::SerialPortError error) +{ + Q_UNUSED(error); +} + +void SerialPortCommander::sendCommand(QByteArray data) +{ + m_serialPort->write(data); +} diff --git a/serialportcommander/serialportcommander.h b/serialportcommander/serialportcommander.h new file mode 100644 index 00000000..e49fc4a7 --- /dev/null +++ b/serialportcommander/serialportcommander.h @@ -0,0 +1,68 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Bernhard Trinnes * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef SERIALPORTCOMMANDER_H +#define SERIALPORTCOMMANDER_H + +#include +#include +#include "extern-plugininfo.h" +#include "devicemanager.h" + +class SerialPortCommander : public QObject +{ + Q_OBJECT +public: + explicit SerialPortCommander(QSerialPort *serialPort ,QObject *parent = 0); + ~SerialPortCommander(); + + enum ComparisonType { + IsExactly, + Contains, + ContainsNot, + StartsWith, + EndsWith + }; + + void addOutputDevice(Device *device); + void addInputDevice(Device *device); + void removeInputDevice(Device *device); + bool isEmpty(); + bool hasOutputDevice(); + void removeOutputDevice(); + void sendCommand(QByteArray data); + QSerialPort *serialPort(); + Device *outputDevice(); + +private: + QList m_inputDevices; + Device *m_outputDevice; + QSerialPort *m_serialPort; + +signals: + void commandReceived(Device *device); + +public slots: + void onReadyRead(); + void onSerialError(QSerialPort::SerialPortError error); + +}; + +#endif // SERIALPORTCOMMANDER_H diff --git a/serialportcommander/serialportcommander.pro b/serialportcommander/serialportcommander.pro new file mode 100644 index 00000000..ad155012 --- /dev/null +++ b/serialportcommander/serialportcommander.pro @@ -0,0 +1,14 @@ +include(../plugins.pri) + +QT += serialport + +TARGET = $$qtLibraryTarget(nymea_devicepluginserialportcommander) + +SOURCES += \ + devicepluginserialportcommander.cpp \ + serialportcommander.cpp + + +HEADERS += \ + devicepluginserialportcommander.h \ + serialportcommander.h From 75bb3f9bd7c909f924e1fef6edd797a1b1ebcca5 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 13 Jun 2018 23:07:37 +0200 Subject: [PATCH 02/15] fixed device discovery --- .../devicepluginserialportcommander.cpp | 89 ++++++++++--------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 462b0755..19e177ac 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -112,53 +112,49 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons QList deviceDescriptors; Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { - qCDebug(dcSerialPortCommander()) << "Found Serial interface:" << port.systemLocation() << port.portName(); - QString description = port.manufacturer() + " | " + port.description(); - DeviceDescriptor descriptor(deviceClassId, port.systemLocation(), description); - ParamList parameters; + if (m_serialPortCommanders.contains(port.portName())){ + SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(port.portName()); + QSerialPort *serialPort = serialPortCommander->serialPort(); + QString description = "Note, this serial port is also used by another device"; + DeviceDescriptor descriptor(deviceClassId, serialPort->portName(), description); + ParamList parameters; - if (deviceClassId == serialPortInputDeviceClassId) { - //TODO add all currenly used devices - parameters.append(Param(serialPortInputSerialPortParamTypeId, port.systemLocation())); - } - - if (deviceClassId == serialPortOutputDeviceClassId) { - //TODO add currently only as input used devices - parameters.append(Param(serialPortOutputSerialPortParamTypeId, port.systemLocation())); - } - descriptor.setParams(parameters); - deviceDescriptors.append(descriptor); - } - - Q_FOREACH(SerialPortCommander *serialPortCommander, m_serialPortCommanders.values()) { - - QSerialPort *serialPort = serialPortCommander->serialPort(); - QString description = "also used by another device"; - DeviceDescriptor descriptor(deviceClassId, serialPort->portName(), description); - ParamList parameters; - - if (deviceClassId == serialPortInputDeviceClassId) { - parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); - } - - if (deviceClassId == serialPortOutputDeviceClassId) { - if (serialPortCommander->hasOutputDevice()){ - continue; + if (deviceClassId == serialPortInputDeviceClassId) { + parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); } - parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); - parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); - parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); - parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); - parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); - parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); + if (deviceClassId == serialPortOutputDeviceClassId) { + if (serialPortCommander->hasOutputDevice()){ + continue; + } + } + parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); + parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); + parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); + parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); + parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); + parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); + descriptor.setParams(parameters); + deviceDescriptors.append(descriptor); + } else { + + qCDebug(dcSerialPortCommander()) << "Found Serial interface:" << port.portName(); + QString description = port.manufacturer() + " | " + port.description(); + DeviceDescriptor descriptor(deviceClassId, port.portName(), description); + ParamList parameters; + + if (deviceClassId == serialPortInputDeviceClassId) { + parameters.append(Param(serialPortInputSerialPortParamTypeId, port.portName())); + } + + if (deviceClassId == serialPortOutputDeviceClassId) { + parameters.append(Param(serialPortOutputSerialPortParamTypeId, port.portName())); + } + descriptor.setParams(parameters); + deviceDescriptors.append(descriptor); } - descriptor.setParams(parameters); - deviceDescriptors.append(descriptor); } - - emit devicesDiscovered(deviceClassId, deviceDescriptors); return DeviceManager::DeviceErrorAsync; } @@ -192,16 +188,21 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); serialPortCommander = m_serialPortCommanders.value(interface); serialPortCommander->removeInputDevice(device); + if (serialPortCommander->isEmpty()) { + m_serialPortCommanders.remove(interface); + serialPortCommander->deleteLater(); + } } if (device->deviceClassId() == serialPortOutputDeviceClassId) { interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); serialPortCommander = m_serialPortCommanders.value(interface); serialPortCommander->removeOutputDevice(); + if (serialPortCommander->isEmpty()) { + m_serialPortCommanders.remove(interface); + serialPortCommander->deleteLater(); + } } - - if (serialPortCommander->isEmpty()) - m_serialPortCommanders.remove(interface); } From fd5f018d221b7a3e600acf856c4336651f8be9f9 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 13 Jun 2018 23:36:32 +0200 Subject: [PATCH 03/15] fixed event connection --- .../devicepluginserialportcommander.cpp | 15 +++++++-------- .../devicepluginserialportcommander.h | 2 -- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 19e177ac..6519adbd 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -55,7 +55,6 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); - connect(serialPortCommander, &SerialPortCommander::commandReceived, this, &DevicePluginSerialPortCommander::onCommandReceived); m_serialPortCommanders.insert(interface, serialPortCommander); } else { @@ -85,17 +84,15 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); return DeviceManager::DeviceSetupStatusFailure; } - qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; - connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); - connect(serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); - SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); + connect(serialPortCommander, SIGNAL(commandReceived(Device *)), this, SLOT(onCommandReceived(Device *))); + serialPortCommander->addInputDevice(device); m_serialPortCommanders.insert(interface, serialPortCommander); } else { SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); - //connect(serialPortCommander, SIGNAL(commandReceived(Device *), this, SLOT(onCommandReceived(Device *)); + connect(serialPortCommander, SIGNAL(commandReceived(Device *)), this, SLOT(onCommandReceived(Device *))); serialPortCommander->addInputDevice(device); } return DeviceManager::DeviceSetupStatusSuccess; @@ -138,8 +135,8 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons deviceDescriptors.append(descriptor); } else { - qCDebug(dcSerialPortCommander()) << "Found Serial interface:" << port.portName(); - QString description = port.manufacturer() + " | " + port.description(); + qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName(); + QString description = port.manufacturer() + " " + port.description(); DeviceDescriptor descriptor(deviceClassId, port.portName(), description); ParamList parameters; @@ -190,6 +187,7 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) serialPortCommander->removeInputDevice(device); if (serialPortCommander->isEmpty()) { m_serialPortCommanders.remove(interface); + serialPortCommander->serialPort()->close(); serialPortCommander->deleteLater(); } } @@ -200,6 +198,7 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) serialPortCommander->removeOutputDevice(); if (serialPortCommander->isEmpty()) { m_serialPortCommanders.remove(interface); + serialPortCommander->serialPort()->close(); serialPortCommander->deleteLater(); } } diff --git a/serialportcommander/devicepluginserialportcommander.h b/serialportcommander/devicepluginserialportcommander.h index 90d26ef8..b36fba54 100644 --- a/serialportcommander/devicepluginserialportcommander.h +++ b/serialportcommander/devicepluginserialportcommander.h @@ -45,8 +45,6 @@ public: void init() override; private: - //QHash m_outputSerialPorts; - //QHash m_inputSerialPorts; QHash m_serialPortCommanders; private slots: From 499dd538baf171e79e0c4b950dd1b37beca62448 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 13 Jun 2018 23:47:21 +0200 Subject: [PATCH 04/15] fixed event connection --- .../devicepluginserialportcommander.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 6519adbd..0ddec3b2 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -55,6 +55,7 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); + serialPortCommander->addOutputDevice(device); m_serialPortCommanders.insert(interface, serialPortCommander); } else { @@ -117,24 +118,33 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons ParamList parameters; if (deviceClassId == serialPortInputDeviceClassId) { + //take the params from the already existing in/output device parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); + parameters.append(Param(serialPortInputBaudRateParamTypeId, serialPort->baudRate())); + parameters.append(Param(serialPortInputDataBitsParamTypeId, serialPort->dataBits())); + parameters.append(Param(serialPortInputFlowControlParamTypeId, serialPort->flowControl())); + parameters.append(Param(serialPortInputStopBitsParamTypeId, serialPort->stopBits())); + parameters.append(Param(serialPortInputParityParamTypeId, serialPort->parity())); } if (deviceClassId == serialPortOutputDeviceClassId) { if (serialPortCommander->hasOutputDevice()){ + //only one output per port is allowed continue; } + //take the params from the already existing input device + parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); + parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); + parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); + parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); + parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); + parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); } - parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); - parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); - parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); - parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); - parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); - parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); + descriptor.setParams(parameters); deviceDescriptors.append(descriptor); } else { - + //Serial port is not yet used, create now a new one qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName(); QString description = port.manufacturer() + " " + port.description(); DeviceDescriptor descriptor(deviceClassId, port.portName(), description); From ce9f0c77b8643236962fad8319d098404d8d85f0 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 13 Jun 2018 23:58:19 +0200 Subject: [PATCH 05/15] fixed event connection --- serialportcommander/devicepluginserialportcommander.cpp | 2 -- serialportcommander/serialportcommander.cpp | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 0ddec3b2..71b832f2 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -197,7 +197,6 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) serialPortCommander->removeInputDevice(device); if (serialPortCommander->isEmpty()) { m_serialPortCommanders.remove(interface); - serialPortCommander->serialPort()->close(); serialPortCommander->deleteLater(); } } @@ -208,7 +207,6 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) serialPortCommander->removeOutputDevice(); if (serialPortCommander->isEmpty()) { m_serialPortCommanders.remove(interface); - serialPortCommander->serialPort()->close(); serialPortCommander->deleteLater(); } } diff --git a/serialportcommander/serialportcommander.cpp b/serialportcommander/serialportcommander.cpp index 7ebde253..6c3d4800 100644 --- a/serialportcommander/serialportcommander.cpp +++ b/serialportcommander/serialportcommander.cpp @@ -45,7 +45,7 @@ void SerialPortCommander::addOutputDevice(Device* device) void SerialPortCommander::removeOutputDevice() { - m_outputDevice = NULL; + m_outputDevice = nullptr; } @@ -63,7 +63,7 @@ void SerialPortCommander::removeInputDevice(Device* device) bool SerialPortCommander::isEmpty() { - return(!hasOutputDevice() || m_inputDevices.empty()); + return(!hasOutputDevice() && m_inputDevices.empty()); } From d7d4e5672ce203bb703897d43df3161354fe4275 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Thu, 14 Jun 2018 01:14:26 +0200 Subject: [PATCH 06/15] added serial port settings synchronisation --- .../devicepluginserialportcommander.cpp | 16 +++--- .../devicepluginserialportcommander.json | 4 +- serialportcommander/serialportcommander.cpp | 56 ++++++++++++++++++- serialportcommander/serialportcommander.h | 6 ++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 71b832f2..c6926bfb 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -44,8 +44,9 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); - serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt())); + //TODO set parity serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); + //TODO set flow control if (!serialPort->open(QIODevice::ReadWrite)) { qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); @@ -78,8 +79,9 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); - serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt())); + //TODO set parity serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); + //TODO set flow control if (!serialPort->open(QIODevice::ReadWrite)) { qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); @@ -122,13 +124,13 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); parameters.append(Param(serialPortInputBaudRateParamTypeId, serialPort->baudRate())); parameters.append(Param(serialPortInputDataBitsParamTypeId, serialPort->dataBits())); - parameters.append(Param(serialPortInputFlowControlParamTypeId, serialPort->flowControl())); + //TODO set flow control parameters.append(Param(serialPortInputStopBitsParamTypeId, serialPort->stopBits())); - parameters.append(Param(serialPortInputParityParamTypeId, serialPort->parity())); + //TODO set parity } if (deviceClassId == serialPortOutputDeviceClassId) { - if (serialPortCommander->hasOutputDevice()){ + if (serialPortCommander->hasOutputDevice()) { //only one output per port is allowed continue; } @@ -136,9 +138,9 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); - parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl())); + //TODO set flow control parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); - parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity())); + //TODO set parity } descriptor.setParams(parameters); diff --git a/serialportcommander/devicepluginserialportcommander.json b/serialportcommander/devicepluginserialportcommander.json index 686d7f70..2c5e437e 100644 --- a/serialportcommander/devicepluginserialportcommander.json +++ b/serialportcommander/devicepluginserialportcommander.json @@ -26,7 +26,7 @@ "displayName": "Serial port", "type": "QString", "inputType": "TextLine", - "defaultValue": "/dev/ttyAMA0" + "defaultValue": "ttyAMA0" }, { "id": "45dfc828-f238-4263-89a3-9b35cf5dea39", @@ -114,7 +114,7 @@ "displayName": "Serial Port", "type": "QString", "inputType": "TextLine", - "defaultValue": "/dev/ttyAMA0" + "defaultValue": "ttyAMA0" }, { "id": "45dfc828-f238-4263-89a3-9b35cf5dea39", diff --git a/serialportcommander/serialportcommander.cpp b/serialportcommander/serialportcommander.cpp index 6c3d4800..2455043f 100644 --- a/serialportcommander/serialportcommander.cpp +++ b/serialportcommander/serialportcommander.cpp @@ -26,6 +26,11 @@ SerialPortCommander::SerialPortCommander(QSerialPort *serialPort, QObject *paren { connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(m_serialPort, SIGNAL(baudRateChanged(qint32, QSerialPort::Direction)), this, SLOT(onBaudRateChanged(qint32, QSerialPort::Direction))); + connect(m_serialPort, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(onParityChanged(QSerialPort::Parity))); + connect(m_serialPort, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(onDataBitsChanged(QSerialPort::DataBits))); + connect(m_serialPort, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(onStopBitsChanged(QSerialPort::StopBits))); + connect(m_serialPort, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(onFlowControlChanged(QSerialPort::FlowControl))); } @@ -39,7 +44,6 @@ SerialPortCommander::~SerialPortCommander() void SerialPortCommander::addOutputDevice(Device* device) { m_outputDevice = device; - return; } @@ -57,7 +61,7 @@ void SerialPortCommander::addInputDevice(Device* device) void SerialPortCommander::removeInputDevice(Device* device) { - m_inputDevices.removeOne(device); + m_inputDevices.removeAll(device); } @@ -69,7 +73,7 @@ bool SerialPortCommander::isEmpty() bool SerialPortCommander::hasOutputDevice() { - if (m_outputDevice == NULL) { + if (m_outputDevice == nullptr) { return false; } else { return true; @@ -127,6 +131,52 @@ void SerialPortCommander::onSerialError(QSerialPort::SerialPortError error) Q_UNUSED(error); } +void SerialPortCommander::onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction) +{ + Q_UNUSED(direction); + foreach(Device *device, m_inputDevices) { + device->setParamValue(serialPortInputBaudRateParamTypeId, baudRate); + } + if(m_outputDevice != nullptr) + m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, baudRate); +} + +void SerialPortCommander::onParityChanged(QSerialPort::Parity parity) +{ + foreach(Device *device, m_inputDevices) { + device->setParamValue(serialPortInputParityParamTypeId, parity); //TODO Strings not int + } + if(m_outputDevice != nullptr) + m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, parity); +} + +void SerialPortCommander::onDataBitsChanged(QSerialPort::DataBits dataBits) +{ + foreach(Device *device, m_inputDevices) { + device->setParamValue(serialPortInputDataBitsParamTypeId, dataBits); + } + if(m_outputDevice != nullptr) + m_outputDevice->setParamValue(serialPortOutputDataBitsParamTypeId, dataBits); +} + +void SerialPortCommander::onStopBitsChanged(QSerialPort::StopBits stopBits) +{ + foreach(Device *device, m_inputDevices) { + device->setParamValue(serialPortInputStopBitsParamTypeId, stopBits); + } + if(m_outputDevice != nullptr) + m_outputDevice->setParamValue(serialPortOutputStopBitsParamTypeId, stopBits); +} + +void SerialPortCommander::onFlowControlChanged(QSerialPort::FlowControl flowControl) +{ + //foreach(Device *device, m_inputDevices) { //TODO enum to string + //device->setParamValue(serialPortInputFlowControlParamTypeId, QVariant::fromValue(QSerialPort::FlowControl).value()); + //} + if(m_outputDevice != nullptr) + m_outputDevice->setParamValue(serialPortOutputFlowControlParamTypeId, flowControl); +} + void SerialPortCommander::sendCommand(QByteArray data) { m_serialPort->write(data); diff --git a/serialportcommander/serialportcommander.h b/serialportcommander/serialportcommander.h index e49fc4a7..3b877623 100644 --- a/serialportcommander/serialportcommander.h +++ b/serialportcommander/serialportcommander.h @@ -60,8 +60,14 @@ signals: void commandReceived(Device *device); public slots: + void onReadyRead(); void onSerialError(QSerialPort::SerialPortError error); + void onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction); + void onParityChanged(QSerialPort::Parity parity); + void onDataBitsChanged(QSerialPort::DataBits dataBits); + void onStopBitsChanged(QSerialPort::StopBits stopBits); + void onFlowControlChanged(QSerialPort::FlowControl flowControl); }; From b7548e8f7de04e9e16cc622e620664715b1b8faa Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Tue, 19 Jun 2018 10:31:04 +0200 Subject: [PATCH 07/15] on the quest to find the segfault --- serialportcommander/serialportcommander.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serialportcommander/serialportcommander.cpp b/serialportcommander/serialportcommander.cpp index 2455043f..cc4c6d14 100644 --- a/serialportcommander/serialportcommander.cpp +++ b/serialportcommander/serialportcommander.cpp @@ -170,7 +170,7 @@ void SerialPortCommander::onStopBitsChanged(QSerialPort::StopBits stopBits) void SerialPortCommander::onFlowControlChanged(QSerialPort::FlowControl flowControl) { - //foreach(Device *device, m_inputDevices) { //TODO enum to string + //foreach(Device *device, m_inputDevices) { //TODO enum to stringau //device->setParamValue(serialPortInputFlowControlParamTypeId, QVariant::fromValue(QSerialPort::FlowControl).value()); //} if(m_outputDevice != nullptr) From 1163fe5add5c1ab751500587a6f82e95cb937ddb Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 9 Jan 2019 17:43:52 +0100 Subject: [PATCH 08/15] Serial commander: combined out and input into one deviceclass --- .../devicepluginserialportcommander.cpp | 180 ++++++------------ .../devicepluginserialportcommander.h | 27 +-- .../devicepluginserialportcommander.json | 112 +---------- serialportcommander/serialportcommander.pro | 2 - 4 files changed, 81 insertions(+), 240 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index c6926bfb..0a873251 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -1,20 +1,22 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2017 Bernhard Trinnes * + * Copyright (C) 2019 Bernhard Trinnes * * * - * This file is part of guh. * + * This file is part of nymea. * * * - * Guh is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 2 of the License. * + * 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. * * * - * Guh is distributed in the hope that it will be useful, * + * 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 General Public License for more details. * + * 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 General Public License * - * along with guh. If not, see . * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -33,20 +35,40 @@ void DevicePluginSerialPortCommander::init() DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) { - if (device->deviceClassId() == serialPortOutputDeviceClassId) { - QString interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); + if (device->deviceClassId() == serialPortCommanderDeviceClassId) { + QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); - if (!m_serialPortCommanders.contains(interface)) { + if (!m_serialPorts.contains(interface)) { QSerialPort *serialPort = new QSerialPort(interface, this); if(!serialPort) return DeviceManager::DeviceSetupStatusFailure; - serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); - serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); - //TODO set parity - serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); - //TODO set flow control + serialPort->setBaudRate(device->paramValue(serialPortCommanderDeviceBaudRateParamTypeId).toInt()); + serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortCommanderDeviceDataBitsParamTypeId).toInt())); + + if (device->paramValue(serialPortCommanderDeviceParityParamTypeId).toString().contains("Even")) { + serialPort->setParity(QSerialPort::Parity::EvenParity ); + } else if (device->paramValue(serialPortCommanderDeviceParityParamTypeId).toString().contains("Odd")) { + serialPort->setParity(QSerialPort::Parity::OddParity ); + } else if (device->paramValue(serialPortCommanderDeviceParityParamTypeId).toString().contains("Space")) { + serialPort->setParity(QSerialPort::Parity::SpaceParity ); + } else if (device->paramValue(serialPortCommanderDeviceParityParamTypeId).toString().contains("Mark")) { + serialPort->setParity(QSerialPort::Parity::MarkParity ); + } else { + serialPort->setParity(QSerialPort::Parity::NoParity); + } + + serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortCommanderDeviceStopBitsParamTypeId).toInt())); + + if (device->paramValue(serialPortCommanderDeviceFlowControlParamTypeId).toString().contains("Hardware")) { + serialPort->setFlowControl(QSerialPort::FlowControl::HardwareControl); + } else if (device->paramValue(serialPortCommanderDeviceFlowControlParamTypeId).toString().contains("Software")) { + serialPort->setFlowControl(QSerialPort::FlowControl::SoftwareControl); + } else { + serialPort->setFlowControl(QSerialPort::FlowControl::NoFlowControl); + } + if (!serialPort->open(QIODevice::ReadWrite)) { qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); @@ -54,49 +76,10 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De } qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; - - SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); - serialPortCommander->addOutputDevice(device); - m_serialPortCommanders.insert(interface, serialPortCommander); + m_serialPorts.insert(interface, serialPort); } else { - SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); - if (serialPortCommander->hasOutputDevice()) return DeviceManager::DeviceSetupStatusFailure; - serialPortCommander->addOutputDevice(device); - - } - return DeviceManager::DeviceSetupStatusSuccess; - - } else if (device->deviceClassId() == serialPortInputDeviceClassId) { - QString interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); - - if (!m_serialPortCommanders.contains(interface)) { - - QSerialPort *serialPort = new QSerialPort(interface, this); - if(!serialPort) - return DeviceManager::DeviceSetupStatusFailure; - - serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); - serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); - //TODO set parity - serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); - //TODO set flow control - - if (!serialPort->open(QIODevice::ReadWrite)) { - qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); - return DeviceManager::DeviceSetupStatusFailure; - } - qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; - SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this); - connect(serialPortCommander, SIGNAL(commandReceived(Device *)), this, SLOT(onCommandReceived(Device *))); - serialPortCommander->addInputDevice(device); - m_serialPortCommanders.insert(interface, serialPortCommander); - - } else { - SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); - connect(serialPortCommander, SIGNAL(commandReceived(Device *)), this, SLOT(onCommandReceived(Device *))); - serialPortCommander->addInputDevice(device); } return DeviceManager::DeviceSetupStatusSuccess; } @@ -112,39 +95,9 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons QList deviceDescriptors; Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { - if (m_serialPortCommanders.contains(port.portName())){ - SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(port.portName()); - QSerialPort *serialPort = serialPortCommander->serialPort(); - QString description = "Note, this serial port is also used by another device"; - DeviceDescriptor descriptor(deviceClassId, serialPort->portName(), description); - ParamList parameters; - - if (deviceClassId == serialPortInputDeviceClassId) { - //take the params from the already existing in/output device - parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName())); - parameters.append(Param(serialPortInputBaudRateParamTypeId, serialPort->baudRate())); - parameters.append(Param(serialPortInputDataBitsParamTypeId, serialPort->dataBits())); - //TODO set flow control - parameters.append(Param(serialPortInputStopBitsParamTypeId, serialPort->stopBits())); - //TODO set parity - } - - if (deviceClassId == serialPortOutputDeviceClassId) { - if (serialPortCommander->hasOutputDevice()) { - //only one output per port is allowed - continue; - } - //take the params from the already existing input device - parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName())); - parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate())); - parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits())); - //TODO set flow control - parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits())); - //TODO set parity - } - - descriptor.setParams(parameters); - deviceDescriptors.append(descriptor); + if (m_serialPorts.contains(port.portName())){ + //device already in use + qCDebug(dcSerialPortCommander()) << "Found serial port that is already used:" << port.portName(); } else { //Serial port is not yet used, create now a new one qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName(); @@ -152,13 +105,10 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons DeviceDescriptor descriptor(deviceClassId, port.portName(), description); ParamList parameters; - if (deviceClassId == serialPortInputDeviceClassId) { - parameters.append(Param(serialPortInputSerialPortParamTypeId, port.portName())); + if (deviceClassId == serialPortCommanderDeviceClassId) { + parameters.append(Param(serialPortCommanderDeviceSerialPortParamTypeId, port.portName())); } - if (deviceClassId == serialPortOutputDeviceClassId) { - parameters.append(Param(serialPortOutputSerialPortParamTypeId, port.portName())); - } descriptor.setParams(parameters); deviceDescriptors.append(descriptor); } @@ -171,13 +121,13 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device *device, const Action &action) { - if (device->deviceClassId() == serialPortOutputDeviceClassId ) { + if (device->deviceClassId() == serialPortCommanderDeviceClassId ) { - if (action.actionTypeId() == serialPortOutputTriggerActionTypeId) { + if (action.actionTypeId() == serialPortCommanderTriggerActionTypeId) { - QString interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); - SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); - serialPortCommander->sendCommand(action.param(serialPortOutputOutputDataAreaParamTypeId).value().toByteArray()); + QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); + QSerialPort *serialPort = m_serialPorts.value(interface); + serialPort->write(action.param(serialPortCommanderTriggerActionOutputDataParamTypeId).value().toByteArray()); return DeviceManager::DeviceErrorNoError; } @@ -190,32 +140,20 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device void DevicePluginSerialPortCommander::deviceRemoved(Device *device) { QString interface; - SerialPortCommander *serialPortCommander; + QSerialPort *serialPort; - if (device->deviceClassId() == serialPortInputDeviceClassId) { + if (device->deviceClassId() == serialPortCommanderDeviceClassId) { - interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); - serialPortCommander = m_serialPortCommanders.value(interface); - serialPortCommander->removeInputDevice(device); - if (serialPortCommander->isEmpty()) { - m_serialPortCommanders.remove(interface); - serialPortCommander->deleteLater(); - } - } - - if (device->deviceClassId() == serialPortOutputDeviceClassId) { - interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); - serialPortCommander = m_serialPortCommanders.value(interface); - serialPortCommander->removeOutputDevice(); - if (serialPortCommander->isEmpty()) { - m_serialPortCommanders.remove(interface); - serialPortCommander->deleteLater(); - } + interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); + serialPort = m_serialPorts.value(interface); + serialPort->close(); + m_serialPorts.remove(interface); + serialPort->deleteLater(); } } void DevicePluginSerialPortCommander::onCommandReceived(Device *device) { - emitEvent(Event(serialPortInputTriggeredEventTypeId, device->id())); + emitEvent(Event(serialPortCommanderTriggeredEventTypeId, device->id())); } diff --git a/serialportcommander/devicepluginserialportcommander.h b/serialportcommander/devicepluginserialportcommander.h index b36fba54..b8823dbd 100644 --- a/serialportcommander/devicepluginserialportcommander.h +++ b/serialportcommander/devicepluginserialportcommander.h @@ -1,20 +1,22 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2017 Bernhard Trinnes * + * Copyright (C) 2019 Bernhard Trinnes * * * - * This file is part of guh. * + * This file is part of nymea. * * * - * Guh is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 2 of the License. * + * 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. * * * - * Guh is distributed in the hope that it will be useful, * + * 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 General Public License for more details. * + * 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 General Public License * - * along with guh. If not, see . * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -23,7 +25,6 @@ #include "plugin/deviceplugin.h" #include "devicemanager.h" -#include "serialportcommander.h" #include #include @@ -31,7 +32,7 @@ class DevicePluginSerialPortCommander : public DevicePlugin { Q_OBJECT - Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "devicepluginserialportcommander.json") + Q_PLUGIN_METADATA(IID "io.nymea.DevicePlugin" FILE "devicepluginserialportcommander.json") Q_INTERFACES(DevicePlugin) public: @@ -45,7 +46,7 @@ public: void init() override; private: - QHash m_serialPortCommanders; + QHash m_serialPorts; private slots: void onCommandReceived(Device *device); diff --git a/serialportcommander/devicepluginserialportcommander.json b/serialportcommander/devicepluginserialportcommander.json index 2c5e437e..61b76d09 100644 --- a/serialportcommander/devicepluginserialportcommander.json +++ b/serialportcommander/devicepluginserialportcommander.json @@ -11,11 +11,11 @@ { "id": "540566d8-a2a6-4ce2-9a1e-a66a989e6199", - "name": "serialPortOutput", - "displayName": "Serial port output", + "name": "serialPortCommander", + "displayName": "Serial port commander", "deviceIcon": "Network", "createMethods": ["user", "discovery"], - "interfaces": ["outputtrigger"], + "interfaces": ["outputtrigger", "inputtrigger"], "basicTags": [ "Device" ], @@ -58,8 +58,7 @@ "allowedValues": [ "No Flow Control", "Hardware Control", - "Software Control", - "Unknown Flow Control" + "Software Control" ], "defaultValue": "No Flow Control" }, @@ -74,8 +73,7 @@ "Even Parity", "Odd Parity", "Space Parity", - "Mark Parity", - "Unknown Parity" + "Mark Parity" ], "defaultValue": "No Parity" } @@ -88,107 +86,13 @@ "paramTypes": [ { "id": "a27ecedc-424e-49ce-8956-9dbca2feac02", - "name": "outputDataArea", + "name": "outputData", "displayName": "Data", "type": "QString", "inputType": "TextArea" } ] } - ] - }, - { - "id": "b4862936-e6a1-4720-b386-7292ca4d0f6d", - "name": "serialPortInput", - "displayName": "Serial Port Input", - "deviceIcon": "Network", - "createMethods": ["user", "discovery"], - "interfaces": ["inputtrigger"], - "basicTags": [ - "Device" - ], - "paramTypes": [ - { - "id": "ed49f7d8-ab18-4c37-9b80-1004b75dcb91", - "name": "serialPort", - "displayName": "Serial Port", - "type": "QString", - "inputType": "TextLine", - "defaultValue": "ttyAMA0" - }, - { - "id": "45dfc828-f238-4263-89a3-9b35cf5dea39", - "name": "baudRate", - "displayName": "Baud Rate", - "type": "int", - "defaultValue": 9600 - }, - { - "id": "add4f7fb-1be9-4944-a420-3355b20174f9", - "name": "dataBits", - "displayName": "Data Bits", - "type": "int", - "defaultValue": 8 - }, - { - "id": "4ea8bcdf-d4c5-45a4-a54f-f10ac3f08a78", - "name": "stopBits", - "displayName": "Stop Bits", - "type": "int", - "defaultValue": 0 - }, - { - "id": "7e5d197f-0224-4c6f-8e86-0e7c867da5f1", - "name": "flowControl", - "displayName": "Flow Control", - "type": "QString", - "inputType": "TextLine", - "allowedValues": [ - "No Flow Control", - "Hardware Control", - "Software Control", - "Unknown Flow Control" - ], - "defaultValue": "No Flow Control" - }, - { - "id": "71de1b08-2a27-49c5-90e0-8788c3ea1da3", - "name": "parity", - "displayName": "Parity", - "type": "QString", - "inputType": "TextLine", - "allowedValues": [ - "No Parity", - "Even Parity", - "Odd Parity", - "Space Parity", - "Mark Parity", - "Unknown Parity" - ], - "defaultValue": "No Parity" - }, - { - "id": "e99f55c7-0e14-45ee-b0f0-33f1d1d2e674", - "name": "comparisonType", - "displayName": "Comparison Type", - "type": "QString", - "allowedValues": [ - "Is exactly", - "Contains", - "Contains not", - "Starts with", - "Ends with" - ], - "defaultValue": "Exactly" - }, - { - "id": "13051bdf-3f50-41fa-abde-bc4fe0bcc4fc", - "name": "inputCommand", - "displayName": "Data", - "type": "QString", - "inputType": "TextArea", - "defaultValue": "" - } ], "stateTypes": [ { @@ -202,9 +106,9 @@ ], "eventTypes": [ { - "id": "6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b", + "id": "32087633-616c-45a7-85af-4f1695c22359", "name": "triggered", - "displayName": "Command received" + "displayName": "Data received" } ] } diff --git a/serialportcommander/serialportcommander.pro b/serialportcommander/serialportcommander.pro index ad155012..ffee06b0 100644 --- a/serialportcommander/serialportcommander.pro +++ b/serialportcommander/serialportcommander.pro @@ -6,9 +6,7 @@ TARGET = $$qtLibraryTarget(nymea_devicepluginserialportcommander) SOURCES += \ devicepluginserialportcommander.cpp \ - serialportcommander.cpp HEADERS += \ devicepluginserialportcommander.h \ - serialportcommander.h From 604c740797aad398185d689b0a602d75b1011aff Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Wed, 9 Jan 2019 18:15:42 +0100 Subject: [PATCH 09/15] Serialport Commander: added and connected signals --- .../devicepluginserialportcommander.cpp | 82 ++++++++++++++++--- .../devicepluginserialportcommander.h | 11 ++- 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 0a873251..fd5b9d90 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -31,14 +31,13 @@ void DevicePluginSerialPortCommander::init() { } - DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) { if (device->deviceClassId() == serialPortCommanderDeviceClassId) { QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); - if (!m_serialPorts.contains(interface)) { + if (!m_usedInterfaces.contains(interface)) { QSerialPort *serialPort = new QSerialPort(interface, this); if(!serialPort) @@ -75,11 +74,18 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De return DeviceManager::DeviceSetupStatusFailure; } + connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); + connect(serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(serialPort, SIGNAL(baudRateChanged(qint32, QSerialPort::Direction)), this, SLOT(onBaudRateChanged(qint32, QSerialPort::Direction))); + connect(serialPort, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(onParityChanged(QSerialPort::Parity))); + connect(serialPort, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(onDataBitsChanged(QSerialPort::DataBits))); + connect(serialPort, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(onStopBitsChanged(QSerialPort::StopBits))); + connect(serialPort, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(onFlowControlChanged(QSerialPort::FlowControl))); qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; - m_serialPorts.insert(interface, serialPort); + m_serialPorts.insert(device, serialPort); } else { - return DeviceManager::DeviceSetupStatusFailure; + return DeviceManager::DeviceSetupStatusFailure; } return DeviceManager::DeviceSetupStatusSuccess; } @@ -95,9 +101,9 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons QList deviceDescriptors; Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { - if (m_serialPorts.contains(port.portName())){ + if (m_usedInterfaces.contains(port.portName())){ //device already in use - qCDebug(dcSerialPortCommander()) << "Found serial port that is already used:" << port.portName(); + qCDebug(dcSerialPortCommander()) << "Found serial port that is already used:" << port.portName(); } else { //Serial port is not yet used, create now a new one qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName(); @@ -125,8 +131,7 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device if (action.actionTypeId() == serialPortCommanderTriggerActionTypeId) { - QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); - QSerialPort *serialPort = m_serialPorts.value(interface); + QSerialPort *serialPort = m_serialPorts.value(device); serialPort->write(action.param(serialPortCommanderTriggerActionOutputDataParamTypeId).value().toByteArray()); return DeviceManager::DeviceErrorNoError; @@ -144,16 +149,67 @@ void DevicePluginSerialPortCommander::deviceRemoved(Device *device) if (device->deviceClassId() == serialPortCommanderDeviceClassId) { - interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); - serialPort = m_serialPorts.value(interface); + m_usedInterfaces.removeAll(device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString()); + serialPort = m_serialPorts.value(device); serialPort->close(); - m_serialPorts.remove(interface); + m_serialPorts.remove(device); serialPort->deleteLater(); } } - -void DevicePluginSerialPortCommander::onCommandReceived(Device *device) +void DevicePluginSerialPortCommander::onReadyRead() { + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + + QByteArray data; + while (!serialPort->atEnd()) { + data = serialPort->read(100); + } + qDebug(dcSerialPortCommander()) << "Message received" << data; + + device->setStateValue(serialPortCommanderInputDataStateTypeId, data); emitEvent(Event(serialPortCommanderTriggeredEventTypeId, device->id())); } + +void DevicePluginSerialPortCommander::onSerialError(QSerialPort::SerialPortError error) +{ + Q_UNUSED(error) +} + +void DevicePluginSerialPortCommander::onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction) +{ + Q_UNUSED(direction) + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + device->setParamValue(serialPortCommanderDeviceBaudRateParamTypeId, baudRate); +} + +void DevicePluginSerialPortCommander::onParityChanged(QSerialPort::Parity parity) +{ + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + device->setParamValue(serialPortCommanderDeviceParityParamTypeId, parity); +} + +void DevicePluginSerialPortCommander::onDataBitsChanged(QSerialPort::DataBits dataBits) +{ + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + device->setParamValue(serialPortCommanderDeviceDataBitsParamTypeId, dataBits); +} + +void DevicePluginSerialPortCommander::onStopBitsChanged(QSerialPort::StopBits stopBits) +{ + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + device->setParamValue(serialPortCommanderDeviceStopBitsParamTypeId, stopBits); +} + +void DevicePluginSerialPortCommander::onFlowControlChanged(QSerialPort::FlowControl flowControl) +{ + QSerialPort *serialPort = static_cast(sender()); + Device *device = m_serialPorts.key(serialPort); + device->setParamValue(serialPortCommanderDeviceFlowControlParamTypeId, flowControl); +} + diff --git a/serialportcommander/devicepluginserialportcommander.h b/serialportcommander/devicepluginserialportcommander.h index b8823dbd..277e6e43 100644 --- a/serialportcommander/devicepluginserialportcommander.h +++ b/serialportcommander/devicepluginserialportcommander.h @@ -46,10 +46,17 @@ public: void init() override; private: - QHash m_serialPorts; + QHash m_serialPorts; + QList m_usedInterfaces; private slots: - void onCommandReceived(Device *device); + void onReadyRead(); + void onSerialError(QSerialPort::SerialPortError error); + void onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction); + void onParityChanged(QSerialPort::Parity parity); + void onDataBitsChanged(QSerialPort::DataBits dataBits); + void onStopBitsChanged(QSerialPort::StopBits stopBits); + void onFlowControlChanged(QSerialPort::FlowControl flowControl); signals: From ce6dbc673a5ce4810fa8a1b10cbf7a5d71a26d53 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Sat, 12 Jan 2019 19:31:58 +0100 Subject: [PATCH 10/15] Serial port commander: removed deprecated files --- .../devicepluginserialportcommander.cpp | 20 +- serialportcommander/serialportcommander.cpp | 183 ------------------ serialportcommander/serialportcommander.h | 74 ------- 3 files changed, 17 insertions(+), 260 deletions(-) delete mode 100644 serialportcommander/serialportcommander.cpp delete mode 100644 serialportcommander/serialportcommander.h diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index fd5b9d90..02815518 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -20,6 +20,23 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/*! + \page serialportcommander.html + \title Serial Port Commander + \brief Plug-In to send and receive strings over a serial port. + + \ingroup plugins + \ingroup nymea-plugins + + \chapter Plugin properties + Following JSON file contains the definition and the description of all available \l{DeviceClass}{DeviceClasses} + 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}. + + \quotefile plugins/deviceplugins/serialportcommander/devicepluginserialportcommander.json +*/ + #include "devicepluginserialportcommander.h" #include "plugininfo.h" @@ -33,7 +50,6 @@ void DevicePluginSerialPortCommander::init() DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) { - if (device->deviceClassId() == serialPortCommanderDeviceClassId) { QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString(); @@ -68,7 +84,6 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De serialPort->setFlowControl(QSerialPort::FlowControl::NoFlowControl); } - if (!serialPort->open(QIODevice::ReadWrite)) { qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); return DeviceManager::DeviceSetupStatusFailure; @@ -96,7 +111,6 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(params) - // Create the list of available serial interfaces QList deviceDescriptors; diff --git a/serialportcommander/serialportcommander.cpp b/serialportcommander/serialportcommander.cpp deleted file mode 100644 index cc4c6d14..00000000 --- a/serialportcommander/serialportcommander.cpp +++ /dev/null @@ -1,183 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Bernhard Trinnes * - * * - * This file is part of guh. * - * * - * Guh is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 2 of the License. * - * * - * Guh 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 General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "serialportcommander.h" - -SerialPortCommander::SerialPortCommander(QSerialPort *serialPort, QObject *parent) : - QObject(parent), - m_serialPort(serialPort) -{ - connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); - connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); - connect(m_serialPort, SIGNAL(baudRateChanged(qint32, QSerialPort::Direction)), this, SLOT(onBaudRateChanged(qint32, QSerialPort::Direction))); - connect(m_serialPort, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(onParityChanged(QSerialPort::Parity))); - connect(m_serialPort, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(onDataBitsChanged(QSerialPort::DataBits))); - connect(m_serialPort, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(onStopBitsChanged(QSerialPort::StopBits))); - connect(m_serialPort, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(onFlowControlChanged(QSerialPort::FlowControl))); -} - - -SerialPortCommander::~SerialPortCommander() -{ - m_serialPort->close(); - m_serialPort->deleteLater(); -} - - -void SerialPortCommander::addOutputDevice(Device* device) -{ - m_outputDevice = device; -} - - -void SerialPortCommander::removeOutputDevice() -{ - m_outputDevice = nullptr; -} - - -void SerialPortCommander::addInputDevice(Device* device) -{ - m_inputDevices.append(device); -} - - -void SerialPortCommander::removeInputDevice(Device* device) -{ - m_inputDevices.removeAll(device); -} - - -bool SerialPortCommander::isEmpty() -{ - return(!hasOutputDevice() && m_inputDevices.empty()); -} - - -bool SerialPortCommander::hasOutputDevice() -{ - if (m_outputDevice == nullptr) { - return false; - } else { - return true; - } -} - - -QSerialPort * SerialPortCommander::serialPort() -{ - return m_serialPort; -} - - -Device * SerialPortCommander::outputDevice() -{ - return m_outputDevice; -} - - -void SerialPortCommander::onReadyRead() -{ - QByteArray data; - while (!m_serialPort->atEnd()) { - data = m_serialPort->read(100); - } - qDebug(dcSerialPortCommander()) << "Message received" << data; - - foreach (Device *device, m_inputDevices) { - if (device->paramValue(serialPortInputComparisonTypeParamTypeId).toString() == "Is exactly") { - if (data == device->paramValue(serialPortInputInputCommandParamTypeId)) { - emit commandReceived(device); - } - } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId).toString() == "Contains") { - if (data.contains(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { - emit commandReceived(device); - } - } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Contains not") { - if (!data.contains(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { - emit commandReceived(device); - } - } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Starts with") { - if (data.startsWith(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { - emit commandReceived(device); - } - } else if (device->paramValue(serialPortInputComparisonTypeParamTypeId) == "Ends with") { - if (data.endsWith(device->paramValue(serialPortInputInputCommandParamTypeId).toByteArray())) { - emit commandReceived(device); - } - } - } -} - -void SerialPortCommander::onSerialError(QSerialPort::SerialPortError error) -{ - Q_UNUSED(error); -} - -void SerialPortCommander::onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction) -{ - Q_UNUSED(direction); - foreach(Device *device, m_inputDevices) { - device->setParamValue(serialPortInputBaudRateParamTypeId, baudRate); - } - if(m_outputDevice != nullptr) - m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, baudRate); -} - -void SerialPortCommander::onParityChanged(QSerialPort::Parity parity) -{ - foreach(Device *device, m_inputDevices) { - device->setParamValue(serialPortInputParityParamTypeId, parity); //TODO Strings not int - } - if(m_outputDevice != nullptr) - m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, parity); -} - -void SerialPortCommander::onDataBitsChanged(QSerialPort::DataBits dataBits) -{ - foreach(Device *device, m_inputDevices) { - device->setParamValue(serialPortInputDataBitsParamTypeId, dataBits); - } - if(m_outputDevice != nullptr) - m_outputDevice->setParamValue(serialPortOutputDataBitsParamTypeId, dataBits); -} - -void SerialPortCommander::onStopBitsChanged(QSerialPort::StopBits stopBits) -{ - foreach(Device *device, m_inputDevices) { - device->setParamValue(serialPortInputStopBitsParamTypeId, stopBits); - } - if(m_outputDevice != nullptr) - m_outputDevice->setParamValue(serialPortOutputStopBitsParamTypeId, stopBits); -} - -void SerialPortCommander::onFlowControlChanged(QSerialPort::FlowControl flowControl) -{ - //foreach(Device *device, m_inputDevices) { //TODO enum to stringau - //device->setParamValue(serialPortInputFlowControlParamTypeId, QVariant::fromValue(QSerialPort::FlowControl).value()); - //} - if(m_outputDevice != nullptr) - m_outputDevice->setParamValue(serialPortOutputFlowControlParamTypeId, flowControl); -} - -void SerialPortCommander::sendCommand(QByteArray data) -{ - m_serialPort->write(data); -} diff --git a/serialportcommander/serialportcommander.h b/serialportcommander/serialportcommander.h deleted file mode 100644 index 3b877623..00000000 --- a/serialportcommander/serialportcommander.h +++ /dev/null @@ -1,74 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Bernhard Trinnes * - * * - * This file is part of guh. * - * * - * Guh is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 2 of the License. * - * * - * Guh 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 General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef SERIALPORTCOMMANDER_H -#define SERIALPORTCOMMANDER_H - -#include -#include -#include "extern-plugininfo.h" -#include "devicemanager.h" - -class SerialPortCommander : public QObject -{ - Q_OBJECT -public: - explicit SerialPortCommander(QSerialPort *serialPort ,QObject *parent = 0); - ~SerialPortCommander(); - - enum ComparisonType { - IsExactly, - Contains, - ContainsNot, - StartsWith, - EndsWith - }; - - void addOutputDevice(Device *device); - void addInputDevice(Device *device); - void removeInputDevice(Device *device); - bool isEmpty(); - bool hasOutputDevice(); - void removeOutputDevice(); - void sendCommand(QByteArray data); - QSerialPort *serialPort(); - Device *outputDevice(); - -private: - QList m_inputDevices; - Device *m_outputDevice; - QSerialPort *m_serialPort; - -signals: - void commandReceived(Device *device); - -public slots: - - void onReadyRead(); - void onSerialError(QSerialPort::SerialPortError error); - void onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction); - void onParityChanged(QSerialPort::Parity parity); - void onDataBitsChanged(QSerialPort::DataBits dataBits); - void onStopBitsChanged(QSerialPort::StopBits stopBits); - void onFlowControlChanged(QSerialPort::FlowControl flowControl); - -}; - -#endif // SERIALPORTCOMMANDER_H From 360d5bcc31cc91bbbaf806a61608115a1ed0f7fe Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Sat, 12 Jan 2019 19:44:44 +0100 Subject: [PATCH 11/15] Added serial port commander to debian packages --- debian/control | 17 ++++++++++++++++- .../nymea-plugin-serialportcommander.install.in | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 debian/nymea-plugin-serialportcommander.install.in diff --git a/debian/control b/debian/control index 68b49f11..ee197854 100644 --- a/debian/control +++ b/debian/control @@ -724,7 +724,7 @@ Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, nymea-plugins-translations, -Description: nymea.io plugin for remote ssh connection +Description: nymea.io plugin for UniPi devices The nymea daemon is a plugin based IoT (Internet of Things) server. The server works like a translator for devices, things and services and allows them to interact. @@ -748,6 +748,20 @@ Description: nymea.io plugin for remote ssh connection . This package will install the nymea.io plugin for unipi devices +Package: nymea-plugin-serialportcommander +Architecture: any +Depends: ${shlibs:Depends}, + ${misc:Depends}, + nymea-plugins-translations, +Description: nymea.io plugin to send and receive strings over a serial port + The nymea daemon is a plugin based IoT (Internet of Things) server. The + server works like a translator for devices, things and services and + allows them to interact. + With the powerful rule engine you are able to connect any device available + in the system and create individual scenes and behaviors for your environment. + . + This package will install the nymea.io plugin for serial ports + Package: nymea-plugins-translations Section: misc @@ -815,6 +829,7 @@ Depends: nymea-plugin-boblight, nymea-plugin-mqttclient, nymea-plugin-remotessh, nymea-plugin-unipi, + nymea-plugin-serialportcommander, Replaces: guh-plugins-maker Description: Plugins for nymea IoT server - Meta package for makers, tinkers and hackers The nymea daemon is a plugin based IoT (Internet of Things) server. The diff --git a/debian/nymea-plugin-serialportcommander.install.in b/debian/nymea-plugin-serialportcommander.install.in new file mode 100644 index 00000000..b5a25a6c --- /dev/null +++ b/debian/nymea-plugin-serialportcommander.install.in @@ -0,0 +1 @@ +usr/lib/@DEB_HOST_MULTIARCH@/nymea/plugins/libnymea_devicepluginserialportcommander.so From 6c5615884838e1db774a3ab2af7a94937ab160ea Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Tue, 22 Jan 2019 19:14:14 +0100 Subject: [PATCH 12/15] Serialport commander: incorporating changes from review --- .../devicepluginserialportcommander.cpp | 21 ++++--------------- .../devicepluginserialportcommander.h | 2 -- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index 02815518..f99d577b 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -44,10 +44,6 @@ DevicePluginSerialPortCommander::DevicePluginSerialPortCommander() { } -void DevicePluginSerialPortCommander::init() -{ -} - DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) { if (device->deviceClassId() == serialPortCommanderDeviceClassId) { @@ -97,8 +93,8 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De connect(serialPort, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(onStopBitsChanged(QSerialPort::StopBits))); connect(serialPort, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(onFlowControlChanged(QSerialPort::FlowControl))); qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface; + m_usedInterfaces.append(interface); m_serialPorts.insert(device, serialPort); - } else { return DeviceManager::DeviceSetupStatusFailure; } @@ -124,16 +120,11 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons QString description = port.manufacturer() + " " + port.description(); DeviceDescriptor descriptor(deviceClassId, port.portName(), description); ParamList parameters; - - if (deviceClassId == serialPortCommanderDeviceClassId) { - parameters.append(Param(serialPortCommanderDeviceSerialPortParamTypeId, port.portName())); - } - + parameters.append(Param(serialPortCommanderDeviceSerialPortParamTypeId, port.portName())); descriptor.setParams(parameters); deviceDescriptors.append(descriptor); } } - emit devicesDiscovered(deviceClassId, deviceDescriptors); return DeviceManager::DeviceErrorAsync; } @@ -158,15 +149,11 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device void DevicePluginSerialPortCommander::deviceRemoved(Device *device) { - QString interface; - QSerialPort *serialPort; - if (device->deviceClassId() == serialPortCommanderDeviceClassId) { m_usedInterfaces.removeAll(device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString()); - serialPort = m_serialPorts.value(device); + QSerialPort *serialPort = m_serialPorts.take(device); serialPort->close(); - m_serialPorts.remove(device); serialPort->deleteLater(); } } @@ -188,7 +175,7 @@ void DevicePluginSerialPortCommander::onReadyRead() void DevicePluginSerialPortCommander::onSerialError(QSerialPort::SerialPortError error) { - Q_UNUSED(error) + qCWarning(dcSerialPortCommander) << "Serial Port error happened:" << error; } void DevicePluginSerialPortCommander::onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction) diff --git a/serialportcommander/devicepluginserialportcommander.h b/serialportcommander/devicepluginserialportcommander.h index 277e6e43..d1e2b45e 100644 --- a/serialportcommander/devicepluginserialportcommander.h +++ b/serialportcommander/devicepluginserialportcommander.h @@ -41,9 +41,7 @@ public: DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; void deviceRemoved(Device *device) override; DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); - DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; - void init() override; private: QHash m_serialPorts; From 623eb0cbcb6c4bcce0860ca16989e4d3a0cf9f64 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Tue, 22 Jan 2019 19:45:55 +0100 Subject: [PATCH 13/15] Serialport commander: moved input data state to an event param --- .../devicepluginserialportcommander.cpp | 7 ++++-- .../devicepluginserialportcommander.json | 22 +++++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/serialportcommander/devicepluginserialportcommander.cpp b/serialportcommander/devicepluginserialportcommander.cpp index f99d577b..20d7fed2 100644 --- a/serialportcommander/devicepluginserialportcommander.cpp +++ b/serialportcommander/devicepluginserialportcommander.cpp @@ -169,8 +169,11 @@ void DevicePluginSerialPortCommander::onReadyRead() } qDebug(dcSerialPortCommander()) << "Message received" << data; - device->setStateValue(serialPortCommanderInputDataStateTypeId, data); - emitEvent(Event(serialPortCommanderTriggeredEventTypeId, device->id())); + Event event(serialPortCommanderTriggeredEventTypeId, device->id()); + ParamList parameters; + parameters.append(Param(serialPortCommanderTriggeredEventInputDataParamTypeId, data)); + event.setParams(parameters); + emitEvent(event); } void DevicePluginSerialPortCommander::onSerialError(QSerialPort::SerialPortError error) diff --git a/serialportcommander/devicepluginserialportcommander.json b/serialportcommander/devicepluginserialportcommander.json index 61b76d09..cfed8bda 100644 --- a/serialportcommander/devicepluginserialportcommander.json +++ b/serialportcommander/devicepluginserialportcommander.json @@ -94,21 +94,21 @@ ] } ], - "stateTypes": [ - { - "id": "b98fdacc-59d7-41c4-b790-1fdca50dfb22", - "name": "inputData", - "displayName": "Received Data", - "displayNameEvent": "received data changed", - "type": "QString", - "defaultValue": "" - } - ], "eventTypes": [ { "id": "32087633-616c-45a7-85af-4f1695c22359", "name": "triggered", - "displayName": "Data received" + "displayName": "Data received", + "paramTypes": [ + { + "id": "b98fdacc-59d7-41c4-b790-1fdca50dfb22", + "name": "inputData", + "displayName": "Received Data", + "displayNameEvent": "received data changed", + "type": "QString", + "defaultValue": "" + } + ] } ] } From 54e51497bc5c6ebd619bb53be809cc241f64e398 Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Fri, 25 Jan 2019 11:03:02 +0100 Subject: [PATCH 14/15] Serialport commander: fixed json file --- serialportcommander/devicepluginserialportcommander.json | 1 - 1 file changed, 1 deletion(-) diff --git a/serialportcommander/devicepluginserialportcommander.json b/serialportcommander/devicepluginserialportcommander.json index cfed8bda..891696a2 100644 --- a/serialportcommander/devicepluginserialportcommander.json +++ b/serialportcommander/devicepluginserialportcommander.json @@ -104,7 +104,6 @@ "id": "b98fdacc-59d7-41c4-b790-1fdca50dfb22", "name": "inputData", "displayName": "Received Data", - "displayNameEvent": "received data changed", "type": "QString", "defaultValue": "" } From b4cfa245a2628488d1b40f1eef0bfb4a20e1eddb Mon Sep 17 00:00:00 2001 From: Bernhard Trinnes Date: Mon, 25 Feb 2019 14:22:57 +0100 Subject: [PATCH 15/15] Serialport commander: added missing debian package --- debian/control | 1 + 1 file changed, 1 insertion(+) diff --git a/debian/control b/debian/control index ee197854..405a6f75 100644 --- a/debian/control +++ b/debian/control @@ -752,6 +752,7 @@ Package: nymea-plugin-serialportcommander Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, + libqt5serialport5, nymea-plugins-translations, Description: nymea.io plugin to send and receive strings over a serial port The nymea daemon is a plugin based IoT (Internet of Things) server. The