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