Skip to content

Commit

Permalink
Remove all uses of implicit conversions from json.
Browse files Browse the repository at this point in the history
These are prone to breaking in the future or with some compiler versions, so it is better to remove them.
  • Loading branch information
Jojo-1000 committed Aug 31, 2020
1 parent ceedfc1 commit 83d4883
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 76 deletions.
42 changes: 21 additions & 21 deletions hueplusplus/ExtendedColorHueStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorHueSaturation(hue, sat, 1))
{
return false;
Expand All @@ -59,8 +59,8 @@ bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, Hue
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorHueSaturation(hue, sat, 1))
{
return false;
Expand All @@ -83,7 +83,7 @@ bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, Hue
}
else if (cType == "ct")
{
uint16_t oldCT = light.state["state"]["ct"];
uint16_t oldCT = light.state["state"]["ct"].get<uint16_t>();
if (!light.setColorHueSaturation(hue, sat, 1))
{
return false;
Expand Down Expand Up @@ -113,12 +113,12 @@ bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, Hue
bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorXY(x, y, 1))
{
return false;
Expand All @@ -141,8 +141,8 @@ bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorXY(x, y, 1))
{
return false;
Expand All @@ -165,7 +165,7 @@ bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const
}
else if (cType == "ct")
{
uint16_t oldCT = light.state["state"]["ct"];
uint16_t oldCT = light.state["state"]["ct"].get<uint16_t>();
if (!light.setColorXY(x, y, 1))
{
return false;
Expand Down Expand Up @@ -195,12 +195,12 @@ bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const
bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorRGB(r, g, b, 1))
{
return false;
Expand All @@ -223,8 +223,8 @@ bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLigh
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorRGB(r, g, b, 1))
{
return false;
Expand All @@ -247,7 +247,7 @@ bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLigh
}
else if (cType == "ct")
{
uint16_t oldCT = light.state["state"]["ct"];
uint16_t oldCT = light.state["state"]["ct"].get<uint16_t>();
if (!light.setColorRGB(r, g, b, 1))
{
return false;
Expand Down
14 changes: 7 additions & 7 deletions hueplusplus/ExtendedColorTemperatureStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ bool ExtendedColorTemperatureStrategy::setColorTemperature(
bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorTemperature(mired, 1))
{
return false;
Expand All @@ -99,8 +99,8 @@ bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueL
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorTemperature(mired, 1))
{
return false;
Expand All @@ -123,7 +123,7 @@ bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueL
}
else if (cType == "ct")
{
uint16_t oldCT = light.state["state"]["ct"];
uint16_t oldCT = light.state["state"]["ct"].get<uint16_t>();
if (!light.setColorTemperature(mired, 1))
{
return false;
Expand Down
4 changes: 2 additions & 2 deletions hueplusplus/Hue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ std::string Hue::requestUsername()
if (jsonUser != nullptr)
{
// [{"success":{"username": "<username>"}}]
username = jsonUser;
username = jsonUser.get<std::string>();
// Update commands with new username and ip
commands = HueCommandAPI(ip, port, username, http_handler);
std::cout << "Success! Link button was pressed!\n";
Expand Down Expand Up @@ -235,7 +235,7 @@ HueLight& Hue::getLight(int id)
throw HueException(CURRENT_FILE_INFO, "Light id is not valid");
}
// std::cout << state["lights"][std::to_string(id)] << std::endl;
std::string type = state["lights"][std::to_string(id)]["modelid"];
std::string type = state["lights"][std::to_string(id)]["modelid"].get<std::string>();
// std::cout << type << std::endl;
if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001")
{
Expand Down
24 changes: 12 additions & 12 deletions hueplusplus/HueLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ bool HueLight::Off(uint8_t transition)
bool HueLight::isOn()
{
refreshState();
return state["state"]["on"];
return state["state"]["on"].get<bool>();
}

bool HueLight::isOn() const
{
return state["state"]["on"];
return state["state"]["on"].get<bool>();
}

int HueLight::getId() const
Expand All @@ -60,30 +60,30 @@ int HueLight::getId() const

std::string HueLight::getType() const
{
return state["type"];
return state["type"].get<std::string>();
}

std::string HueLight::getName()
{
refreshState();
return state["name"];
return state["name"].get<std::string>();
}

std::string HueLight::getName() const
{
return state["name"];
return state["name"].get<std::string>();
}

std::string HueLight::getModelId() const
{
return state["modelid"];
return state["modelid"].get<std::string>();
}

std::string HueLight::getUId() const
{
if (state.count("uniqueid"))
{
return state["uniqueid"];
return state["uniqueid"].get<std::string>();
}
return std::string();
}
Expand All @@ -92,7 +92,7 @@ std::string HueLight::getManufacturername() const
{
if (state.count("manufacturername"))
{
return state["manufacturername"];
return state["manufacturername"].get<std::string>();
}
return std::string();
}
Expand All @@ -101,7 +101,7 @@ std::string HueLight::getProductname() const
{
if (state.count("productname"))
{
return state["productname"];
return state["productname"].get<std::string>();
}
return std::string();
}
Expand All @@ -110,20 +110,20 @@ std::string HueLight::getLuminaireUId() const
{
if (state.count("luminaireuniqueid"))
{
return state["luminaireuniqueid"];
return state["luminaireuniqueid"].get<std::string>();
}
return std::string();
}

std::string HueLight::getSwVersion()
{
refreshState();
return state["swversion"];
return state["swversion"].get<std::string>();
}

std::string HueLight::getSwVersion() const
{
return state["swversion"];
return state["swversion"].get<std::string>();
}

bool HueLight::setName(const std::string& name)
Expand Down
4 changes: 2 additions & 2 deletions hueplusplus/SimpleBrightnessStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ bool SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transitio
unsigned int SimpleBrightnessStrategy::getBrightness(HueLight& light) const
{
light.refreshState();
return light.state["state"]["bri"];
return light.state["state"]["bri"].get<unsigned int>();
}

unsigned int SimpleBrightnessStrategy::getBrightness(const HueLight& light) const
{
return light.state["state"]["bri"];
return light.state["state"]["bri"].get<unsigned int>();
}
46 changes: 22 additions & 24 deletions hueplusplus/SimpleColorHueStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ bool SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const
bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorHueSaturation(hue, sat, 1))
{
return false;
Expand All @@ -249,8 +249,8 @@ bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLi
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorHueSaturation(hue, sat, 1))
{
return false;
Expand Down Expand Up @@ -280,12 +280,12 @@ bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLi
bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorXY(x, y, 1))
{
return false;
Expand All @@ -308,8 +308,8 @@ bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorXY(x, y, 1))
{
return false;
Expand Down Expand Up @@ -339,12 +339,12 @@ bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const
bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const
{
light.refreshState();
std::string cType = light.state["state"]["colormode"];
bool on = light.state["state"]["on"];
std::string cType = light.state["state"]["colormode"].get<std::string>();
bool on = light.state["state"]["on"].get<bool>();
if (cType == "hs")
{
uint16_t oldHue = light.state["state"]["hue"];
uint8_t oldSat = light.state["state"]["sat"];
uint16_t oldHue = light.state["state"]["hue"].get<uint16_t>();
uint8_t oldSat = light.state["state"]["sat"].get<uint8_t>();
if (!light.setColorRGB(r, g, b, 1))
{
return false;
Expand All @@ -367,8 +367,8 @@ bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight&
}
else if (cType == "xy")
{
float oldX = light.state["state"]["xy"][0];
float oldY = light.state["state"]["xy"][1];
float oldX = light.state["state"]["xy"][0].get<float>();
float oldY = light.state["state"]["xy"][1].get<float>();
if (!light.setColorRGB(r, g, b, 1))
{
return false;
Expand Down Expand Up @@ -398,25 +398,23 @@ bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight&
std::pair<uint16_t, uint8_t> SimpleColorHueStrategy::getColorHueSaturation(HueLight& light) const
{
light.refreshState();
return std::pair<uint16_t, uint8_t>(
static_cast<uint16_t>(light.state["state"]["hue"]), static_cast<uint8_t>(light.state["state"]["sat"]));
return std::make_pair(light.state["state"]["hue"].get<uint16_t>(), light.state["state"]["sat"].get<uint8_t>());
}

std::pair<uint16_t, uint8_t> SimpleColorHueStrategy::getColorHueSaturation(const HueLight& light) const
{
return std::pair<uint16_t, uint8_t>(
static_cast<uint16_t>(light.state["state"]["hue"]), static_cast<uint8_t>(light.state["state"]["sat"]));
return std::make_pair(light.state["state"]["hue"].get<uint16_t>(), light.state["state"]["sat"].get<uint8_t>());
}

std::pair<float, float> SimpleColorHueStrategy::getColorXY(HueLight& light) const
{
light.refreshState();
return std::pair<float, float>(light.state["state"]["xy"][0], light.state["state"]["xy"][1]);
return std::make_pair(light.state["state"]["xy"][0].get<float>(), light.state["state"]["xy"][1].get<float>());
}

std::pair<float, float> SimpleColorHueStrategy::getColorXY(const HueLight& light) const
{
return std::pair<float, float>(light.state["state"]["xy"][0], light.state["state"]["xy"][1]);
return std::make_pair(light.state["state"]["xy"][0].get<float>(), light.state["state"]["xy"][1].get<float>());
}
/*bool SimpleColorHueStrategy::pointInTriangle(float pointx, float pointy, float
x0, float y0, float x1, float y1, float x2, float y2)
Expand Down
Loading

0 comments on commit 83d4883

Please sign in to comment.