2016-03-23 11:12:34 +01:00
|
|
|
#include "LedDeviceFile.h"
|
2013-08-15 21:11:02 +02:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <Qt>
|
|
|
|
#include <QTextStream>
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig)
|
2016-08-14 10:46:44 +02:00
|
|
|
: LedDevice()
|
2020-06-28 23:05:32 +02:00
|
|
|
, _file (nullptr)
|
2013-08-15 21:11:02 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
_devConfig = deviceConfig;
|
|
|
|
_deviceReady = false;
|
|
|
|
_printTimeStamp = false;
|
2013-08-15 21:11:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:12:34 +01:00
|
|
|
LedDeviceFile::~LedDeviceFile()
|
2013-08-15 21:11:02 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
if ( _file != nullptr )
|
|
|
|
{
|
|
|
|
_file->deleteLater();
|
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDevice* LedDeviceFile::construct(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
|
|
|
return new LedDeviceFile(deviceConfig);
|
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
bool LedDeviceFile::init(const QJsonObject &deviceConfig)
|
2019-12-08 13:12:01 +01:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
bool initOK = LedDevice::init(deviceConfig);
|
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
_fileName = deviceConfig["output"].toString("/dev/null");
|
|
|
|
_printTimeStamp = deviceConfig["printTimeStamp"].toBool(false);
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
initFile(_fileName);
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
return initOK;
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
void LedDeviceFile::initFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
if ( _file == nullptr )
|
|
|
|
{
|
|
|
|
_file = new QFile(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
int LedDeviceFile::open()
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
int retval = -1;
|
|
|
|
QString errortext;
|
|
|
|
_deviceReady = false;
|
|
|
|
|
|
|
|
if ( init(_devConfig) )
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
if ( ! _file->isOpen() )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
Debug(_log, "QIODevice::WriteOnly, %s", QSTRING_CSTR(_fileName));
|
|
|
|
if ( !_file->open(QIODevice::WriteOnly | QIODevice::Text) )
|
|
|
|
{
|
|
|
|
errortext = QString ("(%1) %2, file: (%3)").arg(_file->error()).arg(_file->errorString()).arg(_fileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_deviceReady = true;
|
|
|
|
setEnable(true);
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
{
|
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceFile::close()
|
|
|
|
{
|
|
|
|
LedDevice::close();
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
if ( _file != nullptr)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
// Test, if device requires closing
|
|
|
|
if ( _file->isOpen() )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
// close device physically
|
|
|
|
Debug(_log,"File: %s", QSTRING_CSTR(_fileName) );
|
|
|
|
_file->close();
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
2013-08-15 21:11:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:12:34 +01:00
|
|
|
int LedDeviceFile::write(const std::vector<ColorRgb> & ledValues)
|
2013-08-15 21:11:02 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
QTextStream out(_file);
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
//printLedValues (ledValues);
|
2020-06-28 23:05:32 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
if ( _printTimeStamp )
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
qint64 elapsedTimeMs = _lastWriteTime.msecsTo(now);
|
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
|
|
|
|
out << now.toString(Qt::ISODateWithMs) << " | +" << QString("%1").arg( elapsedTimeMs,4);
|
|
|
|
#else
|
|
|
|
out << now.toString(Qt::ISODate) << now.toString(".zzz") << " | +" << QString("%1").arg( elapsedTimeMs,4);
|
|
|
|
#endif
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
out << " [";
|
2013-11-11 10:00:37 +01:00
|
|
|
for (const ColorRgb& color : ledValues)
|
2013-08-15 21:11:02 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
out << color;
|
2013-08-15 21:11:02 +02:00
|
|
|
}
|
2020-06-28 23:05:32 +02:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
|
|
|
out << "]" << Qt::endl;
|
|
|
|
#else
|
|
|
|
out << "]" << endl;
|
|
|
|
#endif
|
2013-08-15 21:11:02 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|