2019-01-01 19:47:07 +01:00
|
|
|
#include <leddevice/LedDeviceWrapper.h>
|
|
|
|
|
|
|
|
#include <leddevice/LedDevice.h>
|
|
|
|
#include <leddevice/LedDeviceFactory.h>
|
|
|
|
|
|
|
|
// following file is auto generated by cmake! it contains all available leddevice headers
|
|
|
|
#include "LedDevice_headers.h"
|
|
|
|
|
|
|
|
// util
|
|
|
|
#include <hyperion/Hyperion.h>
|
|
|
|
#include <utils/JsonUtils.h>
|
|
|
|
|
|
|
|
// qt
|
2020-08-02 10:44:42 +02:00
|
|
|
#include <QMutexLocker>
|
2019-01-01 19:47:07 +01:00
|
|
|
#include <QThread>
|
|
|
|
#include <QDir>
|
|
|
|
|
2020-08-02 10:44:42 +02:00
|
|
|
LedDeviceRegistry LedDeviceWrapper::_ledDeviceMap {};
|
2021-11-16 18:12:56 +01:00
|
|
|
|
2022-01-07 14:47:51 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
2021-11-16 18:12:56 +01:00
|
|
|
QRecursiveMutex LedDeviceWrapper::_ledDeviceMapLock;
|
|
|
|
#else
|
|
|
|
QMutex LedDeviceWrapper::_ledDeviceMapLock{ QMutex::Recursive };
|
|
|
|
#endif
|
2019-01-01 19:47:07 +01:00
|
|
|
|
|
|
|
LedDeviceWrapper::LedDeviceWrapper(Hyperion* hyperion)
|
|
|
|
: QObject(hyperion)
|
|
|
|
, _hyperion(hyperion)
|
|
|
|
, _ledDevice(nullptr)
|
2020-02-10 15:21:58 +01:00
|
|
|
, _enabled(false)
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// prepare the device constructor map
|
2019-01-01 19:47:07 +01:00
|
|
|
#define REGISTER(className) LedDeviceWrapper::addToDeviceMap(QString(#className).toLower(), LedDevice##className::construct);
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// the REGISTER() calls are auto-generated by cmake.
|
2019-01-01 19:47:07 +01:00
|
|
|
#include "LedDevice_register.cpp"
|
|
|
|
|
|
|
|
#undef REGISTER
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
_hyperion->setNewComponentState(hyperion::COMP_LEDDEVICE, false);
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LedDeviceWrapper::~LedDeviceWrapper()
|
|
|
|
{
|
|
|
|
stopDeviceThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceWrapper::createLedDevice(const QJsonObject& config)
|
|
|
|
{
|
|
|
|
if(_ledDevice != nullptr)
|
2019-07-02 19:06:36 +02:00
|
|
|
{
|
|
|
|
stopDeviceThread();
|
|
|
|
}
|
2019-01-01 19:47:07 +01:00
|
|
|
|
2019-07-02 19:06:36 +02:00
|
|
|
// create thread and device
|
|
|
|
QThread* thread = new QThread(this);
|
2020-08-02 10:44:42 +02:00
|
|
|
thread->setObjectName("LedDeviceThread");
|
2019-07-02 19:06:36 +02:00
|
|
|
_ledDevice = LedDeviceFactory::construct(config);
|
2022-01-22 17:48:03 +01:00
|
|
|
|
|
|
|
QString subComponent = parent()->property("instance").toString();
|
|
|
|
_ledDevice->setLogger(Logger::getInstance("LEDDEVICE", subComponent));
|
|
|
|
|
2019-07-02 19:06:36 +02:00
|
|
|
_ledDevice->moveToThread(thread);
|
|
|
|
// setup thread management
|
|
|
|
connect(thread, &QThread::started, _ledDevice, &LedDevice::start);
|
2019-01-01 19:47:07 +01:00
|
|
|
|
2019-07-02 19:06:36 +02:00
|
|
|
// further signals
|
2020-02-10 15:21:58 +01:00
|
|
|
connect(this, &LedDeviceWrapper::updateLeds, _ledDevice, &LedDevice::updateLeds, Qt::QueuedConnection);
|
2020-09-14 17:19:14 +02:00
|
|
|
|
|
|
|
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);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
2019-07-07 10:47:43 +02:00
|
|
|
connect(_ledDevice, &LedDevice::enableStateChanged, this, &LedDeviceWrapper::handleInternalEnableState, Qt::QueuedConnection);
|
2019-01-01 19:47:07 +01:00
|
|
|
|
2019-07-02 19:06:36 +02:00
|
|
|
// start the thread
|
|
|
|
thread->start();
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QJsonObject LedDeviceWrapper::getLedDeviceSchemas()
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(LedDeviceSchemas);
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
// read the JSON schema from the resource
|
2020-08-02 10:44:42 +02:00
|
|
|
QDir dir(":/leddevices/");
|
2019-01-01 19:47:07 +01:00
|
|
|
QJsonObject result, schemaJson;
|
|
|
|
|
2020-08-02 10:44:42 +02:00
|
|
|
for(QString &item : dir.entryList())
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
|
|
|
QString schemaPath(QString(":/leddevices/")+item);
|
|
|
|
QString devName = item.remove("schema-");
|
|
|
|
|
|
|
|
QString data;
|
2022-01-22 17:48:03 +01:00
|
|
|
if(!FileUtils::readFile(schemaPath, data, Logger::getInstance("LEDDEVICE")))
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("ERROR: Schema not found: " + item.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject schema;
|
2022-01-22 17:48:03 +01:00
|
|
|
if(!JsonUtils::parse(schemaPath, data, schema, Logger::getInstance("LEDDEVICE")))
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
throw std::runtime_error("ERROR: JSON schema wrong of file: " + item.toStdString());
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
schemaJson = schema;
|
|
|
|
schemaJson["title"] = QString("edt_dev_spec_header_title");
|
|
|
|
|
|
|
|
result[devName] = schemaJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWrapper::addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr)
|
|
|
|
{
|
2020-08-02 10:44:42 +02:00
|
|
|
QMutexLocker lock(&_ledDeviceMapLock);
|
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
_ledDeviceMap.emplace(name,funcPtr);
|
2020-08-02 10:44:42 +02:00
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LedDeviceRegistry& LedDeviceWrapper::getDeviceMap()
|
|
|
|
{
|
2020-08-02 10:44:42 +02:00
|
|
|
QMutexLocker lock(&_ledDeviceMapLock);
|
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
return _ledDeviceMap;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
int LedDeviceWrapper::getLatchTime() const
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2020-08-02 10:44:42 +02:00
|
|
|
int value = 0;
|
|
|
|
QMetaObject::invokeMethod(_ledDevice, "getLatchTime", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, value));
|
|
|
|
return value;
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QString LedDeviceWrapper::getActiveDeviceType() const
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2020-08-02 10:44:42 +02:00
|
|
|
QString value = 0;
|
|
|
|
QMetaObject::invokeMethod(_ledDevice, "getActiveDeviceType", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QString, value));
|
|
|
|
return value;
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QString LedDeviceWrapper::getColorOrder() const
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2020-08-02 10:44:42 +02:00
|
|
|
QString value;
|
|
|
|
QMetaObject::invokeMethod(_ledDevice, "getColorOrder", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QString, value));
|
|
|
|
return value;
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
unsigned int LedDeviceWrapper::getLedCount() const
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
int value = 0;
|
|
|
|
QMetaObject::invokeMethod(_ledDevice, "getLedCount", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, value));
|
2020-08-02 10:44:42 +02:00
|
|
|
return value;
|
2020-02-10 15:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
bool LedDeviceWrapper::enabled() const
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
void LedDeviceWrapper::handleComponentState(hyperion::Components component, bool state)
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
|
|
|
if(component == hyperion::COMP_LEDDEVICE)
|
|
|
|
{
|
2020-09-14 17:19:14 +02:00
|
|
|
if ( state )
|
|
|
|
{
|
|
|
|
emit enable();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit disable();
|
|
|
|
}
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
|
|
//Get device's state, considering situations where it is not ready
|
2020-08-02 10:44:42 +02:00
|
|
|
bool deviceState = false;
|
|
|
|
QMetaObject::invokeMethod(_ledDevice, "componentState", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, deviceState));
|
2020-02-10 15:21:58 +01:00
|
|
|
_hyperion->setNewComponentState(hyperion::COMP_LEDDEVICE, deviceState);
|
|
|
|
_enabled = deviceState;
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceWrapper::handleInternalEnableState(bool newState)
|
|
|
|
{
|
|
|
|
_hyperion->setNewComponentState(hyperion::COMP_LEDDEVICE, newState);
|
|
|
|
_enabled = newState;
|
2020-09-14 17:19:14 +02:00
|
|
|
|
|
|
|
if (_enabled)
|
|
|
|
{
|
|
|
|
_hyperion->update();
|
|
|
|
}
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceWrapper::stopDeviceThread()
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
// turns the LEDs off & stop refresh timers
|
2020-09-14 17:19:14 +02:00
|
|
|
emit stopLedDevice();
|
2019-07-07 10:47:43 +02:00
|
|
|
|
|
|
|
// get current thread
|
2019-01-01 19:47:07 +01:00
|
|
|
QThread* oldThread = _ledDevice->thread();
|
2020-02-10 15:21:58 +01:00
|
|
|
disconnect(oldThread, nullptr, nullptr, nullptr);
|
2019-07-07 10:47:43 +02:00
|
|
|
oldThread->quit();
|
2019-07-08 16:22:28 +02:00
|
|
|
oldThread->wait();
|
|
|
|
delete oldThread;
|
2019-07-07 10:47:43 +02:00
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
disconnect(_ledDevice, nullptr, nullptr, nullptr);
|
2019-07-08 16:22:28 +02:00
|
|
|
delete _ledDevice;
|
2019-07-07 10:47:43 +02:00
|
|
|
_ledDevice = nullptr;
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|