Added light ids to device config.

Former-commit-id: 601616b0d7509b0e8ebb5226f00af5028fa905b5
This commit is contained in:
Tim Niggemann 2015-03-03 15:45:09 +01:00
parent acc0f61e60
commit ecea6e55ea
3 changed files with 21 additions and 6 deletions

View File

@ -196,7 +196,11 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
const std::string username = deviceConfig.get("username", "newdeveloper").asString(); const std::string username = deviceConfig.get("username", "newdeveloper").asString();
const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool(); const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool();
const int transitiontime = deviceConfig.get("transitiontime", 1).asInt(); const int transitiontime = deviceConfig.get("transitiontime", 1).asInt();
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime); std::vector<unsigned int> lightIds;
for (size_t i = 0; i < deviceConfig["lightIds"].size(); i++) {
lightIds.push_back(deviceConfig["lightIds"][i].asInt());
}
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds);
} }
else if (type == "test") else if (type == "test")
{ {

View File

@ -133,9 +133,9 @@ CiColor PhilipsHueLamp::rgbToCiColor(float red, float green, float blue) {
} }
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::string& username, bool switchOffOnBlack, LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::string& username, bool switchOffOnBlack,
int transitiontime) : int transitiontime, std::vector<unsigned int> lightIds) :
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime( host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
transitiontime) { transitiontime), lightIds(lightIds) {
http = new QHttp(host); http = new QHttp(host);
timer.setInterval(3000); timer.setInterval(3000);
timer.setSingleShot(true); timer.setSingleShot(true);
@ -251,10 +251,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Use json parser to parse reponse. // Use json parser to parse reponse.
Json::Reader reader; Json::Reader reader;
Json::FastWriter writer; Json::FastWriter writer;
// Create light ids if none supplied by the user.
if (lightIds.size() != nLights) {
lightIds.clear();
for (unsigned int i = 0; i < nLights; i++) {
lightIds.push_back(i + 1);
}
}
// Iterate lights. // Iterate lights.
for (unsigned int i = 0; i < nLights; i++) { for (unsigned int i = 0; i < nLights; i++) {
// Read the response. // Read the response.
QByteArray response = get(getRoute(i + 1)); QByteArray response = get(getRoute(lightIds.at(i)));
// Parse JSON. // Parse JSON.
Json::Value json; Json::Value json;
if (!reader.parse(QString(response).toStdString(), json)) { if (!reader.parse(QString(response).toStdString(), json)) {
@ -272,7 +279,7 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", ""); QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", "");
QString originalState = QString(writer.write(state).c_str()).trimmed(); QString originalState = QString(writer.write(state).c_str()).trimmed();
// Save state object. // Save state object.
lamps.push_back(PhilipsHueLamp(i + 1, originalState, modelId)); lamps.push_back(PhilipsHueLamp(lightIds.at(i), originalState, modelId));
} }
} }

View File

@ -131,8 +131,10 @@ public:
/// ///
/// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms). /// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms).
/// ///
/// @param lightIds light ids of the lights to control if not starting at one in ascending order.
///
LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack = LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack =
false, int transitiontime = 1); false, int transitiontime = 1, std::vector<unsigned int> lightIds = {});
/// ///
/// Destructor of this device /// Destructor of this device
@ -171,6 +173,8 @@ private:
/// Transition time in multiples of 100 ms. /// Transition time in multiples of 100 ms.
/// The default of the Hue lights will be 400 ms, but we want to have it snapier /// The default of the Hue lights will be 400 ms, but we want to have it snapier
int transitiontime; int transitiontime;
/// Array of the light ids.
std::vector<unsigned int> lightIds;
/// ///
/// Sends a HTTP GET request (blocking). /// Sends a HTTP GET request (blocking).