@@ -23,46 +23,60 @@ long APIRequest(std::string method, std::string location, std::string URI, json*
23
23
24
24
CURL* curl = curl_easy_init ();
25
25
26
- // Set remote URL.
26
+ /* -------------------------------------------------------------*\
27
+ | Set remote URL. |
28
+ \*-------------------------------------------------------------*/
27
29
curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, method.c_str ());
28
30
curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
29
31
30
- // Don't bother trying IPv6, which would increase DNS resolution time.
32
+ /* -------------------------------------------------------------*\
33
+ | Don't bother trying IPv6, which would increase DNS resolution |
34
+ | time. |
35
+ \*-------------------------------------------------------------*/
31
36
curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
32
37
33
- // Don't wait forever, time out after 10 seconds.
38
+ /* -------------------------------------------------------------*\
39
+ | Don't wait forever, time out after 10 seconds. |
40
+ \*-------------------------------------------------------------*/
34
41
curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10 );
35
42
36
- // Follow HTTP redirects if necessary.
43
+ /* -------------------------------------------------------------*\
44
+ | Follow HTTP redirects if necessary. |
45
+ \*-------------------------------------------------------------*/
37
46
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
38
47
39
48
if (request_data)
40
49
{
41
- // LOG_DEBUG("[Nanoleaf] Sending data: %s", request_data->dump().c_str());
42
50
curl_easy_setopt (curl, CURLOPT_COPYPOSTFIELDS, request_data->dump ().c_str ());
43
51
}
44
52
45
- // Response information.
46
- long httpCode (0 );
47
- std::unique_ptr<std::string> httpData (new std::string ());
53
+ /* -------------------------------------------------------------*\
54
+ | Response information. |
55
+ \*-------------------------------------------------------------*/
56
+ long httpCode (0 );
57
+ std::unique_ptr<std::string> httpData (new std::string ());
48
58
49
- // Hook up data handling function.
59
+ /* -------------------------------------------------------------*\
60
+ | Hook up data handling function. |
61
+ \*-------------------------------------------------------------*/
50
62
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
51
63
52
- /* ---------------------------------------------------------*\
53
- | Hook up data container (will be passed as the last |
54
- | parameter to the callback handling function). Can be any |
55
- | pointer type, since it will internally be passed as a |
56
- | void pointer. |
57
- \*---------------------------------------------------------*/
64
+ /* -------------------------------------------------------------*\
65
+ | Hook up data container (will be passed as the last parameter |
66
+ | to the callback handling function). Can be any pointer type, |
67
+ | since it will internally be passed as a void pointer. |
68
+ \*-------------------------------------------------------------*/
58
69
curl_easy_setopt (curl, CURLOPT_WRITEDATA, httpData.get ());
59
70
60
- // Run our HTTP GET command, capture the HTTP response code, and clean up.
71
+ /* -------------------------------------------------------------*\
72
+ | Run our HTTP GET command, capture the HTTP response code, and |
73
+ | clean up. |
74
+ \*-------------------------------------------------------------*/
61
75
curl_easy_perform (curl);
62
76
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpCode);
63
77
curl_easy_cleanup (curl);
64
78
65
- if ( httpCode/ 100 == 2 )
79
+ if (( httpCode / 100 ) == 2 )
66
80
{
67
81
if (response_data)
68
82
{
@@ -79,22 +93,22 @@ long APIRequest(std::string method, std::string location, std::string URI, json*
79
93
80
94
NanoleafController::NanoleafController (std::string a_address, int a_port, std::string a_auth_token)
81
95
{
82
- address = a_address;
83
- port = a_port;
84
- auth_token = a_auth_token;
85
- location = address+ " :" + std::to_string (port);
96
+ address = a_address;
97
+ port = a_port;
98
+ auth_token = a_auth_token;
99
+ location = address + " :" + std::to_string (port);
86
100
87
101
json data;
88
102
if (APIRequest (" GET" , location, " /api/v1/" +auth_token, nullptr , &data) == 200 )
89
103
{
90
- name = data[" name" ];
91
- serial = data[" serialNo" ];
92
- manufacturer = data[" manufacturer" ];
93
- firmware_version = data[" firmwareVersion" ];
94
- model = data[" model" ];
104
+ name = data[" name" ];
105
+ serial = data[" serialNo" ];
106
+ manufacturer = data[" manufacturer" ];
107
+ firmware_version = data[" firmwareVersion" ];
108
+ model = data[" model" ];
95
109
96
- brightness = data[" state" ][" brightness" ][" value" ];
97
- selectedEffect = data[" effects" ][" select" ];
110
+ brightness = data[" state" ][" brightness" ][" value" ];
111
+ selectedEffect = data[" effects" ][" select" ];
98
112
99
113
for (json::const_iterator it = data[" effects" ][" effectsList" ].begin (); it != data[" effects" ][" effectsList" ].end (); ++it)
100
114
{
@@ -131,46 +145,52 @@ void NanoleafController::Unpair(std::string address, int port, std::string auth_
131
145
{
132
146
const std::string location = address+" :" +std::to_string (port);
133
147
134
- // We really don't care if this fails.
148
+ /* -------------------------------------------------------------*\
149
+ | We really don't care if this fails. |
150
+ \*-------------------------------------------------------------*/
135
151
APIRequest (" DELETE" , location, " /api/v1/" +auth_token, nullptr , nullptr );
136
152
}
137
153
138
154
void NanoleafController::UpdateLEDs (std::vector<RGBColor>& colors)
139
155
{
140
- // Requires StartExternalControl() to have been called prior.
156
+ /* -------------------------------------------------------------*\
157
+ | Requires StartExternalControl() to have been called prior. |
158
+ \*-------------------------------------------------------------*/
141
159
142
160
if (model == NANOLEAF_LIGHT_PANELS_MODEL)
143
161
{
144
- uint8_t size = panel_ids.size ();
162
+ uint8_t size = panel_ids.size ();
145
163
146
- uint8_t * message = (uint8_t *)malloc (size*7 +6 +1 );
164
+ uint8_t * message = (uint8_t *)malloc (size*7 +6 +1 );
147
165
148
- message[0 ] = (uint8_t )size;
166
+ message[0 ] = (uint8_t )size;
149
167
150
- for (int i = 0 ; i < size; i++)
168
+ for (int i = 0 ; i < size; i++)
151
169
{
152
- message[7 *i+ 0 + 1 ] = (uint8_t )panel_ids[i];
153
- message[7 *i+ 1 + 1 ] = (uint8_t )1 ;
154
- message[7 *i+ 2 + 1 ] = (uint8_t )RGBGetRValue (colors[i]);
155
- message[7 *i+ 3 + 1 ] = (uint8_t )RGBGetGValue (colors[i]);
156
- message[7 *i+ 4 + 1 ] = (uint8_t )RGBGetBValue (colors[i]);
157
- message[7 *i+ 5 + 1 ] = (uint8_t )0 ;
158
- message[7 *i+ 6 + 1 ] = (uint8_t )0 ;
170
+ message[( 7 * i) + 0 + 1 ] = (uint8_t )panel_ids[i];
171
+ message[( 7 * i) + 1 + 1 ] = (uint8_t )1 ;
172
+ message[( 7 * i) + 2 + 1 ] = (uint8_t )RGBGetRValue (colors[i]);
173
+ message[( 7 * i) + 3 + 1 ] = (uint8_t )RGBGetGValue (colors[i]);
174
+ message[( 7 * i) + 4 + 1 ] = (uint8_t )RGBGetBValue (colors[i]);
175
+ message[( 7 * i) + 5 + 1 ] = (uint8_t )0 ;
176
+ message[( 7 * i) + 6 + 1 ] = (uint8_t )0 ;
159
177
}
160
178
161
179
external_control_socket.udp_write (reinterpret_cast <char *>(message), size*7 +6 +1 );
162
180
}
163
181
else if (model == NANOLEAF_CANVAS_MODEL)
164
182
{
165
- // Insert V2 protocol implementation here.
183
+ /* ---------------------------------------------------------*\
184
+ | Insert V2 protocol implementation here. |
185
+ \*---------------------------------------------------------*/
166
186
}
167
187
}
168
188
169
189
void NanoleafController::StartExternalControl ()
170
190
{
171
191
json request;
172
- request[" write" ][" command" ] = " display" ;
173
- request[" write" ][" animType" ] = " extControl" ;
192
+ request[" write" ][" command" ] = " display" ;
193
+ request[" write" ][" animType" ] = " extControl" ;
174
194
175
195
if (model == NANOLEAF_LIGHT_PANELS_MODEL)
176
196
{
@@ -182,7 +202,7 @@ void NanoleafController::StartExternalControl()
182
202
}
183
203
184
204
json response;
185
- if (APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /effects" , &request, &response)/ 100 == 2 )
205
+ if (( APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /effects" , &request, &response) / 100 ) == 2 )
186
206
{
187
207
external_control_socket.udp_client (response[" streamControlIpAddr" ].get <std::string>().c_str (), std::to_string (response[" streamControlPort" ].get <int >()).c_str ());
188
208
@@ -194,7 +214,8 @@ void NanoleafController::SelectEffect(std::string effect_name)
194
214
{
195
215
json request;
196
216
request[" select" ] = effect_name;
197
- if (APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /effects" , &request)/100 == 2 )
217
+
218
+ if ((APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /effects" , &request) / 100 ) == 2 )
198
219
{
199
220
selectedEffect = effect_name;
200
221
}
@@ -204,8 +225,59 @@ void NanoleafController::SetBrightness(int a_brightness)
204
225
{
205
226
json request;
206
227
request[" brightness" ][" value" ] = a_brightness;
207
- if (APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /state" , &request)/100 == 2 )
228
+
229
+ if ((APIRequest (" PUT" , location, " /api/v1/" +auth_token+" /state" , &request) / 100 ) == 2 )
208
230
{
209
231
brightness = a_brightness;
210
232
}
211
233
}
234
+
235
+ std::string NanoleafController::GetAuthToken ()
236
+ {
237
+ return auth_token;
238
+ };
239
+
240
+ std::string NanoleafController::GetName ()
241
+ {
242
+ return name;
243
+ };
244
+
245
+ std::string NanoleafController::GetSerial ()
246
+ {
247
+ return serial;
248
+ };
249
+
250
+ std::string NanoleafController::GetManufacturer ()
251
+ {
252
+ return manufacturer;
253
+ };
254
+
255
+ std::string NanoleafController::GetFirmwareVersion ()
256
+ {
257
+ return firmware_version;
258
+ };
259
+
260
+ std::string NanoleafController::GetModel ()
261
+ {
262
+ return model;
263
+ };
264
+
265
+ std::vector<std::string>& NanoleafController::GetEffects ()
266
+ {
267
+ return effects;
268
+ };
269
+
270
+ std::vector<int >& NanoleafController::GetPanelIds ()
271
+ {
272
+ return panel_ids;
273
+ };
274
+
275
+ std::string NanoleafController::GetSelectedEffect ()
276
+ {
277
+ return selectedEffect;
278
+ };
279
+
280
+ int NanoleafController::GetBrightness ()
281
+ {
282
+ return brightness;
283
+ };
0 commit comments