@@ -117,23 +117,19 @@ void BleBox::GetDevicesState()
117
117
{
118
118
case 0 :
119
119
{
120
- if (root[" state" ].empty () == true )
121
- {
122
- _log.Log (LOG_ERROR, " BleBox: node 'state' missing!" );
120
+ if (IsNodeExists (root, " state" ) == false )
123
121
break ;
124
- }
122
+
125
123
const bool state = root[" state" ].asBool ();
126
124
127
125
SendSwitch (node, itt->second , 255 , state, 0 , DevicesType[itt->second ].name );
128
126
break ;
129
127
}
130
128
case 1 :
131
129
{
132
- if (root[" state" ].empty () == true )
133
- {
134
- _log.Log (LOG_ERROR, " BleBox: node 'state' missing!" );
130
+ if (IsNodeExists (root, " state" ) == false )
135
131
break ;
136
- }
132
+
137
133
const int state = root[" state" ].asInt ();
138
134
139
135
const int currentPos = root[" currentPos" ].asInt ();
@@ -149,7 +145,7 @@ void BleBox::GetDevicesState()
149
145
}
150
146
case 2 :
151
147
{
152
- if (IsNodeExists (root, " light" , " currentColor" ) == false )
148
+ if (IsNodesExist (root, " light" , " currentColor" ) == false )
153
149
break ;
154
150
155
151
const std::string currentColor = root[" light" ][" currentColor" ].asString ();
@@ -162,7 +158,7 @@ void BleBox::GetDevicesState()
162
158
}
163
159
case 3 :
164
160
{
165
- if (IsNodeExists (root, " rgbw" , " currentColor" ) == false )
161
+ if (IsNodesExist (root, " rgbw" , " currentColor" ) == false )
166
162
break ;
167
163
168
164
const std::string currentColor = root[" rgbw" ][" currentColor" ].asString ();
@@ -174,11 +170,9 @@ void BleBox::GetDevicesState()
174
170
}
175
171
case 4 :
176
172
{
177
- if (root[" currentPos" ].empty () == true )
178
- {
179
- _log.Log (LOG_ERROR, " BleBox: node 'currentPos' missing!" );
173
+ if (IsNodeExists (root, " currentPos" ) == false )
180
174
break ;
181
- }
175
+
182
176
const int currentPos = root[" currentPos" ].asInt ();
183
177
int level = (int )(currentPos / (255.0 / 100.0 ));
184
178
@@ -187,7 +181,7 @@ void BleBox::GetDevicesState()
187
181
}
188
182
case 5 :
189
183
{
190
- if (IsNodeExists (root, " dimmer" , " currentBrightness" ) == false )
184
+ if (IsNodesExist (root, " dimmer" , " currentBrightness" ) == false )
191
185
break ;
192
186
193
187
const int currentPos = root[" dimmer" ][" currentBrightness" ].asInt ();
@@ -275,11 +269,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
275
269
if (root == " " )
276
270
return false ;
277
271
278
- if (root[" state" ].empty () == true )
279
- {
280
- _log.Log (LOG_ERROR, " BleBox: node 'state' missing!" );
272
+ if (IsNodeExists (root, " state" ) == false )
281
273
return false ;
282
- }
283
274
284
275
if (root[" state" ].asString () != state)
285
276
{
@@ -311,11 +302,8 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
311
302
if (root == " " )
312
303
return false ;
313
304
314
- if (root[" state" ].empty () == true )
315
- {
316
- _log.Log (LOG_ERROR, " BleBox: node 'state' missing!" );
305
+ if (IsNodeExists (root, " state" ) == false )
317
306
return false ;
318
- }
319
307
320
308
// if (root["state"].asString() != state)
321
309
// {
@@ -350,7 +338,7 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
350
338
if (root == " " )
351
339
return false ;
352
340
353
- if (IsNodeExists (root, " light" , " currentColor" ) == false )
341
+ if (IsNodesExist (root, " light" , " currentColor" ) == false )
354
342
return false ;
355
343
356
344
if (root[" light" ][" currentColor" ].asString () != level) // TODO or desiredcolor ??
@@ -380,7 +368,7 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
380
368
if (root == " " )
381
369
return false ;
382
370
383
- if (IsNodeExists (root, " rgbw" , " desiredColor" ) == false )
371
+ if (IsNodesExist (root, " rgbw" , " desiredColor" ) == false )
384
372
return false ;
385
373
386
374
if (root[" rgbw" ][" desiredColor" ].asString () != state)
@@ -393,13 +381,21 @@ bool BleBox::WriteToHardware(const char *pdata, const unsigned char length)
393
381
return true ;
394
382
}
395
383
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)
397
385
{
398
386
if (root[node].empty () == true )
399
387
{
400
388
_log.Log (LOG_ERROR, " BleBox: node '%s' missing!" , node.c_str ());
401
389
return false ;
402
390
}
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
+
403
399
if (root[node][value].empty () == true )
404
400
{
405
401
_log.Log (LOG_ERROR, " BleBox: value '%s' missing!" , value.c_str ());
@@ -706,15 +702,10 @@ std::string BleBox::IdentifyDevice(const std::string &IPAddress)
706
702
707
703
if (root[" device" ].empty () == true )
708
704
{
709
- if (root[" type" ].empty () == true )
710
- {
711
- _log.Log (LOG_ERROR, " BleBox: Invalid data received!" );
705
+ if (IsNodeExists (root, " type" ) == false )
712
706
return " " ;
713
- }
714
707
else
715
- {
716
708
result = root[" type" ].asString ();
717
- }
718
709
}
719
710
else
720
711
{
0 commit comments