added serial port commander

master
Bernhard Trinnes 2018-06-13 22:22:09 +02:00 committed by Michael Zanetti
parent f3e465a551
commit 4403be8d76
7 changed files with 700 additions and 0 deletions

View File

@ -34,6 +34,7 @@ PLUGIN_DIRS = \
plantcare \
remotessh \
senic \
serialportcommander \
simulation \
snapd \
tasmota \

View File

@ -0,0 +1,211 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#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 &params)
{
Q_UNUSED(params)
// Create the list of available serial interfaces
QList<DeviceDescriptor> 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()));
}

View File

@ -0,0 +1,59 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DEVICEPLUGINSERIALPORTCOMMANDER_H
#define DEVICEPLUGINSERIALPORTCOMMANDER_H
#include "plugin/deviceplugin.h"
#include "devicemanager.h"
#include "serialportcommander.h"
#include <QSerialPort>
#include <QSerialPortInfo>
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 &params);
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
void init() override;
private:
//QHash<Device *, QSerialPort *> m_outputSerialPorts;
//QHash<Device *, QSerialPort *> m_inputSerialPorts;
QHash<QString, SerialPortCommander *> m_serialPortCommanders;
private slots:
void onCommandReceived(Device *device);
signals:
};
#endif // DEVICEPLUGINSERIALPORTCOMMANDER_H

View File

@ -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"
}
]
}
]
}
]
}

View File

@ -0,0 +1,133 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#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);
}

View File

@ -0,0 +1,68 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef SERIALPORTCOMMANDER_H
#define SERIALPORTCOMMANDER_H
#include <QObject>
#include <QSerialPort>
#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<Device *> 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

View File

@ -0,0 +1,14 @@
include(../plugins.pri)
QT += serialport
TARGET = $$qtLibraryTarget(nymea_devicepluginserialportcommander)
SOURCES += \
devicepluginserialportcommander.cpp \
serialportcommander.cpp
HEADERS += \
devicepluginserialportcommander.h \
serialportcommander.h