mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Differentiate between LED-Device Enable/Disable and Switch On/Off (#960)
* Switch Off devices, when no input source * Realign Enable/SwitchOn, Disable/SwitchOff * Align to updated LedDevice-Flow * Remove debug statements slipped in * Send last color update after enabling again * Fix WLED getProperties API call * Remove unused signals * LedDevice process flow documentation * LedDevice process flow documentation Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
This commit is contained in:
@@ -28,6 +28,7 @@ LedDevice::LedDevice(const QJsonObject& deviceConfig, QObject* parent)
|
||||
, _isEnabled(false)
|
||||
, _isDeviceInitialised(false)
|
||||
, _isDeviceReady(false)
|
||||
, _isOn(false)
|
||||
, _isDeviceInError(false)
|
||||
, _isInSwitchOff (false)
|
||||
, _lastWriteTime(QDateTime::currentDateTime())
|
||||
@@ -62,14 +63,15 @@ void LedDevice::start()
|
||||
{
|
||||
// Everything is OK -> enable device
|
||||
_isDeviceInitialised = true;
|
||||
setEnable(true);
|
||||
this->enable();
|
||||
}
|
||||
}
|
||||
|
||||
void LedDevice::stop()
|
||||
{
|
||||
setEnable(false);
|
||||
this->disable();
|
||||
this->stopRefreshTimer();
|
||||
Info(_log, " Stopped LedDevice '%s'", QSTRING_CSTR(_activeDeviceType) );
|
||||
}
|
||||
|
||||
int LedDevice::open()
|
||||
@@ -99,27 +101,39 @@ void LedDevice::setInError(const QString& errorMsg)
|
||||
emit enableStateChanged(_isEnabled);
|
||||
}
|
||||
|
||||
void LedDevice::setEnable(bool enable)
|
||||
void LedDevice::enable()
|
||||
{
|
||||
bool isSwitched = false;
|
||||
// switch off device when disabled, default: set black to LEDs when they should go off
|
||||
if ( _isEnabled && !enable)
|
||||
if ( !_isEnabled )
|
||||
{
|
||||
isSwitched = switchOff();
|
||||
}
|
||||
else
|
||||
{
|
||||
// switch on device when enabled
|
||||
if ( !_isEnabled && enable)
|
||||
_isDeviceInError = false;
|
||||
|
||||
if ( ! _isDeviceReady )
|
||||
{
|
||||
isSwitched = switchOn();
|
||||
open();
|
||||
}
|
||||
|
||||
if ( _isDeviceReady )
|
||||
{
|
||||
_isEnabled = true;
|
||||
if ( switchOn() )
|
||||
{
|
||||
emit enableStateChanged(_isEnabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isSwitched )
|
||||
void LedDevice::disable()
|
||||
{
|
||||
if ( _isEnabled )
|
||||
{
|
||||
_isEnabled = enable;
|
||||
emit enableStateChanged(enable);
|
||||
_isEnabled = false;
|
||||
this->stopRefreshTimer();
|
||||
|
||||
switchOff();
|
||||
close();
|
||||
|
||||
emit enableStateChanged(_isEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,30 +147,11 @@ bool LedDevice::init(const QJsonObject &deviceConfig)
|
||||
Debug(_log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
|
||||
_colorOrder = deviceConfig["colorOrder"].toString("RGB");
|
||||
setLedCount(static_cast<unsigned int>( deviceConfig["currentLedCount"].toInt(1) )); // property injected to reflect real led count
|
||||
|
||||
_latchTime_ms =deviceConfig["latchTime"].toInt( _latchTime_ms );
|
||||
_refreshTimerInterval_ms = deviceConfig["rewriteTime"].toInt( _refreshTimerInterval_ms);
|
||||
setLedCount( static_cast<unsigned int>( deviceConfig["currentLedCount"].toInt(1) ) ); // property injected to reflect real led count
|
||||
setLatchTime( deviceConfig["latchTime"].toInt( _latchTime_ms ) );
|
||||
setRewriteTime ( deviceConfig["rewriteTime"].toInt( _refreshTimerInterval_ms) );
|
||||
|
||||
if ( _refreshTimerInterval_ms > 0 )
|
||||
{
|
||||
_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();
|
||||
|
||||
this->startRefreshTimer();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -176,9 +171,9 @@ void LedDevice::stopRefreshTimer()
|
||||
int LedDevice::updateLeds(const std::vector<ColorRgb>& ledValues)
|
||||
{
|
||||
int retval = 0;
|
||||
if ( !isEnabled() || !_isDeviceReady || _isDeviceInError )
|
||||
if ( !_isEnabled || !_isOn || !_isDeviceReady || _isDeviceInError )
|
||||
{
|
||||
//std::cout << "LedDevice::updateLeds(), LedDevice NOT ready!" << std::endl;
|
||||
//std::cout << "LedDevice::updateLeds(), LedDevice NOT ready! ";
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
@@ -256,20 +251,20 @@ int LedDevice::writeBlack(int numberOfBlack)
|
||||
bool LedDevice::switchOn()
|
||||
{
|
||||
bool rc = false;
|
||||
if ( _isDeviceInitialised && ! _isDeviceReady && ! _isEnabled )
|
||||
|
||||
if ( _isOn )
|
||||
{
|
||||
_isDeviceInError = false;
|
||||
if ( open() < 0 )
|
||||
{
|
||||
rc = false;
|
||||
}
|
||||
else
|
||||
rc = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _isEnabled &&_isDeviceInitialised )
|
||||
{
|
||||
storeState();
|
||||
|
||||
if ( powerOn() )
|
||||
{
|
||||
_isEnabled = true;
|
||||
_isOn = true;
|
||||
rc = true;
|
||||
}
|
||||
}
|
||||
@@ -281,38 +276,37 @@ bool LedDevice::switchOff()
|
||||
{
|
||||
bool rc = false;
|
||||
|
||||
if ( _isDeviceInitialised )
|
||||
if ( !_isOn )
|
||||
{
|
||||
// Disable device to ensure no standard Led updates are written/processed
|
||||
_isEnabled = false;
|
||||
_isInSwitchOff = true;
|
||||
|
||||
this->stopRefreshTimer();
|
||||
|
||||
rc = true;
|
||||
|
||||
if ( _isDeviceReady )
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _isDeviceInitialised )
|
||||
{
|
||||
if ( _isRestoreOrigState )
|
||||
{
|
||||
//Restore devices state
|
||||
restoreState();
|
||||
}
|
||||
else
|
||||
{
|
||||
powerOff();
|
||||
}
|
||||
// Disable device to ensure no standard Led updates are written/processed
|
||||
_isOn = false;
|
||||
_isInSwitchOff = true;
|
||||
|
||||
}
|
||||
if ( close() < 0 )
|
||||
{
|
||||
rc = false;
|
||||
rc = true;
|
||||
|
||||
if ( _isDeviceReady )
|
||||
{
|
||||
if ( _isRestoreOrigState )
|
||||
{
|
||||
//Restore devices state
|
||||
restoreState();
|
||||
}
|
||||
else
|
||||
{
|
||||
powerOff();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
bool LedDevice::powerOff()
|
||||
{
|
||||
bool rc = false;
|
||||
@@ -403,7 +397,33 @@ void LedDevice::setLedCount(unsigned int ledCount)
|
||||
void LedDevice::setLatchTime( int latchTime_ms )
|
||||
{
|
||||
_latchTime_ms = latchTime_ms;
|
||||
Debug(_log, "LatchTime updated to %dms", this->getLatchTime());
|
||||
Debug(_log, "LatchTime updated to %dms", _latchTime_ms);
|
||||
}
|
||||
|
||||
void LedDevice::setRewriteTime( int rewriteTime_ms )
|
||||
{
|
||||
_refreshTimerInterval_ms = rewriteTime_ms;
|
||||
|
||||
if ( _refreshTimerInterval_ms > 0 )
|
||||
{
|
||||
|
||||
_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();
|
||||
}
|
||||
|
||||
Debug(_log, "RewriteTime updated to %dms", _refreshTimerInterval_ms);
|
||||
}
|
||||
|
||||
void LedDevice::printLedValues(const std::vector<ColorRgb>& ledValues)
|
||||
|
@@ -52,14 +52,19 @@ void LedDeviceWrapper::createLedDevice(const QJsonObject& config)
|
||||
thread->setObjectName("LedDeviceThread");
|
||||
_ledDevice = LedDeviceFactory::construct(config);
|
||||
_ledDevice->moveToThread(thread);
|
||||
|
||||
// setup thread management
|
||||
connect(thread, &QThread::started, _ledDevice, &LedDevice::start);
|
||||
|
||||
// further signals
|
||||
connect(this, &LedDeviceWrapper::updateLeds, _ledDevice, &LedDevice::updateLeds, Qt::QueuedConnection);
|
||||
connect(this, &LedDeviceWrapper::setEnable, _ledDevice, &LedDevice::setEnable);
|
||||
connect(this, &LedDeviceWrapper::closeLedDevice, _ledDevice, &LedDevice::stop, Qt::BlockingQueuedConnection);
|
||||
|
||||
connect(this, &LedDeviceWrapper::enable, _ledDevice, &LedDevice::enable);
|
||||
connect(this, &LedDeviceWrapper::disable, _ledDevice, &LedDevice::disable);
|
||||
|
||||
connect(this, &LedDeviceWrapper::switchOn, _ledDevice, &LedDevice::switchOn);
|
||||
connect(this, &LedDeviceWrapper::switchOff, _ledDevice, &LedDevice::switchOff);
|
||||
|
||||
connect(this, &LedDeviceWrapper::stopLedDevice, _ledDevice, &LedDevice::stop, Qt::BlockingQueuedConnection);
|
||||
|
||||
connect(_ledDevice, &LedDevice::enableStateChanged, this, &LedDeviceWrapper::handleInternalEnableState, Qt::QueuedConnection);
|
||||
|
||||
@@ -155,7 +160,14 @@ void LedDeviceWrapper::handleComponentState(hyperion::Components component, bool
|
||||
{
|
||||
if(component == hyperion::COMP_LEDDEVICE)
|
||||
{
|
||||
emit setEnable(state);
|
||||
if ( state )
|
||||
{
|
||||
emit enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
emit disable();
|
||||
}
|
||||
|
||||
//Get device's state, considering situations where it is not ready
|
||||
bool deviceState = false;
|
||||
@@ -169,13 +181,17 @@ void LedDeviceWrapper::handleInternalEnableState(bool newState)
|
||||
{
|
||||
_hyperion->setNewComponentState(hyperion::COMP_LEDDEVICE, newState);
|
||||
_enabled = newState;
|
||||
|
||||
if (_enabled)
|
||||
{
|
||||
_hyperion->update();
|
||||
}
|
||||
}
|
||||
|
||||
void LedDeviceWrapper::stopDeviceThread()
|
||||
{
|
||||
// turns the LEDs off & stop refresh timers
|
||||
emit closeLedDevice();
|
||||
std::cout << "[hyperiond LedDeviceWrapper] <INFO> LedDevice \'" << QSTRING_CSTR(getActiveDeviceType()) << "\' closed" << std::endl;
|
||||
emit stopLedDevice();
|
||||
|
||||
// get current thread
|
||||
QThread* oldThread = _ledDevice->thread();
|
||||
|
@@ -116,11 +116,12 @@ LedDeviceNanoleaf::~LedDeviceNanoleaf()
|
||||
bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
// Overwrite non supported/required features
|
||||
_devConfig["latchTime"] = 0;
|
||||
setLatchTime(0);
|
||||
setRewriteTime(0);
|
||||
|
||||
if (deviceConfig["rewriteTime"].toInt(0) > 0)
|
||||
{
|
||||
Info (_log, "Device Nanoleaf does not require rewrites. Refresh time is ignored.");
|
||||
_devConfig["rewriteTime"] = 0;
|
||||
}
|
||||
|
||||
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
@@ -133,7 +134,7 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig)
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR( this->getActiveDeviceType() ));
|
||||
Debug(_log, "LedCount : %u", configuredLedCount);
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
|
||||
Debug(_log, "RefreshTime : %d", _refreshTimerInterval_ms);
|
||||
Debug(_log, "RewriteTime : %d", this->getRewriteTime());
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
|
||||
// Read panel organisation configuration
|
||||
@@ -356,8 +357,6 @@ int LedDeviceNanoleaf::open()
|
||||
int retval = -1;
|
||||
_isDeviceReady = false;
|
||||
|
||||
// Set Nanoleaf to External Control (UDP) mode
|
||||
Debug(_log, "Set Nanoleaf to External Control (UDP) streaming mode");
|
||||
QJsonDocument responseDoc = changeToExternalControlMode();
|
||||
// Resolve port for Light Panels
|
||||
QJsonObject jsonStreamControllInfo = responseDoc.object();
|
||||
@@ -488,6 +487,8 @@ bool LedDeviceNanoleaf::powerOn()
|
||||
{
|
||||
if ( _isDeviceReady)
|
||||
{
|
||||
changeToExternalControlMode();
|
||||
|
||||
//Power-on Nanoleaf device
|
||||
_restApi->setPath(API_STATE);
|
||||
_restApi->put( getOnOffRequest(true) );
|
||||
@@ -514,6 +515,7 @@ QString LedDeviceNanoleaf::getOnOffRequest(bool isOn) const
|
||||
|
||||
QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode()
|
||||
{
|
||||
Debug(_log, "Set Nanoleaf to External Control (UDP) streaming mode");
|
||||
_extControlVersion = EXTCTRLVER_V2;
|
||||
//Enable UDP Mode v2
|
||||
|
||||
|
@@ -215,15 +215,10 @@ QJsonObject LedDeviceWled::getProperties(const QJsonObject& params)
|
||||
apiPort = API_DEFAULT_PORT;
|
||||
}
|
||||
|
||||
if ( filter.startsWith("/") )
|
||||
filter.remove(0,1);
|
||||
|
||||
initRestAPI(apiHost, apiPort);
|
||||
_restApi->setPath(API_PATH_INFO);
|
||||
_restApi->setPath(filter);
|
||||
|
||||
// Perform request
|
||||
// TODO: WLED::getProperties - Check, if filter is supported
|
||||
httpResponse response = _restApi->put(filter);
|
||||
httpResponse response = _restApi->get();
|
||||
if ( response.error() )
|
||||
{
|
||||
Warning (_log, "%s get properties failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
|
||||
|
@@ -997,10 +997,11 @@ LedDevice* LedDeviceYeelight::construct(const QJsonObject &deviceConfig)
|
||||
bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
// Overwrite non supported/required features
|
||||
setRewriteTime(0);
|
||||
|
||||
if (deviceConfig["rewriteTime"].toInt(0) > 0)
|
||||
{
|
||||
Info (_log, "Yeelights do not require rewrites. Refresh time is ignored.");
|
||||
_devConfig["rewriteTime"] = 0;
|
||||
}
|
||||
|
||||
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData() );
|
||||
@@ -1012,7 +1013,7 @@ bool LedDeviceYeelight::init(const QJsonObject &deviceConfig)
|
||||
Debug(_log, "DeviceType : %s", QSTRING_CSTR( this->getActiveDeviceType() ));
|
||||
Debug(_log, "LedCount : %u", this->getLedCount());
|
||||
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
|
||||
Debug(_log, "RefreshTime : %d", _refreshTimerInterval_ms);
|
||||
Debug(_log, "RewriteTime : %d", this->getRewriteTime());
|
||||
Debug(_log, "LatchTime : %d", this->getLatchTime());
|
||||
|
||||
//Get device specific configuration
|
||||
|
@@ -193,7 +193,7 @@ void ProviderRs232::setInError(const QString& errorMsg)
|
||||
|
||||
int ProviderRs232::writeBytes(const qint64 size, const uint8_t *data)
|
||||
{
|
||||
DebugIf(_isInSwitchOff, _log, "_inClosing [%d], enabled [%d], _deviceReady [%d], _frameDropCounter [%d]", _isInSwitchOff, this->isEnabled(), _isDeviceReady, _frameDropCounter);
|
||||
DebugIf(_isInSwitchOff, _log, "_inClosing [%d], enabled [%d], _deviceReady [%d], _frameDropCounter [%d]", _isInSwitchOff, _isEnabled, _isDeviceReady, _frameDropCounter);
|
||||
|
||||
int rc = 0;
|
||||
if (!_rs232Port.isOpen())
|
||||
@@ -249,7 +249,7 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t *data)
|
||||
}
|
||||
}
|
||||
|
||||
DebugIf(_isInSwitchOff, _log, "[%d], _inClosing[%d], enabled [%d], _deviceReady [%d]", rc, _isInSwitchOff, this->isEnabled(), _isDeviceReady);
|
||||
DebugIf(_isInSwitchOff, _log, "[%d], _inClosing[%d], enabled [%d], _deviceReady [%d]", rc, _isInSwitchOff, _isEnabled, _isDeviceReady);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user