From 5d157ad0e615ac66e09decf0e1cd33215df09318 Mon Sep 17 00:00:00 2001 From: ToMe25 <38815969+ToMe25@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:37:18 +0100 Subject: [PATCH] Replace AbortSignal.timeout because one of the devices I want this to run on doesn't support that. Also check heap usage on request for prometheus, rather than every second. --- data/gzip/index.js.gz | Bin 715 -> 812 bytes data/index.js | 16 +++++++++++++++- src/html/index.js | 16 +++++++++++++++- src/prometheus.cpp | 9 +-------- src/prometheus.h | 3 --- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/data/gzip/index.js.gz b/data/gzip/index.js.gz index df6b13f23ab98cfb350eee7eb0b9f9e6c91a8302..3bb6753c6252c42f887908a55b43952aa6408cdc 100644 GIT binary patch literal 812 zcmV+{1JnE;iwFn+00002|7mVyWq2-Xa{!!D!EPEc5WV{=%u11Mg|48vk-AcwL`@Dk zm7J<5+OPvSU2Np_LQ#}|ug5m93nZ0#*|o=S-pssttHM$Ti{%qkumDX~{yuLOs#I>1 zVlD)oT0kezEX%^dlhO|Cs;G7#+YP|5s<7Uj>cAGPeBWlXsW(j7>zdal4u8elpR25huCEL<6&o}Wzjur@# z8rJjx&c!83q1v|mNQrVB~+q+iT99yu5CYs7>v7yS#|(PbD2++@y~AEwArDArHp zg5+)vnsW=a&wmXAM8ckIbO}?Xp~QYGDD1XWLZDU_Tw^u$>46ya6NwTLNZB&UUViCc zb9@0?cm_DK#{qQvOipi8cEY$c>@^nghY#Gbg}m!FQ z--xn4@_G7><1o#l;Zl(kqsr_-|MtD<%)_GoNP#xQ0fL>df4(2$Q){;KofaawaBdn~ zO9bRIa1Z0FTMY2+^FryyK?iUjLpw+f{$F+=KVzuHQdQ~%?!FcGV4T9m?Z_-c6+(Ak zh66urdN`MC!2HO&Yk;&~M+_5F-yf$Y&{1;g=;P!BYQ5*F@_r`Ia71woIWE%aagQbM zn4%L#dC`Y5QFI)(ENmTK{e#l8;Iibm%@Rzso#@G|4)18Pu7R_wLNgb6!mg-!eu0Uj zW=fHQp4EBQk7P8miR4qNo2tTYlDkiNd51~XT2?BKmxm+w8jD|p$L(#zMA#SJ@(T3K z&3p3VsA+cXHKB1Nnw`}dRW((5RaC0%v3}_CumSWUN+~t0DdzKmAi=tIu`vp q_2Xfv)GjO;FZ~Hg$7XSeN{{sLVkkQ;q2mk<8+mS{9 literal 715 zcmV;+0yO;}iwFn+00002|7mVyWq2-Xa{!c5O>f&U487}Ds0Ip2sAnYWEtm#uZGraC zQ`^%J6mHBU>KrMMmqmXth58W$;%F8hpE@f=x5CLn?g*sN*89E8C{yZkV-2@$h_7wr;Mf; za`8^~*2t!*0(aM>c!Z?m(P$*dkis!|K(y%H>9R1h8n4S1;BMlfKi}>O&Je@|*_Y*X zLqM{~<=<#Td2lC`T<5leNB??oR_O)rkI9tQU?1U+6Qn1my!Nk5n=KdQqo-noppJ8J zvn6Lf%rjiY()Sg(r70%N6Jlwl7CJ8{KVFP=58<8RU{gDOiBf$ClDQ>l&Mj0zet#zx zTx~V=VHk*;MFa6&0n(FqM_l@9h83{6p8yBK?m^o!Ij(~or14yoE-pcvoz}I5oK{j~ zvm9mPU~M>PW^`pr$b4)pXE*qnP?lod!w1?2pP}?>e&QEOnLO#=ewR)}t0#`wv?j}FsV8kKhaLIt<4*P$8Zbl3owr}X*gy^@oHJE5SaVC>0)X`)^11DFxW-iVN z|B9NISC~0!<`hTJ^D0ZyIE=<=qVXx!by*^t#@)xf++iAPtxFZp%fgd;jm59gw2dZA zg#U$6R)Su*<(a%ZAkD75CN!Qzlk+N}s-jA7@=_INEDbF837{8IN~vK>v7e6w3D&KG xb#KvjakD3s#>cpQe4VEItjE)CM;NmJhd;vG{)7Al009600{}Wq{{d77000D#V|V}n diff --git a/data/index.js b/data/index.js index f2cc495..f894dce 100644 --- a/data/index.js +++ b/data/index.js @@ -19,8 +19,19 @@ update_time=Date.now() } function update(){ -fetch('data.json',{method:'get',signal:AbortSignal.timeout(3000)}) +var options={method:'GET'} +var timeout +if(typeof(AbortController)=='function'){ +const abort=new AbortController(); +options.signal=abort.signal +timeout=setTimeout(()=>abort.abort(),3000); +} + +fetch('data.json',options) .then((res)=>{ +if(timeout!=undefined){ +clearTimeout(timeout) +} return res.json() }).then((out)=>{ temp_elemen.innerText=out.temperature @@ -29,6 +40,9 @@ time_element.innerText=time_element.dateTime=out.time json_time=parseTimeString(out.time) update_time=Date.now() }).catch((err)=>{ +if(timeout!=undefined){ +clearTimeout(timeout) +} console.error('Error: ',err) }) } diff --git a/src/html/index.js b/src/html/index.js index 8d780ad..905edaa 100644 --- a/src/html/index.js +++ b/src/html/index.js @@ -29,8 +29,19 @@ function init() { * This function fetches new measurements from the ESP and updates the page. */ function update() { - fetch('data.json', { method: 'get', signal: AbortSignal.timeout(3000) }) + var options = { method: 'GET' } + var timeout + if (typeof (AbortController) == 'function') { + const abort = new AbortController(); + options.signal = abort.signal + timeout = setTimeout(() => abort.abort(), 3000); + } + + fetch('data.json', options) .then((res) => { + if (timeout != undefined) { + clearTimeout(timeout) + } return res.json() }).then((out) => { temp_elemen.innerText = out.temperature @@ -39,6 +50,9 @@ function update() { json_time = parseTimeString(out.time) update_time = Date.now() }).catch((err) => { + if (timeout != undefined) { + clearTimeout(timeout) + } console.error('Error: ', err) }) } diff --git a/src/prometheus.cpp b/src/prometheus.cpp index 78cc3b1..8677ae9 100644 --- a/src/prometheus.cpp +++ b/src/prometheus.cpp @@ -15,9 +15,6 @@ #include #include "fallback_log.h" -#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266. -uint32_t prom::used_heap = 0; -#endif #if ENABLE_WEB_SERVER == 1 && (ENABLE_PROMETHEUS_PUSH == 1 || ENABLE_PROMETHEUS_SCRAPE_SUPPORT == 1) std::map, uint64_t>> prom::http_requests_total; #endif @@ -36,11 +33,6 @@ void prom::setup() { } void prom::loop() { -#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266. - // TODO move to getMetrics - used_heap = ESP.getHeapSize() - ESP.getFreeHeap(); -#endif - #if ENABLE_PROMETHEUS_PUSH == 1 pushMetrics(); #endif @@ -114,6 +106,7 @@ String prom::getMetrics(const bool openmetrics) { // From what I could find this seems to be impossible on a ESP8266. #ifdef ESP32 + const uint64_t used_heap = ESP.getHeapSize() - ESP.getFreeHeap(); len += writeMetric(buffer + len, "process", "heap", "bytes", "The amount of heap used on the ESP in bytes.", "gauge", (double) used_heap, openmetrics); diff --git a/src/prometheus.h b/src/prometheus.h index 530ea29..6cfc67c 100644 --- a/src/prometheus.h +++ b/src/prometheus.h @@ -33,9 +33,6 @@ * This header, and the source file with the same name, contain everything for the prometheus integration. */ namespace prom { -#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266. -extern uint32_t used_heap; -#endif #if ENABLE_WEB_SERVER == 1 && (ENABLE_PROMETHEUS_PUSH == 1 || ENABLE_PROMETHEUS_SCRAPE_SUPPORT == 1) extern std::map, uint64_t>> http_requests_total; #endif