Skip to content

Commit b4ce7ee

Browse files
committed
[BleBox] refactoring
1 parent 7956c0f commit b4ce7ee

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

hardware/BleBox.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,19 @@ void BleBox::GetDevicesState()
117117
{
118118
case 0:
119119
{
120-
if (root["state"].empty() == true)
121-
{
122-
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
120+
if (IsNodeExists(root, "state") == false)
123121
break;
124-
}
122+
125123
const bool state = root["state"].asBool();
126124

127125
SendSwitch(node, itt->second, 255, state, 0, DevicesType[itt->second].name);
128126
break;
129127
}
130128
case 1:
131129
{
132-
if (root["state"].empty() == true)
133-
{
134-
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
130+
if (IsNodeExists(root, "state") == false)
135131
break;
136-
}
132+
137133
const int state = root["state"].asInt();
138134

139135
const int currentPos = root["currentPos"].asInt();
@@ -149,7 +145,7 @@ void BleBox::GetDevicesState()
149145
}
150146
case 2:
151147
{
152-
if (IsNodeExists(root, "light", "currentColor") == false)
148+
if (IsNodesExist(root, "light", "currentColor") == false)
153149
break;
154150

155151
const std::string currentColor = root["light"]["currentColor"].asString();
@@ -162,7 +158,7 @@ void BleBox::GetDevicesState()
162158
}
163159
case 3:
164160
{
165-
if (IsNodeExists(root, "rgbw", "currentColor") == false)
161+
if (IsNodesExist(root, "rgbw", "currentColor") == false)
166162
break;
167163

168164
const std::string currentColor = root["rgbw"]["currentColor"].asString();
@@ -174,11 +170,9 @@ void BleBox::GetDevicesState()
174170
}
175171
case 4:
176172
{
177-
if (root["currentPos"].empty() == true)
178-
{
179-
_log.Log(LOG_ERROR, "BleBox: node 'currentPos' missing!");
173+
if (IsNodeExists(root, "currentPos") == false)
180174
break;
181-
}
175+
182176
const int currentPos = root["currentPos"].asInt();
183177
int level = (int)(currentPos / (255.0 / 100.0));
184178

@@ -187,7 +181,7 @@ void BleBox::GetDevicesState()
187181
}
188182
case 5:
189183
{
190-
if (IsNodeExists(root, "dimmer", "currentBrightness") == false)
184+
if (IsNodesExist(root, "dimmer", "currentBrightness") == false)
191185
break;
192186

193187
const int currentPos = root["dimmer"]["currentBrightness"].asInt();
@@ -275,11 +269,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
275269
if (root == "")
276270
return false;
277271

278-
if (root["state"].empty() == true)
279-
{
280-
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
272+
if (IsNodeExists(root, "state") == false)
281273
return false;
282-
}
283274

284275
if (root["state"].asString() != state)
285276
{
@@ -311,11 +302,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
311302
if (root == "")
312303
return false;
313304

314-
if (root["state"].empty() == true)
315-
{
316-
_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
305+
if (IsNodeExists(root, "state") == false)
317306
return false;
318-
}
319307

320308
//if (root["state"].asString() != state)
321309
//{
@@ -350,7 +338,7 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
350338
if (root == "")
351339
return false;
352340

353-
if (IsNodeExists(root, "light", "currentColor") == false)
341+
if (IsNodesExist(root, "light", "currentColor") == false)
354342
return false;
355343

356344
if (root["light"]["currentColor"].asString() != level) // TODO or desiredcolor ??
@@ -380,7 +368,7 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
380368
if (root == "")
381369
return false;
382370

383-
if (IsNodeExists(root, "rgbw", "desiredColor") == false)
371+
if (IsNodesExist(root, "rgbw", "desiredColor") == false)
384372
return false;
385373

386374
if (root["rgbw"]["desiredColor"].asString() != state)
@@ -393,13 +381,21 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
393381
return true;
394382
}
395383

396-
bool BleBox::IsNodesExist(const Json::Value root, const std::string node, const std::string value)
384+
bool BleBox::IsNodeExists(const Json::Value root, const std::string node)
397385
{
398386
if (root[node].empty() == true)
399387
{
400388
_log.Log(LOG_ERROR, "BleBox: node '%s' missing!", node.c_str());
401389
return false;
402390
}
391+
return true;
392+
}
393+
394+
bool BleBox::IsNodesExist(const Json::Value root, const std::string node, const std::string value)
395+
{
396+
if (IsNodeExists(root, node) == false)
397+
return false;
398+
403399
if (root[node][value].empty() == true)
404400
{
405401
_log.Log(LOG_ERROR, "BleBox: value '%s' missing!", value.c_str());
@@ -706,15 +702,10 @@ std::string BleBox::IdentifyDevice(const std::string &IPAddress)
706702

707703
if (root["device"].empty() == true)
708704
{
709-
if (root["type"].empty() == true)
710-
{
711-
_log.Log(LOG_ERROR, "BleBox: Invalid data received!");
705+
if (IsNodeExists(root, "type") == false)
712706
return "";
713-
}
714707
else
715-
{
716708
result = root["type"].asString();
717-
}
718709
}
719710
else
720711
{

hardware/BleBox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BleBox : public CDomoticzHardwareBase
4343
bool StopHardware();
4444
void Do_Work();
4545

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

4849
std::string IdentifyDevice(const std::string &IPAddress);

0 commit comments

Comments
 (0)