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:
LordGrey
2020-06-28 23:05:32 +02:00
committed by GitHub
parent e365a2839d
commit bfb50b8d91
61 changed files with 530 additions and 365 deletions

View File

@@ -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;
}

View File

@@ -1,74 +1,84 @@
#pragma once
#ifndef LEDEVICEFILE_H
#define LEDEVICEFILE_H
// STL includes
#include <fstream>
#include <chrono>
// Leddevice includes
// LedDevice includes
#include <leddevice/LedDevice.h>
// Qt includes
#include <QFile>
#include <QDateTime>
///
/// Implementation of the LedDevice that write the led-colors to an
/// ASCII-textfile('/home/pi/LedDevice.out')
/// Implementation of the LedDevice that write the LED-colors to an
/// ASCII-textfile primarily for testing purposes
///
class LedDeviceFile : public LedDevice
{
public:
///
/// Constructs specific LedDevice
/// @brief Constructs a file output LED-device
///
/// @param deviceConfig json device config
/// @param deviceConfig Device's configuration as JSON-Object
///
explicit LedDeviceFile(const QJsonObject &deviceConfig);
///
/// Destructor of this test-device
/// @brief Destructor of the LedDevice
///
virtual ~LedDeviceFile() override;
/// constructs leddevice
///
/// @brief Constructs the LED-device
///
/// @param[in] deviceConfig Device's configuration as JSON-Object
/// @return LedDevice constructed
static LedDevice* construct(const QJsonObject &deviceConfig);
public slots:
///
/// Sets configuration
/// @brief Closes the output device.
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
/// @return Zero on success (i.e. device is closed), else negative
///
virtual void close() override;
protected:
///
/// Opens and initiatialises the output device
/// @brief Initialise the device's configuration
///
/// @return Zero on succes (i.e. device is ready and enabled) else negative
/// @param[in] deviceConfig the JSON device configuration
/// @return True, if success
///
virtual bool init(const QJsonObject &deviceConfig) override;
///
/// @brief Opens the output device.
///
/// @return Zero on success (i.e. device is ready), else negative
///
virtual int open() override;
///
/// Writes the given led-color values to the output stream
/// @brief Writes the RGB-Color values to the LEDs.
///
/// @param ledValues The color-value per led
///
/// @return Zero on success else negative
/// @param[in] ledValues The RGB-color per LED
/// @return Zero on success, else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues) override;
/// The outputstream
std::ofstream _ofs;
QFile* _file;
private:
void initFile(const QString &filename);
QString _fileName;
/// Timestamp for the output record
bool _printTimeStamp;
/// Last write/output timestamp
std::chrono::system_clock::time_point lastWriteTime = std::chrono::system_clock::now();
};
#endif // LEDEVICEFILE_H