alexa: display the used name in the webui

This commit is contained in:
Maxim Prokhorov
2021-07-18 21:54:19 +03:00
parent a536dab029
commit 6b2c34eaae

View File

@@ -52,10 +52,52 @@ private:
};
std::queue<AlexaEvent> _alexa_events;
fauxmoESP _alexa;
} // namespace
namespace alexa {
namespace build {
constexpr bool createServer() {
return !WEB_SUPPORT;
}
constexpr uint16_t port() {
return 80;
}
const __FlashStringHelper* hostname() {
return F(ALEXA_HOSTNAME);
}
constexpr bool enabled() {
return 1 == ALEXA_ENABLED;
}
} // namespace build
namespace settings {
bool enabled() {
return getSetting("alexaEnabled", build::enabled());
}
// Use custom alexa hostname if defined, device hostname otherwise
String hostname() {
auto out = getSetting("alexaName", build::hostname());
if (!out.length()) {
out = getSetting("hostname", getIdentifier());
}
return out;
}
} // namespace settings
} // namespace alexa
void _alexaSettingsMigrate(int version) {
if (version && (version < 3)) {
moveSetting("fauxmoEnabled", "alexaEnabled");
}
}
// -----------------------------------------------------------------------------
// ALEXA
@@ -66,12 +108,12 @@ bool _alexaWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
}
void _alexaWebSocketOnConnected(JsonObject& root) {
root["alexaEnabled"] = alexaEnabled();
root["alexaName"] = getSetting("alexaName");
root["alexaEnabled"] = alexa::settings::enabled();
root["alexaName"] = alexa::settings::hostname();
}
void _alexaConfigure() {
_alexa.enable(wifiConnected() && alexaEnabled());
_alexa.enable(wifiConnected() && alexa::settings::enabled());
}
#if WEB_SUPPORT
@@ -106,14 +148,8 @@ void _alexaUpdateRelay(size_t id, bool status) {
}
#endif
// -----------------------------------------------------------------------------
bool alexaEnabled() {
return getSetting("alexaEnabled", 1 == ALEXA_ENABLED);
}
void alexaLoop() {
void _alexaLoop() {
_alexa.handle();
while (!_alexa_events.empty()) {
@@ -135,22 +171,14 @@ void alexaLoop() {
_alexa_events.pop();
}
}
constexpr bool _alexaCreateServer() {
return !WEB_SUPPORT;
}
} // namespace
constexpr const char* _alexaHostname() {
return ALEXA_HOSTNAME;
}
// -----------------------------------------------------------------------------
void _alexaSettingsMigrate(int version) {
if (version && (version < 3)) {
moveSetting("fauxmoEnabled", "alexaEnabled");
}
bool alexaEnabled() {
return alexa::settings::enabled();
}
void alexaSetup() {
@@ -158,15 +186,10 @@ void alexaSetup() {
_alexaSettingsMigrate(migrateVersion());
// Basic fauxmoESP configuration
_alexa.createServer(_alexaCreateServer());
_alexa.setPort(80);
// Use custom alexa hostname if defined, device hostname otherwise
String hostname = getSetting("alexaName", _alexaHostname());
if (!hostname.length()) {
hostname = getSetting("hostname", getIdentifier());
}
_alexa.createServer(alexa::build::createServer());
_alexa.setPort(alexa::build::port());
auto hostname = alexa::settings::hostname();
auto deviceName = [&](size_t index) {
auto name = hostname;
name += ' ';
@@ -225,8 +248,7 @@ void alexaSetup() {
#endif
espurnaRegisterReload(_alexaConfigure);
espurnaRegisterLoop(alexaLoop);
espurnaRegisterLoop(_alexaLoop);
}
#endif