Settings API
This commit is contained in:
@@ -53,6 +53,32 @@ bool firmwareUpdate()
|
||||
return true;
|
||||
}
|
||||
|
||||
void sessionManagerSetup()
|
||||
{
|
||||
Preferences scalePrefs;
|
||||
scalePrefs.begin("st_prefs");
|
||||
int valueRightShift = scalePrefs.getInt("valueRightShift", CONFIG_VALUE_RIGHT_SHIFT);
|
||||
uint8_t tareAvgCount = scalePrefs.getUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
|
||||
MeasurementT autoStartMinThreshold = scalePrefs.getUInt("aStartMinTh", CONFIG_AUTO_START_MIN_THRESHOLD);
|
||||
MeasurementT autoStartMaxThreshold = scalePrefs.getUInt("aStartMaxTh", CONFIG_AUTO_START_MAX_THRESHOLD);
|
||||
uint32_t autoStartMaxMeasurementsBetweenPeaks = scalePrefs.getUInt("aStartCount", CONFIG_AUTO_START_MAX_MEASUREMENTS_BETWEEN_PEAKS);
|
||||
MeasurementT autoStopThreshold = scalePrefs.getUInt("aStopTh", CONFIG_AUTO_STOP_THRESHOLD);
|
||||
uint32_t autoStopNumMeasurements = scalePrefs.getUInt("aStopCount", CONFIG_AUTO_STOP_NUM_MEASUREMENTS);
|
||||
|
||||
bool autoStartEnabled = scalePrefs.getBool("aStartEnabled", true);
|
||||
bool autoStopEnabled = scalePrefs.getBool("aStopEnabled", true);
|
||||
if (wifiManager.inProvisioningMode())
|
||||
autoStartEnabled = false;
|
||||
|
||||
// Session
|
||||
sessionManager.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN,
|
||||
tareAvgCount, valueRightShift,
|
||||
autoStartMinThreshold, autoStartMaxThreshold, autoStartMaxMeasurementsBetweenPeaks,
|
||||
autoStopThreshold, autoStopNumMeasurements);
|
||||
sessionManager.enableAutoStart(autoStartEnabled);
|
||||
sessionManager.enableAutoStop(autoStopEnabled);
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManager)
|
||||
{
|
||||
@@ -95,6 +121,8 @@ void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManage
|
||||
sessionObj["started"] = session.getStartTime();
|
||||
sessionObj["num_measurements"] = session.numMeasurements();
|
||||
}
|
||||
sessionObj["auto_start"] = sessionManager->autoStartEnabled();
|
||||
sessionObj["auto_stop"] = sessionManager->autoStopEnabled();
|
||||
}
|
||||
// scale
|
||||
{
|
||||
@@ -216,6 +244,86 @@ void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManage
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
httpd_resp_send(req, "Invalid keys in JSON", -1);
|
||||
};
|
||||
auto cbSettingsGet = [](httpd_req_t *req) {
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "Content-Type", "application/json");
|
||||
|
||||
StaticJsonDocument<1024> json;
|
||||
Preferences prefs;
|
||||
prefs.begin("st_prefs");
|
||||
|
||||
json["valueRightShift"] = prefs.getInt("valueRightShift", CONFIG_VALUE_RIGHT_SHIFT);
|
||||
json["tareAvgCount"] = prefs.getUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
|
||||
json["autoStartMinThreshold"] = prefs.getUInt("aStartMinTh", CONFIG_AUTO_START_MIN_THRESHOLD);
|
||||
json["autoStartMaxThreshold"] = prefs.getUInt("aStartMaxTh", CONFIG_AUTO_START_MAX_THRESHOLD);
|
||||
json["autoStartMaxMeasurementsBetweenPeaks"] = prefs.getUInt("aStartCount", CONFIG_AUTO_START_MAX_MEASUREMENTS_BETWEEN_PEAKS);
|
||||
json["autoStopThreshold"] = prefs.getUInt("aStopTh", CONFIG_AUTO_STOP_THRESHOLD);
|
||||
json["autoStopNumMeasurements"] = prefs.getUInt("aStopCount", CONFIG_AUTO_STOP_NUM_MEASUREMENTS);
|
||||
json["autoStartEnabled"] = prefs.getBool("aStartEnabled", true);
|
||||
json["autoStopEnabled"] = prefs.getBool("aStopEnabled", true);
|
||||
char jsonText[1024];
|
||||
auto bytesWritten = serializeJson(json, jsonText);
|
||||
httpd_resp_send(req, jsonText, bytesWritten);
|
||||
};
|
||||
auto cbSettingsPost = [](httpd_req_t *req) {
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
StaticJsonDocument<1024> json;
|
||||
char content[512];
|
||||
size_t recvSize = min(req->content_len, sizeof(content)); //truncate if too long
|
||||
int ret = httpd_req_recv(req, content, recvSize);
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (ret == HTTPD_SOCK_ERR_TIMEOUT)
|
||||
httpd_resp_send_408(req);
|
||||
return;
|
||||
}
|
||||
DeserializationError err = deserializeJson(json, content);
|
||||
if (err)
|
||||
{
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
httpd_resp_send(req, "JSON parse error", -1);
|
||||
return;
|
||||
}
|
||||
|
||||
Preferences prefs;
|
||||
prefs.begin("st_prefs");
|
||||
if(json.containsKey("valueRightShift"))
|
||||
prefs.putInt("valueRightShift", json["valueRightShift"].as<int>());
|
||||
if(json.containsKey("tareAvgCount"))
|
||||
prefs.putUInt("tareAvgCount", json["tareAvgCount"].as<unsigned int>());
|
||||
if(json.containsKey("autoStartMinThreshold"))
|
||||
prefs.putUInt("aStartMinTh", json["autoStartMinThreshold"].as<unsigned int>());
|
||||
if(json.containsKey("autoStartMaxThreshold"))
|
||||
prefs.putUInt("aStartMaxTh", json["autoStartMaxThreshold"].as<unsigned int>());
|
||||
if(json.containsKey("autoStartMaxMeasurementsBetweenPeaks"))
|
||||
prefs.putUInt("aStartCount", json["autoStartMaxMeasurementsBetweenPeaks"].as<unsigned int>());
|
||||
if(json.containsKey("autoStopThreshold"))
|
||||
prefs.putUInt("aStopTh", json["autoStopThreshold"].as<unsigned int>());
|
||||
if(json.containsKey("autoStopNumMeasurements"))
|
||||
prefs.putUInt("aStopCount", json["autoStopNumMeasurements"].as<unsigned int>());
|
||||
if(json.containsKey("autoStartEnabled"))
|
||||
prefs.putBool("aStartEnabled", json["autoStartEnabled"].as<bool>());
|
||||
if(json.containsKey("autoStopEnabled"))
|
||||
prefs.putBool("aStopEnabled", json["autoStopEnabled"].as<bool>());
|
||||
|
||||
sessionManagerSetup();
|
||||
httpd_resp_send(req, "OK", -1);
|
||||
};
|
||||
auto cbSettingsDelete = [](httpd_req_t *req) {
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
Preferences prefs;
|
||||
prefs.begin("st_prefs");
|
||||
prefs.putInt("valueRightShift", CONFIG_VALUE_RIGHT_SHIFT);
|
||||
prefs.putUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
|
||||
prefs.putUInt("aStartMinTh", CONFIG_AUTO_START_MIN_THRESHOLD);
|
||||
prefs.putUInt("aStartMaxTh", CONFIG_AUTO_START_MAX_THRESHOLD);
|
||||
prefs.putUInt("aStartCount", CONFIG_AUTO_START_MAX_MEASUREMENTS_BETWEEN_PEAKS);
|
||||
prefs.putUInt("aStopTh", CONFIG_AUTO_STOP_THRESHOLD);
|
||||
prefs.putUInt("aStopCount", CONFIG_AUTO_STOP_NUM_MEASUREMENTS);
|
||||
prefs.putBool("aStartEnabled", true);
|
||||
prefs.putBool("aStopEnabled", true);
|
||||
httpd_resp_send(req, "OK", -1);
|
||||
};
|
||||
|
||||
espHttpServer.start();
|
||||
|
||||
@@ -236,6 +344,10 @@ void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManage
|
||||
espHttpServer.on("/api/tare", HTTP_GET, cbTare);
|
||||
espHttpServer.on("/api/firmwareupdate", HTTP_GET, cbFirmwareUpdate);
|
||||
|
||||
espHttpServer.on("/api/settings", HTTP_GET, cbSettingsGet);
|
||||
espHttpServer.on("/api/settings", HTTP_POST, cbSettingsPost);
|
||||
espHttpServer.on("/api/settings", HTTP_DELETE, cbSettingsDelete);
|
||||
|
||||
auto webdav = webdavHandler("/webdav/", "/dat");
|
||||
espHttpServer.on("/webdav/*?", HTTP_GET, webdav);
|
||||
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav);
|
||||
@@ -296,22 +408,8 @@ void setup()
|
||||
|
||||
mdnsSetup(fullHostname);
|
||||
|
||||
Preferences scalePrefs;
|
||||
scalePrefs.begin("st_prefs");
|
||||
int valueRightShift = scalePrefs.getInt("valueRightShift", CONFIG_VALUE_RIGHT_SHIFT);
|
||||
uint8_t tareAvgCount = scalePrefs.getUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
|
||||
MeasurementT autoStartMinThreshold = scalePrefs.getUInt("autoStartMinThreshold", CONFIG_AUTO_START_MIN_THRESHOLD);
|
||||
MeasurementT autoStartMaxThreshold = scalePrefs.getUInt("autoStartMinThreshold", CONFIG_AUTO_START_MAX_THRESHOLD);
|
||||
uint32_t autoStartMaxMeasurementsBetweenPeaks = scalePrefs.getUInt("autoStartMaxMeasurementsBetweenPeaks", CONFIG_AUTO_START_MAX_MEASUREMENTS_BETWEEN_PEAKS);
|
||||
MeasurementT autoStopThreshold = scalePrefs.getUInt("autoStopThreshold", CONFIG_AUTO_STOP_THRESHOLD);
|
||||
uint32_t autoStopNumMeasurements = scalePrefs.getUInt("autoStopNumMeasurements", CONFIG_AUTO_STOP_NUM_MEASUREMENTS);
|
||||
|
||||
|
||||
// Session
|
||||
sessionManager.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN,
|
||||
tareAvgCount, valueRightShift,
|
||||
autoStartMinThreshold, autoStartMaxThreshold, autoStartMaxMeasurementsBetweenPeaks,
|
||||
autoStopThreshold, autoStopNumMeasurements);
|
||||
sessionManagerSetup();
|
||||
sessionManager.tare();
|
||||
|
||||
// HTTP & Websocket server
|
||||
httpSetup(&sessionManager, &wifiManager);
|
||||
|
||||
Reference in New Issue
Block a user