abort events, polling instead

This commit is contained in:
Torsten Ruger 2021-07-02 23:20:42 +03:00
parent 3fad0f86f6
commit 1785f07706
6 changed files with 35 additions and 63 deletions

View File

@ -15,6 +15,8 @@
<div class="ct-chart chart1"></div>
<h1> Monthly </h1>
<div class="ct-chart chart2"></div>
<h1> Log </h1>
<div id="log"></div>
<script >
var chart;
function load_chart( url , clazz ){
@ -34,27 +36,22 @@
load_chart("/weekly" , ".chart1");
load_chart("/monthly" , ".chart2");
}
if (!!window.EventSource) {
var source = new EventSource('/events');
var log = document.getElementById("log")
source.addEventListener('open', function(e) {
console.log("Events Connected");
}, false);
source.addEventListener('error', function(e) {
if (e.target.readyState != EventSource.OPEN) {
console.log("Events Disconnected");
function myCallback() {
function get_log( ){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
log.innerHTML += this.responseText;
}
}
}, false);
source.addEventListener('message', function(e) {
console.log("message", e.data);
}, false);
source.addEventListener('myevent', function(e) {
console.log("myevent", e.data);
}, false);
xhttp.open("GET", "/log", true);
xhttp.send();
};
}
var intervalID = window.setInterval(get_log, 1500);
</script>
<script onload="load_charts();" src="/chartist.min.js.gz"> </script>

View File

@ -1,27 +1,24 @@
#include <Arduino.h>
#include <queue>
void serial_setup();
void serial_loop();
class WsSerial{
std::queue<String> debug;
public:
void print(const String& );
void println(const String& );
inline void print(const String& line){ debug.push(line); }
inline void println(const String& line){ debug.push(line); }
inline void println(const float num){ println(String(num)) ; };
inline const String& pop(){ const String& first = debug.front(); debug.pop(); return first;};
inline bool empty(){ return debug.empty();};
};
static WsSerial debug_out;
#if(false)
#define DEBUG_OUT Serial
#define DEBUG_SERIAL true
#else
#define DEBUG_SERIAL false
static WsSerial debug_out;
#define DEBUG_OUT debug_out
#endif

View File

@ -23,5 +23,5 @@ lib_deps =
monitor_speed = 115200
upload_speed = 115200
upload_port = 192.168.4.1
upload_protocol = espota
;upload_port = 192.168.4.1
;upload_protocol = espota

View File

@ -10,9 +10,8 @@ const char* ssid = "fan_XXX";
void setup() {
Serial.begin(115200);
server_setup();
serial_setup();
ota_setup(ssid);
server_setup();
collector_setup();
DEBUG_OUT.println(F("Setup done"));
DEBUG_OUT.print("IP address: ");
@ -22,6 +21,5 @@ void setup() {
void loop() {
ArduinoOTA.handle();
server_loop();
serial_loop();
collector_loop();
collector_loop();
}

View File

@ -1,31 +0,0 @@
#include "serial.hpp"
#include "server.hpp"
void WsSerial::print(const String& line){
debug.push(line);
}
void WsSerial::println(const String& line){
debug.push(line);
}
void serial_setup(){
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it gat is: %u\n", client->lastId());
}
//send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!",NULL,millis(),1000);
});
}
void serial_loop(){
#if DEBUG_SERIAL
return;
#else
if(debug_out.empty()) return ;
const String & line = debug_out.pop();
Serial.println(line.c_str());
events.send(line.c_str() , "message");
#endif
}

View File

@ -37,6 +37,7 @@ void server_setup(){
if(week_counter == 2*WEEK){ // sent all data (ret 0), reset counter
week_counter = 0;
DEBUG_OUT.println("weekly end");
return 0;
}
String to_send = "";
@ -51,7 +52,6 @@ void server_setup(){
return to_send.length();
});
request->send(response);
DEBUG_OUT.println("weekly end");
});
server.on("/monthly", HTTP_ANY, [](AsyncWebServerRequest * request) {
@ -61,6 +61,7 @@ void server_setup(){
if(month_counter == 2*MONTH){ // sent all data (ret 0), reset counter
month_counter = 0;
DEBUG_OUT.println("monthly done");
return 0;
}
String to_send = "";
@ -76,8 +77,18 @@ void server_setup(){
return to_send.length();
});
request->send(response);
DEBUG_OUT.println("monthly done");
});
server.on("/log", HTTP_ANY, [](AsyncWebServerRequest * request) {
DEBUG_OUT.println("log start");
AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
if(debug_out.empty()) return 0;
String to_send = debug_out.pop();
to_send += "<br/>";
to_send.getBytes(buffer, maxLen);
return to_send.length();
});
request->send(response);
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){