Skip to content

Commit

Permalink
Back to SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
vitotai committed Aug 24, 2016
1 parent c43be4b commit a7a52b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 78 deletions.
65 changes: 20 additions & 45 deletions BrewPiLess/BrewPiLess.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ extern "C" {
#include "ESPUpdateServer.h"
#include "WifiSetup.h"

#define UseWebSocket true

#if UseWebSocket != true
#include "AsyncServerSideEvent.h"
#endif

#include "BrewPiProxy.h"


//WebSocket seems to be unstable, at least on iPhone.
//Go back to ServerSide Event.
#define UseWebSocket false
#define UseServerSideEvent true

/**************************************************************************************/
/* Start of Configuration */
Expand Down Expand Up @@ -89,11 +86,8 @@ R"END(
#define PROFILE_FILENAME "/brewing.json"
#define CONFIG_FILENAME "/brewpi.cfg"

#if UseWebSocket == true
#define WS_PATH "/websocket"
#else
#define SSE_PATH "/getline"
#endif

#define POLLING_PATH "/getline_p"
#define PUTLINE_PATH "/putline"
Expand Down Expand Up @@ -127,9 +121,10 @@ BrewKeeper brewKeeper([](const char* str){ brewPi.putLine(str);});
DataLogger dataLogger;
#endif

#if UseWebSocket != true
AsyncServerSideEventServer sse(SSE_PATH);
#if UseServerSideEvent == true
AsyncEventSource sse(SSE_PATH);
#endif

// use in sprintf, put into PROGMEM complicates it.
const char *confightml=R"END(
<html><head><title>Configuration</title></head><body>
Expand Down Expand Up @@ -399,9 +394,7 @@ BrewPiWebHandler brewPiWebHandler;


#if UseWebSocket == true

AsyncWebSocket ws(WS_PATH);

void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
{
if(type == WS_EVT_CONNECT){
Expand Down Expand Up @@ -453,42 +446,20 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
}
}
}
#endif //#if UseWebSocket == true

void stringAvailable(const char *str)
{
DBG_PRINTF("BroadCast:%s\n",str);
ws.textAll(str,strlen(str));
}

#else //#if UseWebSocket == true

uint8_t clientCount;
void sseEventHandler(AsyncServerSideEventServer * server, AsyncServerSideEventClient * client, SseEventType type)
{
// DBG_PRINTF("eventHandler type:\n");
// DBG_PRINTF(type);

if(type ==SseEventConnected){

clientCount ++;
DBG_PRINTF("***New connection, current client number:%d\n",clientCount);

char *line=brewPi.getLastLine();

if(line[0]!='\0') client->sendData(line);
else client->sendData("");
}else if (type ==SseEventDisconnected){
clientCount --;
DBG_PRINTF("***Disconnected, current client number:%d\n",clientCount);
}
}
#if UseWebSocket == true
ws.textAll(str,strlen(str));
#endif

void stringAvailable(const char *str)
{
DBG_PRINTF("BroadCast:%s\n",str);
sse.broadcastData(str);
#if UseServerSideEvent == true
sse.send(str);
#endif
}
#endif //#if UseWebSocket == true

//{brewpi

Expand Down Expand Up @@ -715,8 +686,12 @@ void setup(void){
#if UseWebSocket == true
ws.onEvent(onWsEvent);
server.addHandler(&ws);
#else
sse.onEvent(sseEventHandler);
#endif

#if UseServerSideEvent == true
sse.onConnect([](AsyncEventSourceClient *client){
DBG_PRINTF("SSE Connect\n");
});
server.addHandler(&sse);
#endif

Expand Down
4 changes: 2 additions & 2 deletions BrewPiLess/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@

#define PIN_SDA NODEMCU_PIN_D2
#define PIN_SCL NODEMCU_PIN_D1
#define IIC_LCD_ADDRESS 0x3F //0x27
#define IIC_LCD_ADDRESS 0x27
#define BREWPI_IIC_LCD 1
#define oneWirePin NODEMCU_PIN_D6 // If oneWirePin is specified, beerSensorPin and fridgeSensorPin are ignored

Expand All @@ -210,7 +210,7 @@
#define doorPin NODEMCU_PIN_D3


#define BACKLIGHT_AUTO_OFF_PERIOD 600
#define BACKLIGHT_AUTO_OFF_PERIOD 0 //600

// Pay attention when changing the pins for the rotary encoder.
// They should be connected to external interrupt INT0, INT1 and INT3
Expand Down
64 changes: 34 additions & 30 deletions BrewPiLess/data/bwf.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ process:function(msg){
this.raw(msg);
return;
}
//console.log("rcv:" + msg);
console.log("rcv:" + msg);
eval("m={" + msg + "}");
// console.log("json:"+m);
for(var key in m){
Expand All @@ -54,46 +54,50 @@ on:function(lb,handler){
},
send:function(data,opt){
opt = (typeof opt == "undefined")? {}:opt;
//console.log("snd:" + data);
console.log("snd:" + data);
var b=this;
b.ws.send(data);
invoke({m:"POST", url:"/putline",mime:"application/x-www-form-urlencoded",
data:"data="+encodeURI(data),
success:function(){if(typeof opt.success !=="undefined") opt.success();},
fail:function(a){if(typeof opt.fail !=="undefined") opt.fail(a); else b.error(a);}
});
},
connect:function(){
var b=this;
b.ws= new WebSocket('ws://'+document.location.host+'/websocket');

b.ws.onopen = function(){
console.log("Connected");
b.onopen();
};
b.ws.onclose = function(){
console.log("Disconnected");
setTimeout(function(){
b.connect();
},1500);
b.onclose();
};
b.ws.onerror = function(e){
console.log("ws error", e);
b.error(e);
};
b.ws.onmessage = function(e){
var es = new EventSource("/getline");
es.onmessage = function(e) {
b.process(e.data);
};
es.onerror=function(){
setTimeout(function(){
b.connnect();
},3100);
// b.error(-2);
};
setTimeout(function(){
b.onopen();
},1000);
},
init:function(arg){
var b=this;
this.error = (typeof arg.error == "undefined")? function(e){alert("error:"+e);}:arg.error;
this.handlers=(typeof arg.handlers == "undefined")? {}:arg.handlers;
this.raw=(typeof arg.raw == "undefined")? null:arg.raw;
this.onopen=(typeof arg.onopen == "undefined")? function(){}:arg.onopen;
this.onclose=(typeof arg.onclose == "undefined")? function(){console.log("connection close");}:arg.onclose;
if(typeof WebSocket ==="undefined") {
console.log("not support S");
alert("WebSocket not Supporte");
b.error = (typeof arg.error == "undefined")? function(){}:arg.error;
b.handlers=(typeof arg.handlers == "undefined")? {}:arg.handlers;
b.raw=(typeof arg.raw == "undefined")? null:arg.raw;
b.onopen=(typeof arg.onopen == "undefined")? function(){}:arg.onopen;

if(typeof EventSource ==="undefined") {
console.log("not support SSE");
b.sse=false;
var p=(typeof arg.polling == "undefined")? 5000:arg.polling;
// setup timer
setInterval(function(){
invoke({m:"GET",url:"/getline_p",
success:function(d){b.process(d);},
fail:function(a){b.error(a);}
});
},p);
return;
}

b.connect();
},
save:function(file,data,success,fail){
Expand Down
2 changes: 1 addition & 1 deletion BrewPiLess/data_bwf_js.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const char data_bwf_js[] PROGMEM =
R"END(
function invoke(b){var c=new XMLHttpRequest();c.onreadystatechange=function(){if(c.readyState==4){if(c.status==200){b.success(c.responseText)}else{c.onerror(c.status)}}};c.ontimeout=function(){if(typeof b["timeout"]!="undefined")b.timeout();else c.onerror(-1)},c.onerror=function(a){if(typeof b["fail"]!="undefined")b.fail(a)};c.open(b.m,b.url,true);if(typeof b["data"]!="undefined"){c.setRequestHeader("Content-Type",(typeof b["mime"]!="undefined")?b["mime"]:"application/x-www-form-urlencoded");c.send(b.data)}else c.send()}var BWF={BrewProfile:"/brewing.json",sse:true,process:function(a){if(this.raw!=null){this.raw(a);return}eval("m={"+a+"}");for(var b in m){if(typeof this.handlers[b]!="undefined"){this.handlers[b](m[b])}}},on:function(a,b){this.handlers[a]=b},send:function(a,c){c=(typeof c=="undefined")?{}:c;var b=this;b.ws.send(a)},connect:function(){var b=this;b.ws=new WebSocket('ws://'+document.location.host+'/websocket');b.ws.onopen=function(){console.log("Connected");b.onopen()};b.ws.onclose=function(){console.log("Disconnected");setTimeout(function(){b.connect()},1500);b.onclose()};b.ws.onerror=function(e){console.log("ws error",e);b.error(e)};b.ws.onmessage=function(e){b.process(e.data)}},init:function(a){var b=this;this.error=(typeof a.error=="undefined")?function(e){alert("error:"+e)}:a.error;this.handlers=(typeof a.handlers=="undefined")?{}:a.handlers;this.raw=(typeof a.raw=="undefined")?null:a.raw;this.onopen=(typeof a.onopen=="undefined")?function(){}:a.onopen;this.onclose=(typeof a.onclose=="undefined")?function(){console.log("connection close")}:a.onclose;if(typeof WebSocket==="undefined"){console.log("not support S");alert("WebSocket not Supporte");return}b.connect()},save:function(a,b,c,d){invoke({m:"POST",url:"/fputs",data:"path="+a+"&content="+b,success:function(){c()},fail:function(e){d(e)}})},load:function(a,b,c){invoke({m:"GET",url:a,success:function(d){b(d)},fail:function(e){c(e)}})}};
function invoke(b){var c=new XMLHttpRequest();c.onreadystatechange=function(){if(c.readyState==4){if(c.status==200){b.success(c.responseText)}else{c.onerror(c.status)}}};c.ontimeout=function(){if(typeof b["timeout"]!="undefined")b.timeout();else c.onerror(-1)},c.onerror=function(a){if(typeof b["fail"]!="undefined")b.fail(a)};c.open(b.m,b.url,true);if(typeof b["data"]!="undefined"){c.setRequestHeader("Content-Type",(typeof b["mime"]!="undefined")?b["mime"]:"application/x-www-form-urlencoded");c.send(b.data)}else c.send()}var BWF={BrewProfile:"/brewing.json",sse:true,process:function(a){if(this.raw!=null){this.raw(a);return}eval("m={"+a+"}");for(var b in m){if(typeof this.handlers[b]!="undefined"){this.handlers[b](m[b])}}},on:function(a,b){this.handlers[a]=b},send:function(c,d){d=(typeof d=="undefined")?{}:d;var b=this;invoke({m:"POST",url:"/putline",mime:"application/x-www-form-urlencoded",data:"data="+encodeURI(c),success:function(){if(typeof d.success!=="undefined")d.success()},fail:function(a){if(typeof d.fail!=="undefined")d.fail(a);else b.error(a)}})},connect:function(){var b=this;var a=new EventSource("/getline");a.onmessage=function(e){b.process(e.data)};a.onerror=function(){setTimeout(function(){b.connnect()},3100)};setTimeout(function(){b.onopen()},1000)},init:function(c){var b=this;b.error=(typeof c.error=="undefined")?function(){}:c.error;b.handlers=(typeof c.handlers=="undefined")?{}:c.handlers;b.raw=(typeof c.raw=="undefined")?null:c.raw;b.onopen=(typeof c.onopen=="undefined")?function(){}:c.onopen;if(typeof EventSource==="undefined"){console.log("not support SSE");b.sse=false;var p=(typeof c.polling=="undefined")?5000:c.polling;setInterval(function(){invoke({m:"GET",url:"/getline_p",success:function(d){b.process(d)},fail:function(a){b.error(a)}})},p);return}b.connect()},save:function(a,b,c,d){invoke({m:"POST",url:"/fputs",data:"path="+a+"&content="+b,success:function(){c()},fail:function(e){d(e)}})},load:function(a,b,c){invoke({m:"GET",url:a,success:function(d){b(d)},fail:function(e){c(e)}})}};
)END";

0 comments on commit a7a52b8

Please sign in to comment.