Skip to content

Commit 96be994

Browse files
author
gemi254
committed
Added websockets connections handler
1 parent 714c2f6 commit 96be994

File tree

1 file changed

+118
-27
lines changed

1 file changed

+118
-27
lines changed

examples/ControlAssist-BrowserLog/remoteLogViewer.h

Lines changed: 118 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,41 @@ body{
1515
font-family: monospace;
1616
font-size: 0.7rem;
1717
}
18+
19+
#conLed {
20+
width: 10px;
21+
height: 10px;
22+
background-color: lightGray;
23+
border-radius: 10px;
24+
25+
}
26+
#conLed.on {
27+
background-color: lightGreen;
28+
border: 1px solid #4CAF50;
29+
}
1830
</style>
1931
<body>
32+
<table><tr>
33+
<td><div id="conLed">&nbsp;</td>
34+
<td><span id="wsStatus">Disconnected</span></td>
35+
</tr>
36+
</table>
37+
2038
<input type="text" id="logLine" style="Display: None;">
2139
<span id="logText"></span>
40+
41+
<script>
42+
document.getElementById("wsStatus").addEventListener("change", (event) => {
43+
conLed = document.getElementById("conLed")
44+
if(event.target.innerHTML.startsWith("Connected: ")){
45+
conLed.classList.add("on");
46+
}else{
47+
conLed.classList.remove("on");
48+
}
49+
conLed.title = event.target.innerHTML
50+
});
51+
</script>
52+
2253
</body>
2354
)=====";
2455

@@ -74,9 +105,20 @@ const toColor = (line) => {
74105
)=====";
75106

76107
#include <ControlAssist.h>
77-
78-
static class RemoteLogViewer *pLogView = NULL; // Pointer to instance of log print viewer class
79-
#define MAX_LOG_BUFFER_LEN 8192 // Maximum size of log buffer to store when disconnected
108+
// Log print arguments
109+
#define MAX_LOG_BUFFER_LEN 8192 // Maximum size of log buffer to store when disconnected
110+
#define MAX_LOG_FMT 256 // Maximum size of log format
111+
static char fmtBuf[MAX_LOG_FMT]; // Format buffer
112+
static char outBuf[512]; // Output buffer
113+
static va_list arglist;
114+
static File log_file;
115+
// Logger file open/close
116+
// Must called before LOG_I function to write to file and
117+
// closed on application shutdown
118+
#define LOGGER_OPEN_LOG() if(!log_file) log_file = STORAGE.open(LOGGER_LOG_FILENAME, "a+")
119+
#define LOGGER_CLOSE_LOG() if(log_file) log_file.close()
120+
static class RemoteLogViewer *_pLogView = NULL; // Pointer to instance of log print viewer class
121+
void wsStatusChange(); // Handles connect / disconnect
80122

81123
// Remote log print viewer class
82124
class RemoteLogViewer: public ControlAssist{
@@ -87,35 +129,84 @@ class RemoteLogViewer: public ControlAssist{
87129
RemoteLogViewer(uint16_t port) { ControlAssist::setPort(port); _logBuffer = ""; }
88130
virtual ~RemoteLogViewer() { }
89131
public:
90-
void setup(){
132+
void init(){
91133
// Must not initalized
92-
if(pLogView){
93-
LOG_E("Already initialized with addr: %p\n", pLogView);
134+
if(_pLogView){
135+
LOG_E("Already initialized with addr: %p\n", _pLogView);
94136
return;
95137
}
96138
// Store this instance pointer to global
97-
pLogView = this;
139+
_pLogView = this;
98140
// Setup control assist
99141
ControlAssist::setHtmlBody(LOGGER_VIEWER_HTML_BODY);
100142
ControlAssist::setHtmlFooter(LOGGER_VIEWER_HTML_SCRIPT);
143+
ControlAssist::bind("wsStatus","Disconnected", wsStatusChange);
101144
ControlAssist::bind("logLine");
102145
}
146+
// Delete the log file
147+
void resetLogFile(){
148+
if(!STORAGE.exists(LOGGER_LOG_FILENAME)) return;
149+
LOG_W("Reseting log\n");
150+
LOGGER_CLOSE_LOG();
151+
STORAGE.remove(LOGGER_LOG_FILENAME);
152+
// Reopen log file to store log lines
153+
LOGGER_OPEN_LOG();
154+
}
155+
156+
// Send server responce on /log
157+
void handleLog(WEB_SERVER &server){
158+
String res = "";
159+
while( getHtmlChunk(res) ){
160+
server.sendContent(res);
161+
}
162+
server.sendContent("");
163+
}
164+
// Reset or display log file
165+
void handleLogFile(WEB_SERVER &server){
166+
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
167+
if(server.hasArg("reset")){
168+
resetLogFile();
169+
server.sendContent("Reseted log");
170+
return;
171+
}
172+
if(!STORAGE.exists(LOGGER_LOG_FILENAME)){
173+
server.sendContent("Logging file not exists");
174+
return;
175+
}
176+
LOGGER_CLOSE_LOG();
177+
// Open for reading
178+
File file = STORAGE.open(LOGGER_LOG_FILENAME, "r");
179+
// Send log file contents
180+
String res = "";
181+
while( file.available() ){
182+
res = file.readStringUntil('\n') + "\n";
183+
server.sendContent(res);
184+
}
185+
file.close();
186+
server.sendContent("");
187+
// Reopen log file to store log lines
188+
LOGGER_OPEN_LOG();
189+
}
190+
void sendBuffer(){
191+
if(_logBuffer == "") return;
192+
String logLine = "";
193+
int l = 0;
194+
// Send log buffer lines
195+
while(_logBuffer.length() > 0){
196+
l = _logBuffer.indexOf("\n", 0);
197+
if(l < 0) break;
198+
logLine = _logBuffer.substring(0, l );
199+
ControlAssist::put("logLine", logLine );
200+
_logBuffer = _logBuffer.substring(l + 1 , _logBuffer.length() );
201+
}
202+
}
103203

104204
// Custom log print function
105205
void print(String line){
106206
// Are clients connected?
107207
if(ControlAssist::getClientsNum()>0){
108208
if(_logBuffer != ""){
109-
String logLine = "";
110-
int l = 0;
111-
// Send log buffer lines
112-
while(_logBuffer.length() > 0){
113-
l = _logBuffer.indexOf("\n", 0);
114-
if(l < 0) break;
115-
logLine = _logBuffer.substring(0, l );
116-
ControlAssist::put("logLine", logLine );
117-
_logBuffer = _logBuffer.substring(l + 1 , _logBuffer.length() );
118-
}
209+
sendBuffer();
119210
_logBuffer = "";
120211
}
121212
// Send current line
@@ -128,33 +219,33 @@ class RemoteLogViewer: public ControlAssist{
128219
if(_logBuffer.length() > MAX_LOG_BUFFER_LEN){
129220
int l = _logBuffer.indexOf("\n", 0);
130221
if(l >= 0)
131-
_logBuffer = _logBuffer.substring(l + 1, _logBuffer.length() - 1);
222+
_logBuffer = _logBuffer.substring(l + 1, _logBuffer.length());
132223
}
133224
}
134225
public:
135226
String _logBuffer; // String buffer to hold log lines
136227
};
137228

138-
// Log print arguments
139-
#define MAX_LOG_FMT 256 // Maximum size of log format
140-
static char fmtBuf[MAX_LOG_FMT]; // Format buffer
141-
static char outBuf[512]; // Output buffer
142-
static va_list arglist;
143-
static File log_file;
144-
145-
// Custom log print function.. Prints on pLogView instance
229+
// Custom log print function.. Prints on _pLogView instance
146230
void _log_printf(const char *format, ...){
147231
strncpy(fmtBuf, format, MAX_LOG_FMT);
148232
fmtBuf[MAX_LOG_FMT - 1] = 0;
149233
va_start(arglist, format);
150234
vsnprintf(outBuf, MAX_LOG_FMT, fmtBuf, arglist);
151235
va_end(arglist);
152236
Serial.print(outBuf);
153-
if(pLogView) pLogView->print(outBuf);
237+
if(_pLogView) _pLogView->print(outBuf);
154238
if(log_file){
155239
log_file.print(outBuf);
156240
log_file.flush();
157241
}
158242
}
243+
// Handle websocket connections
244+
void wsStatusChange(){
245+
if(!_pLogView) return;
246+
String wsStatus = _pLogView->getVal("wsStatus");
247+
if(wsStatus.startsWith("Connected:"))
248+
_pLogView->sendBuffer();
249+
}
159250

160251
#endif

0 commit comments

Comments
 (0)