hyperion.ng/libsrc/leddevice/LedDeviceFactory.cpp
redPanther 317a903b14 Leddevices source tree refactoring (#461)
* rework structure of leddevice source tree

* fix data type vor v4l sig detection value in webui

* automate leddevicefactory.cpp
2017-08-07 10:05:46 +02:00

67 lines
1.7 KiB
C++
Executable File

// Stl includes
#include <exception>
#include <map>
// Build configuration
#include <HyperionConfig.h>
// Leddevice includes
#include <leddevice/LedDeviceFactory.h>
#include <utils/Logger.h>
#include <leddevice/LedDevice.h>
// following file is auto generated by cmake! it contains all available leddevice headers
#include "LedDevice_headers.h"
LedDevice * LedDeviceFactory::construct(const QJsonObject & deviceConfig, const int ledCount)
{
Logger * log = Logger::getInstance("LedDevice");
QJsonDocument config(deviceConfig);
QString ss(config.toJson(QJsonDocument::Indented));
QString type = deviceConfig["type"].toString("UNSPECIFIED").toLower();
// set amount of led to leddevice
LedDevice::setLedCount(ledCount);
#define REGISTER(className) LedDevice::addToDeviceMap(QString(#className).toLower(), LedDevice##className::construct);
// the REGISTER() calls are autogenerated by cmake.
#include "LedDevice_register.cpp"
#undef REGISTER
const LedDeviceRegistry& devList = LedDevice::getDeviceMap();
LedDevice* device = nullptr;
try
{
for ( auto dev: devList)
{
if (dev.first == type)
{
device = dev.second(deviceConfig);
LedDevice::setActiveDevice(dev.first);
Info(log,"LedDevice '%s' configured.", QSTRING_CSTR(dev.first));
break;
}
}
if (device == nullptr)
{
Error(log, "Dummy device used, because configured device '%s' is unknown", QSTRING_CSTR(type) );
throw std::runtime_error("unknown device");
}
}
catch(std::exception& e)
{
Error(log, "Dummy device used, because configured device '%s' throws error '%s'", QSTRING_CSTR(type), e.what());
const QJsonObject dummyDeviceConfig;
device = LedDeviceFile::construct(QJsonObject());
}
device->open();
return device;
}