Skip to content

Commit

Permalink
Refactor get/set/del/hasSetting (#2048)
Browse files Browse the repository at this point in the history
* experimental: refactor get/set/del/hasSetting

* sensors

* lights

* cleaup

* r

* tuya

* fixup! sensors

* fixup! tuya

* header defaults types

* fix lights

* setup already checks for max

* helper for flashstring

* fix overload

* oops

* refactor includes

* warnings

* test with migrate

* add ids in a separate file

* cleanup

rev: crash

rev: domoticz

rev: encoder

rev: loopdelay

rev: hass

rev: i2c

rev2: hass

rev: mqtt

rev: rfm69

rev: relay

rev: rpn

rev: settings setup

rev: hb settings

rev: telnet preprocessor fix

rev: settings wrap

rev: tspk bool style

rev: wifi types

rev: util hb

rev: settings

fixup! rev: settings

* rev: cleanup wifi injections based on new getters

* hasSetting now can return true for empty key

* show hardcoded network in web

* oops

* fix ws referencing wrong index

* ensure empty strings are written

* c/p

* use experimental schema style for payload, mark network as not deletable

* allow to customize converter

* shorter syntax, try using with wifi

* use proper #if syntax to handle definitions that are missing

* fixup ota sc checks getter, cast schEnabled to bool

* add utils header to sensors
  • Loading branch information
mcspr authored Jan 20, 2020
1 parent c196fa2 commit 298ce8c
Show file tree
Hide file tree
Showing 73 changed files with 20,195 additions and 19,405 deletions.
2 changes: 1 addition & 1 deletion code/espurna/alexa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void _alexaBrokerCallback(const String& topic, unsigned char id, unsigned int va
// -----------------------------------------------------------------------------

bool alexaEnabled() {
return (getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
return getSetting<bool>("alexaEnabled", 1 == ALEXA_ENABLED);
}

void alexaSetup() {
Expand Down
36 changes: 24 additions & 12 deletions code/espurna/api.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,27 @@ std::vector<web_api_t> _apis;

// -----------------------------------------------------------------------------

bool _apiEnabled() {
return getSetting("apiEnabled", 1 == API_ENABLED);
}

bool _apiRestFul() {
return getSetting("apiRestFul", 1 == API_RESTFUL);
}

String _apiKey() {
return getSetting("apiKey", API_KEY);
}

bool _apiWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
return (strncmp(key, "api", 3) == 0);
}

void _apiWebSocketOnConnected(JsonObject& root) {
root["apiEnabled"] = getSetting("apiEnabled", API_ENABLED).toInt() == 1;
root["apiKey"] = getSetting("apiKey", API_KEY);
root["apiRealTime"] = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
root["apiRestFul"] = getSetting("apiRestFul", API_RESTFUL).toInt() == 1;
root["apiEnabled"] = _apiEnabled();
root["apiKey"] = _apiKey();
root["apiRestFul"] = _apiRestFul();
root["apiRealTime"] = getSetting("apiRealTime", 1 == API_REAL_TIME_VALUES);
}

void _apiConfigure() {
Expand All @@ -45,15 +57,15 @@ void _apiConfigure() {

bool _authAPI(AsyncWebServerRequest *request) {

const String key = getSetting("apiKey", API_KEY);
if (!key.length() || getSetting("apiEnabled", API_ENABLED).toInt() == 0) {
const auto key = _apiKey();
if (!key.length() || !_apiEnabled()) {
DEBUG_MSG_P(PSTR("[WEBSERVER] HTTP API is not enabled\n"));
request->send(403);
return false;
}

AsyncWebParameter* p = request->getParam("apikey", (request->method() == HTTP_PUT));
if (!p || !p->value().equals(key)) {
AsyncWebParameter* keyParam = request->getParam("apikey", (request->method() == HTTP_PUT));
if (!keyParam || !keyParam->value().equals(key)) {
DEBUG_MSG_P(PSTR("[WEBSERVER] Wrong / missing apikey parameter\n"));
request->send(403);
return false;
Expand All @@ -76,12 +88,12 @@ void _onAPIsText(AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("text/plain");
String output;
output.reserve(48);
for (unsigned int i=0; i < _apis.size(); i++) {
for (auto& api : _apis) {
output = "";
output += _apis[i].key;
output += api.key;
output += " -> ";
output += "/api/";
output += _apis[i].key;
output += api.key;
output += '\n';
response->write(output.c_str());
}
Expand Down Expand Up @@ -185,7 +197,7 @@ bool _apiRequestCallback(AsyncWebServerRequest *request) {

// Check if its a PUT
if (api.putFn != NULL) {
if ((getSetting("apiRestFul", API_RESTFUL).toInt() != 1) || (request->method() == HTTP_PUT)) {
if (!_apiRestFul() || (request->method() == HTTP_PUT)) {
if (request->hasParam("value", request->method() == HTTP_PUT)) {
AsyncWebParameter* p = request->getParam("value", request->method() == HTTP_PUT);
(api.putFn)((p->value()).c_str());
Expand Down
18 changes: 18 additions & 0 deletions code/espurna/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
BOARD MODULE
*/

#pragma once

String getIdentifier();

String getEspurnaModules();
String getEspurnaOTAModules();
String getEspurnaSensors();

String getEspurnaWebUI();

int getBoardId();

Loading

0 comments on commit 298ce8c

Please sign in to comment.