mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
mDNS Support (#1452)
* Allow build, if no grabbers are enabled * Align available functions to right Qt version * Update to next development version * Align available functions to right Qt version * fix workflows (apt/nightly) * Disable QNetworkConfigurationManager deprecation warnings * Initial go on Smart Pointers * Add Deallocation * Correct QT_WARNING_DISABLE_DEPRECATED (available since 5.9) * Cluster Build Variables * Hyperion Light * Address build warnings * Hyperion Light - UI * Update Protobuf to latest master * Removed compiler warnings * Added restart ability to systray * Correct Protobuf * Ignore 'no-return' warning on protobuf build * hyperion-remote: Fix auto discovery of hyperion server * Fix Qt version override * Update changelog * Remove Grabber Components, if no Grabber exists * Standalone Grabber - Fix fps default * Remote Control - Have Source Selction accrosswhole screen * Enable Blackborder detection only, if relevant input sources available * Enable Blackborder detection only, if relevant input sources available * Remote UI - rearrange containers * Checkout * Fix compilation on windows * Re-added qmdnsengine template cmake * chrono added for linux * Removed existing AVAHI/Bonjour, allow to enable/disable mDNS * hyperiond macos typo fix * Fix macOS Bundle build * Fix macOS bundle info details * Correct CMake files * Removed existing AVAHI/Bonjour (2) * Share hyperion's services via mDNS * Add mDNS Browser and mDNS for LED-Devices * Support mDNS discovery for standalone grabbers * Remove ZLib Dependency & Cleanup * mDNS - hanle 2.local2 an ".local." domains equally * Hue - Link discovery to bridge class, workaround port 443 for mDNS discovery * Fix save button state when switching between devices * Removed sessions (of other hyperions) * mDNS Publisher - Simplify service naming * mDNS refactoring & Forwarder discovery * mDNS Updates to use device service name * Consistency of standalone grabbers with mDNS Service Registry * Merge branch 'hyperion-project:master' into mDNS * Start JSON and WebServers only after Instance 0 is available * Remove bespoke qDebug Output again * MDNS updates and refactor Forwarder * Minor updates * Upgrade to CMake 3.1 * typo * macOS fix * Correct merge * - Remove dynamic linker flag from standalone dispmanX Grabber - Added ability to use system qmdns libs * Cec handler library will load at runtime * typo fix * protobuf changes * mDNS changes for Windows/macOS * test window build qmdnsengine * absolute path to protobuf cmake dir * Rework Hue Wizard supporting mDNS * LED-Devices - Retry support + Refactoring (excl. Hue) * LED-Devices - Refactoring/Retry support Hue + additional alignments * Address LGTM findings * Fix CI-Build, revert test changes * Build Windows in Release mode to avoid python problem * Correct that WebServerObject is available earlier * Ensure that instance name in logs for one instance are presented * Update content LEDs * Rework mDNS Address lookup * Fix LED UI * Fix for non mDNS Services (ignore default port) * Disbale device when now input is available * Revert back some updates, ensure last color is updated when switched on * Handle reopening case and changed IP, port for API-calls * Add UPD-DDP Device * WLED support for DDP * Fix printout * LEDDevice - Allow more retries, udapte defaults * LED-Net Devices - Select Custom device, if configured Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com> Co-authored-by: Paulchen Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
@@ -15,84 +15,91 @@
|
||||
//std includes
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <chrono>
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
|
||||
// Configuration settings
|
||||
const char CONFIG_CURRENT_LED_COUNT[] = "currentLedCount";
|
||||
const char CONFIG_COLOR_ORDER[] = "colorOrder";
|
||||
const char CONFIG_AUTOSTART[] = "autoStart";
|
||||
const char CONFIG_LATCH_TIME[] = "latchTime";
|
||||
const char CONFIG_REWRITE_TIME[] = "rewriteTime";
|
||||
// Configuration settings
|
||||
const char CONFIG_CURRENT_LED_COUNT[] = "currentLedCount";
|
||||
const char CONFIG_COLOR_ORDER[] = "colorOrder";
|
||||
const char CONFIG_AUTOSTART[] = "autoStart";
|
||||
const char CONFIG_LATCH_TIME[] = "latchTime";
|
||||
const char CONFIG_REWRITE_TIME[] = "rewriteTime";
|
||||
|
||||
int DEFAULT_LED_COUNT = 1;
|
||||
const char DEFAULT_COLOR_ORDER[] = "RGB";
|
||||
const bool DEFAULT_IS_AUTOSTART = true;
|
||||
int DEFAULT_LED_COUNT{ 1 };
|
||||
const char DEFAULT_COLOR_ORDER[]{ "RGB" };
|
||||
const bool DEFAULT_IS_AUTOSTART{ true };
|
||||
|
||||
const char CONFIG_ENABLE_ATTEMPTS[] = "enableAttempts";
|
||||
const char CONFIG_ENABLE_ATTEMPTS_INTERVALL[] = "enableAttemptsInterval";
|
||||
|
||||
const int DEFAULT_MAX_ENABLE_ATTEMPTS{ 5 };
|
||||
constexpr std::chrono::seconds DEFAULT_ENABLE_ATTEMPTS_INTERVAL{ 5 };
|
||||
|
||||
} //End of constants
|
||||
|
||||
LedDevice::LedDevice(const QJsonObject& deviceConfig, QObject* parent)
|
||||
: QObject(parent)
|
||||
, _devConfig(deviceConfig)
|
||||
, _log(Logger::getInstance("LEDDEVICE"))
|
||||
, _ledBuffer(0)
|
||||
, _refreshTimer(nullptr)
|
||||
, _refreshTimerInterval_ms(0)
|
||||
, _latchTime_ms(0)
|
||||
, _ledCount(0)
|
||||
, _isRestoreOrigState(false)
|
||||
, _isEnabled(false)
|
||||
, _isDeviceInitialised(false)
|
||||
, _isDeviceReady(false)
|
||||
, _isOn(false)
|
||||
, _isDeviceInError(false)
|
||||
, _isInSwitchOff (false)
|
||||
, _lastWriteTime(QDateTime::currentDateTime())
|
||||
, _isRefreshEnabled (false)
|
||||
, _isAutoStart(true)
|
||||
, _devConfig(deviceConfig)
|
||||
, _log(Logger::getInstance("LEDDEVICE"))
|
||||
, _ledBuffer(0)
|
||||
, _refreshTimer(nullptr)
|
||||
, _enableAttemptsTimer(nullptr)
|
||||
, _refreshTimerInterval_ms(0)
|
||||
, _enableAttemptTimerInterval(DEFAULT_ENABLE_ATTEMPTS_INTERVAL)
|
||||
, _enableAttempts(0)
|
||||
, _maxEnableAttempts(DEFAULT_MAX_ENABLE_ATTEMPTS)
|
||||
, _latchTime_ms(0)
|
||||
, _ledCount(0)
|
||||
, _isRestoreOrigState(false)
|
||||
, _isEnabled(false)
|
||||
, _isDeviceInitialised(false)
|
||||
, _isDeviceReady(false)
|
||||
, _isOn(false)
|
||||
, _isDeviceInError(false)
|
||||
, _lastWriteTime(QDateTime::currentDateTime())
|
||||
, _isRefreshEnabled(false)
|
||||
, _isAutoStart(true)
|
||||
{
|
||||
_activeDeviceType = deviceConfig["type"].toString("UNSPECIFIED").toLower();
|
||||
}
|
||||
|
||||
LedDevice::~LedDevice()
|
||||
{
|
||||
delete _refreshTimer;
|
||||
this->stopEnableAttemptsTimer();
|
||||
this->stopRefreshTimer();
|
||||
}
|
||||
|
||||
void LedDevice::start()
|
||||
{
|
||||
Info(_log, "Start LedDevice '%s'.", QSTRING_CSTR(_activeDeviceType));
|
||||
|
||||
// setup refreshTimer
|
||||
if ( _refreshTimer == nullptr )
|
||||
{
|
||||
_refreshTimer = new QTimer(this);
|
||||
_refreshTimer->setTimerType(Qt::PreciseTimer);
|
||||
_refreshTimer->setInterval( _refreshTimerInterval_ms );
|
||||
connect(_refreshTimer, &QTimer::timeout, this, &LedDevice::rewriteLEDs );
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
_isDeviceInitialised = false;
|
||||
// General initialisation and configuration of LedDevice
|
||||
if ( init(_devConfig) )
|
||||
|
||||
if (init(_devConfig))
|
||||
{
|
||||
// Everything is OK -> enable device
|
||||
_isDeviceInitialised = true;
|
||||
|
||||
if (_isAutoStart)
|
||||
{
|
||||
this->enable();
|
||||
if (!_isEnabled)
|
||||
{
|
||||
Debug(_log, "Not enabled -> enable device");
|
||||
enable();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LedDevice::stop()
|
||||
{
|
||||
Debug(_log, "Stop device");
|
||||
this->disable();
|
||||
this->stopRefreshTimer();
|
||||
Info(_log, " Stopped LedDevice '%s'", QSTRING_CSTR(_activeDeviceType) );
|
||||
Info(_log, " Stopped LedDevice '%s'", QSTRING_CSTR(_activeDeviceType));
|
||||
}
|
||||
|
||||
int LedDevice::open()
|
||||
@@ -113,6 +120,7 @@ int LedDevice::close()
|
||||
|
||||
void LedDevice::setInError(const QString& errorMsg)
|
||||
{
|
||||
_isOn = false;
|
||||
_isDeviceInError = true;
|
||||
_isDeviceReady = false;
|
||||
_isEnabled = false;
|
||||
@@ -124,21 +132,53 @@ void LedDevice::setInError(const QString& errorMsg)
|
||||
|
||||
void LedDevice::enable()
|
||||
{
|
||||
if ( !_isEnabled )
|
||||
Debug(_log, "Enable device %s'", QSTRING_CSTR(_activeDeviceType));
|
||||
|
||||
if (!_isEnabled)
|
||||
{
|
||||
if (_enableAttemptsTimer != nullptr && _enableAttemptsTimer->isActive())
|
||||
{
|
||||
_enableAttemptsTimer->stop();
|
||||
}
|
||||
|
||||
_isDeviceInError = false;
|
||||
|
||||
if ( ! _isDeviceReady )
|
||||
if (!_isDeviceInitialised)
|
||||
{
|
||||
_isDeviceInitialised = init(_devConfig);
|
||||
}
|
||||
|
||||
if (!_isDeviceReady)
|
||||
{
|
||||
open();
|
||||
}
|
||||
|
||||
if ( _isDeviceReady )
|
||||
bool isEnableFailed(true);
|
||||
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
_isEnabled = true;
|
||||
if ( switchOn() )
|
||||
if (switchOn())
|
||||
{
|
||||
stopEnableAttemptsTimer();
|
||||
_isEnabled = true;
|
||||
isEnableFailed = false;
|
||||
emit enableStateChanged(_isEnabled);
|
||||
Info(_log, "LedDevice '%s' enabled", QSTRING_CSTR(_activeDeviceType));
|
||||
}
|
||||
}
|
||||
|
||||
if (isEnableFailed)
|
||||
{
|
||||
emit enableStateChanged(false);
|
||||
|
||||
if (_maxEnableAttempts > 0)
|
||||
{
|
||||
Debug(_log, "Device's enablement failed - Start retry timer. Retried already done [%d], isEnabled: [%d]", _enableAttempts, _isEnabled);
|
||||
startEnableAttemptsTimer();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log, "Device's enablement failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,9 +186,11 @@ void LedDevice::enable()
|
||||
|
||||
void LedDevice::disable()
|
||||
{
|
||||
if ( _isEnabled )
|
||||
Debug(_log, "Disable device %s'", QSTRING_CSTR(_activeDeviceType));
|
||||
if (_isEnabled)
|
||||
{
|
||||
_isEnabled = false;
|
||||
this->stopEnableAttemptsTimer();
|
||||
this->stopRefreshTimer();
|
||||
|
||||
switchOff();
|
||||
@@ -163,47 +205,110 @@ void LedDevice::setActiveDeviceType(const QString& deviceType)
|
||||
_activeDeviceType = deviceType;
|
||||
}
|
||||
|
||||
bool LedDevice::init(const QJsonObject &deviceConfig)
|
||||
bool LedDevice::init(const QJsonObject& deviceConfig)
|
||||
{
|
||||
Debug(_log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
_colorOrder = deviceConfig[CONFIG_COLOR_ORDER].toString(DEFAULT_COLOR_ORDER);
|
||||
_isAutoStart = deviceConfig[CONFIG_AUTOSTART].toBool(DEFAULT_IS_AUTOSTART);
|
||||
|
||||
setLedCount( deviceConfig[CONFIG_CURRENT_LED_COUNT].toInt(DEFAULT_LED_COUNT) ); // property injected to reflect real led count
|
||||
setLatchTime( deviceConfig[CONFIG_LATCH_TIME].toInt( _latchTime_ms ) );
|
||||
setRewriteTime ( deviceConfig[CONFIG_REWRITE_TIME].toInt( _refreshTimerInterval_ms) );
|
||||
setLedCount(deviceConfig[CONFIG_CURRENT_LED_COUNT].toInt(DEFAULT_LED_COUNT)); // property injected to reflect real led count
|
||||
setColorOrder(deviceConfig[CONFIG_COLOR_ORDER].toString(DEFAULT_COLOR_ORDER));
|
||||
setLatchTime(deviceConfig[CONFIG_LATCH_TIME].toInt(_latchTime_ms));
|
||||
setRewriteTime(deviceConfig[CONFIG_REWRITE_TIME].toInt(_refreshTimerInterval_ms));
|
||||
setAutoStart(deviceConfig[CONFIG_AUTOSTART].toBool(DEFAULT_IS_AUTOSTART));
|
||||
setEnableAttempts(deviceConfig[CONFIG_ENABLE_ATTEMPTS].toInt(DEFAULT_MAX_ENABLE_ATTEMPTS),
|
||||
std::chrono::seconds(deviceConfig[CONFIG_ENABLE_ATTEMPTS_INTERVALL].toInt(DEFAULT_ENABLE_ATTEMPTS_INTERVAL.count()))
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void LedDevice::startRefreshTimer()
|
||||
{
|
||||
if ( _isDeviceReady && _isEnabled )
|
||||
if (_refreshTimerInterval_ms > 0)
|
||||
{
|
||||
_refreshTimer->start();
|
||||
if (_isDeviceReady && _isOn)
|
||||
{
|
||||
// setup refreshTimer
|
||||
if (_refreshTimer == nullptr)
|
||||
{
|
||||
_refreshTimer = new QTimer(this);
|
||||
_refreshTimer->setTimerType(Qt::PreciseTimer);
|
||||
connect(_refreshTimer, &QTimer::timeout, this, &LedDevice::rewriteLEDs);
|
||||
}
|
||||
_refreshTimer->setInterval(_refreshTimerInterval_ms);
|
||||
|
||||
//Debug(_log, "Start refresh timer with interval = %ims", _refreshTimer->interval());
|
||||
_refreshTimer->start();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log, "Device is not ready to start a refresh timer");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LedDevice::stopRefreshTimer()
|
||||
{
|
||||
if ( _refreshTimer != nullptr )
|
||||
if (_refreshTimer != nullptr)
|
||||
{
|
||||
//Debug(_log, "Stopping refresh timer");
|
||||
_refreshTimer->stop();
|
||||
delete _refreshTimer;
|
||||
_refreshTimer = nullptr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int LedDevice::updateLeds(const std::vector<ColorRgb>& ledValues)
|
||||
void LedDevice::startEnableAttemptsTimer()
|
||||
{
|
||||
++_enableAttempts;
|
||||
|
||||
if (_enableAttempts <= _maxEnableAttempts)
|
||||
{
|
||||
if (_enableAttemptTimerInterval.count() > 0)
|
||||
{
|
||||
// setup enable retry timer
|
||||
if (_enableAttemptsTimer == nullptr)
|
||||
{
|
||||
_enableAttemptsTimer = new QTimer(this);
|
||||
_enableAttemptsTimer->setTimerType(Qt::PreciseTimer);
|
||||
connect(_enableAttemptsTimer, &QTimer::timeout, this, &LedDevice::enable);
|
||||
}
|
||||
_enableAttemptsTimer->setInterval(static_cast<int>(_enableAttemptTimerInterval.count() * 1000)); //NOLINT
|
||||
|
||||
Info(_log, "Start %d. attempt of %d to enable the device in %d seconds", _enableAttempts, _maxEnableAttempts, _enableAttemptTimerInterval.count());
|
||||
_enableAttemptsTimer->start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Error(_log, "Device disabled. Maximum number of %d attempts enabling the device reached. Tried for %d seconds.", _maxEnableAttempts, _enableAttempts * _enableAttemptTimerInterval.count());
|
||||
_enableAttempts = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void LedDevice::stopEnableAttemptsTimer()
|
||||
{
|
||||
if (_enableAttemptsTimer != nullptr)
|
||||
{
|
||||
Debug(_log, "Stopping enable retry timer");
|
||||
_enableAttemptsTimer->stop();
|
||||
delete _enableAttemptsTimer;
|
||||
_enableAttemptsTimer = nullptr;
|
||||
_enableAttempts = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int LedDevice::updateLeds(std::vector<ColorRgb> ledValues)
|
||||
{
|
||||
int retval = 0;
|
||||
if ( !_isEnabled || !_isOn || !_isDeviceReady || _isDeviceInError )
|
||||
if (!_isEnabled || !_isOn || !_isDeviceReady || _isDeviceInError)
|
||||
{
|
||||
//std::cout << "LedDevice::updateLeds(), LedDevice NOT ready! ";
|
||||
retval = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
qint64 elapsedTimeMs = _lastWriteTime.msecsTo( QDateTime::currentDateTime() );
|
||||
qint64 elapsedTimeMs = _lastWriteTime.msecsTo(QDateTime::currentDateTime());
|
||||
if (_latchTime_ms == 0 || elapsedTimeMs >= _latchTime_ms)
|
||||
{
|
||||
//std::cout << "LedDevice::updateLeds(), Elapsed time since last write (" << elapsedTimeMs << ") ms > _latchTime_ms (" << _latchTime_ms << ") ms" << std::endl;
|
||||
@@ -211,16 +316,16 @@ int LedDevice::updateLeds(const std::vector<ColorRgb>& ledValues)
|
||||
_lastWriteTime = QDateTime::currentDateTime();
|
||||
|
||||
// if device requires refreshing, save Led-Values and restart the timer
|
||||
if ( _isRefreshEnabled && _isEnabled )
|
||||
if (_isRefreshEnabled && _isEnabled)
|
||||
{
|
||||
this->startRefreshTimer();
|
||||
_lastLedValues = ledValues;
|
||||
this->startRefreshTimer();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//std::cout << "LedDevice::updateLeds(), Skip write. elapsedTime (" << elapsedTimeMs << ") ms < _latchTime_ms (" << _latchTime_ms << ") ms" << std::endl;
|
||||
if ( _isRefreshEnabled )
|
||||
if (_isRefreshEnabled)
|
||||
{
|
||||
//Stop timer to allow for next non-refresh update
|
||||
this->stopRefreshTimer();
|
||||
@@ -234,18 +339,21 @@ int LedDevice::rewriteLEDs()
|
||||
{
|
||||
int retval = -1;
|
||||
|
||||
if ( _isDeviceReady && _isEnabled )
|
||||
if (_isEnabled && _isOn && _isDeviceReady && !_isDeviceInError)
|
||||
{
|
||||
// qint64 elapsedTimeMs = _lastWriteTime.msecsTo(QDateTime::currentDateTime());
|
||||
// std::cout << "LedDevice::rewriteLEDs(): Rewrite LEDs now, elapsedTime [" << elapsedTimeMs << "] ms" << std::endl;
|
||||
// //:TESTING: Inject "white" output records to differentiate from normal writes
|
||||
// _lastLedValues.clear();
|
||||
// _lastLedValues.resize(static_cast<unsigned long>(_ledCount), ColorRgb::WHITE);
|
||||
// printLedValues(_lastLedValues);
|
||||
// //:TESTING:
|
||||
// qint64 elapsedTimeMs = _lastWriteTime.msecsTo(QDateTime::currentDateTime());
|
||||
// std::cout << "LedDevice::rewriteLEDs(): Rewrite LEDs now, elapsedTime [" << elapsedTimeMs << "] ms" << std::endl;
|
||||
// //:TESTING: Inject "white" output records to differentiate from normal writes
|
||||
// _lastLedValues.clear();
|
||||
// _lastLedValues.resize(static_cast<unsigned long>(_ledCount), ColorRgb::WHITE);
|
||||
// printLedValues(_lastLedValues);
|
||||
// //:TESTING:
|
||||
|
||||
retval = write(_lastLedValues);
|
||||
_lastWriteTime = QDateTime::currentDateTime();
|
||||
if (!_lastLedValues.empty())
|
||||
{
|
||||
retval = write(_lastLedValues);
|
||||
_lastWriteTime = QDateTime::currentDateTime();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -257,6 +365,7 @@ int LedDevice::rewriteLEDs()
|
||||
|
||||
int LedDevice::writeBlack(int numberOfWrites)
|
||||
{
|
||||
Debug(_log, "Set LED strip to black to switch of LEDs");
|
||||
return writeColor(ColorRgb::BLACK, numberOfWrites);
|
||||
}
|
||||
|
||||
@@ -273,7 +382,7 @@ int LedDevice::writeColor(const ColorRgb& color, int numberOfWrites)
|
||||
QTimer::singleShot(_latchTime_ms, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
}
|
||||
_lastLedValues = std::vector<ColorRgb>(static_cast<unsigned long>(_ledCount),color);
|
||||
_lastLedValues = std::vector<ColorRgb>(static_cast<unsigned long>(_ledCount), color);
|
||||
rc = write(_lastLedValues);
|
||||
}
|
||||
return rc;
|
||||
@@ -281,24 +390,31 @@ int LedDevice::writeColor(const ColorRgb& color, int numberOfWrites)
|
||||
|
||||
bool LedDevice::switchOn()
|
||||
{
|
||||
bool rc = false;
|
||||
bool rc{ false };
|
||||
|
||||
if ( _isOn )
|
||||
if (_isOn)
|
||||
{
|
||||
Debug(_log, "Device %s is already on. Skipping.", QSTRING_CSTR(_activeDeviceType));
|
||||
rc = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _isEnabled &&_isDeviceInitialised )
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
if ( storeState() )
|
||||
Info(_log, "Switching device %s ON", QSTRING_CSTR(_activeDeviceType));
|
||||
if (storeState())
|
||||
{
|
||||
if ( powerOn() )
|
||||
if (powerOn())
|
||||
{
|
||||
Info(_log, "Device %s is ON", QSTRING_CSTR(_activeDeviceType));
|
||||
_isOn = true;
|
||||
_isInSwitchOff = false;
|
||||
emit enableStateChanged(_isEnabled);
|
||||
rc = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Warning(_log, "Failed switching device %s ON", QSTRING_CSTR(_activeDeviceType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,32 +423,40 @@ bool LedDevice::switchOn()
|
||||
|
||||
bool LedDevice::switchOff()
|
||||
{
|
||||
bool rc = false;
|
||||
bool rc{ false };
|
||||
|
||||
if ( !_isOn )
|
||||
if (!_isOn)
|
||||
{
|
||||
rc = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _isDeviceInitialised )
|
||||
if (_isDeviceInitialised)
|
||||
{
|
||||
// Disable device to ensure no standard Led updates are written/processed
|
||||
Info(_log, "Switching device %s OFF", QSTRING_CSTR(_activeDeviceType));
|
||||
|
||||
// Disable device to ensure no standard LED updates are written/processed
|
||||
_isOn = false;
|
||||
_isInSwitchOff = true;
|
||||
|
||||
rc = true;
|
||||
|
||||
if ( _isDeviceReady )
|
||||
if (_isDeviceReady)
|
||||
{
|
||||
if ( _isRestoreOrigState )
|
||||
if (_isRestoreOrigState)
|
||||
{
|
||||
//Restore devices state
|
||||
restoreState();
|
||||
}
|
||||
else
|
||||
{
|
||||
powerOff();
|
||||
if (powerOff())
|
||||
{
|
||||
Info(_log, "Device %s is OFF", QSTRING_CSTR(_activeDeviceType));
|
||||
}
|
||||
else
|
||||
{
|
||||
Warning(_log, "Failed switching device %s OFF", QSTRING_CSTR(_activeDeviceType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,10 +466,12 @@ bool LedDevice::switchOff()
|
||||
|
||||
bool LedDevice::powerOff()
|
||||
{
|
||||
bool rc = false;
|
||||
bool rc{ false };
|
||||
|
||||
Debug(_log, "Power Off: %s", QSTRING_CSTR(_activeDeviceType));
|
||||
|
||||
// Simulate power-off by writing a final "Black" to have a defined outcome
|
||||
if ( writeBlack() >= 0 )
|
||||
if (writeBlack() >= 0)
|
||||
{
|
||||
rc = true;
|
||||
}
|
||||
@@ -354,15 +480,18 @@ bool LedDevice::powerOff()
|
||||
|
||||
bool LedDevice::powerOn()
|
||||
{
|
||||
bool rc = true;
|
||||
bool rc{ true };
|
||||
|
||||
Debug(_log, "Power On: %s", QSTRING_CSTR(_activeDeviceType));
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool LedDevice::storeState()
|
||||
{
|
||||
bool rc = true;
|
||||
bool rc{ true };
|
||||
|
||||
if ( _isRestoreOrigState )
|
||||
if (_isRestoreOrigState)
|
||||
{
|
||||
// Save device's original state
|
||||
// _originalStateValues = get device's state;
|
||||
@@ -373,9 +502,9 @@ bool LedDevice::storeState()
|
||||
|
||||
bool LedDevice::restoreState()
|
||||
{
|
||||
bool rc = true;
|
||||
bool rc{ true };
|
||||
|
||||
if ( _isRestoreOrigState )
|
||||
if (_isRestoreOrigState)
|
||||
{
|
||||
// Restore device's original state
|
||||
// update device using _originalStateValues
|
||||
@@ -393,7 +522,7 @@ QJsonObject LedDevice::discover(const QJsonObject& /*params*/)
|
||||
QJsonArray deviceList;
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
|
||||
Debug(_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
||||
@@ -401,75 +530,98 @@ QString LedDevice::discoverFirst()
|
||||
{
|
||||
QString deviceDiscovered;
|
||||
|
||||
Debug(_log, "deviceDiscovered: [%s]", QSTRING_CSTR(deviceDiscovered) );
|
||||
Debug(_log, "deviceDiscovered: [%s]", QSTRING_CSTR(deviceDiscovered));
|
||||
return deviceDiscovered;
|
||||
}
|
||||
|
||||
|
||||
QJsonObject LedDevice::getProperties(const QJsonObject& params)
|
||||
{
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
QJsonObject properties;
|
||||
|
||||
QJsonObject deviceProperties;
|
||||
properties.insert("properties", deviceProperties);
|
||||
|
||||
Debug(_log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
Debug(_log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
void LedDevice::setLogger(Logger* log)
|
||||
{
|
||||
_log = log;
|
||||
}
|
||||
|
||||
void LedDevice::setLedCount(int ledCount)
|
||||
{
|
||||
assert(ledCount >= 0);
|
||||
_ledCount = ledCount;
|
||||
_ledRGBCount = _ledCount * sizeof(ColorRgb);
|
||||
_ledCount = static_cast<uint>(ledCount);
|
||||
_ledRGBCount = _ledCount * sizeof(ColorRgb);
|
||||
_ledRGBWCount = _ledCount * sizeof(ColorRgbw);
|
||||
Debug(_log, "LedCount set to %d", _ledCount);
|
||||
}
|
||||
|
||||
void LedDevice::setLatchTime( int latchTime_ms )
|
||||
void LedDevice::setColorOrder(const QString& colorOrder)
|
||||
{
|
||||
_colorOrder = colorOrder;
|
||||
Debug(_log, "ColorOrder set to %s", QSTRING_CSTR(_colorOrder.toUpper()));
|
||||
}
|
||||
|
||||
void LedDevice::setLatchTime(int latchTime_ms)
|
||||
{
|
||||
assert(latchTime_ms >= 0);
|
||||
_latchTime_ms = latchTime_ms;
|
||||
Debug(_log, "LatchTime updated to %dms", _latchTime_ms);
|
||||
Debug(_log, "LatchTime set to %dms", _latchTime_ms);
|
||||
}
|
||||
|
||||
void LedDevice::setRewriteTime( int rewriteTime_ms )
|
||||
void LedDevice::setAutoStart(bool isAutoStart)
|
||||
{
|
||||
assert(rewriteTime_ms >= 0);
|
||||
_isAutoStart = isAutoStart;
|
||||
Debug(_log, "AutoStart %s", (_isAutoStart ? "enabled" : "disabled"));
|
||||
}
|
||||
|
||||
//Check, if refresh timer was not initialised due to getProperties/identify sceanrios
|
||||
if (_refreshTimer != nullptr)
|
||||
void LedDevice::setRewriteTime(int rewriteTime_ms)
|
||||
{
|
||||
_refreshTimerInterval_ms = qMax(rewriteTime_ms, 0);
|
||||
|
||||
if (_refreshTimerInterval_ms > 0)
|
||||
{
|
||||
_refreshTimerInterval_ms = rewriteTime_ms;
|
||||
_isRefreshEnabled = true;
|
||||
|
||||
if (_refreshTimerInterval_ms > 0)
|
||||
if (_refreshTimerInterval_ms <= _latchTime_ms)
|
||||
{
|
||||
|
||||
_isRefreshEnabled = true;
|
||||
|
||||
if (_refreshTimerInterval_ms <= _latchTime_ms)
|
||||
{
|
||||
int new_refresh_timer_interval = _latchTime_ms + 10;
|
||||
Warning(_log, "latchTime(%d) is bigger/equal rewriteTime(%d), set rewriteTime to %dms", _latchTime_ms, _refreshTimerInterval_ms, new_refresh_timer_interval);
|
||||
_refreshTimerInterval_ms = new_refresh_timer_interval;
|
||||
_refreshTimer->setInterval(_refreshTimerInterval_ms);
|
||||
}
|
||||
|
||||
Debug(_log, "Refresh interval = %dms", _refreshTimerInterval_ms);
|
||||
_refreshTimer->setInterval(_refreshTimerInterval_ms);
|
||||
|
||||
_lastWriteTime = QDateTime::currentDateTime();
|
||||
int new_refresh_timer_interval = _latchTime_ms + 10; //NOLINT
|
||||
Warning(_log, "latchTime(%d) is bigger/equal rewriteTime(%d), set rewriteTime to %dms", _latchTime_ms, _refreshTimerInterval_ms, new_refresh_timer_interval);
|
||||
_refreshTimerInterval_ms = new_refresh_timer_interval;
|
||||
}
|
||||
|
||||
Debug(_log, "RewriteTime updated to %dms", _refreshTimerInterval_ms);
|
||||
Debug(_log, "Refresh interval = %dms", _refreshTimerInterval_ms);
|
||||
startRefreshTimer();
|
||||
}
|
||||
else
|
||||
{
|
||||
_isRefreshEnabled = false;
|
||||
stopRefreshTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void LedDevice::setEnableAttempts(int maxEnableRetries, std::chrono::seconds enableRetryTimerInterval)
|
||||
{
|
||||
stopEnableAttemptsTimer();
|
||||
maxEnableRetries = qMax(maxEnableRetries, 0);
|
||||
|
||||
_enableAttempts = 0;
|
||||
_maxEnableAttempts = maxEnableRetries;
|
||||
_enableAttemptTimerInterval = enableRetryTimerInterval;
|
||||
|
||||
Debug(_log, "Max enable retries: %d, enable retry interval = %llds", _maxEnableAttempts, static_cast<int>(_enableAttemptTimerInterval.count()));
|
||||
}
|
||||
|
||||
void LedDevice::printLedValues(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
std::cout << "LedValues [" << ledValues.size() <<"] [";
|
||||
std::cout << "LedValues [" << ledValues.size() << "] [";
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
std::cout << color;
|
||||
@@ -477,24 +629,24 @@ void LedDevice::printLedValues(const std::vector<ColorRgb>& ledValues)
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
|
||||
QString LedDevice::uint8_t_to_hex_string(const uint8_t * data, const int size, int number) const
|
||||
QString LedDevice::uint8_t_to_hex_string(const uint8_t* data, const int size, int number)
|
||||
{
|
||||
if ( number <= 0 || number > size)
|
||||
if (number <= 0 || number > size)
|
||||
{
|
||||
number = size;
|
||||
}
|
||||
|
||||
QByteArray bytes (reinterpret_cast<const char*>(data), number);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
||||
return bytes.toHex(':');
|
||||
#else
|
||||
return bytes.toHex();
|
||||
#endif
|
||||
QByteArray bytes(reinterpret_cast<const char*>(data), number);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
||||
return bytes.toHex(':');
|
||||
#else
|
||||
return bytes.toHex();
|
||||
#endif
|
||||
}
|
||||
|
||||
QString LedDevice::toHex(const QByteArray& data, int number) const
|
||||
QString LedDevice::toHex(const QByteArray& data, int number)
|
||||
{
|
||||
if ( number <= 0 || number > data.size())
|
||||
if (number <= 0 || number > data.size())
|
||||
{
|
||||
number = data.size();
|
||||
}
|
||||
@@ -505,3 +657,47 @@ QString LedDevice::toHex(const QByteArray& data, int number) const
|
||||
return data.left(number).toHex();
|
||||
#endif
|
||||
}
|
||||
bool LedDevice::isInitialised() const
|
||||
{
|
||||
return _isDeviceInitialised;
|
||||
}
|
||||
|
||||
bool LedDevice::isReady() const
|
||||
{
|
||||
return _isDeviceReady;
|
||||
}
|
||||
|
||||
bool LedDevice::isInError() const
|
||||
{
|
||||
return _isDeviceInError;
|
||||
}
|
||||
|
||||
int LedDevice::getLatchTime() const
|
||||
{
|
||||
return _latchTime_ms;
|
||||
}
|
||||
|
||||
int LedDevice::getRewriteTime() const
|
||||
{
|
||||
return _refreshTimerInterval_ms;
|
||||
}
|
||||
|
||||
int LedDevice::getLedCount() const
|
||||
{
|
||||
return static_cast<int>(_ledCount);
|
||||
}
|
||||
|
||||
QString LedDevice::getActiveDeviceType() const
|
||||
{
|
||||
return _activeDeviceType;
|
||||
}
|
||||
|
||||
QString LedDevice::getColorOrder() const
|
||||
{
|
||||
return _colorOrder;
|
||||
}
|
||||
|
||||
bool LedDevice::componentState() const {
|
||||
return _isEnabled;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user