|
| 1 | +/*-----------------------------------------*\ |
| 2 | +| NanoleafController.cpp | |
| 3 | +| | |
| 4 | +| API Interface for Nanoleaf devices | |
| 5 | +| | |
| 6 | +| Nikita Rushmanov 01/13/2022 | |
| 7 | +\*-----------------------------------------*/ |
| 8 | + |
| 9 | +#include "NanoleafController.h" |
| 10 | +#include "LogManager.h" |
| 11 | +#include <curl/curl.h> |
| 12 | + |
| 13 | +std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t num, std::string* out) |
| 14 | +{ |
| 15 | + const std::size_t totalBytes(size * num); |
| 16 | + out->append(in, totalBytes); |
| 17 | + return totalBytes; |
| 18 | +} |
| 19 | + |
| 20 | +long APIRequest(std::string method, std::string location, std::string URI, json* request_data = nullptr, json* response_data = nullptr) |
| 21 | +{ |
| 22 | + const std::string url("http://"+location+URI); |
| 23 | + |
| 24 | + CURL* curl = curl_easy_init(); |
| 25 | + |
| 26 | + // Set remote URL. |
| 27 | + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()); |
| 28 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 29 | + |
| 30 | + // Don't bother trying IPv6, which would increase DNS resolution time. |
| 31 | + curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
| 32 | + |
| 33 | + // Don't wait forever, time out after 10 seconds. |
| 34 | + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); |
| 35 | + |
| 36 | + // Follow HTTP redirects if necessary. |
| 37 | + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 38 | + |
| 39 | + if(request_data) |
| 40 | + { |
| 41 | + // LOG_DEBUG("[Nanoleaf] Sending data: %s", request_data->dump().c_str()); |
| 42 | + curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request_data->dump().c_str()); |
| 43 | + } |
| 44 | + |
| 45 | + // Response information. |
| 46 | + long httpCode(0); |
| 47 | + std::unique_ptr<std::string> httpData(new std::string()); |
| 48 | + |
| 49 | + // Hook up data handling function. |
| 50 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); |
| 51 | + |
| 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 | + \*---------------------------------------------------------*/ |
| 58 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get()); |
| 59 | + |
| 60 | + // Run our HTTP GET command, capture the HTTP response code, and clean up. |
| 61 | + curl_easy_perform(curl); |
| 62 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); |
| 63 | + curl_easy_cleanup(curl); |
| 64 | + |
| 65 | + if (httpCode/100 == 2) |
| 66 | + { |
| 67 | + if(response_data) |
| 68 | + { |
| 69 | + *response_data = json::parse(*httpData.get()); |
| 70 | + } |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method, url); |
| 75 | + } |
| 76 | + |
| 77 | + return httpCode; |
| 78 | +} |
| 79 | + |
| 80 | +NanoleafController::NanoleafController(std::string a_address, int a_port, std::string a_auth_token) |
| 81 | +{ |
| 82 | + address = a_address; |
| 83 | + port = a_port; |
| 84 | + auth_token = a_auth_token; |
| 85 | + location = address+":"+std::to_string(port); |
| 86 | + |
| 87 | + json data; |
| 88 | + if(APIRequest("GET", location, "/api/v1/"+auth_token, nullptr, &data) == 200) |
| 89 | + { |
| 90 | + name = data["name"]; |
| 91 | + serial = data["serialNo"]; |
| 92 | + manufacturer = data["manufacturer"]; |
| 93 | + firmware_version = data["firmwareVersion"]; |
| 94 | + model = data["model"]; |
| 95 | + |
| 96 | + brightness = data["state"]["brightness"]["value"]; |
| 97 | + selectedEffect = data["effects"]["select"]; |
| 98 | + |
| 99 | + for(json::const_iterator it = data["effects"]["effectsList"].begin(); it != data["effects"]["effectsList"].end(); ++it) |
| 100 | + { |
| 101 | + effects.push_back(it.value()); |
| 102 | + } |
| 103 | + |
| 104 | + for(json::const_iterator it = data["panelLayout"]["layout"]["positionData"].begin(); it != data["panelLayout"]["layout"]["positionData"].end(); ++it) |
| 105 | + { |
| 106 | + panel_ids.push_back(it.value()["panelId"].get<int>()); |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw std::exception(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +std::string NanoleafController::Pair(std::string address, int port) |
| 116 | +{ |
| 117 | + const std::string location = address+":"+std::to_string(port); |
| 118 | + |
| 119 | + json data; |
| 120 | + if(APIRequest("POST", location, "/api/v1/new", nullptr, &data) == 200) |
| 121 | + { |
| 122 | + return data["auth_token"]; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + throw std::exception(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void NanoleafController::Unpair(std::string address, int port, std::string auth_token) |
| 131 | +{ |
| 132 | + const std::string location = address+":"+std::to_string(port); |
| 133 | + |
| 134 | + // We really don't care if this fails. |
| 135 | + APIRequest("DELETE", location, "/api/v1/"+auth_token, nullptr, nullptr); |
| 136 | +} |
| 137 | + |
| 138 | +void NanoleafController::UpdateLEDs(std::vector<RGBColor>& colors) |
| 139 | +{ |
| 140 | + // Requires StartExternalControl() to have been called prior. |
| 141 | + |
| 142 | + if(model == NANOLEAF_LIGHT_PANELS_MODEL) |
| 143 | + { |
| 144 | + uint8_t size = panel_ids.size(); |
| 145 | + |
| 146 | + uint8_t* message = (uint8_t*)malloc(size*7+6+1); |
| 147 | + |
| 148 | + message[0] = (uint8_t)size; |
| 149 | + |
| 150 | + for (int i = 0; i < size; i++) |
| 151 | + { |
| 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; |
| 159 | + } |
| 160 | + |
| 161 | + external_control_socket.udp_write(reinterpret_cast<char*>(message), size*7+6+1); |
| 162 | + } |
| 163 | + else if(model == NANOLEAF_CANVAS_MODEL) |
| 164 | + { |
| 165 | + // Insert V2 protocol implementation here. |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +void NanoleafController::StartExternalControl() |
| 170 | +{ |
| 171 | + json request; |
| 172 | + request["write"]["command"] = "display"; |
| 173 | + request["write"]["animType"] = "extControl"; |
| 174 | + |
| 175 | + if(model == NANOLEAF_LIGHT_PANELS_MODEL) |
| 176 | + { |
| 177 | + request["write"]["extControlVersion"] = "v1"; |
| 178 | + } |
| 179 | + else if(model == NANOLEAF_CANVAS_MODEL) |
| 180 | + { |
| 181 | + request["write"]["extControlVersion"] = "v2"; |
| 182 | + } |
| 183 | + |
| 184 | + json response; |
| 185 | + if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request, &response)/100 == 2) |
| 186 | + { |
| 187 | + external_control_socket.udp_client(response["streamControlIpAddr"].get<std::string>().c_str(), std::to_string(response["streamControlPort"].get<int>()).c_str()); |
| 188 | + |
| 189 | + selectedEffect = NANOLEAF_DIRECT_MODE_EFFECT_NAME; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +void NanoleafController::SelectEffect(std::string effect_name) |
| 194 | +{ |
| 195 | + json request; |
| 196 | + request["select"] = effect_name; |
| 197 | + if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request)/100 == 2) |
| 198 | + { |
| 199 | + selectedEffect = effect_name; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +void NanoleafController::SetBrightness(int a_brightness) |
| 204 | +{ |
| 205 | + json request; |
| 206 | + request["brightness"]["value"] = a_brightness; |
| 207 | + if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/state", &request)/100 == 2) |
| 208 | + { |
| 209 | + brightness = a_brightness; |
| 210 | + } |
| 211 | +} |
0 commit comments