int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
	// Save light states if not done before.
	if (!areStatesSaved()) {
		saveStates((unsigned int) ledValues.size());
		switchOn((unsigned int) ledValues.size());
	}
	// If there are less states saved than colors given, then maybe something went wrong before.
	if (lights.size() != ledValues.size()) {
		restoreStates();
		return 0;
	}
	// Iterate through colors and set light states.
	unsigned int idx = 0;
	for (const ColorRgb& color : ledValues) {
		// Get lamp.
		PhilipsHueLight& lamp = lights.at(idx);
		// Scale colors from [0, 255] to [0, 1] and convert to xy space.
		CiColor xy = lamp.rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f);
		// Write color if color has been changed.
		if (xy != lamp.color) {
			// From a color to black.
			if (switchOffOnBlack && lamp.color != lamp.black && xy == lamp.black) {
				put(getStateRoute(lamp.id), QString("{\"on\": false}"));
			}
			// From black to a color
			else if (switchOffOnBlack && lamp.color == lamp.black && xy != lamp.black) {
				// Send adjust color and brightness command in JSON format.
				// We have to set the transition time each time.
				// Send also command to switch the lamp on.
				put(getStateRoute(lamp.id),
						QString("{\"on\": true, \"xy\": [%1, %2], \"bri\": %3, \"transitiontime\": %4}").arg(xy.x).arg(
								xy.y).arg(qRound(xy.bri * 255.0f)).arg(transitiontime));
			}
			// Normal color change.
			else {
				// Send adjust color and brightness command in JSON format.
				// We have to set the transition time each time.
				put(getStateRoute(lamp.id),
						QString("{\"xy\": [%1, %2], \"bri\": %3, \"transitiontime\": %4}").arg(xy.x).arg(xy.y).arg(
								qRound(xy.bri * 255.0f)).arg(transitiontime));
			}
		}
		// Remember last color.
		lamp.color = xy;
		// Next light id.
		idx++;
	}
	timer.start();
	return 0;
}
示例#2
0
void HM_LC_SWX_FM::saveVariables()
{
	try
	{
		if(_deviceID == 0) return;
		HomeMaticDevice::saveVariables();
		saveVariable(1000, _channelCount);
		saveStates(); //1001
	}
	catch(const std::exception& ex)
    {
    	Output::printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(Exception& ex)
    {
    	Output::printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
    	Output::printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
}