Threading and more

- webui remove restarts
- threading for LedDevice
This commit is contained in:
Paulchen-Panther
2019-01-01 19:47:07 +01:00
parent 7b6df922ea
commit 7352ff4d42
24 changed files with 482 additions and 282 deletions

View File

@@ -201,7 +201,7 @@ void PhilipsHueBridge::resolveReply(QNetworkReply* reply)
void PhilipsHueBridge::post(QString route, QString content)
{
Debug(log, "Post %s: %s", QSTRING_CSTR(QString("http://IP/api/USR/%1").arg(route)), QSTRING_CSTR(content));
//Debug(log, "Post %s: %s", QSTRING_CSTR(QString("http://IP/api/USR/%1").arg(route)), QSTRING_CSTR(content));
QNetworkRequest request(QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route));
manager.put(request, content.toLatin1());
@@ -214,7 +214,7 @@ const std::set<QString> PhilipsHueLight::GAMUT_B_MODEL_IDS =
const std::set<QString> PhilipsHueLight::GAMUT_C_MODEL_IDS =
{ "LLC020", "LST002", "LCT011", "LCT012", "LCT010", "LCT014" };
PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id, QJsonObject values)
PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge* bridge, unsigned int id, QJsonObject values)
: log(log)
, bridge(bridge)
, id(id)
@@ -297,7 +297,7 @@ PhilipsHueLight::~PhilipsHueLight()
void PhilipsHueLight::set(QString state)
{
bridge.post(QString("lights/%1/state").arg(id), state);
bridge->post(QString("lights/%1/state").arg(id), state);
}
void PhilipsHueLight::setOn(bool on)
@@ -345,18 +345,25 @@ LedDevice* LedDevicePhilipsHue::construct(const QJsonObject &deviceConfig)
}
LedDevicePhilipsHue::LedDevicePhilipsHue(const QJsonObject& deviceConfig)
: LedDevice()
, bridge(_log, deviceConfig["output"].toString(), deviceConfig["username"].toString())
: LedDevice(deviceConfig)
, _bridge(nullptr)
{
_deviceReady = init(deviceConfig);
connect(&bridge, &PhilipsHueBridge::newLights, this, &LedDevicePhilipsHue::newLights);
connect(this, &LedDevice::enableStateChanged, this, &LedDevicePhilipsHue::stateChanged);
}
LedDevicePhilipsHue::~LedDevicePhilipsHue()
{
switchOff();
delete _bridge;
}
void LedDevicePhilipsHue::start()
{
_bridge = new PhilipsHueBridge(_log, _devConfig["output"].toString(), _devConfig["username"].toString());
_deviceReady = init(_devConfig);
connect(_bridge, &PhilipsHueBridge::newLights, this, &LedDevicePhilipsHue::newLights);
connect(this, &LedDevice::enableStateChanged, this, &LedDevicePhilipsHue::stateChanged);
}
bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig)
@@ -374,7 +381,7 @@ bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig)
lightIds.push_back(i.toInt());
}
// get light info from bridge
bridge.bConnect();
_bridge->bConnect();
// adapt latchTime to count of user lightIds (bridge 10Hz max overall)
newDC.insert("latchTime",QJsonValue(100*(int)lightIds.size()));
@@ -399,7 +406,7 @@ void LedDevicePhilipsHue::newLights(QMap<quint16, QJsonObject> map)
{
if (map.contains(id))
{
lights.push_back(PhilipsHueLight(_log, bridge, id, map.value(id)));
lights.push_back(PhilipsHueLight(_log, _bridge, id, map.value(id)));
}
else
{
@@ -427,7 +434,6 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
{
// Get color.
ColorRgb color = ledValues.at(idx);
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
CiColor xy = CiColor::rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f,
light.getColorSpace());
@@ -453,7 +459,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
void LedDevicePhilipsHue::stateChanged(bool newState)
{
if(newState)
bridge.bConnect();
_bridge->bConnect();
else
lights.clear();
}

View File

@@ -139,7 +139,7 @@ class PhilipsHueLight
{
private:
Logger* log;
PhilipsHueBridge& bridge;
PhilipsHueBridge* bridge;
/// light id
unsigned int id;
bool on;
@@ -172,7 +172,7 @@ public:
/// @param bridge the bridge
/// @param id the light id
///
PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id, QJsonObject values);
PhilipsHueLight(Logger* log, PhilipsHueBridge* bridge, unsigned int id, QJsonObject values);
~PhilipsHueLight();
///
@@ -227,6 +227,10 @@ public:
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
public slots:
/// thread start
virtual void start();
private slots:
/// creates new PhilipsHueLight(s) based on user lightid with bridge feedback
///
@@ -249,7 +253,7 @@ protected:
private:
/// bridge class
PhilipsHueBridge bridge;
PhilipsHueBridge* _bridge;
///
bool switchOffOnBlack;