2016-03-23 11:12:34 +01:00
|
|
|
#include "LedDeviceFile.h"
|
2013-08-15 21:11:02 +02:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig)
|
2016-08-14 10:46:44 +02:00
|
|
|
: LedDevice()
|
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
|
|
|
{
|
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-02-10 15:21:58 +01:00
|
|
|
return initOK;
|
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) )
|
|
|
|
{
|
|
|
|
if ( _ofs.is_open() )
|
|
|
|
{
|
|
|
|
_ofs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_ofs.open( QSTRING_CSTR(_fileName));
|
|
|
|
if ( _ofs.fail() )
|
|
|
|
{
|
|
|
|
errortext = QString ("Failed to open file (%1). Error message: %2").arg(_fileName, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_deviceReady = true;
|
|
|
|
setEnable(true);
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
{
|
|
|
|
this->setInError( errortext );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceFile::close()
|
|
|
|
{
|
|
|
|
LedDevice::close();
|
|
|
|
|
|
|
|
// LedDevice specific closing activites
|
|
|
|
if ( _ofs )
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
|
|
|
_ofs.close();
|
2020-02-10 15:21:58 +01:00
|
|
|
if ( _ofs.fail() )
|
|
|
|
{
|
|
|
|
Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_fileName), strerror(errno) );
|
|
|
|
}
|
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-02-10 15:21:58 +01:00
|
|
|
//printLedValues (ledValues);
|
2019-12-08 13:12:01 +01:00
|
|
|
if ( _printTimeStamp )
|
|
|
|
{
|
|
|
|
// get a precise timestamp as a string
|
|
|
|
const auto now = std::chrono::system_clock::now();
|
|
|
|
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
|
2020-05-25 21:51:11 +02:00
|
|
|
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
|
|
const auto elapsedTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastWriteTime);
|
2019-12-08 13:12:01 +01:00
|
|
|
|
|
|
|
_ofs
|
|
|
|
<< std::put_time(std::localtime(&nowAsTimeT), "%Y-%m-%d %T")
|
2020-02-10 15:21:58 +01:00
|
|
|
<< '.' << std::setfill('0') << std::setw(3) << nowMs.count()
|
|
|
|
<< " | +" << std::setfill('0') << std::setw(4) << elapsedTimeMs.count();
|
2019-12-08 13:12:01 +01:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
lastWriteTime = now;
|
2019-12-08 13:12:01 +01:00
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
_ofs << " [";
|
2013-11-11 10:00:37 +01:00
|
|
|
for (const ColorRgb& color : ledValues)
|
2013-08-15 21:11:02 +02:00
|
|
|
{
|
|
|
|
_ofs << color;
|
|
|
|
}
|
|
|
|
_ofs << "]" << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|