forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.cpp
914 lines (820 loc) · 26.3 KB
/
gateway.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#include <QBuffer>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
#include <QTime>
#include <deconz.h>
#include "gateway.h"
#include "group.h"
#include "json.h"
#define PHILIPS_MAC_PREFIX QLatin1String("001788")
enum GW_Event
{
ActionProcess,
EventTimeout,
EventResponse,
EventCommandAdded
};
class Command
{
public:
quint16 groupId;
quint16 clusterId;
quint8 commandId;
union {
quint8 sceneId;
quint8 level;
} param;
quint8 mode;
quint16 transitionTime;
};
class GatewayPrivate
{
public:
void startTimer(int msec, GW_Event event);
void handleEvent(GW_Event event);
void handleEventStateOffline(GW_Event event);
void handleEventStateNotAuthorized(GW_Event event);
void handleEventStateConnected(GW_Event event);
void checkConfigResponse(const QByteArray &data);
void checkGroupsResponse(const QByteArray &data);
DeRestPluginPrivate *parent;
Gateway::State state;
bool pairingEnabled;
bool needSaveDatabase;
QString apikey;
QString name;
QString uuid;
QHostAddress address;
quint16 port;
QTimer *timer;
GW_Event timerAction;
QNetworkAccessManager *manager;
QBuffer *reqBuffer;
QNetworkReply *reply;
int pings;
std::vector<Gateway::Group> groups;
std::vector<Gateway::CascadeGroup> cascadeGroups;
std::vector<Command> commands;
};
Gateway::Gateway(DeRestPluginPrivate *parent) :
QObject(parent),
d_ptr(new GatewayPrivate)
{
Q_D(Gateway);
d->parent = parent;
d->pings = 0;
d->state = Gateway::StateOffline;
d->pairingEnabled = false;
d->needSaveDatabase = false;
d->reply = 0;
d->manager = new QNetworkAccessManager(this);
connect(d->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
d->timer = new QTimer(this);
d->timer->setSingleShot(true);
d->reqBuffer = new QBuffer(this);
connect(d->timer, SIGNAL(timeout()), this, SLOT(timerFired()));
d->startTimer(5000, ActionProcess);
}
Gateway::~Gateway()
{
Q_ASSERT(d_ptr != 0);
if (d_ptr)
{
delete d_ptr;
d_ptr = 0;
}
}
void Gateway::setAddress(const QHostAddress &address)
{
Q_D(Gateway);
if (d->address != address)
{
d->address = address;
d->needSaveDatabase = true;
}
}
const QString &Gateway::name() const
{
Q_D(const Gateway);
return d->name;
}
void Gateway::setName(const QString &name)
{
Q_D(Gateway);
if (d->name != name)
{
d->name = name;
d->needSaveDatabase = true;
}
}
const QString &Gateway::uuid() const
{
Q_D(const Gateway);
return d->uuid;
}
void Gateway::setUuid(const QString &uuid)
{
Q_D(Gateway);
if (d->uuid != uuid)
{
d->uuid = uuid;
d->needSaveDatabase = true;
}
}
const QHostAddress &Gateway::address() const
{
Q_D(const Gateway);
return d->address;
}
quint16 Gateway::port() const
{
Q_D(const Gateway);
return d->port;
}
void Gateway::setPort(quint16 port)
{
Q_D(Gateway);
if (d->port != port)
{
d->port = port;
d->needSaveDatabase = true;
}
}
void Gateway::setApiKey(const QString &apiKey)
{
Q_D(Gateway);
if (d->apikey != apiKey)
{
d->apikey = apiKey;
d->needSaveDatabase = true;
}
}
const QString &Gateway::apiKey() const
{
Q_D(const Gateway);
return d->apikey;
}
bool Gateway::pairingEnabled() const
{
Q_D(const Gateway);
return d->pairingEnabled;
}
void Gateway::setPairingEnabled(bool pairingEnabled)
{
Q_D(Gateway);
if (d->pairingEnabled != pairingEnabled)
{
d->pairingEnabled = pairingEnabled;
d->needSaveDatabase = true;
}
}
Gateway::State Gateway::state() const
{
Q_D(const Gateway);
return d->state;
}
bool Gateway::needSaveDatabase() const
{
Q_D(const Gateway);
return d->needSaveDatabase;
}
void Gateway::setNeedSaveDatabase(bool save)
{
Q_D(Gateway);
d->needSaveDatabase = save;
}
void Gateway::addCascadeGroup(quint16 local, quint16 remote)
{
Q_D(Gateway);
for (size_t i = 0; i < d->cascadeGroups.size(); i++)
{
if (d->cascadeGroups[i].local == local && d->cascadeGroups[i].remote == remote)
{
// already known
return;
}
}
CascadeGroup cg;
cg.local = local;
cg.remote = remote;
d->cascadeGroups.push_back(cg);
d->needSaveDatabase = true;
}
void Gateway::removeCascadeGroup(quint16 local, quint16 remote)
{
Q_D(Gateway);
for (size_t i = 0; i < d->cascadeGroups.size(); i++)
{
if (d->cascadeGroups[i].local == local && d->cascadeGroups[i].remote == remote)
{
d->cascadeGroups[i].local = d->cascadeGroups.back().local;
d->cascadeGroups[i].remote = d->cascadeGroups.back().remote;
d->cascadeGroups.pop_back();
d->needSaveDatabase = true;
return;
}
}
}
void Gateway::handleGroupCommand(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
Q_D(Gateway);
if (d->state != StateConnected)
{
return;
}
if (ind.dstAddressMode() != deCONZ::ApsGroupAddress)
{
return;
}
for (size_t j = 0; j < d->cascadeGroups.size(); j++)
{
const CascadeGroup &cg = d->cascadeGroups[j];
if (cg.local == ind.dstAddress().group())
{
Command cmd;
cmd.transitionTime = 0;
// filter
if (ind.clusterId() == SCENE_CLUSTER_ID)
{
switch(zclFrame.commandId())
{
case SCENE_COMMAND_RECALL_SCENE:
if (zclFrame.payload().size() < 3) // sanity
{
continue;
}
// payload U16 group, U8 scene
cmd.param.sceneId = zclFrame.payload().at(2);
break;
case SCENE_COMMAND_IKEA_MOVE_CT:
cmd.mode = zclFrame.payload().at(0);
cmd.transitionTime = 2540.0 / 83; // value for DimUp/Down
break;
case SCENE_COMMAND_IKEA_STEP_CT:
// payload U8 mode
cmd.mode = zclFrame.payload().at(0);
cmd.param.level = 43; // value for DimUp/Down
cmd.transitionTime = 5; // value for DimUp/Down
break;
case SCENE_COMMAND_IKEA_STOP_CT:
break;
default:
continue;
}
}
else if (ind.clusterId() == ONOFF_CLUSTER_ID) // onoff
{
switch(zclFrame.commandId())
{
case ONOFF_COMMAND_OFF:
// Set on: false trough REST API
break;
case ONOFF_COMMAND_ON:
// Set on: true trough REST API
// Hue dimmer switch On
break;
case ONOFF_COMMAND_TOGGLE:
// IKEA Trådfri Remote On/Off
{
::Group *group;
QVariantMap map;
QVariantMap state;
group = d->parent->getGroupForId(cg.local);
if (group && d->parent->groupToMap(group , map))
{
state = map["state"].toMap();
cmd.param.level = state["all_on"].toBool() ? 0x00 : 0x01;
break;
}
}
continue;
case ONOFF_COMMAND_OFF_WITH_EFFECT:
// Hue dimmer switch Off
cmd.transitionTime = 4;
break;
case ONOFF_COMMAND_ON_WITH_TIMED_OFF:
// IKEA Trådfri motion sensor
break;
default:
continue;
}
}
else if (ind.clusterId() == LEVEL_CLUSTER_ID)
{
switch (zclFrame.commandId())
{
case LEVEL_COMMAND_MOVE_TO_LEVEL:
// Set bri through REST API
// payload U8 level, U16 transition time
cmd.param.level = zclFrame.payload().at(0);
cmd.transitionTime = zclFrame.payload().at(1);
break;
case LEVEL_COMMAND_MOVE_WITH_ON_OFF:
// IKEA Trådfri remote DimUp Hold
// fall through
case LEVEL_COMMAND_MOVE:
// IKEA Trådfri remote DimDown Hold
// payload U8 mode, U8 rate
cmd.mode = zclFrame.payload().at(0);
cmd.param.level = zclFrame.payload().at(1);
// DBG_Printf(DBG_INFO_L2, "GW level %u\n", cmd.param.level);
cmd.transitionTime = 2540.0 / cmd.param.level;
break;
case LEVEL_COMMAND_STEP_WITH_ON_OFF:
// IKEA Trådfri remote DimUp Short Release
// fall through
case LEVEL_COMMAND_STEP:
// Hue dimmer switch DimUp, DimDown Press, Hold
// IKEA Trådfri remote DimDown Short Release
// payload U8 mode, U8 step size, U16 transitionTime
cmd.mode = zclFrame.payload().at(0);
cmd.param.level = zclFrame.payload().at(1);
cmd.transitionTime = zclFrame.payload().at(2);
break;
case LEVEL_COMMAND_STOP_WITH_ON_OFF:
// IKEA Trådfri remote DimUp Long Release
// fall through
case LEVEL_COMMAND_STOP:
// Hue dimmer switch DimUp, DimDown Long Release
// IKEA Trådfri remote DimDown Long Release
break;
default:
continue;
}
}
else
{
continue;
}
cmd.clusterId = ind.clusterId();
cmd.groupId = cg.remote;
cmd.commandId = zclFrame.commandId();
d->commands.push_back(cmd);
d->handleEvent(EventCommandAdded);
DBG_Printf(DBG_INFO, "GW %s forward command 0x%02X on cluster 0x%04X on group 0x%04X to remote group 0x%04X\n", qPrintable(d->name), zclFrame.commandId(), ind.clusterId(), cg.local, cg.remote);
}
}
}
const std::vector<Gateway::Group> &Gateway::groups() const
{
Q_D(const Gateway);
return d->groups;
}
const std::vector<Gateway::CascadeGroup> &Gateway::cascadeGroups() const
{
Q_D(const Gateway);
return d->cascadeGroups;
}
void Gateway::timerFired()
{
Q_D(Gateway);
d->handleEvent(d->timerAction);
}
void Gateway::finished(QNetworkReply *reply)
{
Q_D(Gateway);
if (d->reply == reply)
{
d->handleEvent(EventResponse);
}
}
void Gateway::error(QNetworkReply::NetworkError)
{
Q_D(Gateway);
if (d->reply && sender() == d->reply)
{
d->handleEvent(EventResponse);
}
}
void GatewayPrivate::startTimer(int msec, GW_Event event)
{
timerAction = event;
timer->start(msec);
}
void GatewayPrivate::handleEvent(GW_Event event)
{
if (state == Gateway::StateOffline) { handleEventStateOffline(event); }
else if (state == Gateway::StateNotAuthorized) { handleEventStateNotAuthorized(event); }
else if (state == Gateway::StateConnected) { handleEventStateConnected(event); }
else
{
Q_ASSERT(0);
}
}
void GatewayPrivate::handleEventStateOffline(GW_Event event)
{
if (event == ActionProcess)
{
if (port == 0 || address.isNull())
{
// need parameters
startTimer(1000, ActionProcess);
return;
}
pings = 0;
QString url;
if (!apikey.isEmpty())
{
url.sprintf("http://%s:%u/api/%s/config",
qPrintable(address.toString()), port, qPrintable(apikey));
}
else
{
url.sprintf("http://%s:%u/api/config", qPrintable(address.toString()), port);
}
reply = manager->get(QNetworkRequest(url));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
manager->parent(), SLOT(error(QNetworkReply::NetworkError)));
startTimer(2000, EventTimeout);
}
else if (event == EventResponse)
{
QNetworkReply *r = reply;
if (reply)
{
timer->stop();
reply = 0;
int code = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
r->deleteLater();
if (code == 403)
{
state = Gateway::StateNotAuthorized;
if (!apikey.isEmpty())
{
apikey.clear();
needSaveDatabase = true;
}
startTimer(5000, ActionProcess);
}
else if (code == 200)
{
checkConfigResponse(r->readAll());
state = Gateway::StateConnected;
startTimer(5000, ActionProcess);
}
else
{
DBG_Printf(DBG_INFO, "unhandled http status code in offline state %d\n", code);
startTimer(10000, EventTimeout);
}
}
}
else if (event == EventTimeout)
{
if (reply)
{
QNetworkReply *r = reply;
reply = 0;
if (r->isRunning())
{
r->abort();
}
r->deleteLater();
}
startTimer(10000, ActionProcess);
}
}
void GatewayPrivate::handleEventStateNotAuthorized(GW_Event event)
{
if (event == ActionProcess)
{
if (!pairingEnabled)
{
startTimer(5000, ActionProcess);
return;
}
pings = 0;
// try to create user account
QString url;
url.sprintf("http://%s:%u/api/", qPrintable(address.toString()), port);
QVariantMap map;
map[QLatin1String("devicetype")] = QLatin1String("x-gw");
map[QLatin1String("username")] = apikey;
QString json = deCONZ::jsonStringFromMap(map);
reqBuffer->close();
reqBuffer->setData(json.toUtf8());
reqBuffer->open(QBuffer::ReadOnly);
QNetworkRequest req(url);
reply = manager->sendCustomRequest(req, "POST", reqBuffer);
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
manager->parent(), SLOT(error(QNetworkReply::NetworkError)));
startTimer(5000, EventTimeout);
}
else if (event == EventResponse)
{
QNetworkReply *r = reply;
if (reply)
{
timer->stop();
reply = 0;
int code = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// {
// QByteArray data = r->readAll();
// DBG_Printf(DBG_INFO, "GW create user reply %d: %s\n", code, qPrintable(data));
// }
r->deleteLater();
if (code == 403)
{
// gateway must be unlocked ...
}
else if (code == 200)
{
// go to state offline first to query config
state = Gateway::StateOffline;
startTimer(100, ActionProcess);
}
// retry
if (!timer->isActive())
{
startTimer(10000, ActionProcess);
}
}
}
else if (event == EventTimeout)
{
state = Gateway::StateOffline;
startTimer(5000, ActionProcess);
}
}
void GatewayPrivate::handleEventStateConnected(GW_Event event)
{
if (event == ActionProcess)
{
Q_ASSERT(reply == 0);
if (commands.empty())
{
QString url;
url.sprintf("http://%s:%u/api/%s/groups",
qPrintable(address.toString()), port, qPrintable(apikey));
pings++;
reply = manager->get(QNetworkRequest(url));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
manager->parent(), SLOT(error(QNetworkReply::NetworkError)));
}
else
{
bool ok = false;
double level;
QString url;
QVariantMap map;
const Command &cmd = commands.back();
if (cmd.clusterId == SCENE_CLUSTER_ID)
{
switch (cmd.commandId) {
case SCENE_COMMAND_RECALL_SCENE:
ok = true;
if (uuid.startsWith(PHILIPS_MAC_PREFIX)) // cascade gateway is Hue bridge
{
QString scene;
scene.sprintf("g%us%u", cmd.groupId, cmd.param.sceneId);
map[QLatin1String("scene")] = scene;
}
else
{
url.sprintf("http://%s:%u/api/%s/groups/%u/scenes/%u/recall",
qPrintable(address.toString()), port, qPrintable(apikey), cmd.groupId, cmd.param.sceneId);
}
break;
case SCENE_COMMAND_IKEA_STEP_CT:
ok = true;
level = cmd.param.level * (cmd.mode == 0x00 ? 1 : -1);
map[QLatin1String("ct_inc")] = level;
break;
case SCENE_COMMAND_IKEA_MOVE_CT:
ok = true;
level = cmd.mode == 0x00 ? 254 : -254;
map[QLatin1String("ct_inc")] = level;
break;
case SCENE_COMMAND_IKEA_STOP_CT:
ok = true;
level = 0;
map[QLatin1String("ct_inc")] = level;
break;
default:
break;
}
}
else if (cmd.clusterId == ONOFF_CLUSTER_ID) // onoff
{
switch (cmd.commandId) {
case ONOFF_COMMAND_OFF_WITH_EFFECT:
// Hue dimmer switch Off
// fall through
case ONOFF_COMMAND_OFF:
// Set on: false through REST API
ok = true;
map[QLatin1String("on")] = false;
break;
case ONOFF_COMMAND_ON:
// Set on: true through REST API
// Hue dimmer switch On
ok = true;
map[QLatin1String("on")] = true;
break;
case ONOFF_COMMAND_TOGGLE:
ok = true;
map[QLatin1String("on")] = cmd.param.level == 0x01;
break;
default:
break;
}
}
else if (cmd.clusterId == LEVEL_CLUSTER_ID)
{
switch (cmd.commandId)
{
case LEVEL_COMMAND_MOVE_TO_LEVEL:
// Set bri through REST API
ok = true;
level = cmd.param.level;
map[QLatin1String("bri")] = level;
break;
case LEVEL_COMMAND_MOVE_WITH_ON_OFF:
if (cmd.mode == 0x00)
{
map[QLatin1String("on")] = true;
}
// fall through
case LEVEL_COMMAND_MOVE:
// IKEA Trådfri remote DimDown Hold
ok = true;
level = cmd.mode == 0x00 ? 254 : -254;
map[QLatin1String("bri_inc")] = level;
break;
case LEVEL_COMMAND_STEP_WITH_ON_OFF:
// IKEA Trådfri remote DimUp Short Release
if (cmd.mode == 0x00)
{
map[QLatin1String("on")] = true;
}
// fall through
case LEVEL_COMMAND_STEP:
// Hue dimmer switch DimUp, DimDown Short Release, Hold
// IKEA Trådfri remote DimDown Short Release
ok = true;
level = cmd.param.level * (cmd.mode == 0x00 ? 1 : -1);
map[QLatin1String("bri_inc")] = level;
break;
case LEVEL_COMMAND_STOP_WITH_ON_OFF:
// IKEA Trådfri remote DimUp Long Release
// fall through
case LEVEL_COMMAND_STOP:
// Philips Hue dimmer DimUp, DimDown Long Release
// IKEA Trådfri remote DimDown Long Release
ok = true;
level = 0;
map[QLatin1String("bri_inc")] = level;
break;
default:
break;
}
}
commands.pop_back();
if (!ok)
{
startTimer(50, EventTimeout);
return;
}
if (url.isEmpty())
{
url.sprintf("http://%s:%u/api/%s/groups/%u/action",
qPrintable(address.toString()), port, qPrintable(apikey), cmd.groupId);
}
QString json;
if (!map.isEmpty())
{
if (cmd.transitionTime != 0)
{
map[QLatin1String("transitiontime")] = (double) cmd.transitionTime;
}
json = deCONZ::jsonStringFromMap(map);
DBG_Printf(DBG_INFO_L2, "GW body %s\n", qPrintable(json));
}
else
{
json = QLatin1String("{}");
}
reqBuffer->close();
reqBuffer->setData(json.toUtf8());
reqBuffer->open(QBuffer::ReadOnly);
QNetworkRequest req(url);
reply = manager->put(req, reqBuffer);
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
manager->parent(), SLOT(error(QNetworkReply::NetworkError)));
}
startTimer(1000, EventTimeout);
}
else if (event == EventResponse)
{
QNetworkReply *r = reply;
if (reply)
{
timer->stop();
reply = 0;
int code = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (code == 200)
{
// ok check again later
if (r->url().toString().endsWith(QLatin1String("/groups")))
{
pings = 0;
checkGroupsResponse(r->readAll());
}
startTimer(15000, ActionProcess);
}
else
{
DBG_Printf(DBG_INFO, "unhandled http status code in connected state %d switch to offline state\n", code);
state = Gateway::StateOffline;
startTimer(5000, ActionProcess);
}
r->deleteLater();
}
}
else if (event == EventTimeout)
{
if (reply)
{
QNetworkReply *r = reply;
reply = 0;
if (r->isRunning())
{
r->abort();
}
r->deleteLater();
}
if (pings > 5)
{
DBG_Printf(DBG_INFO, "max request timeout in connected state switch to offline state\n");
state = Gateway::StateOffline;
}
startTimer(5000, ActionProcess);
}
else if (event == EventCommandAdded)
{
if (!reply) // not busy
{
startTimer(50, ActionProcess);
}
}
}
void GatewayPrivate::checkConfigResponse(const QByteArray &data)
{
bool ok;
QVariant var = Json::parse(data, ok);
if (var.type() != QVariant::Map)
return;
QVariantMap map = var.toMap();
if (!ok || map.isEmpty())
{
return;
}
if (map.contains(QLatin1String("name")))
{
name = map[QLatin1String("name")].toString();
}
}
void GatewayPrivate::checkGroupsResponse(const QByteArray &data)
{
bool ok;
QVariant var = Json::parse(data, ok);
if (var.type() != QVariant::Map)
return;
QVariantMap map = var.toMap();
if (!ok || map.isEmpty())
{
return;
}
QStringList groupIds = map.keys();
QStringList::iterator i = groupIds.begin();
QStringList::iterator end = groupIds.end();
if (groups.size() != (size_t)groupIds.size())
{
groups.clear();
}
for (size_t j = 0; i != end; ++i, j++)
{
QVariantMap g = map[*i].toMap();
QString name = g["name"].toString();
if (j == groups.size())
{
Gateway::Group group;
group.name = name;
group.id = *i;
groups.push_back(group);
DBG_Printf(DBG_INFO, "\tgroup %s: %s\n", qPrintable(group.id), qPrintable(group.name));
}
else if (j < groups.size())
{
Gateway::Group &group = groups[j];
if (group.name != name || group.id != *i)
{
// update
group.name = name;
group.id = *i;
DBG_Printf(DBG_INFO, "\tgroup %s: %s\n", qPrintable(group.id), qPrintable(group.name));
}
}
}
}