Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 41 additions & 67 deletions hardware/BleBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,19 @@ void BleBox::GetDevicesState()
{
case 0:
{
if (root["state"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
if (IsNodeExists(root, "state") == false)
break;
}

const bool state = root["state"].asBool();

SendSwitch(node, itt->second, 255, state, 0, DevicesType[itt->second].name);
break;
}
case 1:
{
if (root["state"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
if (IsNodeExists(root, "state") == false)
break;
}

const int state = root["state"].asInt();

const int currentPos = root["currentPos"].asInt();
Expand All @@ -149,16 +145,9 @@ void BleBox::GetDevicesState()
}
case 2:
{
if (root["light"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'light' missing!");
if (IsNodesExist(root, "light", "currentColor") == false)
break;
}
if (root["light"]["currentColor"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!");
break;
}

const std::string currentColor = root["light"]["currentColor"].asString();
int hexNumber;
sscanf(currentColor.c_str(), "%x", &hexNumber);
Expand All @@ -169,16 +158,9 @@ void BleBox::GetDevicesState()
}
case 3:
{
if (root["rgbw"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'rgbw' missing!");
break;
}
if (root["rgbw"]["currentColor"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!");
if (IsNodesExist(root, "rgbw", "currentColor") == false)
break;
}

const std::string currentColor = root["rgbw"]["currentColor"].asString();
int hexNumber;
sscanf(currentColor.c_str(), "%x", &hexNumber);
Expand All @@ -188,11 +170,9 @@ void BleBox::GetDevicesState()
}
case 4:
{
if (root["currentPos"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'currentPos' missing!");
if (IsNodeExists(root, "currentPos") == false)
break;
}

const int currentPos = root["currentPos"].asInt();
int level = (int)(currentPos / (255.0 / 100.0));

Expand All @@ -201,12 +181,10 @@ void BleBox::GetDevicesState()
}
case 5:
{
if (root["currentBrightness"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'currentBrightness' missing!");
if (IsNodesExist(root, "dimmer", "currentBrightness") == false)
break;
}
const int currentPos = root["currentBrightness"].asInt();

const int currentPos = root["dimmer"]["currentBrightness"].asInt();
int level = (int)(currentPos / (255.0 / 100.0));

SendSwitch(node, itt->second, 255, level > 0, level, DevicesType[itt->second].name);
Expand Down Expand Up @@ -291,11 +269,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
if (root == "")
return false;

if (root["state"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
if (IsNodeExists(root, "state") == false)
return false;
}

if (root["state"].asString() != state)
{
Expand Down Expand Up @@ -327,11 +302,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
if (root == "")
return false;

if (root["state"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
if (IsNodeExists(root, "state") == false)
return false;
}

//if (root["state"].asString() != state)
//{
Expand Down Expand Up @@ -366,16 +338,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
if (root == "")
return false;

if (root["light"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'light' missing!");
if (IsNodesExist(root, "light", "currentColor") == false)
return false;
}
if (root["light"]["currentColor"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!");
return false;
}

if (root["light"]["currentColor"].asString() != level) // TODO or desiredcolor ??
{
Expand Down Expand Up @@ -404,16 +368,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
if (root == "")
return false;

if (root["rgbw"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'rgbw' missing!");
return false;
}
if (root["rgbw"]["desiredColor"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node 'desiredColor' missing!");
if (IsNodesExist(root, "rgbw", "desiredColor") == false)
return false;
}

if (root["rgbw"]["desiredColor"].asString() != state)
{
Expand All @@ -425,6 +381,29 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
return true;
}

bool BleBox::IsNodeExists(const Json::Value root, const std::string node)
{
if (root[node].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: node '%s' missing!", node.c_str());
return false;
}
return true;
}

bool BleBox::IsNodesExist(const Json::Value root, const std::string node, const std::string value)
{
if (IsNodeExists(root, node) == false)
return false;

if (root[node][value].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: value '%s' missing!", value.c_str());
return false;
}
return true;
}

void BleBox::SetSettings(const int pollIntervalSec)
{
m_PollInterval = 30;
Expand Down Expand Up @@ -723,15 +702,10 @@ std::string BleBox::IdentifyDevice(const std::string &IPAddress)

if (root["device"].empty() == true)
{
if (root["type"].empty() == true)
{
_log.Log(LOG_ERROR, "BleBox: Invalid data received!");
if (IsNodeExists(root, "type") == false)
return "";
}
else
{
result = root["type"].asString();
}
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions hardware/BleBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class BleBox : public CDomoticzHardwareBase
bool StopHardware();
void Do_Work();

bool IsNodeExists(const Json::Value root, const std::string node);
bool IsNodesExist(const Json::Value root, const std::string node, const std::string value);

std::string IdentifyDevice(const std::string &IPAddress);
int GetDeviceTypeByApiName(const std::string &apiName);
std::string GetDeviceIP(const tRBUF *id);
Expand Down