mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-23 00:27:06 +01:00
Merge branch 'alexa' into dev
This commit is contained in:
@@ -11,13 +11,13 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
#include <fauxmoESP.h>
|
||||
fauxmoESP alexa;
|
||||
|
||||
struct AlexaDevChange {
|
||||
AlexaDevChange(unsigned char device_id, bool state) : device_id(device_id), state(state) {};
|
||||
unsigned char device_id = 0;
|
||||
bool state = false;
|
||||
};
|
||||
#include <queue>
|
||||
static std::queue<AlexaDevChange> _alexa_dev_changes;
|
||||
typedef struct {
|
||||
unsigned char device_id;
|
||||
bool state;
|
||||
unsigned char value;
|
||||
} alexa_queue_element_t;
|
||||
static std::queue<alexa_queue_element_t> _alexa_queue;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// ALEXA
|
||||
@@ -29,15 +29,19 @@ bool _alexaWebSocketOnReceive(const char * key, JsonVariant& value) {
|
||||
|
||||
void _alexaWebSocketOnSend(JsonObject& root) {
|
||||
root["alexaVisible"] = 1;
|
||||
root["alexaEnabled"] = getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1;
|
||||
root["alexaEnabled"] = alexaEnabled();
|
||||
}
|
||||
|
||||
void _alexaConfigure() {
|
||||
alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
|
||||
alexa.enable(wifiConnected() && alexaEnabled());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
bool alexaEnabled() {
|
||||
return (getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
|
||||
}
|
||||
|
||||
void alexaSetup() {
|
||||
|
||||
// Backwards compatibility
|
||||
@@ -46,32 +50,56 @@ void alexaSetup() {
|
||||
// Load & cache settings
|
||||
_alexaConfigure();
|
||||
|
||||
#if WEB_SUPPORT
|
||||
// Uses hostname as base name for all devices
|
||||
// TODO: use custom switch name when available
|
||||
String hostname = getSetting("hostname");
|
||||
|
||||
// Websockets
|
||||
wsOnSendRegister(_alexaWebSocketOnSend);
|
||||
wsOnAfterParseRegister(_alexaConfigure);
|
||||
wsOnReceiveRegister(_alexaWebSocketOnReceive);
|
||||
// Lights
|
||||
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
|
||||
|
||||
// Global switch
|
||||
alexa.addDevice(hostname.c_str());
|
||||
|
||||
// For each channel
|
||||
for (unsigned char i = 1; i <= lightChannels(); i++) {
|
||||
alexa.addDevice((hostname + " " + i).c_str());
|
||||
}
|
||||
|
||||
// Relays
|
||||
#else
|
||||
|
||||
unsigned int relays = relayCount();
|
||||
if (relays == 1) {
|
||||
alexa.addDevice(hostname.c_str());
|
||||
} else {
|
||||
for (unsigned int i=1; i<=relays; i++) {
|
||||
alexa.addDevice((hostname + " " + i).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
unsigned int relays = relayCount();
|
||||
String hostname = getSetting("hostname");
|
||||
if (relays == 1) {
|
||||
alexa.addDevice(hostname.c_str());
|
||||
} else {
|
||||
for (unsigned int i=0; i<relays; i++) {
|
||||
alexa.addDevice((hostname + "_" + i).c_str());
|
||||
}
|
||||
}
|
||||
// Websockets
|
||||
#if WEB_SUPPORT
|
||||
wsOnSendRegister(_alexaWebSocketOnSend);
|
||||
wsOnAfterParseRegister(_alexaConfigure);
|
||||
wsOnReceiveRegister(_alexaWebSocketOnReceive);
|
||||
#endif
|
||||
|
||||
alexa.onSetState([&](unsigned char device_id, const char * name, bool state) {
|
||||
AlexaDevChange change(device_id, state);
|
||||
_alexa_dev_changes.push(change);
|
||||
// Register wifi callback
|
||||
wifiRegister([](justwifi_messages_t code, char * parameter) {
|
||||
if ((MESSAGE_CONNECTED == code) || (MESSAGE_DISCONNECTED == code)) {
|
||||
_alexaConfigure();
|
||||
}
|
||||
});
|
||||
|
||||
alexa.onGetState([](unsigned char device_id, const char * name) {
|
||||
return relayStatus(device_id);
|
||||
// Callback
|
||||
alexa.onSetState([&](unsigned char device_id, const char * name, bool state, unsigned char value) {
|
||||
alexa_queue_element_t element;
|
||||
element.device_id = device_id;
|
||||
element.state = state;
|
||||
element.value = value;
|
||||
_alexa_queue.push(element);
|
||||
});
|
||||
|
||||
// Register loop
|
||||
@@ -83,11 +111,24 @@ void alexaLoop() {
|
||||
|
||||
alexa.handle();
|
||||
|
||||
while (!_alexa_dev_changes.empty()) {
|
||||
AlexaDevChange& change = _alexa_dev_changes.front();
|
||||
DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s\n"), change.device_id, change.state ? "ON" : "OFF");
|
||||
relayStatus(change.device_id, change.state);
|
||||
_alexa_dev_changes.pop();
|
||||
while (!_alexa_queue.empty()) {
|
||||
|
||||
alexa_queue_element_t element = _alexa_queue.front();
|
||||
DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s value: %d\n"), element.device_id, element.state ? "ON" : "OFF", element.value);
|
||||
|
||||
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
|
||||
if (0 == element.device_id) {
|
||||
relayStatus(0, element.state);
|
||||
} else {
|
||||
lightState(element.device_id - 1, element.state);
|
||||
lightChannel(element.device_id - 1, element.value);
|
||||
lightUpdate(true, true);
|
||||
}
|
||||
#else
|
||||
relayStatus(element.device_id, element.state);
|
||||
#endif
|
||||
|
||||
_alexa_queue.pop();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ lib_deps =
|
||||
https://github.com/plerup/espsoftwareserial#3.4.1
|
||||
https://github.com/me-no-dev/ESPAsyncTCP#55cd520
|
||||
https://github.com/me-no-dev/ESPAsyncWebServer#05306e4
|
||||
https://bitbucket.org/xoseperez/fauxmoesp.git#2.4.2
|
||||
https://bitbucket.org/xoseperez/fauxmoesp.git#3.0.0
|
||||
https://github.com/xoseperez/hlw8012.git#1.1.0
|
||||
https://github.com/markszabo/IRremoteESP8266#v2.2.0
|
||||
https://github.com/xoseperez/justwifi.git#2.0.1
|
||||
|
||||
Reference in New Issue
Block a user