Serial commander: combined out and input into one deviceclass

This commit is contained in:
Bernhard Trinnes 2019-01-09 17:43:52 +01:00 committed by Michael Zanetti
parent 7279ef2a65
commit e59ed2e08e
4 changed files with 81 additions and 240 deletions

View File

@ -1,20 +1,22 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> * * Copyright (C) 2019 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* * * *
* This file is part of guh. * * This file is part of nymea. *
* * * *
* Guh is free software: you can redistribute it and/or modify * * This library is free software; you can redistribute it and/or *
* it under the terms of the GNU General Public License as published by * * modify it under the terms of the GNU Lesser General Public *
* the Free Software Foundation, version 2 of the License. * * 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 * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* GNU General Public License for more details. * * Lesser General Public License for more details. *
* * * *
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU Lesser General Public *
* along with guh. If not, see <http://www.gnu.org/licenses/>. * * License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -33,20 +35,40 @@ void DevicePluginSerialPortCommander::init()
DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(Device *device)
{ {
if (device->deviceClassId() == serialPortOutputDeviceClassId) { if (device->deviceClassId() == serialPortCommanderDeviceClassId) {
QString interface = device->paramValue(serialPortOutputSerialPortParamTypeId).toString(); QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString();
if (!m_serialPortCommanders.contains(interface)) { if (!m_serialPorts.contains(interface)) {
QSerialPort *serialPort = new QSerialPort(interface, this); QSerialPort *serialPort = new QSerialPort(interface, this);
if(!serialPort) if(!serialPort)
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt()); serialPort->setBaudRate(device->paramValue(serialPortCommanderDeviceBaudRateParamTypeId).toInt());
serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt())); serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortCommanderDeviceDataBitsParamTypeId).toInt()));
//TODO set parity
serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt())); if (device->paramValue(serialPortCommanderDeviceParityParamTypeId).toString().contains("Even")) {
//TODO set flow control 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)) { if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString(); 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; qCDebug(dcSerialPortCommander()) << "Setup successfully serial port" << interface;
m_serialPorts.insert(interface, serialPort);
SerialPortCommander *serialPortCommander = new SerialPortCommander(serialPort, this);
serialPortCommander->addOutputDevice(device);
m_serialPortCommanders.insert(interface, serialPortCommander);
} else { } else {
SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface);
if (serialPortCommander->hasOutputDevice())
return DeviceManager::DeviceSetupStatusFailure; 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; return DeviceManager::DeviceSetupStatusSuccess;
} }
@ -112,39 +95,9 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons
QList<DeviceDescriptor> deviceDescriptors; QList<DeviceDescriptor> deviceDescriptors;
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
if (m_serialPortCommanders.contains(port.portName())){ if (m_serialPorts.contains(port.portName())){
SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(port.portName()); //device already in use
QSerialPort *serialPort = serialPortCommander->serialPort(); qCDebug(dcSerialPortCommander()) << "Found serial port that is already used:" << port.portName();
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);
} else { } else {
//Serial port is not yet used, create now a new one //Serial port is not yet used, create now a new one
qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName(); qCDebug(dcSerialPortCommander()) << "Found serial port:" << port.portName();
@ -152,13 +105,10 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons
DeviceDescriptor descriptor(deviceClassId, port.portName(), description); DeviceDescriptor descriptor(deviceClassId, port.portName(), description);
ParamList parameters; ParamList parameters;
if (deviceClassId == serialPortInputDeviceClassId) { if (deviceClassId == serialPortCommanderDeviceClassId) {
parameters.append(Param(serialPortInputSerialPortParamTypeId, port.portName())); parameters.append(Param(serialPortCommanderDeviceSerialPortParamTypeId, port.portName()));
} }
if (deviceClassId == serialPortOutputDeviceClassId) {
parameters.append(Param(serialPortOutputSerialPortParamTypeId, port.portName()));
}
descriptor.setParams(parameters); descriptor.setParams(parameters);
deviceDescriptors.append(descriptor); deviceDescriptors.append(descriptor);
} }
@ -171,13 +121,13 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons
DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device *device, const Action &action) 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(); QString interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString();
SerialPortCommander *serialPortCommander = m_serialPortCommanders.value(interface); QSerialPort *serialPort = m_serialPorts.value(interface);
serialPortCommander->sendCommand(action.param(serialPortOutputOutputDataAreaParamTypeId).value().toByteArray()); serialPort->write(action.param(serialPortCommanderTriggerActionOutputDataParamTypeId).value().toByteArray());
return DeviceManager::DeviceErrorNoError; return DeviceManager::DeviceErrorNoError;
} }
@ -190,32 +140,20 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::executeAction(Device
void DevicePluginSerialPortCommander::deviceRemoved(Device *device) void DevicePluginSerialPortCommander::deviceRemoved(Device *device)
{ {
QString interface; QString interface;
SerialPortCommander *serialPortCommander; QSerialPort *serialPort;
if (device->deviceClassId() == serialPortInputDeviceClassId) { if (device->deviceClassId() == serialPortCommanderDeviceClassId) {
interface = device->paramValue(serialPortInputSerialPortParamTypeId).toString(); interface = device->paramValue(serialPortCommanderDeviceSerialPortParamTypeId).toString();
serialPortCommander = m_serialPortCommanders.value(interface); serialPort = m_serialPorts.value(interface);
serialPortCommander->removeInputDevice(device); serialPort->close();
if (serialPortCommander->isEmpty()) { m_serialPorts.remove(interface);
m_serialPortCommanders.remove(interface); serialPort->deleteLater();
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();
}
} }
} }
void DevicePluginSerialPortCommander::onCommandReceived(Device *device) void DevicePluginSerialPortCommander::onCommandReceived(Device *device)
{ {
emitEvent(Event(serialPortInputTriggeredEventTypeId, device->id())); emitEvent(Event(serialPortCommanderTriggeredEventTypeId, device->id()));
} }

View File

@ -1,20 +1,22 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> * * Copyright (C) 2019 Bernhard Trinnes <bernhard.trinnes@nymea.io> *
* * * *
* This file is part of guh. * * This file is part of nymea. *
* * * *
* Guh is free software: you can redistribute it and/or modify * * This library is free software; you can redistribute it and/or *
* it under the terms of the GNU General Public License as published by * * modify it under the terms of the GNU Lesser General Public *
* the Free Software Foundation, version 2 of the License. * * 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 * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* GNU General Public License for more details. * * Lesser General Public License for more details. *
* * * *
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU Lesser General Public *
* along with guh. If not, see <http://www.gnu.org/licenses/>. * * License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -23,7 +25,6 @@
#include "plugin/deviceplugin.h" #include "plugin/deviceplugin.h"
#include "devicemanager.h" #include "devicemanager.h"
#include "serialportcommander.h"
#include <QSerialPort> #include <QSerialPort>
#include <QSerialPortInfo> #include <QSerialPortInfo>
@ -31,7 +32,7 @@ class DevicePluginSerialPortCommander : public DevicePlugin
{ {
Q_OBJECT 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) Q_INTERFACES(DevicePlugin)
public: public:
@ -45,7 +46,7 @@ public:
void init() override; void init() override;
private: private:
QHash<QString, SerialPortCommander *> m_serialPortCommanders; QHash<QString, QSerialPort *> m_serialPorts;
private slots: private slots:
void onCommandReceived(Device *device); void onCommandReceived(Device *device);

View File

@ -11,11 +11,11 @@
{ {
"id": "540566d8-a2a6-4ce2-9a1e-a66a989e6199", "id": "540566d8-a2a6-4ce2-9a1e-a66a989e6199",
"name": "serialPortOutput", "name": "serialPortCommander",
"displayName": "Serial port output", "displayName": "Serial port commander",
"deviceIcon": "Network", "deviceIcon": "Network",
"createMethods": ["user", "discovery"], "createMethods": ["user", "discovery"],
"interfaces": ["outputtrigger"], "interfaces": ["outputtrigger", "inputtrigger"],
"basicTags": [ "basicTags": [
"Device" "Device"
], ],
@ -58,8 +58,7 @@
"allowedValues": [ "allowedValues": [
"No Flow Control", "No Flow Control",
"Hardware Control", "Hardware Control",
"Software Control", "Software Control"
"Unknown Flow Control"
], ],
"defaultValue": "No Flow Control" "defaultValue": "No Flow Control"
}, },
@ -74,8 +73,7 @@
"Even Parity", "Even Parity",
"Odd Parity", "Odd Parity",
"Space Parity", "Space Parity",
"Mark Parity", "Mark Parity"
"Unknown Parity"
], ],
"defaultValue": "No Parity" "defaultValue": "No Parity"
} }
@ -88,107 +86,13 @@
"paramTypes": [ "paramTypes": [
{ {
"id": "a27ecedc-424e-49ce-8956-9dbca2feac02", "id": "a27ecedc-424e-49ce-8956-9dbca2feac02",
"name": "outputDataArea", "name": "outputData",
"displayName": "Data", "displayName": "Data",
"type": "QString", "type": "QString",
"inputType": "TextArea" "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": [ "stateTypes": [
{ {
@ -202,9 +106,9 @@
], ],
"eventTypes": [ "eventTypes": [
{ {
"id": "6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b", "id": "32087633-616c-45a7-85af-4f1695c22359",
"name": "triggered", "name": "triggered",
"displayName": "Command received" "displayName": "Data received"
} }
] ]
} }

View File

@ -6,9 +6,7 @@ TARGET = $$qtLibraryTarget(nymea_devicepluginserialportcommander)
SOURCES += \ SOURCES += \
devicepluginserialportcommander.cpp \ devicepluginserialportcommander.cpp \
serialportcommander.cpp
HEADERS += \ HEADERS += \
devicepluginserialportcommander.h \ devicepluginserialportcommander.h \
serialportcommander.h