Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor "/info" #257

Merged
merged 14 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions html/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,31 @@
"forum": {
"title": "Forum",
"desc": "Du hast Probleme mit ESPuino oder bist an einem Erfahrungsaustausch interessiert?<br />Dann schaue doch mal im <a href=\"https://forum.espuino.de\" target=\"_blank\">ESPuino-Forum</a> vorbei! Insbesondere gibt es dort auch einen<br /><a href=\"https://forum.espuino.de/c/dokumentation/anleitungen/10\" target=\"_blank\">Bereich</a>, in dem reichlich Dokumentation hinterlegt ist. Wir freuen uns auf deinen Besuch!"
},
"datetime": {
"day": "Tag",
"days": "Tage",
"hour": "Stunde",
"hours": "Stunden",
"minute": "Minute",
"minutes": "Minuten",
"second": "Sekunde",
"seconds": "Sekunden"
},
"systeminfo": {
"title": "Information",
"softwareversion": "ESPuino {{softwareversion}}",
"gitversion": "ESPuino {{gitversion}}",
"arduinoversion": "Arduino Version: {{arduinoversion}} (ESP-IDF {{idfversion}})",
"hardware": "Hardware: {{hwmodel}}, Revision {{hwrevision}}, CPU: {{hwfreq}} MHZ",
"freeheap": "Freier Heap: {{freeheap}} Bytes",
"largestfreeblock": "Größter freier Heap-Block: {{largestfreeblock}} Bytes",
"freepsram": "Freier PS-RAM: {{freepsram}} Bytes",
"currentip": "Aktuelle IP-Adresse: {{currentip}}",
"rssi": "WLAN Signalstärke: {{rssi}} dBm",
"audiotime": "Audio-Gesamtspielzeit: {{audiotimetotal}} (seit letztem Start: {{audiotimesincestart}})",
"currvoltage": "Aktuelle Batteriespannung: {{currvoltage}} V",
"chargelevel": "Aktuelle Batterieladung: {{chargelevel}} %",
"hallsensor": "HallEffectSensor NullFieldValue: {{hsnullfieldvalue}}, actual: {{hsactual}}, diff:{{hsdiff}}, LastWaitFor_State:{{hslastwaitforstate}} (waited:{{hswaited}} ms)"
}
}
26 changes: 26 additions & 0 deletions html/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,31 @@
"forum": {
"title": "Forum",
"desc": "Having problems or aim to discuss about ESPuino?<br /> Join us at <a href=\"https://forum.espuino.de\" target=\"_blank\">ESPuino-Forum</a>! Especially there's a lot of (german)<br /><a href=\"https://forum.espuino.de/c/dokumentation/anleitungen/10\" target=\"_blank\">documentation</a> online!"
},
"datetime": {
"day": "day",
"days": "days",
"hour": "hour",
"hours": "hours",
"minute": "minute",
"minutes": "minutes",
"second": "second",
"seconds": "seconds"
},
"systeminfo": {
"title": "Information",
"softwareversion": "ESPuino {{softwareversion}}",
"gitversion": "ESPuino {{gitversion}}",
"arduinoversion": "Arduino Version: {{arduinoversion}} (ESP-IDF {{idfversion}})",
"hardware": "Hardware: {{hwmodel}}, Revision {{hwrevision}}, CPU: {{hwfreq}} MHZ",
"freeheap": "Free heap: {{freeheap}} Bytes",
"largestfreeblock": "Largest free heap-block: {{largestfreeblock}} Bytes",
"freepsram": "Free PS-RAM: {{freepsram}} Bytes",
"currentip": "Current IP-Address: {{currentip}}",
"rssi": "WiFi signal strength: {{rssi}} dBm",
"audiotime": "Total audio playtime: {{audiotimetotal}} (Since last start: {{audiotimesincestart}})",
"currvoltage": "Current battery voltage: {{currvoltage}} V",
"chargelevel": "Current charge level: {{chargelevel}} %",
"hallsensor": "HallEffectSensor NullFieldValue: {{hsnullfieldvalue}}, actual: {{hsactual}}, diff:{{hsdiff}}, LastWaitFor_State:{{hslastwaitforstate}} (waited:{{hswaited}} ms)"
}
}
56 changes: 49 additions & 7 deletions html/management.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
<h5 class="modal-title">Infos</h5>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body" style="white-space: pre">
<div class="modal-body" style="white-space: pre" id="modalInfoContent">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
Expand Down Expand Up @@ -1823,6 +1823,53 @@ <h5 class="modal-title" data-i18n="wifi.delete.title"></h5>
setTimeout(tryRedirect, 2000);
}
}

function timestampToDuration(ms, withSeconds) {
if (ms == 0) {
return "--";
}
seconds = Number(ms/1000);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);

var dDisplay = d > 0 ? d + (d == 1 ? " " + i18next.t("datetime.day") + ", " : " " + i18next.t("datetime.days") + ", ") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " " + i18next.t("datetime.hour") + ", " : " " + i18next.t("datetime.hours") + ", ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " " + i18next.t("datetime.minute") + ", " : " " + i18next.t("datetime.minutes") + ", ") : "";
if (withSeconds) {
var sDisplay = s > 0 ? s + (s == 1 ? " " + i18next.t("datetime.second") : " " + i18next.t("datetime.seconds")) : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}
return dDisplay + hDisplay + mDisplay;
}

async function showInfoDialog() {
$('.modal-title').text(i18next.t("systeminfo.title"));
let info = await (await fetch("/info")).json();
console.log(info);
let content = i18next.t("systeminfo.softwareversion", { softwareversion: info.software.version }) + "\n";
content += i18next.t("systeminfo.gitversion", { gitversion: info.software.git }) + "\n";
content += i18next.t("systeminfo.arduinoversion", { arduinoversion: info.software.arduino, idfversion: info.software.idf }) + "\n";
content += i18next.t("systeminfo.hardware", { hwmodel: info.hardware.model, hwrevision: info.hardware.revision, hwfreq: info.hardware.freq }) + "\n";
content += i18next.t("systeminfo.freeheap", { freeheap: info.memory.freeHeap }) + "\n";
content += i18next.t("systeminfo.largestfreeblock", { largestfreeblock: info.memory.largestFreeBlock }) + "\n";
content += i18next.t("systeminfo.freepsram", { freepsram: info.memory.freePSRam }) + "\n";
content += i18next.t("systeminfo.currentip", { currentip: info.wifi.ip }) + "\n";
content += i18next.t("systeminfo.rssi", { rssi: info.wifi.rssi }) + "\n";
content += i18next.t("systeminfo.audiotime", { audiotimetotal: timestampToDuration(info.audio.playtimeTotal, false), audiotimesincestart: timestampToDuration(info.audio.playtimeSinceStart, true)}) + "\n";
if (info.battery) {
content += i18next.t("systeminfo.currvoltage", { currvoltage: info.battery.currVoltage.toFixed(2) }) + "\n";
content += i18next.t("systeminfo.chargelevel", { chargelevel: info.battery.chargeLevel.toFixed(1) }) + "\n";
}
if (info.hallsensor) {
content += i18next.t("systeminfo.hallsensor", { hsnullfieldvalue: info.hallsensor.nullFieldValue, hsactual: info.hallsensor.actual, hsdiff: info.hallsensor.diff,
hslastwaitforstate: info.hallsensor.lastWaitState, hswaited: info.hallsensor.lastWaitMS}) + "\n";
}
$('#modalInfoContent').text(content);
$('#modalInfo').modal({show: true});
}

$(document).ready(function () {
connect();
buildFileSystemTree("/");
Expand All @@ -1834,12 +1881,7 @@ <h5 class="modal-title" data-i18n="wifi.delete.title"></h5>
$(this).find('.btn-ok').attr('href', "javascript:deleteSSID('" + ssid + "')");
})

$('.openPopupInfo').on('click', function(){
$('.modal-title').text(i18next.t("info"));
$('.modal-body').load("/info", function(){
$('#modalInfo').modal({show: true});
});
});
$('.openPopupInfo').on('click', showInfoDialog);
$('.openPopupLog').on('click', function(){
$('.modal-title').text(i18next.t("log"));
$('.modal-body').load("/log", function(){
Expand Down
154 changes: 84 additions & 70 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static void handleGetWiFiConfig(AsyncWebServerRequest *request);
static void handlePostWiFiConfig(AsyncWebServerRequest *request, JsonVariant &json);
static void handleCoverImageRequest(AsyncWebServerRequest *request);
static void handleWiFiScanRequest(AsyncWebServerRequest *request);
static void handleGetInfo(AsyncWebServerRequest *request);
static void handleGetSettings(AsyncWebServerRequest *request);
static void handlePostSettings(AsyncWebServerRequest *request, JsonVariant &json);

Expand Down Expand Up @@ -270,31 +271,6 @@ void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}

String formatTimeToStr(time_t t) {

String sTime;
uint32_t s = (t / 1000);
static char buf[32];

// days
int days = (s / 86400UL);
if (days > 0) {
#if (LANGUAGE == DE)
sprintf(buf, "%d Tage, ", days);
#else
sprintf(buf, "%d days, ", days);
#endif
sTime = buf;
}
// hours, minues & seconds
int hours = (s % 86400UL) / 3600UL;
int minutes = (s / 60UL) % 60UL;
int seconds = (s % 60UL);
sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
sTime+= buf;
return sTime;
}

void webserverStart(void) {
if (Wlan_IsConnected() && !webserverStarted) {
// attach AsyncWebSocket for Mgmt-Interface
Expand Down Expand Up @@ -333,51 +309,8 @@ void webserverStart(void) {
request->send(200, "text/plain; charset=utf-8", Log_GetRingBuffer());
});

// software/wifi/heap/psram-info
wServer.on(
"/info", HTTP_GET, [](AsyncWebServerRequest *request) {
char buffer[128];
String info = "ESPuino " + (String) softwareRevision;
info += "\nESPuino " + (String) gitRevision;
info += "\nArduino version: " + String(ESP_ARDUINO_VERSION_MAJOR) + "." + String(ESP_ARDUINO_VERSION_MINOR) + "." + String(ESP_ARDUINO_VERSION_PATCH) + " (ESP-IDF " + String(ESP.getSdkVersion()) + ")";
info += "\nHardware: " + String(ESP.getChipModel()) + ", Revision " + String(ESP.getChipRevision()) + ", CPU: " + String((unsigned long)ESP.getCpuFreqMHz()) + "MHZ";
#if (LANGUAGE == DE)
info += "\nFreier Heap: " + String(ESP.getFreeHeap()) + " Bytes";
info += "\nGroesster freier Heap-Block: " + String((uint32_t)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)) + " Bytes";
info += "\nFreier PSRAM: ";
info += (!psramInit()) ? "nicht verfuegbar" : String(ESP.getFreePsram());
if (Wlan_IsConnected()) {
IPAddress myIP = WiFi.localIP();
info += "\nAktuelle IP: " + String(myIP[0]) + '.' + String(myIP[1]) + '.' + String(myIP[2]) + '.' + String(myIP[3]);
info += "\nWLAN-Signalstaerke: " + String((int8_t)Wlan_GetRssi()) + " dBm";
}
info += "\nAudio-Gesamtspielzeit: " + formatTimeToStr(AudioPlayer_GetPlayTimeAllTime()) + " (seit letztem Start: " + formatTimeToStr(AudioPlayer_GetPlayTimeSinceStart()) + ")";
#else
info += "\nFree heap: " + String(ESP.getFreeHeap()) + " bytes";
info += "\nLargest free heap-block: " + String((uint32_t)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)) + " bytes";
info += "\nFree PSRAM: ";
info += (!psramInit()) ? "not available" : String(ESP.getFreePsram());
if (Wlan_IsConnected()) {
IPAddress myIP = WiFi.localIP();
info += "\nCurrent IP: " + String(myIP[0]) + '.' + String(myIP[1]) + '.' + String(myIP[2]) + '.' + String(myIP[3]);
info += "\nWiFi signal-strength: " + String((int8_t)Wlan_GetRssi()) + " dBm";
}
info += "\nAudio total playtime: " + secondsToTimeStr(AudioPlayer_GetPlayTimeSecsAllTime()) + " (since last start: " + secondsToTimeStr(AudioPlayer_GetPlayTimeSecsSinceStart()) + ")";
#endif
#ifdef BATTERY_MEASURE_ENABLE
snprintf(buffer, sizeof(buffer), currentVoltageMsg, Battery_GetVoltage());
info += "\n" + String(buffer);
snprintf(buffer, sizeof(buffer), currentChargeMsg, Battery_EstimateLevel() * 100);
info += "\n" + String(buffer);
#endif
#ifdef HALLEFFECT_SENSOR_ENABLE
uint16_t sva = gHallEffectSensor.readSensorValueAverage(true);
int diff = sva-gHallEffectSensor.NullFieldValue();
snprintf(buffer, sizeof(buffer), "\nHallEffectSensor NullFieldValue:%d, actual:%d, diff:%d, LastWaitFor_State:%d (waited:%d ms)", gHallEffectSensor.NullFieldValue(), sva, diff, gHallEffectSensor.LastWaitForState(), gHallEffectSensor.LastWaitForStateMS());
info += buffer;
#endif
request->send_P(200, "text/plain", info.c_str());
});
// info
wServer.on("/info", HTTP_GET, handleGetInfo);

// NVS-backup-upload
wServer.on(
Expand Down Expand Up @@ -802,6 +735,87 @@ static void settingsToJSON(JsonObject obj, String section) {
#endif
}

// handle get info
void handleGetInfo(AsyncWebServerRequest *request) {

// param to get a single info section
String section = "";
if (request->hasParam("section")) {
section = request->getParam("section")->value();
}
#ifdef BOARD_HAS_PSRAM
SpiRamJsonDocument doc(512);
#else
StaticJsonDocument<512> doc;
#endif
JsonObject infoObj = doc.createNestedObject("info");
// software
if ((section == "") || (section == "software")) {
JsonObject softwareObj = infoObj.createNestedObject("software");
softwareObj["version"] = (String) softwareRevision;
softwareObj["git"] = (String) gitRevision;
softwareObj["arduino"] = String(ESP_ARDUINO_VERSION_MAJOR) + "." + String(ESP_ARDUINO_VERSION_MINOR) + "." + String(ESP_ARDUINO_VERSION_PATCH);
softwareObj["idf"] = String(ESP.getSdkVersion());
}
// hardware
if ((section == "") || (section == "hardware")) {
JsonObject hardwareObj = infoObj.createNestedObject("hardware");
hardwareObj["model"] = String(ESP.getChipModel());
hardwareObj["revision"] = ESP.getChipRevision();
hardwareObj["freq"] = ESP.getCpuFreqMHz();
}
// memory
if ((section == "") || (section == "memory")) {
JsonObject memoryObj = infoObj.createNestedObject("memory");
memoryObj["freeHeap"] = ESP.getFreeHeap();
memoryObj["largestFreeBlock"] = (uint32_t)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
if (psramInit()) {
memoryObj["freePSRam"] = ESP.getFreePsram();
}
}
// wifi
if ((section == "") || (section == "wifi")) {
JsonObject wifiObj = infoObj.createNestedObject("wifi");
IPAddress myIP = WiFi.localIP();
wifiObj["ip"] = String(myIP[0]) + '.' + String(myIP[1]) + '.' + String(myIP[2]) + '.' + String(myIP[3]);
wifiObj["rssi"] = (int8_t)Wlan_GetRssi();
}
// audio
if ((section == "") || (section == "audio")) {
JsonObject audioObj = infoObj.createNestedObject("audio");
audioObj["playtimeTotal"] = AudioPlayer_GetPlayTimeAllTime();
audioObj["playtimeSinceStart"] = AudioPlayer_GetPlayTimeSinceStart();
}
#ifdef BATTERY_MEASURE_ENABLE
// battery
if ((section == "") || (section == "battery")) {
JsonObject batteryObj = infoObj.createNestedObject("battery");
batteryObj["currVoltage"] = Battery_GetVoltage();
batteryObj["chargeLevel"] = Battery_EstimateLevel() * 100;
}
#endif
#ifdef HALLEFFECT_SENSOR_ENABLE
if ((section == "") || (section == "hallsensor")) {
// hallsensor
JsonObject hallObj = infoObj.createNestedObject("hallsensor");
uint16_t sva = gHallEffectSensor.readSensorValueAverage(true);
int diff = sva-gHallEffectSensor.NullFieldValue();

hallObj["nullFieldValue"] =gHallEffectSensor.NullFieldValue();
hallObj["actual"] = sva;
hallObj["diff"] = diff;
hallObj["lastWaitState"] = gHallEffectSensor.LastWaitForState();
hallObj["lastWaitMS"] = gHallEffectSensor.LastWaitForStateMS();
}
#endif

String serializedJsonString;
serializeJson(infoObj, serializedJsonString);
request->send(200, "application/json; charset=utf-8", serializedJsonString);
}



// handle get settings
void handleGetSettings(AsyncWebServerRequest *request) {

Expand Down