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)
|
2020-08-08 00:21:19 +02:00
|
|
|
: LedDevice(deviceConfig)
|
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
|
|
|
_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-08-08 00:21:19 +02:00
|
|
|
delete _file;
|
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");
|
2020-11-14 17:58:56 +01:00
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
if (_fileName == "/dev/null" )
|
|
|
|
{
|
|
|
|
_fileName = "NULL";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
_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 )
|
|
|
|
{
|
2020-07-12 09:22:05 +02:00
|
|
|
_file = new QFile(fileName, this);
|
2020-06-28 23:05:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
if ( ! _file->isOpen() )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
Debug(_log, "QIODevice::WriteOnly, %s", QSTRING_CSTR(_fileName));
|
|
|
|
if ( !_file->open(QIODevice::WriteOnly | QIODevice::Text) )
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
QString errortext = QString ("(%1) %2, file: (%3)").arg(_file->error()).arg(_file->errorString(),_fileName);
|
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_isDeviceReady = true;
|
|
|
|
retval = 0;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
int LedDeviceFile::close()
|
2020-02-10 15:21:58 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
int retval = 0;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
_isDeviceReady = false;
|
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
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
return retval;
|
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;
|
|
|
|
}
|