-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurls.h
203 lines (170 loc) · 7.66 KB
/
urls.h
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
#ifndef _URLS_H
#define _URLS_H
#include "httpServer.h"
#include <map>
#include <fstream>
#include "json.h"
#include <string>
#include "apa102.h"
#include "PlayBack/playBack.h"
#include "PlayBack/playBackItemSolid.h"
#include "PlayBack/playBackItemSlide.h"
#include "PlayBack/playBackItemFade.h"
#include "PlayBack/colorParse.h"
class Urls{
private:
void AddSolidColor(json::Object input) {
if (input.hasKey(std::string("time"))) {
this->playBack->push(new PlayBackItemSolid(this->apa102,
ColorParse::Parse(input.get(std::string("color"))->getObject()),
input.get(std::string("time"))->getDouble()
));
}
else
{
this->playBack->push(new PlayBackItemSolid(this->apa102,
ColorParse::Parse(input.get(std::string("color"))->getObject()),
-1));
}
}
private:
void root(HttpConnection* httpConnection) {
httpConnection->setResponseContentType(std::string("text/html"));
json::Object o;
o.set(std::string("count"), this->apa102->getCount());
o.set(std::string("ledStatus"), this->apa102->getLedStatus());
httpConnection->setResponseData(o.json());
}
void clear(HttpConnection* httpConnection) {
try {
this->playBack->clear();
httpConnection->setResponseContentType(std::string("text/html"));
json::Object o;
o.set(std::string("count"), this->apa102->getCount());
o.set(std::string("ledStatus"), this->apa102->getLedStatus());
httpConnection->setResponseData(o.json());
} catch(...){
httpConnection->setResponseHttpStatus(500);
}
}
void solidColor(HttpConnection* httpConnection) {
try {
printf("Post data = %s\n", httpConnection->postValue.c_str());
json::Object input;
input.parse(httpConnection->postValue);
this->AddSolidColor(input);
httpConnection->setResponseContentType(std::string("text/html"));
json::Object o;
o.set(std::string("count"), this->apa102->getCount());
o.set(std::string("ledStatus"), this->apa102->getLedStatus());
o.set(std::string("inputCount"), (int)input.count());
httpConnection->setResponseData(o.json());
} catch(...){
httpConnection->setResponseHttpStatus(500);
}
}
void solidColorAndClear(HttpConnection* httpConnection) {
try {
printf("Post data = %s\n", httpConnection->postValue.c_str());
json::Object input;
input.parse(httpConnection->postValue);
this->playBack->clear();
this->AddSolidColor(input);
httpConnection->setResponseContentType(std::string("text/html"));
json::Object o;
o.set(std::string("count"), this->apa102->getCount());
o.set(std::string("ledStatus"), this->apa102->getLedStatus());
o.set(std::string("inputCount"), (int)input.count());
httpConnection->setResponseData(o.json());
} catch(...){
httpConnection->setResponseHttpStatus(500);
}
}
void fadeColor(HttpConnection* httpConnection, bool clear) {
try {
printf("Post data = %s\n", httpConnection->postValue.c_str());
json::Object input;
input.parse(httpConnection->postValue);
if (clear) {
this->playBack->clear();
}
PlayBackItemFade *item = new PlayBackItemFade(this->apa102,
ColorParse::Parse(input.get(std::string("from"))->getObject()),
ColorParse::Parse(input.get(std::string("to"))->getObject()),
1,
-1
);
this->playBack->push(item);
httpConnection->setResponseContentType(std::string("text/html"));
json::Object o;
o.set(std::string("count"), this->apa102->getCount());
o.set(std::string("ledStatus"), this->apa102->getLedStatus());
o.set(std::string("inputCount"), (int)input.count());
httpConnection->setResponseData(o.json());
} catch(...) {
httpConnection->setResponseHttpStatus(500);
}
}
void fadeColor(HttpConnection* httpConnection) {
this->fadeColor(httpConnection, false);
}
void fadeColorAndClear(HttpConnection* httpConnection) {
this->fadeColor(httpConnection, true);
}
private:
// static const
std::map<std::string, void (Urls::*)(HttpConnection*)> urlMaps = {
{std::string("/"), &Urls::root }
,{std::string("/clear"), &Urls::clear}
,{std::string("/solidColor"), &Urls::solidColor}
,{std::string("/fadeColor"), &Urls::fadeColor}
,{std::string("/solidColorAndClear"), &Urls::solidColorAndClear}
,{std::string("/fadeColorAndClear"), &Urls::fadeColorAndClear}
};
APA102 *apa102;
PlayBack *playBack;
public:
Urls(APA102* apa102, PlayBack* playBack){
this->apa102 = apa102;
this->playBack = playBack;
}
void httpResponse(HttpConnection* httpConnection)
{
printf("--path = %s \n", httpConnection->path.c_str());
auto find = this->urlMaps.find(httpConnection->path);
if (find != this->urlMaps.end()){
(*this.*find->second)(httpConnection);
return;
}
if (httpConnection->path.compare("/favicon.ico") == 0) {
httpConnection->setResponseContentType(std::string("image/ico"));
std::ifstream is( "www/favicon.ico", std::ios::binary );
if (is) {
std::string data;
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
// std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer, length);
/* if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";*/
is.close();
// ...buffer contains the entire file...
httpConnection->setResponseData(std::string(buffer, length));
delete[] buffer;
return;
}
httpConnection->setResponseHttpStatus(404);
return;
}
httpConnection->setResponseContentType(std::string("text/html"));
std::string str = std::string("ok\n");
httpConnection->setResponseData(str);
}
};
#endif