Skip to content

Commit

Permalink
Enhance WebUI + new REST API /process_data (#56)
Browse files Browse the repository at this point in the history
* Cleanup not needed webserver context variables

* Add REST API /process_data

* Add more webpage shortcut icons

* Load overview data with one API call /process_data

* Updated title arrangement

* Load tflite list only once

* Misc optimizations

* Increase server timeout

* Keep image at original size without parsing config

* Update OTA page
  • Loading branch information
Slider0007 authored Aug 2, 2023
1 parent 0b99624 commit 85ceeba
Show file tree
Hide file tree
Showing 26 changed files with 1,179 additions and 390 deletions.
4 changes: 2 additions & 2 deletions code/components/jomjol_controlcamera/server_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ void register_server_camera_uri(httpd_handle_t server)

camuri.uri = "/lighton";
camuri.handler = handler_lightOn;
camuri.user_ctx = (void*) "Light On";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/lightoff";
camuri.handler = handler_lightOff;
camuri.user_ctx = (void*) "Light Off";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/capture";
Expand Down
4 changes: 2 additions & 2 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,13 @@ void register_server_ota_sdcard_uri(httpd_handle_t server)
camuri.method = HTTP_GET;
camuri.uri = "/ota";
camuri.handler = handler_ota_update;
camuri.user_ctx = (void*) "Do OTA";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.method = HTTP_GET;
camuri.uri = "/reboot";
camuri.handler = handler_reboot;
camuri.user_ctx = (void*) "Reboot";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

}
15 changes: 13 additions & 2 deletions code/components/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,12 +897,17 @@ std::string ClassFlowControll::getReadoutAll(int _type)
{
out = out + (*numbers)[i]->name + "\t";
switch (_type) {
/*case READOUT_TYPE_TIMESTAMP_PROCESSED:
out = out + (*numbers)[i]->lastvalue; // up to now not available as string
break;*/
case READOUT_TYPE_TIMESTAMP_VALID:
out = out + (*numbers)[i]->timeStamp;
break;
case READOUT_TYPE_VALUE:
out = out + (*numbers)[i]->ReturnValue;
break;
case READOUT_TYPE_PREVALUE:
if (flowpostprocessing->PreValueUse)
{
if (flowpostprocessing->PreValueUse) {
if ((*numbers)[i]->PreValueOkay)
out = out + (*numbers)[i]->ReturnPreValue;
else
Expand All @@ -917,6 +922,12 @@ std::string ClassFlowControll::getReadoutAll(int _type)
case READOUT_TYPE_ERROR:
out = out + (*numbers)[i]->ErrorMessageText;
break;
case READOUT_TYPE_RATE_PER_MIN:
out = out + (*numbers)[i]->ReturnRateValue;
break;
case READOUT_TYPE_RATE_PER_ROUND:
out = out + (*numbers)[i]->ReturnChangeAbsolute;
break;
}
if (i < (*numbers).size()-1)
out = out + "\r\n";
Expand Down
120 changes: 86 additions & 34 deletions code/components/jomjol_flowcontroll/MainFlowControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
#include "read_wlanini.h"
#include "connect_wlan.h"
#include "psram.h"
#include "cJSON.h"

#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#include "server_mqtt.h"
#endif //ENABLE_MQTT


// support IDF 5.x
#ifndef portTICK_RATE_MS
#define portTICK_RATE_MS portTICK_PERIOD_MS
Expand Down Expand Up @@ -370,6 +372,74 @@ esp_err_t handler_json(httpd_req_t *req)
}


esp_err_t handler_process_data(httpd_req_t *req)
{
esp_err_t retVal = ESP_OK;
std::string sReturnMessage = "E90: Uninitialized"; // Default return error message when no return is programmed

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_process_data - Start");
#endif

cJSON *cJSONObject = cJSON_CreateObject();

if (cJSONObject == NULL) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "E91: Error, JSON object cannot be created");
return ESP_FAIL;
}
else {
if (cJSON_AddStringToObject(cJSONObject, "api_name", "handler_process_data") == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "timestamp_processed", "NOT YET SUPPORTED") == NULL) // TODO: Provide data as timestamp
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "timestamp_valid", flowctrl.getReadoutAll(READOUT_TYPE_TIMESTAMP_VALID).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "actual_value", flowctrl.getReadoutAll(READOUT_TYPE_VALUE).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "pre_value", flowctrl.getReadoutAll(READOUT_TYPE_PREVALUE).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "raw_value", flowctrl.getReadoutAll(READOUT_TYPE_RAWVALUE).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "value_status", flowctrl.getReadoutAll(READOUT_TYPE_ERROR).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "rate_per_min", flowctrl.getReadoutAll(READOUT_TYPE_RATE_PER_MIN).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "rate_per_round", flowctrl.getReadoutAll(READOUT_TYPE_RATE_PER_ROUND).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "process_state", flowctrl.getActStatusWithTime().c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "process_error", std::to_string(flowctrl.getActFlowError()).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "temperature", std::to_string((int)temperatureRead()).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "rssi", std::to_string(get_WIFI_RSSI()).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "uptime", getFormatedUptime(false).c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "round_counter", std::to_string(getCountFlowRounds()).c_str()) == NULL)
retVal = ESP_FAIL;

char *jsonString = cJSON_PrintBuffered(cJSONObject, 1024, 1); // Print with predefined buffer of 1024 bytes, avoid dynamic allocations
sReturnMessage = std::string(jsonString);
cJSON_free(jsonString);
cJSON_Delete(cJSONObject);
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if (retVal == ESP_OK)
httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
else
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "E92: Error while adding JSON elements");

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_process_data - Done");
#endif

return retVal;
}


esp_err_t handler_value(httpd_req_t *req)
{
#ifdef DEBUG_DETAIL_ON
Expand Down Expand Up @@ -1372,90 +1442,72 @@ void register_server_main_flow_task_uri(httpd_handle_t server)

camuri.uri = "/reload_config";
camuri.handler = handler_reload_config;
camuri.user_ctx = (void*) "reload_config";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

// Legacy API => New: "/setPreValue"
camuri.uri = "/setPreValue.html";
camuri.handler = handler_prevalue;
camuri.user_ctx = (void*) "Prevalue";
camuri.uri = "/process_data";
camuri.handler = handler_process_data;
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/setPreValue";
camuri.handler = handler_prevalue;
camuri.user_ctx = (void*) "Prevalue";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/flow_start";
camuri.handler = handler_flow_start;
camuri.user_ctx = (void*) "Flow Start";
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/statusflow.html";
camuri.handler = handler_statusflow;
camuri.user_ctx = (void*) "statusflow.html";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/statusflow";
camuri.handler = handler_statusflow;
camuri.user_ctx = (void*) "statusflow";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/flowerror";
camuri.handler = handler_flowerror;
camuri.user_ctx = (void*) "flowerror";
httpd_register_uri_handler(server, &camuri);

// Legacy API => New: "/cpu_temperature"
camuri.uri = "/cputemp.html";
camuri.handler = handler_cputemp;
camuri.user_ctx = (void*) "cputemp";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/cpu_temperature";
camuri.handler = handler_cputemp;
camuri.user_ctx = (void*) "cpu_temperature";
httpd_register_uri_handler(server, &camuri);

// Legacy API => New: "/rssi"
camuri.uri = "/rssi.html";
camuri.handler = handler_rssi;
camuri.user_ctx = (void*) "Light Off";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/rssi";
camuri.handler = handler_rssi;
camuri.user_ctx = (void*) "Light Off";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/uptime";
camuri.handler = handler_uptime;
camuri.user_ctx = (void*) "Light Off";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/editflow";
camuri.handler = handler_editflow;
camuri.user_ctx = (void*) "EditFlow";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/value";
camuri.handler = handler_value;
camuri.user_ctx = (void*) "Value";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/json";
camuri.handler = handler_json;
camuri.user_ctx = (void*) "JSON";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/heap";
camuri.handler = handler_get_heap;
camuri.user_ctx = (void*) "Heap";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.uri = "/stream";
camuri.handler = handler_stream;
camuri.user_ctx = (void*) "stream";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

}
2 changes: 1 addition & 1 deletion code/components/jomjol_mqtt/server_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void register_server_mqtt_uri(httpd_handle_t server)

uri.uri = "/mqtt_publish_discovery";
uri.handler = scheduleSendingDiscovery_and_static_Topics;
uri.user_ctx = (void*) "";
uri.user_ctx = NULL;
httpd_register_uri_handler(server, &uri);
}

Expand Down
12 changes: 8 additions & 4 deletions code/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@


//ClassFlowControll
#define READOUT_TYPE_VALUE 0
#define READOUT_TYPE_PREVALUE 1
#define READOUT_TYPE_RAWVALUE 2
#define READOUT_TYPE_ERROR 3
#define READOUT_TYPE_TIMESTAMP_PROCESSED 0
#define READOUT_TYPE_TIMESTAMP_VALID 1
#define READOUT_TYPE_VALUE 2
#define READOUT_TYPE_PREVALUE 3
#define READOUT_TYPE_RAWVALUE 4
#define READOUT_TYPE_ERROR 5
#define READOUT_TYPE_RATE_PER_MIN 6
#define READOUT_TYPE_RATE_PER_ROUND 7

//ClassFlowMQTT
#define LWT_TOPIC "connection"
Expand Down
2 changes: 1 addition & 1 deletion code/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ extern "C" void app_main(void)

if (getHTMLcommit().substr(0, 7) != std::string(GIT_REV).substr(0, 7)) { // Compare the first 7 characters of both hashes
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Web UI version (" + getHTMLcommit() + ") does not match firmware version (" + std::string(GIT_REV) + ")");
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Recommendation: Repeat installation using AI-on-the-edge-device__update__*.zip");
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Recommendation: Repeat OTA update using AI-on-the-edge-device__update__*.zip file");
}

// Check reboot reason
Expand Down
4 changes: 2 additions & 2 deletions code/main/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ httpd_handle_t start_webserver(void)
config.max_resp_headers = 8;
config.backlog_conn = 5;
config.lru_purge_enable = true; // this cuts old connections if new ones are needed.
config.recv_wait_timeout = 10; // default: 5 20210924 --> previously 30
config.send_wait_timeout = 10; // default: 5 20210924 --> previously 30
config.recv_wait_timeout = 15; // default: 5 20210924 --> previously 30
config.send_wait_timeout = 15; // default: 5 20210924 --> previously 30
config.global_user_ctx = NULL;
config.global_user_ctx_free_fn = NULL;
config.global_transport_ctx = NULL;
Expand Down
4 changes: 2 additions & 2 deletions code/test/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,13 @@ void register_server_ota_sdcard_uri(httpd_handle_t server)
camuri.method = HTTP_GET;
camuri.uri = "/ota";
camuri.handler = handler_ota_update;
camuri.user_ctx = (void*) "Do OTA";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

camuri.method = HTTP_GET;
camuri.uri = "/reboot";
camuri.handler = handler_reboot;
camuri.user_ctx = (void*) "Reboot";
camuri.user_ctx = NULL;
httpd_register_uri_handler(server, &camuri);

}
21 changes: 9 additions & 12 deletions sd-card/html/common.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@

/* The UI can also be run locally, but you have to set the IP of your devide accordingly.
/* The UI can also be run locally, but you have to set the IP of your devide accordingly.
* And you also might have to disable CORS in your webbrowser! */
var domainname_for_testing = "192.168.2.68";



/* Returns the domainname with prepended protocol.
Eg. http://watermeter.fritz.box or http://192.168.1.5 */
function getDomainname(){
var host = window.location.hostname;
if (((host == "127.0.0.1") || (host == "localhost") || (host == ""))
// && ((window.location.port == "80") || (window.location.port == ""))
)
var domainname;

{
if ((host == "127.0.0.1") || (host == "localhost") || (host == "")) {
console.log("Using pre-defined domainname for testing: " + domainname_for_testing);
domainname = "http://" + domainname_for_testing
}
else
{
else {
domainname = window.location.protocol + "//" + host;
if (window.location.port != "") {
domainname = domainname + ":" + window.location.port;
Expand All @@ -28,6 +23,7 @@ function getDomainname(){
return domainname;
}


function UpdatePage(_dosession = true){
var zw = location.href;
zw = zw.substr(0, zw.indexOf("?"));
Expand All @@ -48,8 +44,8 @@ function LoadHostname() {
xhttp.addEventListener('load', function(event) {
if (xhttp.status >= 200 && xhttp.status < 300) {
hostname = xhttp.responseText;
document.title = hostname + " - AI on the edge";
document.getElementById("id_title").innerHTML = "Digitizer - AI on the edge - " + hostname;
document.title = hostname + " | AI on the Edge";
document.getElementById("id_title").innerHTML += hostname;
}
else {
console.warn(request.statusText, request.responseText);
Expand Down Expand Up @@ -80,7 +76,7 @@ function LoadFwVersion() {
xhttp.addEventListener('load', function(event) {
if (xhttp.status >= 200 && xhttp.status < 300) {
fwVersion = xhttp.responseText;
document.getElementById("Version").innerHTML = fwVersion;
document.getElementById("Version").innerHTML = "Slider0007 Fork | " + fwVersion;
console.log(fwVersion);
compareVersions();
}
Expand All @@ -100,6 +96,7 @@ function LoadFwVersion() {
}
}


function LoadWebUiVersion() {
_domainname = getDomainname();

Expand Down
Loading

0 comments on commit 85ceeba

Please sign in to comment.