mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
refactor: Address (Windows) compile warnings (#840)
* Windows compile errors and (Qt 5.15 deprecation) warnings * Usability - Enable/Disable Instance button Co-authored-by: brindosch <edeltraud70@gmx.de>
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#include "LedDeviceFile.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
// Qt includes
|
||||
#include <Qt>
|
||||
#include <QTextStream>
|
||||
|
||||
LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig)
|
||||
: LedDevice()
|
||||
, _file (nullptr)
|
||||
{
|
||||
_devConfig = deviceConfig;
|
||||
_deviceReady = false;
|
||||
@@ -14,6 +15,10 @@ LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig)
|
||||
|
||||
LedDeviceFile::~LedDeviceFile()
|
||||
{
|
||||
if ( _file != nullptr )
|
||||
{
|
||||
_file->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
LedDevice* LedDeviceFile::construct(const QJsonObject &deviceConfig)
|
||||
@@ -28,9 +33,19 @@ bool LedDeviceFile::init(const QJsonObject &deviceConfig)
|
||||
_fileName = deviceConfig["output"].toString("/dev/null");
|
||||
_printTimeStamp = deviceConfig["printTimeStamp"].toBool(false);
|
||||
|
||||
initFile(_fileName);
|
||||
|
||||
return initOK;
|
||||
}
|
||||
|
||||
void LedDeviceFile::initFile(const QString &fileName)
|
||||
{
|
||||
if ( _file == nullptr )
|
||||
{
|
||||
_file = new QFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
int LedDeviceFile::open()
|
||||
{
|
||||
int retval = -1;
|
||||
@@ -39,26 +54,24 @@ int LedDeviceFile::open()
|
||||
|
||||
if ( init(_devConfig) )
|
||||
{
|
||||
if ( _ofs.is_open() )
|
||||
if ( ! _file->isOpen() )
|
||||
{
|
||||
_ofs.close();
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
_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 );
|
||||
if ( retval < 0 )
|
||||
{
|
||||
this->setInError( errortext );
|
||||
}
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
@@ -68,43 +81,47 @@ void LedDeviceFile::close()
|
||||
{
|
||||
LedDevice::close();
|
||||
|
||||
// LedDevice specific closing activites
|
||||
if ( _ofs )
|
||||
if ( _file != nullptr)
|
||||
{
|
||||
_ofs.close();
|
||||
if ( _ofs.fail() )
|
||||
// Test, if device requires closing
|
||||
if ( _file->isOpen() )
|
||||
{
|
||||
Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_fileName), strerror(errno) );
|
||||
// close device physically
|
||||
Debug(_log,"File: %s", QSTRING_CSTR(_fileName) );
|
||||
_file->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int LedDeviceFile::write(const std::vector<ColorRgb> & ledValues)
|
||||
{
|
||||
QTextStream out(_file);
|
||||
|
||||
//printLedValues (ledValues);
|
||||
|
||||
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);
|
||||
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
qint64 elapsedTimeMs = _lastWriteTime.msecsTo(now);
|
||||
|
||||
const auto elapsedTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastWriteTime);
|
||||
|
||||
_ofs
|
||||
<< std::put_time(std::localtime(&nowAsTimeT), "%Y-%m-%d %T")
|
||||
<< '.' << std::setfill('0') << std::setw(3) << nowMs.count()
|
||||
<< " | +" << std::setfill('0') << std::setw(4) << elapsedTimeMs.count();
|
||||
|
||||
lastWriteTime = 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
|
||||
}
|
||||
|
||||
_ofs << " [";
|
||||
out << " [";
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
_ofs << color;
|
||||
out << color;
|
||||
}
|
||||
_ofs << "]" << std::endl;
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
||||
out << "]" << Qt::endl;
|
||||
#else
|
||||
out << "]" << endl;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user