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
|
|
|
|
#include <QThread>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
LedDeviceRegistry LedDeviceWrapper::_ledDeviceMap = LedDeviceRegistry();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// prepare the device constrcutor map
|
|
|
|
#define REGISTER(className) LedDeviceWrapper::addToDeviceMap(QString(#className).toLower(), LedDevice##className::construct);
|
|
|
|
|
|
|
|
// the REGISTER() calls are autogenerated by cmake.
|
|
|
|
#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);
|
|
|
|
_ledDevice = LedDeviceFactory::construct(config);
|
|
|
|
_ledDevice->moveToThread(thread);
|
|
|
|
// setup thread management
|
|
|
|
connect(thread, &QThread::started, _ledDevice, &LedDevice::start);
|
2019-07-07 10:47:43 +02:00
|
|
|
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
|
|
|
connect(thread, &QThread::finished, _ledDevice, &LedDevice::deleteLater);
|
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);
|
|
|
|
connect(this, &LedDeviceWrapper::setEnable, _ledDevice, &LedDevice::setEnable);
|
|
|
|
|
|
|
|
connect(this, &LedDeviceWrapper::closeLedDevice, _ledDevice, &LedDevice::close, Qt::BlockingQueuedConnection);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const QJsonObject LedDeviceWrapper::getLedDeviceSchemas()
|
|
|
|
{
|
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(LedDeviceSchemas);
|
|
|
|
|
|
|
|
// read the json schema from the resource
|
|
|
|
QDir d(":/leddevices/");
|
|
|
|
QStringList l = d.entryList();
|
|
|
|
QJsonObject result, schemaJson;
|
|
|
|
|
|
|
|
for(QString &item : l)
|
|
|
|
{
|
|
|
|
QString schemaPath(QString(":/leddevices/")+item);
|
|
|
|
QString devName = item.remove("schema-");
|
|
|
|
|
|
|
|
QString data;
|
|
|
|
if(!FileUtils::readFile(schemaPath, data, Logger::getInstance("LedDevice")))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("ERROR: Schema not found: " + item.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject schema;
|
|
|
|
if(!JsonUtils::parse(schemaPath, data, schema, Logger::getInstance("LedDevice")))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("ERROR: Json schema wrong of file: " + item.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaJson = schema;
|
|
|
|
schemaJson["title"] = QString("edt_dev_spec_header_title");
|
|
|
|
|
|
|
|
result[devName] = schemaJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWrapper::addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr)
|
|
|
|
{
|
|
|
|
_ledDeviceMap.emplace(name,funcPtr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LedDeviceRegistry& LedDeviceWrapper::getDeviceMap()
|
|
|
|
{
|
|
|
|
return _ledDeviceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWrapper::getLatchTime()
|
|
|
|
{
|
|
|
|
return _ledDevice->getLatchTime();
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:12:01 +01:00
|
|
|
const QString & LedDeviceWrapper::getActiveDeviceType()
|
2019-01-01 19:47:07 +01:00
|
|
|
{
|
2019-12-08 13:12:01 +01:00
|
|
|
return _ledDevice->getActiveDeviceType();
|
2019-01-01 19:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString & LedDeviceWrapper::getColorOrder()
|
|
|
|
{
|
|
|
|
return _ledDevice->getColorOrder();
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
unsigned int LedDeviceWrapper::getLedCount() const
|
|
|
|
{
|
|
|
|
return _ledDevice->getLedCount();
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
void LedDeviceWrapper::handleComponentState(const hyperion::Components component, const bool state)
|
|
|
|
{
|
|
|
|
if(component == hyperion::COMP_LEDDEVICE)
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
emit setEnable(state);
|
|
|
|
|
|
|
|
//Get device's state, considering situations where it is not ready
|
|
|
|
bool deviceState = _ledDevice->componentState();
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDeviceWrapper::stopDeviceThread()
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
// turns the leds off & stop refresh timers
|
|
|
|
emit closeLedDevice();
|
|
|
|
std::cout << "[hyperiond LedDeviceWrapper] <INFO> LedDevice \'" << QSTRING_CSTR(_ledDevice->getActiveDeviceType()) << "\' closed" << std::endl;
|
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
|
|
|
}
|