forked from tperamun2/Phillips-Hue-Lighting-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomePage.cpp
1969 lines (1544 loc) · 61.6 KB
/
HomePage.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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief the homepage class represents the mission control for the hue lights system
* @author Gurkiran Tatla
* @author Jake Schindler
* @author Justine Kim
* @author Paul Salvatore
* @author Timal Peramune
*/
#include "HomePage.hpp"
/**
* HomePage
* @brief constructor for controlling bridges and user settings
*
* @param env: an envirnmental variable
* @return no return value
*/
HomePage::HomePage(const Wt::WEnvironment &env) : Wt::WApplication(env) {
currentBridge = new Bridge("a", "b", "http://localhost", "8001");
Wt::WApplication::setTitle("hUe");
Wt::WIOService *ioservice = new Wt::WIOService();
Wt::WIOService *ioservice2 = new Wt::WIOService();
ioservice->start();
ioservice2->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
Wt::Http::Client *client2 = new Wt::Http::Client(*ioservice2);
client->done().connect(
boost::bind(&HomePage::handleHttpResponseLights, this, _1, _2));
client->get(currentBridge->getIP() + ":" + currentBridge->getPort() + "/api/newdeveloper/lights");
client2->done().connect(
boost::bind(&HomePage::handleHttpResponseGroups, this, _1, _2));
client2->get(currentBridge->getIP() + ":" + currentBridge->getPort() + "/api/newdeveloper/groups");
ioservice->stop();
ioservice2->stop();
//TOGGLE BUTTONS
//create containers
containerLogin = new Wt::WContainerWidget(root());
containerSettings = new Wt::WContainerWidget(root());
containerHomePage = new Wt::WContainerWidget(root());
containerHomePage->hide();
containerSettings->hide();
HomePageContainerBtn = new Wt::WPushButton("Go to HomePage", containerSettings);
HomePageContainerBtn->clicked().connect(this, &HomePage::toggleHomePage);
loginContainerBtn = new Wt::WPushButton("Logout", containerSettings);
loginContainerBtn->clicked().connect(this, &HomePage::toggleLogin);
//create temp (blank) user to compile
userTemp = new User();
currentUser=userTemp;
//////////////////////////CONTAINERLOGIN
//CREATE USER
createText_ = new Wt::WText(root());
createText_->setText("CREATE USER");
containerLogin->addWidget(createText_);
containerLogin->addWidget(new Wt::WBreak());
firstCreate_ = new Wt::WLineEdit(root());
firstname_ = new Wt::WText(root());
firstname_->setText("firstname");
lastCreate_ = new Wt::WLineEdit(root());
lastname_ = new Wt::WText(root());
lastname_->setText("lastname");
containerLogin->addWidget(firstCreate_);
containerLogin->addWidget(firstname_);
containerLogin->addWidget(lastCreate_);
containerLogin->addWidget(lastname_);
emailCreate_ = new Wt::WLineEdit(root());
emailLoginText_ = new Wt::WText(root());
emailLoginText_->setText("email");
containerLogin->addWidget(emailCreate_);
containerLogin->addWidget(emailLoginText_);
containerLogin->addWidget(new Wt::WBreak());
passwordCreate_ = new Wt::WLineEdit(root());
passwordLoginText_ = new Wt::WText(root());
passwordLoginText_->setText("password");
containerLogin->addWidget(passwordCreate_);
containerLogin->addWidget(passwordLoginText_);
containerLogin->addWidget(new Wt::WBreak());
createBtn = new Wt::WPushButton("Create User", root());
createBtn->clicked().connect(this, &HomePage::createUser);
containerLogin->addWidget(createBtn);
containerLogin->addWidget(new Wt::WBreak());
createUpdateText_ = new Wt::WText(root());
containerLogin->addWidget(createUpdateText_);
containerLogin->addWidget(new Wt::WBreak());
//LOGIN USER
loginText_ = new Wt::WText(root());
loginText_->setText("LOGIN");
containerLogin->addWidget(loginText_);
containerLogin->addWidget(new Wt::WBreak());
emailLogin_ = new Wt::WLineEdit(root());
emailLoginText_ = new Wt::WText(root());
emailLoginText_->setText("email");
containerLogin->addWidget(emailLogin_);
containerLogin->addWidget(emailLoginText_);
containerLogin->addWidget(new Wt::WBreak());
passwordLogin_ = new Wt::WLineEdit(root());
passwordLoginText_ = new Wt::WText(root());
passwordLoginText_->setText("password");
containerLogin->addWidget(passwordLogin_);
containerLogin->addWidget(passwordLoginText_);
containerLogin->addWidget(new Wt::WBreak());
loginBtn = new Wt::WPushButton("Log in", root());
loginBtn->clicked().connect(this, &HomePage::validateLogin);
containerLogin->addWidget(loginBtn);
//CONTAINERLOGIN//////////////////////////
//////////////////////////CONTAINERSETTINGS
containerSettings->addWidget(new Wt::WBreak());
header_ = new Wt::WText("Hello " + currentUser->getFirstName());
containerSettings->addWidget(header_);
containerSettings->addWidget(new Wt::WBreak());
emailEdit_ = new Wt::WLineEdit(root());
containerSettings->addWidget(emailEdit_);
containerSettings->addWidget(new Wt::WText(" new email"));
emailEdit_ -> setFocus();
containerSettings->addWidget(new Wt::WBreak());
firstNameEdit_ = new Wt::WLineEdit(root());
containerSettings->addWidget(firstNameEdit_);
containerSettings->addWidget(new Wt::WText(" new first name"));
containerSettings->addWidget(new Wt::WBreak());
lastNameEdit_ = new Wt::WLineEdit(root());
containerSettings->addWidget(lastNameEdit_);
containerSettings->addWidget(new Wt::WText(" new last name"));
containerSettings->addWidget(new Wt::WBreak());
Wt::WPushButton *btnSubmit = new Wt::WPushButton("Submit", root()); //Submit Button
btnSubmit->setMargin(5, Wt::Left);
containerSettings->addWidget(btnSubmit);
containerSettings->addWidget(new Wt::WBreak()); //Line Break
changeOutput_ = new Wt::WText(root());
containerSettings->addWidget(changeOutput_);
containerSettings->addWidget(new Wt::WBreak());
btnSubmit->clicked().connect(this, &HomePage::update);
emailEdit_->enterPressed().connect(boost::bind(&HomePage::update, this));
containerSettings->addWidget(new Wt::WBreak());
/*
* CHANGE PASSWORD
*/
Wt::WText *passHeader_ = new Wt::WText("Change Password");
containerSettings->addWidget(passHeader_);
containerSettings->addWidget(new Wt::WBreak());
passOld_ = new Wt::WLineEdit(root());
passOld_->setEchoMode(Wt::WLineEdit::Password);
containerSettings->addWidget(passOld_);
containerSettings->addWidget(new Wt::WText(" enter old password"));
containerSettings->addWidget(new Wt::WBreak());
passNew_ = new Wt::WLineEdit(root());
passNew_->setEchoMode(Wt::WLineEdit::Password);
containerSettings->addWidget(passNew_);
containerSettings->addWidget(new Wt::WText(" enter new password"));
containerSettings->addWidget(new Wt::WBreak());
Wt::WPushButton *btnPassSubmit = new Wt::WPushButton("Change Password", root());
btnPassSubmit->setMargin(5, Wt::Left);
containerSettings->addWidget(btnPassSubmit);
containerSettings->addWidget(new Wt::WBreak()); //Line Break
changeOutputPass_ = new Wt::WText(root());
containerSettings->addWidget(changeOutputPass_);
//password button click
btnPassSubmit->clicked().connect(this, &HomePage::updatePassword);
containerSettings->addWidget(btnPassSubmit);
containerSettings->addWidget(new Wt::WBreak());
DeleteAccountButton = new Wt::WPushButton("DELETE ACCOUNT", containerSettings);
DeleteAccountButton->clicked().connect(this, &HomePage::deleteAccount);
containerSettings->addWidget(new Wt::WBreak());
// BRIDGE ADDING
bridgeName = new Wt::WLineEdit();
bridgePlace = new Wt::WLineEdit();
bridgeIPAddress = new Wt::WLineEdit();
bridgePort = new Wt::WLineEdit();
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(bridgeName);
containerSettings->addWidget(new Wt::WText(" Bridge Name"));
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(bridgePlace);
containerSettings->addWidget(new Wt::WText(" Bridge Place"));
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(bridgeIPAddress);
containerSettings->addWidget(new Wt::WText(" Bridge IP Address"));
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(bridgePort);
containerSettings->addWidget(new Wt::WText(" Bridge Port"));
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(new Wt::WBreak());
Wt::WPushButton *createBridge = new Wt::WPushButton("Add Bridge", containerSettings);
createBridge->clicked().connect(this, &HomePage::handleCreateBridge);
containerSettings->addWidget(createBridge);
containerSettings->addWidget(new Wt::WBreak());
containerSettings->addWidget(new Wt::WBreak());
bridgeStatus = new Wt::WText();
containerSettings->addWidget(bridgeStatus);
containerSettings->addWidget(new Wt::WBreak());
bridgeOutput_ = new Wt::WText(containerSettings);
containerSettings->addWidget(bridgeOutput_);
containerSettings->addWidget(new Wt::WBreak());
bridgeSelect = new Wt::WLineEdit();
containerSettings->addWidget(bridgeSelect);
containerSettings->addWidget(new Wt::WText(" Please select a bridge number (if you just added your first bridge, select 1)"));
}
/**
* @brief a function that is controlling the UI of the homepage allowing for all Hue light functionality
*/
void HomePage::loadBridge(){
//////HOMEPAGE
containerHomePage = new Wt::WContainerWidget(root());
containerHomePage->addWidget(new Wt::WText("<br/><br/>Hue Mission Control.<br/><br/><br/><br/>"));
containerHomePage->show();
SettingsContainerBtn = new Wt::WPushButton("User Settings", containerHomePage);
SettingsContainerBtn->clicked().connect(this, &HomePage::toggleSettings);
loginContainerBtn = new Wt::WPushButton("Logout", containerHomePage);
loginContainerBtn->clicked().connect(this, &HomePage::toggleLogin);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
// SCHEDULE BUTTON + DISPLAY
deleteMessage = "";
Wt::WPushButton *createScheduleLight = new Wt::WPushButton("Create Schedule (Light).", containerHomePage);
createScheduleLight->clicked().connect(this, &HomePage::handleScheduleCreateLight);
Wt::WPushButton *createScheduleGroup = new Wt::WPushButton("Create Schedule (Group).", containerHomePage);
createScheduleGroup->clicked().connect(this, &HomePage::handleScheduleCreateGroup);
createScheduleId = new Wt::WLineEdit();
containerHomePage->addWidget(createScheduleId);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(createScheduleLight);
containerHomePage->addWidget(createScheduleGroup);
containerHomePage->addWidget(new Wt::WBreak());
// INFO FOR SCHEDULING
scheduleErrorText = new Wt::WText();
scheduleYear = new Wt::WLineEdit();
scheduleMonth = new Wt::WLineEdit();
scheduleDay = new Wt::WLineEdit();
scheduleHour = new Wt::WLineEdit();
scheduleMin = new Wt::WLineEdit();
scheduleOn = new Wt::WLineEdit();
scheduleTransition = new Wt::WLineEdit();
containerHomePage->addWidget(scheduleYear);
containerHomePage->addWidget(new Wt::WText(" Schedule Year"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleMonth);
containerHomePage->addWidget(new Wt::WText(" Schedule Month"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleDay);
containerHomePage->addWidget(new Wt::WText(" Schedule Day"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleHour);
containerHomePage->addWidget(new Wt::WText(" Schedule Hour"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleMin);
containerHomePage->addWidget(new Wt::WText(" Schedule Minute"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleOn);
containerHomePage->addWidget(new Wt::WText(" Schedule State (1 = On, 0 = Off)"));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleTransition);
containerHomePage->addWidget(new Wt::WText(" Schedule Transition (seconds)"));
containerHomePage->addWidget(new Wt::WBreak());
scheduleHue = new Wt::WSlider(Wt::Horizontal);
scheduleBrightness = new Wt::WSlider(Wt::Horizontal);
scheduleHue->setMinimum(0);
scheduleHue->setMaximum(46920);
scheduleHue->setValue(20000);
scheduleHue->setTickInterval(10000);
scheduleHue->setTickPosition(Wt::WSlider::TicksAbove);
scheduleHue->resize(500, 50);
scheduleBrightness->setMinimum(0);
scheduleBrightness->setMaximum(254);
scheduleBrightness->setValue(127);
scheduleBrightness->setTickInterval(20);
scheduleBrightness->setTickPosition(Wt::WSlider::TicksAbove);
scheduleBrightness->resize(500, 50);
containerHomePage->addWidget(new Wt::WText("Adjust Schedule Hue:<br/>"));
containerHomePage->addWidget(scheduleHue);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("Adjust Schedule Brightness:<br/>"));
containerHomePage->addWidget(scheduleBrightness);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
scheduleErrorText = new Wt::WText();
containerHomePage->addWidget(scheduleErrorText);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
Wt::WPushButton *deleteSchedule = new Wt::WPushButton("Delete Schedule.", containerHomePage);
deleteSchedule->clicked().connect(this, &HomePage::handleScheduleDelete);
deleteScheduleId = new Wt::WLineEdit();
containerHomePage->addWidget(deleteScheduleId);
containerHomePage->addWidget(new Wt::WBreak());
scheduleText = new Wt::WText();
HomePage::getAndDisplaySchedule();
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(scheduleText);
// PREDICTION BUTTON + DISPLAY
containerHomePage->addWidget(new Wt::WBreak());
predictionRecording = new Wt::WPushButton("Toggle predictive recording : ON", containerHomePage);
predictionRecording->clicked().connect(this, &HomePage::toggleThread);
containerHomePage->addWidget(predictionRecording);
containerHomePage->addWidget(new Wt::WBreak());
Wt::WPushButton *prediction = new Wt::WPushButton("Schedule 48-hour predictions.", containerHomePage);
prediction->clicked().connect(this, &HomePage::handlePrediction);
Wt::WPushButton *predictionOff = new Wt::WPushButton("Remove predicted schedules.", containerHomePage);
predictionOff->clicked().connect(this, &HomePage::handlePredictionOff);
predictionText = new Wt::WText();
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(predictionText);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("<br /><br /><br /><h3><b>Your groups:</b></h3><br /><br />"));
for (auto itr = this->groups.begin(); itr != this->groups.end(); ++itr) {
std::cout << (*itr)->getName() << std::endl;
std::string str = "<h3><b><i>" + (*itr)->getName() + "</i></b></h3><br />";
containerHomePage->addWidget(new Wt::WText(str));
std::string *light_names = (*itr)->getAllLightNames();
this->groupHueSliders.push_back(new Wt::WSlider(Wt::Horizontal));
this->groupBriSliders.push_back(new Wt::WSlider(Wt::Horizontal));
containerHomePage->addWidget(new Wt::WText(*light_names));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
}
for (int i = 0; i < this->lights.size(); i++) {
containerHomePage->addWidget(this->lights[i]);
this->lights[i]->setMargin(5, Wt::Right);
this->edits.push_back(new Wt::WLineEdit(
"Edit light name and press 'enter'. Refresh to see changes"));
this->edits[i]->setTextSize(50);
containerHomePage->addWidget(this->edits[i]);
this->edits[i]->setMargin(5, Wt::Right);
this->hueSliders.push_back(new Wt::WSlider(Wt::Horizontal));
this->hueSliders[i]->setMinimum(0);
this->hueSliders[i]->setMaximum(46920);
this->hueSliders[i]->setValue(20000);
this->hueSliders[i]->setTickInterval(10000);
this->hueSliders[i]->setTickPosition(Wt::WSlider::TicksAbove);
this->hueSliders[i]->resize(500, 50);
this->brightnessSliders.push_back(new Wt::WSlider(Wt::Horizontal));
this->brightnessSliders[i]->setMinimum(0);
this->brightnessSliders[i]->setMaximum(254);
this->brightnessSliders[i]->setValue(127);
this->brightnessSliders[i]->setTickInterval(20);
this->brightnessSliders[i]->setTickPosition(Wt::WSlider::TicksAbove);
this->brightnessSliders[i]->resize(500, 50);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("Adjust Hue: "));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(this->hueSliders[i]);
this->hueSliders[i]->setMargin(10, Wt::Right);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("Adjust Brightness: "));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(this->brightnessSliders[i]);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
}
Wt::WPushButton *addGroupButton = new Wt::WPushButton("Add Group", containerHomePage);
this->removeGroupEdit = new Wt::WLineEdit(
"Enter group name to delete --(Refresh to see changes)");
this->removeGroupEdit->setTextSize(50);
addGroupButton->setMargin(40, Wt::Right);
containerHomePage->addWidget(this->removeGroupEdit);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
addGroupButton->clicked().connect(this, &HomePage::addGroup);
this->removeGroupEdit->enterPressed().connect(this, &HomePage::removeGroup);
Wt::WText *addLightText =
new Wt::WText("Add Light (Enter name of light to add followed by the | "
"character. Enter Group name after the |)");
addLightText->setMargin(60, Wt::Right);
containerHomePage->addWidget(addLightText);
containerHomePage->addWidget(new Wt::WBreak());
this->addLightEdit = new Wt::WLineEdit("Enter light name | Enter Group name");
this->addLightEdit->setMargin(20, Wt::Right);
this->addLightEdit->enterPressed().connect(boost::bind(
&HomePage::addLightToGroup, this, addLightEdit->text().toUTF8()));
this->addLightEdit->setTextSize(50);
containerHomePage->addWidget(addLightEdit);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
Wt::WText *removeLightText =
new Wt::WText("Remove Light (Enter name of light to add followed by the "
"| character. Enter Group name after the |)");
removeLightText->setMargin(60, Wt::Right);
containerHomePage->addWidget(removeLightText);
containerHomePage->addWidget(new Wt::WBreak());
this->removeLightEdit =
new Wt::WLineEdit("Enter light name | Enter Group name");
this->removeLightEdit->setMargin(20, Wt::Right);
this->addLightEdit->enterPressed().connect(boost::bind(&HomePage::removeLightFromGroup, this, removeLightEdit->text().toUTF8()));
this->removeLightEdit->setTextSize(50);
containerHomePage->addWidget(removeLightEdit);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WBreak());
for (int i = 0; i < this->groups.size(); i++) {
containerHomePage->addWidget(this->groups[i]);
this->groupHueSliders[i]->setMinimum(0);
this->groupHueSliders[i]->setMaximum(46920);
this->groupHueSliders[i]->setValue(20000);
this->groupHueSliders[i]->setTickInterval(10000);
this->groupHueSliders[i]->setTickPosition(Wt::WSlider::TicksAbove);
this->groupHueSliders[i]->resize(500, 50);
this->groupBriSliders[i]->setMinimum(0);
this->groupBriSliders[i]->setMaximum(254);
this->groupBriSliders[i]->setValue(127);
this->groupBriSliders[i]->setTickInterval(20);
this->groupBriSliders[i]->setTickPosition(Wt::WSlider::TicksAbove);
this->groupBriSliders[i]->resize(500, 50);
this->groupEdits.push_back(new Wt::WLineEdit(
"Edit group name and press 'enter'. Refresh to see changes"));
this->groupEdits[i]->setTextSize(50);
this->groupEdits[i]->setMargin(5, Wt::Left);
containerHomePage->addWidget(this->groupEdits[i]);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("Adjust Hue for Group: "));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(this->groupHueSliders[i]);
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(new Wt::WText("Adjust Brightness for Group: "));
containerHomePage->addWidget(new Wt::WBreak());
containerHomePage->addWidget(this->groupBriSliders[i]);
containerHomePage->addWidget(new Wt::WBreak());
}
// For editing the name of a group
for (int i = 0; i < this->groupEdits.size(); i++) {
this->groupEdits[i]->enterPressed().connect(
boost::bind(&HomePage::changeGroupName, this,
this->groupEdits[i]->text().toUTF8(), i));
}
//For group Hue slider
for(int i = 0; i < this->groupHueSliders.size() ; i++){
this->groupHueSliders[i]->valueChanged().connect(boost::bind(&HomePage::changeGroupHue, this,
this->groupHueSliders[i]->valueText().toUTF8(), i));
}
//for group Brightness Slider
for(int i =0; i < this->groupBriSliders.size(); i++){
this->groupBriSliders[i]->valueChanged().connect(boost::bind(&HomePage::changeGroupBrightness, this,
this->groupBriSliders[i]->valueText().toUTF8(), i));
}
// For Hue Slider
for (int i = 0; i < this->hueSliders.size(); i++) {
this->hueSliders[i]->valueChanged().connect(
boost::bind(&HomePage::changeLightHue, this,
this->hueSliders[i]->valueText().toUTF8(), i));
}
// for Brightness slider
for (int i = 0; i < this->brightnessSliders.size(); i++) {
this->brightnessSliders[i]->valueChanged().connect(
boost::bind(&HomePage::changeLightBrightness, this,
this->brightnessSliders[i]->valueText().toUTF8(), i));
}
// For editing the name of a light
for (int i = 0; i < this->edits.size(); i++) {
this->edits[i]->enterPressed().connect(boost::bind(
&HomePage::changeLightName, this, this->edits[i]->text().toUTF8(), i));
}
// for turning a light on/off
for (int i = 0; i < this->lights.size(); i++) {
this->lights[i]->clicked().connect(this->lights[i], &Light::turnONorOFF);
}
storeLightData();
}
/**
* HomePage::handleHttpResponseAddBridge
* @brief method for handling the adding of a bridge
*
* @param args: boost::system::error_code err, const Wt::Http::Message &response
* @return void*: returns void
*/
void HomePage::handleHttpResponseAddBridge(boost::system::error_code err, const Wt::Http::Message &response){
std::cout << response.body() << std::endl;
if(response.body() != ""){
bridgeFlag = true;
}
}
Bridge *HomePage::getCurrentBridge(){
return this->currentBridge;
}
/**
* HomePage::handleHttpResponseAddBridge
* @brief method for handling the creation of a bridge
*
* @param args: none
* @return void*: returns void
*/
void HomePage::handleCreateBridge(){
// set initially to false incase of malformed destination
bridgeFlag = false;
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
client->done().connect(boost::bind(&HomePage::handleHttpResponseAddBridge, this, _1, _2));
std::string address = (bridgeIPAddress->text()).toUTF8() + ":" + (bridgePort->text()).toUTF8() + "/api/newdeveloper/lights";
std::cout << address << std::endl;
client->get(address);
std::future<int> future = std::async(std::launch::async, [](){
std::this_thread::sleep_for(std::chrono::seconds(2));
return 8;
});
std::cout << "loading, please wait\n";
std::chrono::milliseconds span (100);
while (future.wait_for(span) == std::future_status::timeout){
std::cout << '.';
}
ioservice->stop();
delete client;
delete ioservice;
std::string bName = (bridgeName->text()).toUTF8();
std::string bPlace = (bridgePlace->text()).toUTF8();
if(!bridgeFlag || bName == "" || bPlace == ""){
bridgeStatus->setText("Invalid bridge, please ensure all information is correct. (ips must be preceeded by https://)");
} else {
bridgeStatus->setText("Bridge added");
std::string writeFile = currentUser->getEmail() + "Bridges.txt";
std::ofstream out;
out.open(writeFile, std::ios::app);
std::string toWrite = "";
toWrite += bName + " ";
toWrite += bPlace + " ";
toWrite += (bridgeIPAddress->text()).toUTF8() + " ";
toWrite += (bridgePort->text()).toUTF8() + "\n";
out << toWrite;
HomePage::updateBridgeText();
out.close();
}
}
/**
* @brief a struct to pass args to a new thread
*/
struct args{
std::vector<Light *> lights;
};
/**
* do_work
* @brief method for creating a thread to store light data
*
* @param args: void * representing the arguments passed to the thread
* @return void*: returns void
*/
void *do_work(void *args){
struct args *arguments = (struct args *)args;
std::vector<Light *> lights = arguments->lights;
// infinite loop
while(true){
// sleep for an hour
std::future<int> future = std::async(std::launch::async, [](){
std::this_thread::sleep_for(std::chrono::seconds(SLEEP_PERIOD));
return 8;
});
std::chrono::milliseconds span (SLEEP_PERIOD);
while (future.wait_for(span) == std::future_status::timeout){
}
// iterate through the lights
for (auto itr = lights.begin(); itr != lights.end(); ++itr) {
std::string light_id = (*itr)->getID();
light_id = "lightData" + light_id + ".txt";
if (!std::ifstream(light_id)) {
std::ofstream file(light_id);
}
time_t rawtime;
tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
int wday = timeinfo->tm_wday;
int whour = timeinfo->tm_hour;
int on = (*itr)->getState()->isON() ? 1 : 0;
std::string toWrite = std::to_string(wday + 1) + " " + std::to_string(whour) + " " + std::to_string(on) + "\n";
std::ofstream out;
out.open(light_id, std::ios::app);
out << toWrite;
std::cout<< "Writing: " + toWrite <<std::endl;
out.close();
}
}
}
/**
* storeLightData
* @brief method for creating a thread to store light data
*
* @param none
* @return none
*/
void HomePage::storeLightData(){
std::future<int> future = std::async(std::launch::async, [](){
std::this_thread::sleep_for(std::chrono::seconds(5));
return 8;
});
std::chrono::milliseconds span (SLEEP_PERIOD);
while (future.wait_for(span) == std::future_status::timeout){
}
struct args arguments;
arguments.lights = lights;
threadRunning = true;
if(pthread_create(&worker_thread, NULL, do_work, (void *)&arguments)){
threadRunning = false;
perror("pthread_create()");
}
}
/**
* toggleThread
* @brief turns on/off recording of light states for predictive mode
*
* @param none
* @return none
*/
void HomePage::toggleThread(){
if(threadRunning){
pthread_cancel(worker_thread);
threadRunning = false;
predictionRecording->setText("Toggle predictive recording : OFF");
} else {
storeLightData();
predictionRecording->setText("Toggle predictive recording : ON");
}
}
/**
* HomePage::toggleLogin, HomePage::toggleSettings, HomePage::toggleHomePage
* @brief method for moving between pages
*
* @param args: none
* @return void*: returns void
*/
void HomePage::toggleLogin(){
containerHomePage->hide();
containerSettings->hide();
containerLogin->show();
}
void HomePage::toggleSettings(){
containerHomePage->hide();
containerSettings->show();
HomePage::updateBridgeText();
}
void HomePage::toggleHomePage(){
containerSettings->hide();
HomePage::loadBridge();
}
void HomePage::deleteAccount(){
std::string userFile = currentUser->getEmail() + ".txt";
std::string userFilebr = currentUser->getEmail() + "Bridges.txt";
std::ifstream file(userFile);
if (file){
remove(userFile.c_str());
remove(userFilebr.c_str());
HomePage::toggleLogin();
}
}
/**
* HomePage::updateBridgeText
* @brief method for updating current bridges connected with a user
*
* @param args: none
* @return void*: returns void
*/
void HomePage::updateBridgeText(){
bridgeOutput_->setText("");
std::string userFile = currentUser->getEmail() + "Bridges.txt";
std::ifstream file(userFile);
if (file) { //this file already exists
std::string currentLine;
int i = 1;
while (file>>std::ws && std::getline(file,currentLine)){
bridgeOutput_->setText(bridgeOutput_->text() +std::to_string(i) + ". " + currentLine+"<br/>");
i++;
}
}
}
/**
* HomePage::updateBridgeText
* @brief method to create a user, creates a textfile with user's info
*
* @param args: none
* @return void*: returns void
*/
void HomePage::createUser() {
std::string email = emailCreate_->text().toUTF8();
std::string password = passwordCreate_->text().toUTF8();
std::string firstName = firstCreate_->text().toUTF8();
std::string lastName = lastCreate_->text().toUTF8();
createUpdateText_->setText("");
bool err = false;
//check for filled in fields and change info
//email
if (email.compare("") != 0){
//check if email has @
std::size_t foundAt = email.find("@");
if (foundAt!=std::string::npos){
//check if "." is after the @
std::size_t foundPer = email.find(".", foundAt+2);
if (foundPer!=std::string::npos){
;
}else{
createUpdateText_->setText(createUpdateText_->text() + "Incorrect email format" + ", ");
err = true;
}
}else{
createUpdateText_->setText(createUpdateText_->text() + "Incorrect email format" + ", ");
err = true;
}
}
std::string userFile = email + ".txt";
if (std::ifstream(userFile)){ //if user file exists
createUpdateText_->setText(createUpdateText_->text() + "User with this email already exists" + ", ");
err = true;
}
//firstname
if (firstName.compare("") != 0){
//check to see if less than 50 char
if (firstName.length() < 50) {
;
}else{
createUpdateText_->setText(createUpdateText_->text() + "First name not set TOO LONG, ");
err = true;
}
}else{
err = true;
}
//lastname
if (lastName.compare("") != 0){
//check to see if less than 50 char
if (lastName.length() < 50) {
;
}else{
createUpdateText_->setText(createUpdateText_->text() + "Last name not set TOO LONG, ");
err = true;
}
}else{
err = true;
}
if (err != true){
User *newuser = new User();
newuser->update(firstName, lastName, email, password);
emailCreate_->setText("");
passwordCreate_->setText("");
firstCreate_->setText("");
lastCreate_->setText("");
createUpdateText_->setText("User Created!");
}
}
/**
* HomePage::validateLogin
* @brief method for validating user's login credentials, sets the currentuser to
and object of this user if credentials are correct
*
* @param args: none
* @return void*: returns void
*/
void HomePage::validateLogin() {
std::string email = emailLogin_->text().toUTF8();
std::string password = passwordLogin_->text().toUTF8();
std::string hashPassword = Persistence::encryptDecrypt(password);
std::string userFile = email + ".txt";
if (std::ifstream(userFile)){ //if user file exists
std::ifstream file(userFile);
std::string currentLine;
std::string details[] = {"", "", "", ""};
int i = 0;
while (file>>std::ws && std::getline(file,currentLine)){
std::cout << currentLine <<std::endl;
details[i++] = currentLine;
}
if (details[3] == hashPassword){
User *user = new User();
user->update(details[0],details[1],details[2], password);
currentUser = user;
header_->setText("Hello " + currentUser->getFirstName());
containerLogin->hide();
containerSettings->show();
HomePage::updateBridgeText();
}
}
}
/**