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

View File

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

View File

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

View File

@ -10,9 +10,8 @@ const char* ssid = "fan_XXX";
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
server_setup();
serial_setup();
ota_setup(ssid); ota_setup(ssid);
server_setup();
collector_setup(); collector_setup();
DEBUG_OUT.println(F("Setup done")); DEBUG_OUT.println(F("Setup done"));
DEBUG_OUT.print("IP address: "); DEBUG_OUT.print("IP address: ");
@ -22,6 +21,5 @@ void setup() {
void loop() { void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
server_loop(); 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 if(week_counter == 2*WEEK){ // sent all data (ret 0), reset counter
week_counter = 0; week_counter = 0;
DEBUG_OUT.println("weekly end");
return 0; return 0;
} }
String to_send = ""; String to_send = "";
@ -51,7 +52,6 @@ void server_setup(){
return to_send.length(); return to_send.length();
}); });
request->send(response); request->send(response);
DEBUG_OUT.println("weekly end");
}); });
server.on("/monthly", HTTP_ANY, [](AsyncWebServerRequest * request) { 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 if(month_counter == 2*MONTH){ // sent all data (ret 0), reset counter
month_counter = 0; month_counter = 0;
DEBUG_OUT.println("monthly done");
return 0; return 0;
} }
String to_send = ""; String to_send = "";
@ -76,8 +77,18 @@ void server_setup(){
return to_send.length(); return to_send.length();
}); });
request->send(response); 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){ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){