added locally save and load iriskit settings
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -11,6 +11,7 @@
|
||||
"ranges": "cpp",
|
||||
"memory": "cpp",
|
||||
"random": "cpp",
|
||||
"optional": "cpp"
|
||||
"optional": "cpp",
|
||||
"memory_resource": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,8 @@ extern String g_device_secret;
|
||||
|
||||
// public variable definitions
|
||||
int credential_init_retry = 0;
|
||||
|
||||
t_iriskit_settings iriskit_settings;
|
||||
bool iriskit_settings_loaded = false;
|
||||
|
||||
|
||||
// private variable definitions
|
||||
@@ -86,19 +87,52 @@ void setup() {
|
||||
INFOLN("╚═╝╚═╝ ╚═╝╚═╝╚══════╝");
|
||||
INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby ==");
|
||||
|
||||
// custom parameter for iris credentials
|
||||
WiFiManagerParameter server_address("server", "Server", "", URL_SHORT_MAX);
|
||||
WiFiManagerParameter credential_token("credential", "Credential", "", CREDENTIAL_MAX);
|
||||
// try loading saved iriskit settings
|
||||
if (loadSettings()) {
|
||||
iriskit_settings = getIrisKitSettings();
|
||||
INFOF("saved credentials loaded, token = %s\n",
|
||||
iriskit_settings.credential_token.c_str());
|
||||
iriskit_settings_loaded = true;
|
||||
} else {
|
||||
INFOLN("no credentials saved yet, request new from IRIS server");
|
||||
}
|
||||
|
||||
wifi_manager.addParameter(&server_address);
|
||||
wifi_manager.addParameter(&credential_token);
|
||||
wifi_manager.autoConnect();
|
||||
// custom parameter for iris credentials
|
||||
WiFiManagerParameter* server_address = NULL;
|
||||
WiFiManagerParameter* credential_token = NULL;
|
||||
|
||||
memset(iris_server_address, 0, URL_SHORT_MAX);
|
||||
strcpy(iris_server_address, server_address.getValue());
|
||||
|
||||
memset(iris_credential_token, 0, CREDENTIAL_MAX);
|
||||
strcpy(iris_credential_token, credential_token.getValue());
|
||||
|
||||
if (!iriskit_settings_loaded) {
|
||||
server_address =
|
||||
new WiFiManagerParameter("server_address", "Server Address", "", URL_SHORT_MAX);
|
||||
credential_token =
|
||||
new WiFiManagerParameter("credential_token", "Credential Token", "", CREDENTIAL_MAX);
|
||||
|
||||
if (NULL == server_address || NULL == credential_token) {
|
||||
ERRORLN("not enough memory to create settings");
|
||||
factoryReset();
|
||||
}
|
||||
wifi_manager.addParameter(server_address);
|
||||
wifi_manager.addParameter(credential_token);
|
||||
} else {
|
||||
strcpy(iris_server_address, iriskit_settings.server_address.c_str());
|
||||
strcpy(iris_credential_token, iriskit_settings.credential_token.c_str());
|
||||
}
|
||||
|
||||
wifi_manager.autoConnect();
|
||||
|
||||
if (!iriskit_settings_loaded) {
|
||||
memset(iris_server_address, 0, URL_SHORT_MAX);
|
||||
strcpy(iris_server_address, server_address->getValue());
|
||||
|
||||
memset(iris_credential_token, 0, CREDENTIAL_MAX);
|
||||
strcpy(iris_credential_token, credential_token->getValue());
|
||||
|
||||
delete server_address;
|
||||
delete credential_token;
|
||||
}
|
||||
|
||||
INFOF("Wifi Connected, IRIS server = %s, credential token = %s\n",
|
||||
iris_server_address, iris_credential_token);
|
||||
@@ -121,6 +155,11 @@ void setup() {
|
||||
} while (1);
|
||||
|
||||
INFOF("credential get : %s\n", iris_credential_token);
|
||||
iriskit_settings.server_address = String(iris_server_address);
|
||||
iriskit_settings.credential_token = String(iris_credential_token);
|
||||
saveIrisKitSettings(iriskit_settings);
|
||||
|
||||
saveSettings();
|
||||
|
||||
delay(1000);
|
||||
connectToAliyunIoT();
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
#ifndef IRBABY_H
|
||||
#define IRBABY_H
|
||||
|
||||
typedef struct {
|
||||
String server_address;
|
||||
String credential_token;
|
||||
} t_iriskit_settings;
|
||||
|
||||
|
||||
void IRAM_ATTR factoryReset();
|
||||
|
||||
#endif // IRBABY_H
|
||||
@@ -31,21 +31,41 @@
|
||||
|
||||
#include "IRbabyUserSettings.h"
|
||||
|
||||
|
||||
#define FILE_GENERIC_CONFIG "config"
|
||||
#define FILE_AC_STATUS "ac_status"
|
||||
#define IRISKIT_SETTINGS "iriskit_settings"
|
||||
|
||||
|
||||
StaticJsonDocument<1024> ConfigData;
|
||||
StaticJsonDocument<1024> ACStatus;
|
||||
StaticJsonDocument<1024> IrisKitSettings;
|
||||
|
||||
bool saveSettings() {
|
||||
DEBUGLN("Save Config");
|
||||
File cache = LittleFS.open("/config", "w");
|
||||
DEBUGLN("save configs for IRIS Kit");
|
||||
|
||||
// generic config partial
|
||||
File cache = LittleFS.open(FILE_GENERIC_CONFIG, "w");
|
||||
if (!cache || (serializeJson(ConfigData, cache) == 0)) {
|
||||
ERRORLN("Failed to save config file");
|
||||
ERRORLN("failed to save config file");
|
||||
cache.close();
|
||||
return false;
|
||||
}
|
||||
cache.close();
|
||||
cache = LittleFS.open("/acstatus", "w");
|
||||
|
||||
// AC status partial
|
||||
cache = LittleFS.open(FILE_AC_STATUS, "w");
|
||||
if (!cache || (serializeJson(ACStatus, cache) == 0)) {
|
||||
ERRORLN("ERROR: Failed to save acstatus file");
|
||||
ERRORLN("ERROR: failed to save AC status file");
|
||||
cache.close();
|
||||
return false;
|
||||
}
|
||||
cache.close();
|
||||
|
||||
// credential partial
|
||||
cache = LittleFS.open(IRISKIT_SETTINGS, "w");
|
||||
if (!cache || (serializeJson(IrisKitSettings, cache) == 0)) {
|
||||
ERRORLN("ERROR: failed to save credentials file");
|
||||
cache.close();
|
||||
return false;
|
||||
}
|
||||
@@ -58,50 +78,73 @@ bool loadSettings() {
|
||||
int ret = false;
|
||||
FSInfo64 info;
|
||||
LittleFS.info64(info);
|
||||
DEBUGF("fs total bytes = %llu\n", info.totalBytes);
|
||||
if (LittleFS.exists("/config")) {
|
||||
File cache = LittleFS.open("/config", "r");
|
||||
DEBUGF("fsstats: total bytes = %llu, used = %llu\n", info.totalBytes, info.usedBytes);
|
||||
|
||||
// generic config partial
|
||||
if (LittleFS.exists(FILE_GENERIC_CONFIG)) {
|
||||
File cache = LittleFS.open(FILE_GENERIC_CONFIG, "r");
|
||||
if (!cache) {
|
||||
ERRORLN("Failed to read config file");
|
||||
ERRORLN("failed to read config file");
|
||||
return ret;
|
||||
}
|
||||
if (cache.size() > 0) {
|
||||
DeserializationError error = deserializeJson(ConfigData, cache);
|
||||
if (error) {
|
||||
ERRORLN("Failed to load config settings");
|
||||
ERRORLN("failed to load config settings");
|
||||
return ret;
|
||||
}
|
||||
INFOLN("Load config data:");
|
||||
INFOLN("generic config loaded");
|
||||
ConfigData["version"] = FIRMWARE_VERSION;
|
||||
serializeJsonPretty(ConfigData, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
cache.close();
|
||||
} else {
|
||||
DEBUGLN("Config does not exist");
|
||||
DEBUGLN("config does not exist");
|
||||
}
|
||||
|
||||
if (LittleFS.exists("/acstatus")) {
|
||||
File cache = LittleFS.open("/acstatus", "r");
|
||||
// AC status partial
|
||||
if (LittleFS.exists(FILE_AC_STATUS)) {
|
||||
File cache = LittleFS.open(FILE_AC_STATUS, "r");
|
||||
if (!cache) {
|
||||
ERRORLN("Failed to read acstatus file");
|
||||
ERRORLN("failed to read AC status file");
|
||||
return ret;
|
||||
}
|
||||
if (cache.size() > 0) {
|
||||
DeserializationError error = deserializeJson(ACStatus, cache);
|
||||
if (error) {
|
||||
ERRORLN("Failed to load acstatus settings");
|
||||
ERRORLN("failed to load AC status settings");
|
||||
return ret;
|
||||
}
|
||||
INFOLN("AC status loaded");
|
||||
}
|
||||
cache.close();
|
||||
}
|
||||
|
||||
// credential partial
|
||||
if (LittleFS.exists(IRISKIT_SETTINGS)) {
|
||||
File cache = LittleFS.open(IRISKIT_SETTINGS, "r");
|
||||
if (!cache) {
|
||||
ERRORLN("failed to read acstatus file");
|
||||
return ret;
|
||||
}
|
||||
if (cache.size() > 0) {
|
||||
DeserializationError error = deserializeJson(IrisKitSettings, cache);
|
||||
if (error) {
|
||||
ERRORLN("failed to load credentials settings");
|
||||
return ret;
|
||||
}
|
||||
INFOLN("credentials loaded");
|
||||
}
|
||||
cache.close();
|
||||
}
|
||||
|
||||
ret = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool saveACStatus(String file, t_remote_ac_status status) {
|
||||
bool ret = false;
|
||||
bool ret = true;
|
||||
ACStatus[file]["power"] = (int)status.ac_power;
|
||||
ACStatus[file]["temperature"] = (int)status.ac_temp;
|
||||
ACStatus[file]["mode"] = (int)status.ac_mode;
|
||||
@@ -112,11 +155,11 @@ bool saveACStatus(String file, t_remote_ac_status status) {
|
||||
|
||||
t_remote_ac_status getACStatus(String file) {
|
||||
t_remote_ac_status status;
|
||||
int power = (int)ACStatus[file]["power"];
|
||||
int temperature = (int)ACStatus[file]["temperature"];
|
||||
int mode = (int)ACStatus[file]["mode"];
|
||||
int swing = (int)ACStatus[file]["swing"];
|
||||
int wind_speed = (int)ACStatus[file]["speed"];
|
||||
int power = (int) ACStatus[file]["power"];
|
||||
int temperature = (int) ACStatus[file]["temperature"];
|
||||
int mode = (int) ACStatus[file]["mode"];
|
||||
int swing = (int) ACStatus[file]["swing"];
|
||||
int wind_speed = (int) ACStatus[file]["speed"];
|
||||
status.ac_power = (t_ac_power)power;
|
||||
status.ac_temp = (t_ac_temperature)temperature;
|
||||
status.ac_mode = (t_ac_mode)mode;
|
||||
@@ -124,3 +167,16 @@ t_remote_ac_status getACStatus(String file) {
|
||||
status.ac_wind_speed = (t_ac_wind_speed)wind_speed;
|
||||
return status;
|
||||
}
|
||||
|
||||
bool saveIrisKitSettings(t_iriskit_settings& iriskit_settings) {
|
||||
IrisKitSettings["server_address"] = iriskit_settings.server_address;
|
||||
IrisKitSettings["token"] = iriskit_settings.credential_token;
|
||||
return true;
|
||||
}
|
||||
|
||||
t_iriskit_settings getIrisKitSettings() {
|
||||
t_iriskit_settings iriskit_settings;
|
||||
iriskit_settings.server_address = (String)IrisKitSettings["server_address"];
|
||||
iriskit_settings.credential_token = (String)IrisKitSettings["token"];
|
||||
return iriskit_settings;
|
||||
}
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include "IRbaby.h"
|
||||
#include "ir_ac_control.h"
|
||||
|
||||
/* save settings */
|
||||
@@ -37,7 +38,11 @@ bool loadSettings();
|
||||
bool saveACStatus(String file, t_remote_ac_status status);
|
||||
t_remote_ac_status getACStatus(String file);
|
||||
|
||||
bool saveIrisKitSettings(t_iriskit_settings& iriskit_settings);
|
||||
t_iriskit_settings getIrisKitSettings();
|
||||
|
||||
extern StaticJsonDocument<1024> ConfigData;
|
||||
extern StaticJsonDocument<1024> ACStatus;
|
||||
extern StaticJsonDocument<1024> IrisKitSettings;
|
||||
|
||||
#endif // IRBABY_USER_SETTINGS_H
|
||||
Reference in New Issue
Block a user