improve serial hotplug (#389)

* - disable device when error indecates that the problem is not solvable on reconnect
- introduce a preOpenDelay of 2 seconds (currently value is hardcoded)

* rs232:
- make preOpenDelay available via webui
- fix preOpenDelay
- add basic usb serial detection

* - revert 3819ae7
- fix schema files

* make json checks compat with utf8+bom

* make shutdown effect a bit more flexible
This commit is contained in:
redPanther
2017-02-09 20:10:57 +01:00
committed by GitHub
parent 3819ae72ca
commit 170ad4f5db
26 changed files with 712 additions and 294 deletions

View File

@@ -6,6 +6,7 @@ LedDeviceAdalight::LedDeviceAdalight(const QJsonObject &deviceConfig)
, _ligthBerryAPA102Mode(false)
{
_deviceReady = init(deviceConfig);
connect(this,SIGNAL(receivedData(QByteArray)),this,SLOT(receivedData(QByteArray)));
}
LedDevice* LedDeviceAdalight::construct(const QJsonObject &deviceConfig)
@@ -74,3 +75,7 @@ int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
void LedDeviceAdalight::receivedData(QByteArray data)
{
Debug(_log, ">>received %d bytes data", data.size());
}

View File

@@ -22,6 +22,9 @@ public:
virtual bool init(const QJsonObject &deviceConfig);
public slots:
void receivedData(QByteArray data);
private:
///
/// Writes the led color values to the led-device
@@ -32,6 +35,6 @@ private:
virtual int write(const std::vector<ColorRgb> & ledValues);
const short _headerSize;
bool _ligthBerryAPA102Mode;
bool _ligthBerryAPA102Mode;
};

View File

@@ -9,8 +9,14 @@ LedDeviceDMX::LedDeviceDMX(const QJsonObject &deviceConfig)
, _dmxSlotsPerLed(3)
, _dmxLedCount(0)
, _dmxChannelCount(0)
{
_deviceReady = init(deviceConfig);
}
bool LedDeviceDMX::init(const QJsonObject &deviceConfig)
{
ProviderRs232::init(deviceConfig);
std::string _dmxString = deviceConfig["dmxdevice"].toString("invalid").toStdString();
if (_dmxString == "raw")
{
@@ -40,6 +46,8 @@ LedDeviceDMX::LedDeviceDMX(const QJsonObject &deviceConfig)
_ledBuffer.resize(_dmxChannelCount, 0);
_ledBuffer[0] = 0x00; // NULL START code
return true;
}
LedDevice* LedDeviceDMX::construct(const QJsonObject &deviceConfig)

View File

@@ -18,6 +18,8 @@ public:
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
virtual bool init(const QJsonObject &deviceConfig);
private:
///

View File

@@ -51,9 +51,9 @@ bool LedDeviceFadeCandy::init(const QJsonObject &deviceConfig)
const QJsonArray whitePointConfig = deviceConfig["whitePoint"].toArray();
if ( !whitePointConfig.isEmpty() && whitePointConfig.size() == 3 )
{
_whitePoint_r = whitePointConfig[0].toDouble();
_whitePoint_g = whitePointConfig[1].toDouble();
_whitePoint_b = whitePointConfig[2].toDouble();
_whitePoint_r = whitePointConfig[0].toDouble() / 255.0;
_whitePoint_g = whitePointConfig[1].toDouble() / 255.0;
_whitePoint_b = whitePointConfig[2].toDouble() / 255.0;
}
_opc_data.resize( _ledRGBCount + OPC_HEADER_SIZE );

View File

@@ -20,7 +20,7 @@ LedDevice* LedDeviceSedu::construct(const QJsonObject &deviceConfig)
bool LedDeviceSedu::init(const QJsonObject &deviceConfig)
{
ProviderRs232::init(deviceConfig);
std::vector<FrameSpec> frameSpecs{{0xA1, 256}, {0xA2, 512}, {0xB0, 768}, {0xB1, 1536}, {0xB2, 3072} };
for (const FrameSpec& frameSpec : frameSpecs)

View File

@@ -5,6 +5,9 @@
// Qt includes
#include <QTimer>
#include <QDateTime>
#include <QFile>
#include <QSerialPortInfo>
// Local Hyperion includes
#include "ProviderRs232.h"
@@ -17,6 +20,9 @@ ProviderRs232::ProviderRs232()
, _bytesWritten(0)
, _frameDropCounter(0)
, _lastError(QSerialPort::NoError)
, _preOpenDelayTimeOut(0)
, _preOpenDelay(2000)
, _enableAutoDeviceName(false)
{
connect(&_rs232Port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(error(QSerialPort::SerialPortError)));
connect(&_rs232Port, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
@@ -29,13 +35,30 @@ bool ProviderRs232::init(const QJsonObject &deviceConfig)
LedDevice::init(deviceConfig);
_deviceName = deviceConfig["output"].toString().toStdString();
_deviceName = deviceConfig["output"].toString("auto");
_enableAutoDeviceName = _deviceName == "auto";
_baudRate_Hz = deviceConfig["rate"].toInt();
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(250);
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(1500);
_preOpenDelay = deviceConfig["delayBeforeConnect"].toInt(1500);
return true;
}
QString ProviderRs232::findSerialDevice()
{
// take first available usb serial port - currently no probing!
for( auto port : QSerialPortInfo::availablePorts())
{
if (port.hasProductIdentifier() && port.hasVendorIdentifier() && !port.isBusy())
{
Info(_log, "found serial device: %s", port.systemLocation().toLocal8Bit().constData());
return port.systemLocation();
break;
}
}
return "";
}
void ProviderRs232::bytesWritten(qint64 bytes)
{
_bytesWritten += bytes;
@@ -49,8 +72,8 @@ void ProviderRs232::bytesWritten(qint64 bytes)
void ProviderRs232::readyRead()
{
QByteArray data = _rs232Port.readAll();
Debug(_log, "received %d bytes data", data.size());
emit receivedData(_rs232Port.readAll());
Debug(_log, "received data");
}
@@ -66,7 +89,9 @@ void ProviderRs232::error(QSerialPort::SerialPortError error)
case QSerialPort::DeviceNotFoundError:
Error(_log, "An error occurred while attempting to open an non-existing device."); break;
case QSerialPort::PermissionError:
Error(_log, "An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open."); break;
Error(_log, "An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open. Device disabled.");
_deviceReady = false;
break;
case QSerialPort::OpenError:
Error(_log, "An error occurred while attempting to open an already opened device in this object."); break;
case QSerialPort::NotOpenError:
@@ -84,10 +109,14 @@ void ProviderRs232::error(QSerialPort::SerialPortError error)
case QSerialPort::ResourceError:
Error(_log, "An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system."); break;
case QSerialPort::UnsupportedOperationError:
Error(_log, "The requested device operation is not supported or prohibited by the running operating system."); break;
Error(_log, "The requested device operation is not supported or prohibited by the running operating system. Device disabled.");
_deviceReady = false;
break;
case QSerialPort::TimeoutError:
Error(_log, "A timeout error occurred."); break;
default: Error(_log,"An unidentified error occurred. (%d)", error);
default:
Error(_log,"An unidentified error occurred. Device disabled. (%d)", error);
_deviceReady = false;
}
_rs232Port.clearError();
closeDevice();
@@ -106,35 +135,60 @@ void ProviderRs232::closeDevice()
if (_rs232Port.isOpen())
{
_rs232Port.close();
Debug(_log,"Close UART: %s", _deviceName.c_str());
Debug(_log,"Close UART: %s", _deviceName.toLocal8Bit().constData());
}
}
int ProviderRs232::open()
{
Info(_log, "Opening UART: %s", _deviceName.c_str());
_rs232Port.setPortName(_deviceName.c_str());
return tryOpen(_delayAfterConnect_ms) ? 0 : -1;
}
bool ProviderRs232::tryOpen(const int delayAfterConnect_ms)
{
if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty())
{
if ( _enableAutoDeviceName )
{
_deviceName = findSerialDevice();
if ( _deviceName.isEmpty() )
{
return false;
}
}
Info(_log, "Opening UART: %s", _deviceName.toLocal8Bit().constData());
_rs232Port.setPortName(_deviceName);
}
if ( ! _rs232Port.isOpen() )
{
if ( ! _rs232Port.open(QIODevice::ReadWrite) )
_frameDropCounter = 0;
if (QFile::exists(_deviceName))
{
if ( _stateChanged )
if ( _preOpenDelayTimeOut > QDateTime::currentMSecsSinceEpoch() )
{
Error(_log, "Unable to open RS232 device (%s)", _deviceName.c_str());
_stateChanged = false;
return false;
}
if ( ! _rs232Port.open(QIODevice::ReadWrite) )
{
if ( _stateChanged )
{
Error(_log, "Unable to open RS232 device (%s)", _deviceName.toLocal8Bit().constData());
_stateChanged = false;
}
return false;
}
Debug(_log, "Setting baud rate to %d", _baudRate_Hz);
_rs232Port.setBaudRate(_baudRate_Hz);
_stateChanged = true;
_preOpenDelayTimeOut = 0;
}
else
{
_preOpenDelayTimeOut = QDateTime::currentMSecsSinceEpoch() + _preOpenDelay;
return false;
}
Debug(_log, "Setting baud rate to %d", _baudRate_Hz);
_rs232Port.setBaudRate(_baudRate_Hz);
_stateChanged = true;
}
if (delayAfterConnect_ms > 0)

View File

@@ -3,6 +3,7 @@
#include <QObject>
#include <QSerialPort>
#include <QTimer>
#include <QString>
// Leddevice includes
#include <leddevice/LedDevice.h>
@@ -39,6 +40,19 @@ public:
///
int open();
private slots:
/// Write the last data to the leds again
int rewriteLeds();
/// Unblock the device after a connection delay
void unblockAfterDelay();
void error(QSerialPort::SerialPortError error);
void bytesWritten(qint64 bytes);
void readyRead();
signals:
void receivedData(QByteArray data);
protected:
/**
* Writes the given bytes to the RS232-device and
@@ -52,22 +66,14 @@ protected:
void closeDevice();
private slots:
/// Write the last data to the leds again
int rewriteLeds();
QString findSerialDevice();
/// Unblock the device after a connection delay
void unblockAfterDelay();
void error(QSerialPort::SerialPortError error);
void bytesWritten(qint64 bytes);
void readyRead();
protected:
// tries to open device if not opened
bool tryOpen(const int delayAfterConnect_ms);
/// The name of the output device
std::string _deviceName;
QString _deviceName;
/// The used baudrate of the output device
qint32 _baudRate_Hz;
@@ -86,4 +92,7 @@ protected:
qint64 _bytesWritten;
qint64 _frameDropCounter;
QSerialPort::SerialPortError _lastError;
qint64 _preOpenDelayTimeOut;
int _preOpenDelay;
bool _enableAutoDeviceName;
};

View File

@@ -5,7 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"/dev/ttyUSB0",
"default":"/dev/ttyACM0",
"propertyOrder" : 1
},
"rate": {

View File

@@ -5,6 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"/dev/ttyUSB0",
"propertyOrder" : 1
},
"rate": {

View File

@@ -5,6 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"/dev/ttyUSB0",
"propertyOrder" : 1
},
"rate": {
@@ -18,13 +19,6 @@
"title":"edt_dev_spec_delayAfterConnect_title",
"default": 250,
"propertyOrder" : 3
},
"rewriteTime": {
"type": "integer",
"title":"edt_dev_spec_rewriteTime_title",
"default": 5000,
"append" : "ms",
"propertyOrder" : 4
}
},
"additionalProperties": true

View File

@@ -55,15 +55,15 @@
"type" : "array",
"title" : "edt_dev_spec_whitepoint_title",
"propertyOrder" : 9,
"default" : [1.0,1.0,1.0],
"default" : [255,255,255],
"maxItems" : 3,
"minItems" : 3,
"format" : "table",
"format" : "colorpicker",
"items" : {
"type" : "number",
"minimum" : 0.0,
"maximum": 1.0,
"default" : 1.0
"type" : "integer",
"minimum" : 0,
"maximum": 255,
"default" : 255
}
}
},

View File

@@ -5,6 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"/dev/ttyACM0",
"propertyOrder" : 1
},
"rate": {

View File

@@ -5,6 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"/dev/ttyACM0",
"propertyOrder" : 1
},
"rate": {