WLED/wled00/wled_hue.cpp

207 lines
4.9 KiB
C++
Raw Normal View History

#include "wled_hue.h"
#include "wled.h"
2020-03-25 10:14:23 +01:00
#include "wled_colors.h"
#include "wled_eeprom.h"
#ifndef WLED_DISABLE_HUESYNC
void handleHue()
{
2019-02-18 22:34:21 +01:00
if (hueReceived)
{
2020-02-22 16:17:32 +01:00
colorUpdated(NOTIFIER_CALL_MODE_HUE); hueReceived = false;
2019-02-18 22:34:21 +01:00
if (hueStoreAllowed && hueNewKey)
{
2019-02-18 22:34:21 +01:00
saveSettingsToEEPROM(); //save api key
hueStoreAllowed = false;
hueNewKey = false;
}
}
2019-10-18 14:06:07 +02:00
if (!WLED_CONNECTED || hueClient == nullptr || millis() - hueLastRequestSent < huePollIntervalMs) return;
hueLastRequestSent = millis();
if (huePollingEnabled)
{
reconnectHue();
} else {
hueClient->close();
if (hueError == HUE_ERROR_ACTIVE) hueError = HUE_ERROR_INACTIVE;
2019-10-18 14:06:07 +02:00
}
}
2019-02-18 22:34:21 +01:00
void reconnectHue()
{
2019-10-18 13:26:39 +02:00
if (!WLED_CONNECTED || !huePollingEnabled) return;
2019-02-18 22:34:21 +01:00
DEBUG_PRINTLN("Hue reconnect");
if (hueClient == nullptr) {
hueClient = new AsyncClient();
hueClient->onConnect(&onHueConnect, hueClient);
hueClient->onData(&onHueData, hueClient);
hueClient->onError(&onHueError, hueClient);
hueAuthRequired = (strlen(hueApiKey)<20);
}
2019-02-18 22:34:21 +01:00
hueClient->connect(hueIP, 80);
}
void onHueError(void* arg, AsyncClient* client, int8_t error)
{
DEBUG_PRINTLN("Hue err");
hueError = HUE_ERROR_TIMEOUT;
2019-02-18 22:34:21 +01:00
}
void onHueConnect(void* arg, AsyncClient* client)
{
DEBUG_PRINTLN("Hue connect");
sendHuePoll();
}
void sendHuePoll()
{
if (hueClient == nullptr || !hueClient->connected()) return;
String req = "";
if (hueAuthRequired)
{
req += F("POST /api HTTP/1.1\r\nHost: ");
2019-02-18 22:34:21 +01:00
req += hueIP.toString();
req += F("\r\nContent-Length: 25\r\n\r\n{\"devicetype\":\"wled#esp\"}");
2019-02-18 22:34:21 +01:00
} else
{
req += "GET /api/";
req += hueApiKey;
req += "/lights/" + String(huePollLightId);
req += F(" HTTP/1.1\r\nHost: ");
2019-02-18 22:34:21 +01:00
req += hueIP.toString();
req += "\r\n\r\n";
}
2019-02-18 22:34:21 +01:00
hueClient->add(req.c_str(), req.length());
hueClient->send();
hueLastRequestSent = millis();
}
void onHueData(void* arg, AsyncClient* client, void *data, size_t len)
{
if (!len) return;
char* str = (char*)data;
DEBUG_PRINTLN(hueApiKey);
DEBUG_PRINTLN(str);
//only get response body
str = strstr(str,"\r\n\r\n");
if (str == nullptr) return;
str += 4;
StaticJsonDocument<512> root;
if (str[0] == '[') //is JSON array
{
auto error = deserializeJson(root, str);
if (error)
{
hueError = HUE_ERROR_JSON_PARSING; return;
}
int hueErrorCode = root[0]["error"]["type"];
if (hueErrorCode)//hue bridge returned error
{
hueError = hueErrorCode;
switch (hueErrorCode)
{
case 1: hueAuthRequired = true; break; //Unauthorized user
case 3: huePollingEnabled = false; break; //Invalid light ID
case 101: hueAuthRequired = true; break; //link button not presset
}
return;
}
if (hueAuthRequired)
{
const char* apikey = root[0]["success"]["username"];
2019-03-05 10:59:15 +01:00
if (apikey != nullptr && strlen(apikey) < sizeof(hueApiKey))
{
2019-03-05 10:59:15 +01:00
strcpy(hueApiKey, apikey);
hueAuthRequired = false;
hueNewKey = true;
}
}
2019-02-18 22:34:21 +01:00
return;
}
2019-02-18 22:34:21 +01:00
//else, assume it is JSON object, look for state and only parse that
str = strstr(str,"state");
if (str == nullptr) return;
str = strstr(str,"{");
auto error = deserializeJson(root, str);
if (error)
{
hueError = HUE_ERROR_JSON_PARSING; return;
}
float hueX=0, hueY=0;
uint16_t hueHue=0, hueCt=0;
byte hueBri=0, hueSat=0, hueColormode=0;
if (root["on"]) {
if (root.containsKey("bri")) //Dimmable device
{
hueBri = root["bri"];
hueBri++;
const char* cm =root["colormode"];
if (cm != nullptr) //Color device
{
if (strstr(cm,"ct") != nullptr) //ct mode
{
hueCt = root["ct"];
hueColormode = 3;
} else if (strstr(cm,"xy") != nullptr) //xy mode
{
hueX = root["xy"][0]; // 0.5051
hueY = root["xy"][1]; // 0.4151
hueColormode = 1;
} else //hs mode
{
hueHue = root["hue"];
hueSat = root["sat"];
hueColormode = 2;
}
}
} else //On/Off device
{
hueBri = briLast;
}
} else
{
hueBri = 0;
}
hueError = HUE_ERROR_ACTIVE;
//apply vals
if (hueBri != hueBriLast)
{
if (hueApplyOnOff)
{
if (hueBri==0) {bri = 0;}
else if (bri==0 && hueBri>0) bri = briLast;
}
if (hueApplyBri)
{
if (hueBri>0) bri = hueBri;
}
hueBriLast = hueBri;
}
if (hueApplyColor)
{
switch(hueColormode)
{
case 1: if (hueX != hueXLast || hueY != hueYLast) colorXYtoRGB(hueX,hueY,col); hueXLast = hueX; hueYLast = hueY; break;
case 2: if (hueHue != hueHueLast || hueSat != hueSatLast) colorHStoRGB(hueHue,hueSat,col); hueHueLast = hueHue; hueSatLast = hueSat; break;
case 3: if (hueCt != hueCtLast) colorCTtoRGB(hueCt,col); hueCtLast = hueCt; break;
}
}
2019-02-18 22:34:21 +01:00
hueReceived = true;
}
#else
void handleHue(){}
void reconnectHue(){}
#endif