-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLightService.cpp
More file actions
1211 lines (1110 loc) · 39.2 KB
/
LightService.cpp
File metadata and controls
1211 lines (1110 loc) · 39.2 KB
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
#include "LightService.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include "SSDP.h"
#include <aJSON.h> // Replace avm/pgmspace.h with pgmspace.h there and set #define PRINT_BUFFER_LEN 4096 ################# IMPORTANT
#include <assert.h>
#if PRINT_BUFFER_LEN < 2048
# error aJson print buffer length PRINT_BUFFER_LEN must be increased to at least 4096
#endif
String macString;
String bridgeIDString;
String ipString;
String netmaskString;
String gatewayString;
// The username of the client (currently we authorize all clients simulating a pressed button on the bridge)
String client;
ESP8266WebServer *HTTP;
struct rgbcolor {
rgbcolor(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {};
uint8_t r;
uint8_t g;
uint8_t b;
};
#define MIN(a,b) ((a<b)?a:b)
#define MAX(a,b) ((a>b)?a:b)
struct hsvcolor {
hsvcolor(const rgbcolor& color) {
float r = color.r/COLOR_SATURATION;
float g = color.g/COLOR_SATURATION;
float b = color.b/COLOR_SATURATION;
float min = MIN(MIN(r,g),b);
v = MAX(MAX(r,g),b);
float diff = v - min;
h = 0;
s = (!v)?0:(diff/v);
if (diff) {
if (r == v) {
h = (g - b) / diff + (g < b ? 6.0f : 0.0f);
} else if (g == v) {
h = (b - r) / diff + 2.0f;
} else {
h = (r - g) / diff + 4.0f;
}
h /= 6.0f;
}
};
uint8_t h;
uint8_t s;
uint8_t v;
};
String removeSlashes(String uri) {
if (uri[0] == '/') {
uri = uri.substring(1);
}
if (uri.length() && uri[uri.length() - 1] == '/') {
uri = uri.substring(0, uri.length() - 1);
}
return uri;
}
String getPathSegment(String uri) {
// assume slashes removed
int slash = uri.indexOf("/");
if (slash == -1) {
return uri;
}
return uri.substring(0, slash);
}
String removePathSegment(String uri) {
// assume slashes removed
int slash = uri.indexOf("/");
if (slash == -1) {
return "";
}
return uri.substring(slash);
}
String getWildCard(String requestUri, String wcUri, int n, char wildcard = '*') {
wcUri = removeSlashes(wcUri);
requestUri = removeSlashes(requestUri);
String wildcardStr;
wildcardStr += wildcard;
int i = 0;
while (1) {
String uPath = getPathSegment(wcUri);
String ruPath = getPathSegment(requestUri);
if (uPath == wildcardStr) {
if (i == n) {
return ruPath;
}
i++;
}
wcUri = removeSlashes(removePathSegment(wcUri));
requestUri = removeSlashes(removePathSegment(requestUri));
if (!wcUri.length() && !requestUri.length()) {
return "";
}
if (!wcUri.length() || !requestUri.length()) {
return "";
}
}
return "";
}
class WcFnRequestHandler;
typedef std::function<void(WcFnRequestHandler *handler, String requestUri, HTTPMethod method)> HandlerFunction;
class WcFnRequestHandler : public RequestHandler {
public:
WcFnRequestHandler(HandlerFunction fn, const String &uri, HTTPMethod method, char wildcard = '*')
: _fn(fn)
, _uri(uri)
, _method(method)
, _wildcard(wildcard)
{
assert(_wildcard != '/');
// verify that the URI is reasonable (only contains wildcard at the beginning/end/whole path segments
for(int i = 0; i < _uri.length(); i++) {
if (_uri[i] == _wildcard) {
if (i != 0 && i != _uri.length() - 1 && (_uri[i-1] != '/' || _uri[i+1] != '/')) {
assert(false);
}
}
}
}
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod) {
return false;
}
String uri = removeSlashes(_uri);
requestUri = removeSlashes(requestUri);
String wildcardStr;
wildcardStr += _wildcard;
while (1) {
String uPath = getPathSegment(uri);
String ruPath = getPathSegment(requestUri);
if (uPath != ruPath && uPath != wildcardStr) {
return false;
}
uri = removeSlashes(removePathSegment(uri));
requestUri = removeSlashes(removePathSegment(requestUri));
if (!uri.length() && !requestUri.length()) {
return true;
}
if (!uri.length() || !requestUri.length()) {
return false;
}
}
return true;
}
bool canUpload(String requestUri) override {
return false;
}
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
currentReqUri = requestUri;
_fn(this, requestUri, requestMethod);
currentReqUri = "";
return true;
}
void upload(ESP8266WebServer& server, String requestUri, HTTPUpload& upload) override {}
String getWildCard(int wcIndex) {
return ::getWildCard(currentReqUri, _uri, wcIndex);
}
protected:
String currentReqUri;
HandlerFunction _fn;
String _uri;
HTTPMethod _method;
char _wildcard;
};
LightServiceClass LightService;
LightHandler *lightHandlers[MAX_LIGHT_HANDLERS]; // interfaces exposed to the outside world
bool LightServiceClass::setLightHandler(int index, LightHandler *handler) {
if (index >= currentNumLights || index < 0) return false;
lightHandlers[index] = handler;
return true;
}
bool LightServiceClass::setLightsAvailable(int lights) {
if (lights <= MAX_LIGHT_HANDLERS) {
currentNumLights = lights;
return true;
}
return false;
}
int LightServiceClass::getLightsAvailable() {
return currentNumLights;
}
String StringIPaddress(IPAddress myaddr)
{
String LocalIP = "";
for (int i = 0; i < 4; i++)
{
LocalIP += String(myaddr[i]);
if (i < 3) LocalIP += ".";
}
return LocalIP;
}
LightHandler *LightServiceClass::getLightHandler(int numberOfTheLight) {
if (numberOfTheLight >= currentNumLights || numberOfTheLight < 0) {
return nullptr;
}
if (!lightHandlers[numberOfTheLight]) {
return new LightHandler();
}
return lightHandlers[numberOfTheLight];
}
static const char* _ssdp_response_template =
"HTTP/1.1 200 OK\r\n"
"EXT:\r\n"
"CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
"LOCATION: http://%s:%u/%s\r\n" // WiFi.localIP(), _port, _schemaURL
"SERVER: Arduino/1.0 UPNP/1.1 %s/%s\r\n" // _modelName, _modelNumber
"hue-bridgeid: %s\r\n"
"ST: %s\r\n" // _deviceType
"USN: uuid:%s\r\n" // _uuid
"\r\n";
static const char* _ssdp_notify_template =
"NOTIFY * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
"NTS: ssdp:alive\r\n"
"CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
"LOCATION: http://%s:%u/%s\r\n" // WiFi.localIP(), _port, _schemaURL
"SERVER: Arduino/1.0 UPNP/1.1 %s/%s\r\n" // _modelName, _modelNumber
"hue-bridgeid: %s\r\n"
"NT: %s\r\n" // _deviceType
"USN: uuid:%s\r\n" // _uuid
"\r\n";
int ssdpMsgFormatCallback(SSDPClass *ssdp, char *buffer, int buff_len,
bool isNotify, int interval, char *modelName,
char *modelNumber, char *uuid, char *deviceType,
uint32_t ip, uint16_t port, char *schemaURL) {
if (isNotify) {
return snprintf(buffer, buff_len,
_ssdp_notify_template,
interval,
ipString.c_str(), port, schemaURL,
modelName, modelNumber,
bridgeIDString.c_str(),
deviceType,
uuid);
} else {
return snprintf(buffer, buff_len,
_ssdp_response_template,
interval,
ipString.c_str(), port, schemaURL,
modelName, modelNumber,
bridgeIDString.c_str(),
deviceType,
uuid);
}
}
class LightGroup {
public:
LightGroup(aJsonObject *root) {
aJsonObject* jName = aJson.getObjectItem(root, "name");
aJsonObject* jLights = aJson.getObjectItem(root, "lights");
// jName and jLights guaranteed to exist
name = jName->valuestring;
for (int i = 0; i < aJson.getArraySize(jLights); i++) {
aJsonObject* jLight = aJson.getArrayItem(jLights, i);
// lights are 1-based and map to the 0-based bitfield
int lightNum = atoi(jLight->valuestring);
if (lightNum != 0) {
lights |= (1 << (lightNum - 1));
}
}
}
aJsonObject *getJson() {
aJsonObject *object = aJson.createObject();
aJson.addStringToObject(object, "name", name.c_str());
aJsonObject *lightsArray = aJson.createArray();
aJson.addItemToObject(object, "lights", lightsArray);
for (int i = 0; i < 16; i++) {
if (!((1 << i) & lights)) {
continue;
}
// add light to list
String lightNum = "";
lightNum += (i + 1);
aJson.addItemToArray(lightsArray, aJson.createItem(lightNum.c_str()));
}
return object;
}
aJsonObject *getSceneJson() {
aJsonObject *object = aJson.createObject();
aJson.addStringToObject(object, "name", name.c_str());
aJson.addStringToObject(object, "owner", "api");
aJson.addStringToObject(object, "picture", "");
aJson.addStringToObject(object, "lastupdated", "");
aJson.addBooleanToObject(object, "recycle", false);
aJson.addBooleanToObject(object, "locked", false);
aJson.addNumberToObject(object, "version", 2);
aJsonObject *lightsArray = aJson.createArray();
aJson.addItemToObject(object, "lights", lightsArray);
for (int i = 0; i < 16; i++) {
if (!((1 << i) & lights)) {
continue;
}
// add light to list
String lightNum = "";
lightNum += (i + 1);
aJson.addItemToArray(lightsArray, aJson.createItem(lightNum.c_str()));
}
return object;
}
unsigned int getLightMask() {
return lights;
}
// only used for scenes
String id;
private:
String name;
// use unsigned int to hold members of this group. 2 bytes -> supports up to 16 lights
unsigned int lights = 0;
// no need to hold the group type, only LightGroup is supported for API 1.4
};
void on(HandlerFunction fn, const String &wcUri, HTTPMethod method, char wildcard = '*') {
HTTP->addHandler(new WcFnRequestHandler(fn, wcUri, method, wildcard));
}
void descriptionFn() {
String str = "<root><specVersion><major>1</major><minor>0</minor></specVersion><URLBase>http://" + ipString + ":80/</URLBase><device><deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType><friendlyName>Philips hue (" + ipString + ")</friendlyName><manufacturer>Royal Philips Electronics</manufacturer><manufacturerURL>http://www.philips.com</manufacturerURL><modelDescription>Philips hue Personal Wireless Lighting</modelDescription><modelName>Philips hue bridge 2012</modelName><modelNumber>929000226503</modelNumber><modelURL>http://www.meethue.com</modelURL><serialNumber>"+macString+"</serialNumber><UDN>uuid:2f402f80-da50-11e1-9b23-"+macString+"</UDN><presentationURL>index.html</presentationURL><iconList><icon><mimetype>image/png</mimetype><height>48</height><width>48</width><depth>24</depth><url>hue_logo_0.png</url></icon><icon><mimetype>image/png</mimetype><height>120</height><width>120</width><depth>24</depth><url>hue_logo_3.png</url></icon></iconList></device></root>";
HTTP->send(200, "text/plain", str);
Serial.println(str);
}
void unimpFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
String str = "{}";
HTTP->send(200, "text/plain", str);
Serial.println(str);
}
aJsonObject *wrapWithSuccess(aJsonObject *body) {
aJsonObject *success = aJson.createObject();
aJson.addItemToObject(success, "success", body);
return success;
}
// targetBase is assumed to have a trailing slash (/)
aJsonObject *generateTargetPutResponse(aJsonObject *body, String targetBase) {
aJsonObject *root = aJson.createArray();
for (int i = 0; i < aJson.getArraySize(body); i++) {
aJsonObject *success = aJson.createObject();
aJson.addItemToArray(root, wrapWithSuccess(success));
aJsonObject *entry = aJson.getArrayItem(body, i);
String target = targetBase + entry->name;
switch (entry->type) {
case aJson_Boolean:
aJson.addBooleanToObject(success, target.c_str(), entry->valuebool);
break;
case aJson_Int:
aJson.addNumberToObject(success, target.c_str(), entry->valueint);
break;
case aJson_String:
aJson.addStringToObject(success, target.c_str(), entry->valuestring);
break;
case aJson_Float:
aJson.addNumberToObject(success, target.c_str(), entry->valuefloat);
break;
case aJson_Array: {
aJsonObject *xy = aJson.createArray();
aJson.addItemToObject(success, target.c_str(), xy);
for (int j = 0; j < aJson.getArraySize(entry); j++) {
aJson.addItemToArray(xy, aJson.createItem(aJson.getArrayItem(entry, j)->valuefloat));
}
break;
}
default:
break;
}
}
return root;
}
void addConfigJson(aJsonObject *config);
void sendJson(aJsonObject *config);
void sendUpdated();
void sendError(int type, String path, String description);
void configFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
switch (method) {
case HTTP_GET: {
aJsonObject *root;
root = aJson.createObject();
addConfigJson(root);
sendJson(root);
break;
}
case HTTP_PUT: {
Serial.print("Body: ");
Serial.println(HTTP->arg("plain"));
aJsonObject* body = aJson.parse(( char*) HTTP->arg("plain").c_str());
sendJson(generateTargetPutResponse(body, "/config/"));
aJson.deleteItem(body);
break;
}
default:
sendError(4, requestUri, "Config method not supported");
break;
}
}
void sendSuccess(String name, String value);
void authFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
// On the real bridge, the link button on the bridge must have been recently pressed for the command to execute successfully.
// We try to execute successfully regardless of a button for now.
sendSuccess("username", "api");
}
aJsonObject *getGroupJson();
aJsonObject *getSceneJson();
void addLightsJson(aJsonObject *config);
void wholeConfigFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
// Serial.println("Respond with complete json as in https://github.com/probonopd/ESP8266HueEmulator/wiki/Hue-API#get-all-information-about-the-bridge");
aJsonObject *root;
root = aJson.createObject();
// the default group 0 is never listed
aJson.addItemToObject(root, "groups", getGroupJson());
aJson.addItemToObject(root, "scenes", getSceneJson());
aJsonObject *config;
aJson.addItemToObject(root, "config", config = aJson.createObject());
addConfigJson(config);
aJsonObject *lights;
aJson.addItemToObject(root, "lights", lights = aJson.createObject());
addLightsJson(lights);
aJsonObject *schedules;
aJson.addItemToObject(root, "schedules", schedules = aJson.createObject());
aJsonObject *sensors;
aJson.addItemToObject(root, "sensors", sensors = aJson.createObject());
aJsonObject *rules;
aJson.addItemToObject(root, "rules", rules = aJson.createObject());
sendJson(root);
}
void sceneListingHandler();
void sceneCreationHandler(String body);
void scenesFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
switch (method) {
case HTTP_GET:
sceneListingHandler();
break;
case HTTP_POST:
sceneCreationHandler("");
break;
default:
sendError(4, requestUri, "Scene method not supported");
break;
}
}
int findSceneIndex(String id);
LightGroup *findScene(String id);
bool updateSceneSlot(int slot, String id, String body);
void sendSuccess(String text);
void sceneCreationHandler(String sceneId);
void scenesIdFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
String sceneId = handler->getWildCard(1);
LightGroup *scene = findScene(sceneId);
switch (method) {
case HTTP_GET:
if (scene) {
sendJson(scene->getSceneJson());
} else {
sendError(3, "/scenes/"+sceneId, "Cannot retrieve scene that does not exist");
}
break;
case HTTP_PUT:
// validate body, delete old group, create new group
sceneCreationHandler(sceneId);
// XXX not a valid response according to API
sendUpdated();
break;
case HTTP_DELETE:
if (scene) {
updateSceneSlot(findSceneIndex(sceneId), sceneId, "");
} else {
sendError(3, requestUri, "Cannot delete scene that does not exist");
}
sendSuccess(requestUri+" deleted");
break;
default:
sendError(4, requestUri, "Scene method not supported");
break;
}
}
void scenesIdLightFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
switch (method) {
case HTTP_PUT: {
Serial.print("Body: ");
Serial.println(HTTP->arg("plain"));
// XXX Do something with this information...
aJsonObject* body = aJson.parse(( char*) HTTP->arg("plain").c_str());
sendJson(generateTargetPutResponse(body, "/scenes/" + handler->getWildCard(1) + "/lightstates/" + handler->getWildCard(2) + "/"));
aJson.deleteItem(body);
break;
}
default:
sendError(4, requestUri, "Scene method not supported");
break;
}
}
void groupListingHandler();
void groupCreationHandler();
void groupsFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
switch (method) {
case HTTP_GET:
groupListingHandler();
break;
case HTTP_POST:
groupCreationHandler();
break;
default:
sendError(4, requestUri, "Group method not supported");
break;
}
}
LightGroup *lightGroups[16] = {nullptr, };
void groupCreationHandler(String sceneId);
bool updateGroupSlot(int slot, String body);
void groupsIdFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
String groupNumText = handler->getWildCard(1);
int groupNum = atoi(groupNumText.c_str()) - 1;
if ((groupNum == -1 && groupNumText != "0") || groupNum >= 16 || (groupNum >= 0 && !lightGroups[groupNum])) {
// error, invalid group number
sendError(3, requestUri, "Invalid group number");
return;
}
switch (method) {
case HTTP_GET:
if (groupNum != -1) {
sendJson(lightGroups[groupNum]->getJson());
} else {
aJsonObject *object = aJson.createObject();
aJson.addStringToObject(object, "name", "0");
aJsonObject *lightsArray = aJson.createArray();
aJson.addItemToObject(object, "lights", lightsArray);
for (int i = 0; i < MAX_LIGHT_HANDLERS; i++) {
if (!lightHandlers[i]) {
continue;
}
// add light to list
String lightNum = "";
lightNum += (i + 1);
aJson.addItemToArray(lightsArray, aJson.createItem(lightNum.c_str()));
}
sendJson(object);
}
break;
case HTTP_PUT:
// validate body, delete old group, create new group
updateGroupSlot(groupNum, HTTP->arg("plain"));
sendUpdated();
break;
case HTTP_DELETE:
updateGroupSlot(groupNum, "");
sendSuccess(requestUri+" deleted");
break;
default:
sendError(4, requestUri, "Group method not supported");
break;
}
}
void applyConfigToLightMask(unsigned int lights);
void groupsIdActionFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
if (method != HTTP_PUT) {
// error, only PUT allowed
sendError(4, requestUri, "Only PUT supported for groups/*/action");
return;
}
String groupNumText = handler->getWildCard(1);
int groupNum = atoi(groupNumText.c_str()) - 1;
if ((groupNum == -1 && groupNumText != "0") || groupNum >= 16 || (groupNum >= 0 && !lightGroups[groupNum])) {
// error, invalid group number
sendError(3, requestUri, "Invalid group number");
return;
}
// parse input as if for all lights
unsigned int lightMask;
if (groupNum == -1) {
lightMask == 0xFFFF;
} else {
lightMask = lightGroups[groupNum]->getLightMask();
}
// apply to group
applyConfigToLightMask(lightMask);
}
void lightsFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
switch (method) {
case HTTP_GET: {
// dump existing lights
aJsonObject *lights = aJson.createObject();
addLightsJson(lights);
sendJson(lights);
break;
}
case HTTP_POST:
// "start" a "search" for "new" lights
sendSuccess("/lights", "Searching for new devices");
break;
default:
sendError(4, requestUri, "Light method not supported");
break;
}
}
void addLightJson(aJsonObject* root, int numberOfTheLight, LightHandler *lightHandler);
void lightsIdFn(WcFnRequestHandler *whandler, String requestUri, HTTPMethod method) {
int numberOfTheLight = atoi(whandler->getWildCard(1).c_str()) - 1;
LightHandler *handler = LightService.getLightHandler(numberOfTheLight);
switch (method) {
case HTTP_GET: {
aJsonObject *root;
root = aJson.createObject();
addLightJson(root, numberOfTheLight, handler);
sendJson(root);
break;
}
case HTTP_PUT:
// XXX do something here
sendUpdated();
break;
default:
sendError(4, requestUri, "Light method not supported");
break;
}
}
void lightsIdStateFn(WcFnRequestHandler *whandler, String requestUri, HTTPMethod method) {
int numberOfTheLight = atoi(whandler->getWildCard(1).c_str()) - 1;
LightHandler *handler = LightService.getLightHandler(numberOfTheLight);
if (!handler) {
sendError(3, requestUri, "Requested light not available");
return;
}
switch (method) {
case HTTP_POST:
case HTTP_PUT: {
Serial.print("JSON Body:");
Serial.println(HTTP->arg("plain"));
aJsonObject* parsedRoot = aJson.parse(( char*) HTTP->arg("plain").c_str());
if (!parsedRoot) {
// unparseable json
sendError(2, requestUri, "Bad JSON body in request");
return;
}
HueLightInfo currentInfo = handler->getInfo(numberOfTheLight);
HueLightInfo newInfo;
if (!parseHueLightInfo(currentInfo, parsedRoot, &newInfo)) {
aJson.deleteItem(parsedRoot);
return;
}
handler->handleQuery(numberOfTheLight, newInfo, parsedRoot);
sendJson(generateTargetPutResponse(parsedRoot, "/lights/" + whandler->getWildCard(1) + "/state/"));
aJson.deleteItem(parsedRoot);
break;
}
default:
sendError(4, requestUri, "Light method not supported");
break;
}
}
void lightsNewFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) {
// dump empty object
aJsonObject *lights = aJson.createObject();
sendJson(lights);
}
void LightServiceClass::begin() {
begin(new ESP8266WebServer(80));
}
void LightServiceClass::begin(ESP8266WebServer *svr) {
HTTP = svr;
macString = String(WiFi.macAddress());
bridgeIDString = macString;
bridgeIDString.replace(":", "");
bridgeIDString = bridgeIDString.substring(0, 6) + "FFFE" + bridgeIDString.substring(6);
ipString = StringIPaddress(WiFi.localIP());
netmaskString = StringIPaddress(WiFi.subnetMask());
gatewayString = StringIPaddress(WiFi.gatewayIP());
Serial.print("Starting HTTP at ");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(80);
HTTP->on("/description.xml", HTTP_GET, descriptionFn);
on(configFn, "/api/*/config", HTTP_ANY);
on(configFn, "/api/config", HTTP_GET);
on(wholeConfigFn, "/api/*", HTTP_GET);
on(wholeConfigFn, "/api/", HTTP_GET);
on(authFn, "/api", HTTP_POST);
on(unimpFn, "/api/*/schedules", HTTP_GET);
on(unimpFn, "/api/*/rules", HTTP_GET);
on(unimpFn, "/api/*/sensors", HTTP_GET);
on(scenesFn, "/api/*/scenes", HTTP_ANY);
on(scenesIdFn, "/api/*/scenes/*", HTTP_ANY);
on(scenesIdLightFn, "/api/*/scenes/*/lightstates/*", HTTP_ANY);
on(scenesIdLightFn, "/api/*/scenes/*/lights/*/state", HTTP_ANY);
on(groupsFn, "/api/*/groups", HTTP_ANY);
on(groupsIdFn, "/api/*/groups/*", HTTP_ANY);
on(groupsIdActionFn, "/api/*/groups/*/action", HTTP_ANY);
on(lightsFn, "/api/*/lights", HTTP_ANY);
on(lightsNewFn, "/api/*/lights/new", HTTP_ANY);
on(lightsIdFn, "/api/*/lights/*", HTTP_ANY);
on(lightsIdStateFn, "/api/*/lights/*/state", HTTP_ANY);
HTTP->begin();
Serial.println("Starting SSDP...");
SSDP.begin();
SSDP.setSchemaURL((char*)"description.xml");
SSDP.setHTTPPort(80);
SSDP.setName((char*)"Philips hue clone");
SSDP.setSerialNumber(macString.c_str());
SSDP.setURL((char*)"index.html");
SSDP.setModelName((char*)"IpBridge");
SSDP.setModelNumber((char*)"0.1");
SSDP.setModelURL((char*)"http://www.meethue.com");
SSDP.setManufacturer((char*)"Royal Philips Electronics");
SSDP.setManufacturerURL((char*)"http://www.philips.com");
SSDP.setDeviceType((char*)"upnp:rootdevice");
SSDP.setMessageFormatCallback(ssdpMsgFormatCallback);
Serial.println("SSDP Started");
}
void LightServiceClass::update() {
HTTP->handleClient();
}
void sendJson(aJsonObject *root)
{
// Take aJsonObject and print it to Serial and to WiFi
// From https://github.com/pubnub/msp430f5529/blob/master/msp430f5529.ino
char *msgStr = aJson.print(root);
aJson.deleteItem(root);
Serial.println(millis());
Serial.println(msgStr);
HTTP->send(200, "text/plain", msgStr);
free(msgStr);
}
// ==============================================================================================================
// Color Conversion
// ==============================================================================================================
// TODO: Consider switching to something along the lines of
// https://github.com/patdie421/mea-edomus/blob/master/src/philipshue_color.c
// and/ or https://github.com/kayno/arduinolifx/blob/master/color.h
// for color coversions instead
// ==============================================================================================================
// Based on http://stackoverflow.com/questions/22564187/rgb-to-philips-hue-hsb
// The code is based on this brilliant note: https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/commit/f41091cf671e13fe8c32fcced12604cd31cceaf3
rgbcolor getXYtoRGB(float x, float y, int brightness_raw) {
float brightness = ((float)brightness_raw) / 255.0f;
float bright_y = brightness / y;
float X = x * bright_y;
float Z = (1 - x - y) * bright_y;
// convert to RGB (0.0-1.0) color space
float R = X * 1.4628067 - brightness * 0.1840623 - Z * 0.2743606;
float G = -X * 0.5217933 + brightness * 1.4472381 + Z * 0.0677227;
float B = X * 0.0349342 - brightness * 0.0968930 + Z * 1.2884099;
// apply inverse 2.2 gamma
float inv_gamma = 1.0 / 2.4;
float linear_delta = 0.055;
float linear_interval = 1 + linear_delta;
float r = R <= 0.0031308 ? 12.92 * R : (linear_interval) * pow(R, inv_gamma) - linear_delta;
float g = G <= 0.0031308 ? 12.92 * G : (linear_interval) * pow(G, inv_gamma) - linear_delta;
float b = B <= 0.0031308 ? 12.92 * B : (linear_interval) * pow(B, inv_gamma) - linear_delta;
return rgbcolor(r * COLOR_SATURATION,
g * COLOR_SATURATION,
b * COLOR_SATURATION);
}
int getHue(hsvcolor hsb) {
return hsb.h * 360 * 182.04;
}
int getSaturation(hsvcolor hsb) {
return hsb.s * COLOR_SATURATION;
}
rgbcolor getMirektoRGB(int mirek) {
int hectemp = 10000 / mirek;
int r, g, b;
if (hectemp <= 66) {
r = COLOR_SATURATION;
g = 99.4708025861 * log(hectemp) - 161.1195681661;
b = hectemp <= 19 ? 0 : (138.5177312231 * log(hectemp - 10) - 305.0447927307);
} else {
r = 329.698727446 * pow(hectemp - 60, -0.1332047592);
g = 288.1221695283 * pow(hectemp - 60, -0.0755148492);
b = COLOR_SATURATION;
}
r = r > COLOR_SATURATION ? COLOR_SATURATION : r;
g = g > COLOR_SATURATION ? COLOR_SATURATION : g;
b = b > COLOR_SATURATION ? COLOR_SATURATION : b;
return rgbcolor(r, g, b);
}
void sendError(int type, String path, String description) {
aJsonObject *root = aJson.createArray();
aJsonObject *errorContainer = aJson.createObject();
aJsonObject *errorObject = aJson.createObject();
aJson.addItemToObject(errorObject, "type", aJson.createItem(type));
aJson.addStringToObject(errorObject, "address", path.c_str());
aJson.addStringToObject(errorObject, "description", description.c_str());
aJson.addItemToObject(errorContainer, "error", errorObject);
aJson.addItemToArray(root, errorContainer);
sendJson(root);
}
void sendSuccess(String id, String value) {
aJsonObject *search = aJson.createArray();
aJsonObject *container = aJson.createObject();
aJson.addItemToArray(search, container);
aJsonObject *succeed = aJson.createObject();
aJson.addItemToObject(container, "success", succeed);
aJson.addStringToObject(succeed, id.c_str(), value.c_str());
sendJson(search);
}
void sendSuccess(String value) {
aJsonObject *search = aJson.createArray();
aJsonObject *container = aJson.createObject();
aJson.addItemToArray(search, container);
aJsonObject *succeed = aJson.createObject();
aJson.addStringToObject(container, "success", value.c_str());
sendJson(search);
}
void sendUpdated() {
Serial.println("Updated.");
HTTP->send(200, "text/plain", "Updated.");
}
bool parseHueLightInfo(HueLightInfo currentInfo, aJsonObject *parsedRoot, HueLightInfo *newInfo) {
*newInfo = currentInfo;
aJsonObject* onState = aJson.getObjectItem(parsedRoot, "on");
if (onState) {
newInfo->on = onState->valuebool;
}
// pull brightness
aJsonObject* briState = aJson.getObjectItem(parsedRoot, "bri");
if (briState) {
newInfo->brightness = briState->valueint;
}
// pull effect
aJsonObject* effectState = aJson.getObjectItem(parsedRoot, "effect");
if (effectState) {
const char *effect = effectState->valuestring;
if (!strcmp(effect, "colorloop")) {
newInfo->effect = EFFECT_COLORLOOP;
} else {
newInfo->effect = EFFECT_NONE;
}
}
// pull alert
aJsonObject* alertState = aJson.getObjectItem(parsedRoot, "alert");
if (alertState) {
const char *alert = alertState->valuestring;
if (!strcmp(alert, "select")) {
newInfo->alert = ALERT_SELECT;
} else if (!strcmp(alert, "lselect")) {
newInfo->alert = ALERT_LSELECT;
} else {
newInfo->alert = ALERT_NONE;
}
}
// pull transitiontime
aJsonObject* transitiontimeState = aJson.getObjectItem(parsedRoot, "transitiontime");
if (transitiontimeState) {
newInfo->transitionTime = transitiontimeState->valueint;
}
aJsonObject* hueState = aJson.getObjectItem(parsedRoot, "hue");
aJsonObject* satState = aJson.getObjectItem(parsedRoot, "sat");
aJsonObject* ctState = aJson.getObjectItem(parsedRoot, "ct");
aJsonObject* xyState = aJson.getObjectItem(parsedRoot, "xy");
if (xyState) {
aJsonObject* elem0 = aJson.getArrayItem(xyState, 0);
aJsonObject* elem1 = aJson.getArrayItem(xyState, 1);
if (!elem0 || !elem1) {
sendError(5, "/api/api/lights/?/state", "xy color coordinates incomplete");
return false;
}
hsvcolor hsb = getXYtoRGB(elem0->valuefloat, elem1->valuefloat, newInfo->brightness);
newInfo->hue = getHue(hsb);
newInfo->saturation = getSaturation(hsb);
} else if (ctState) {
int mirek = ctState->valueint;
if (mirek > 500 || mirek < 153) {
sendError(7, "/api/api/lights/?/state", "Invalid vaule for color temperature");
return false;
}
hsvcolor hsb = getMirektoRGB(mirek);
newInfo->hue = getHue(hsb);
newInfo->saturation = getSaturation(hsb);
} else if (hueState || satState) {
if (hueState) newInfo->hue = hueState->valueint;
if (satState) newInfo->saturation = satState->valueint;
}
return true;
}
void addLightJson(aJsonObject* root, int numberOfTheLight, LightHandler *lightHandler) {
if (!lightHandler) return;
String lightName = "" + (String) (numberOfTheLight + 1);
aJsonObject *light;
aJson.addItemToObject(root, lightName.c_str(), light = aJson.createObject());
aJson.addStringToObject(light, "type", "Extended color light"); // type of lamp (all "Extended colour light" for now)
aJson.addStringToObject(light, "name", ("Hue LightStrips " + (String) (numberOfTheLight + 1)).c_str()); // // the name as set through the web UI or app
aJson.addStringToObject(light, "uniqueid", ("AA:BB:CC:DD:EE:FF:00:11-" + (String) (numberOfTheLight + 1)).c_str());
aJson.addStringToObject(light, "modelid", "LST001"); // the model number
aJsonObject *state;
aJson.addItemToObject(light, "state", state = aJson.createObject());
HueLightInfo info = lightHandler->getInfo(numberOfTheLight);
aJson.addBooleanToObject(state, "on", info.on);
aJson.addNumberToObject(state, "hue", info.hue); // hs mode: the hue (expressed in ~deg*182.04)
aJson.addNumberToObject(state, "bri", info.brightness); // brightness between 0-254 (NB 0 is not off!)
aJson.addNumberToObject(state, "sat", info.saturation); // hs mode: saturation between 0-254
double numbers[2] = {0.0, 0.0};
aJson.addItemToObject(state, "xy", aJson.createFloatArray(numbers, 2)); // xy mode: CIE 1931 color co-ordinates
aJson.addNumberToObject(state, "ct", 500); // ct mode: color temp (expressed in mireds range 154-500)
aJson.addStringToObject(state, "alert", "none"); // 'select' flash the lamp once, 'lselect' repeat flash for 30s
aJson.addStringToObject(state, "effect", info.effect == EFFECT_COLORLOOP ? "colorloop" : "none");
aJson.addStringToObject(state, "colormode", "hs"); // the current color mode
aJson.addBooleanToObject(state, "reachable", true); // lamp can be seen by the hub
}
void addLightsJson(aJsonObject *lights) {
for (int i = 0; i < LightService.getLightsAvailable(); i++) {
addLightJson(lights, i, LightService.getLightHandler(i));
}
}
void addConfigJson(aJsonObject *root)
{
aJson.addStringToObject(root, "name", "hue emulator");
aJson.addStringToObject(root, "swversion", "81012917");
aJson.addStringToObject(root, "bridgeid", bridgeIDString.c_str());
aJson.addBooleanToObject(root, "portalservices", false);
aJson.addBooleanToObject(root, "linkbutton", true);
aJson.addStringToObject(root, "mac", macString.c_str());
aJson.addBooleanToObject(root, "dhcp", true);
aJson.addStringToObject(root, "ipaddress", ipString.c_str());
aJson.addStringToObject(root, "netmask", netmaskString.c_str());
aJson.addStringToObject(root, "gateway", gatewayString.c_str());
aJson.addStringToObject(root, "apiversion", "1.3.0");
aJsonObject *whitelist;
aJson.addItemToObject(root, "whitelist", whitelist = aJson.createObject());
aJsonObject *whitelistFirstEntry;
aJson.addItemToObject(whitelist, "api", whitelistFirstEntry = aJson.createObject());