mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-03-04 06:27:06 +00:00
Relocated uptime-for, formatted using prettier
This commit is contained in:
@ -47,26 +47,6 @@ static char *HASS_QOS_CONFIG = " qos: 1\n";
|
||||
static char *HASS_MQTT_NODE = "mqtt:\n";
|
||||
static char *HASS_LIGHT_NODE = " light:\n";
|
||||
|
||||
/*
|
||||
function send_ha_disc(){
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/ha_discovery?prefix="+document.getElementById("ha_disc_topic").value, false);
|
||||
xhr.onload = function() {
|
||||
if (xhr.status === 200) {
|
||||
alert("MQTT discovery queued");
|
||||
}
|
||||
else if (xhr.status === 404) {
|
||||
alert("Error invoking ha_discovery");
|
||||
}
|
||||
}
|
||||
xhr.onerror = function() {
|
||||
alert("Error invoking ha_discovery");
|
||||
}
|
||||
xhr.send();
|
||||
}
|
||||
*/
|
||||
const char HomeAssistantDiscoveryScript[] = "<script>function send_ha_disc(){var xhr=new XMLHttpRequest();xhr.open(\"GET\",\"/ha_discovery?prefix=\"+document.getElementById(\"ha_disc_topic\").value,false);xhr.onload=function(){if(xhr.status===200){alert(\"MQTT discovery queued\")}else if(xhr.status===404){alert(\"Error invoking ha_discovery\")}};xhr.onerror=function(){alert(\"Error invoking ha_discovery\")};xhr.send()}</script>";
|
||||
|
||||
typedef struct template_s {
|
||||
void (*setter)();
|
||||
const char *name;
|
||||
@ -567,12 +547,10 @@ int http_fn_index(http_request_t *request) {
|
||||
|
||||
poststr(request, htmlFooterRefreshLink);
|
||||
http_html_end(request);
|
||||
|
||||
poststr(request, pageScript);
|
||||
}
|
||||
|
||||
poststr(request, NULL);
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_fn_about(http_request_t *request){
|
||||
@ -1419,7 +1397,7 @@ int http_fn_ha_cfg(http_request_t *request) {
|
||||
poststr(request,"<br/><div><label for=\"ha_disc_topic\">Discovery topic:</label><input id=\"ha_disc_topic\" value=\"homeassistant\"><button onclick=\"send_ha_disc();\">Start Home Assistant Discovery</button> <form action=\"cfg_mqtt\" style=\"display:inline-block;\"><button type=\"submit\">Configure MQTT</button></form></div><br/>");
|
||||
poststr(request,htmlFooterReturnToCfgLink);
|
||||
http_html_end(request);
|
||||
poststr(request, HomeAssistantDiscoveryScript);
|
||||
poststr(request, ha_discovery_script);
|
||||
poststr(request, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -171,7 +171,6 @@ void http_setup(http_request_t *request, const char *type){
|
||||
}
|
||||
|
||||
void http_html_start(http_request_t *request, const char *pagename) {
|
||||
// void HTTP_AddHeader(http_request_t *request) {
|
||||
poststr(request, htmlDoctype);
|
||||
poststr(request, "<title>");
|
||||
poststr(request, CFG_GetDeviceName()); // todo: check escaping
|
||||
@ -188,7 +187,6 @@ void http_html_start(http_request_t *request, const char *pagename) {
|
||||
}
|
||||
|
||||
void http_html_end(http_request_t *request) {
|
||||
// was void HTTP_AddBuildFooter(http_request_t *request) {
|
||||
char upTimeStr[128];
|
||||
unsigned char mac[32];
|
||||
|
||||
@ -196,9 +194,8 @@ void http_html_end(http_request_t *request) {
|
||||
poststr(request, htmlFooterInfo);
|
||||
poststr(request, "<br>");
|
||||
poststr(request, g_build_str);
|
||||
poststr(request, "<br>Online for ");
|
||||
misc_formatUpTimeString(Time_getUpTimeSeconds(), upTimeStr);
|
||||
poststr(request, upTimeStr);
|
||||
|
||||
hprintf128(request, "<br>Online for <span id=\"onlineFor\" data-initial=\"%i\">-</span>", Time_getUpTimeSeconds());
|
||||
|
||||
WiFI_GetMacAddress((char *)mac);
|
||||
|
||||
@ -208,6 +205,7 @@ void http_html_end(http_request_t *request) {
|
||||
poststr(request, upTimeStr);
|
||||
|
||||
poststr(request, htmlBodyEnd);
|
||||
poststr(request, pageScript);
|
||||
}
|
||||
|
||||
const char *http_checkArg(const char *p, const char *n) {
|
||||
@ -420,38 +418,6 @@ int poststr(http_request_t *request, const char *str){
|
||||
return postany(request, str, strlen(str));
|
||||
}
|
||||
|
||||
void misc_formatUpTimeString(int totalSeconds, char *o) {
|
||||
int rem_days;
|
||||
int rem_hours;
|
||||
int rem_minutes;
|
||||
int rem_seconds;
|
||||
|
||||
rem_days = totalSeconds / (24*60*60);
|
||||
totalSeconds = totalSeconds % (24*60*60);
|
||||
rem_hours = totalSeconds / (60*60);
|
||||
totalSeconds = totalSeconds % (60*60);
|
||||
rem_minutes = totalSeconds / (60);
|
||||
rem_seconds = totalSeconds % 60;
|
||||
|
||||
*o = 0;
|
||||
if(rem_days > 0)
|
||||
{
|
||||
sprintf(o,"%i days, %i hours, %i minutes and %i seconds ",rem_days,rem_hours,rem_minutes,rem_seconds);
|
||||
}
|
||||
else if(rem_hours > 0)
|
||||
{
|
||||
sprintf(o,"%i hours, %i minutes and %i seconds ",rem_hours,rem_minutes,rem_seconds);
|
||||
}
|
||||
else if(rem_minutes > 0)
|
||||
{
|
||||
sprintf(o,"%i minutes and %i seconds ",rem_minutes,rem_seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(o,"just %i seconds ",rem_seconds);
|
||||
}
|
||||
}
|
||||
|
||||
int hprintf128(http_request_t *request, const char *fmt, ...){
|
||||
va_list argList;
|
||||
//BaseType_t taken;
|
||||
@ -641,5 +607,9 @@ const char htmlHeadStyle[]="<style>div,fieldset,input,select{padding:5px;font-si
|
||||
//region_end htmlHeadStyle
|
||||
|
||||
//region_start pageScript
|
||||
const char pageScript[]="<script type='text/javascript'>var firstTime,lastTime,req=null;function showState(){clearTimeout(firstTime),clearTimeout(lastTime),null!=req&&req.abort(),(req=new XMLHttpRequest).onreadystatechange=()=>{var e;4==req.readyState&&200==req.status&&((\"INPUT\"!=document.activeElement.tagName||\"number\"!=document.activeElement.type&&\"color\"!=document.activeElement.type)&&(e=req.responseText,eb(\"state\").innerHTML=e),clearTimeout(firstTime),clearTimeout(lastTime),lastTime=setTimeout(showState,3e3))},req.open(\"GET\",\"index?state=1\",!0),req.send(),firstTime=setTimeout(showState,3e3)}eb=e=>document.getElementById(e),window.addEventListener(\"load\",showState),history.pushState(null,\"\",\"index\"),setTimeout(()=>{eb(\"changed\").innerHTML=\"\"},5e3);</script>";
|
||||
//region_end pageScript
|
||||
const char pageScript[]="<script type='text/javascript'>var firstTime,lastTime,onlineFor,req=null,onlineForEl=null,getElement=e=>document.getElementById(e);function showState(){clearTimeout(firstTime),clearTimeout(lastTime),null!=req&&req.abort(),(req=new XMLHttpRequest).onreadystatechange=()=>{4==req.readyState&&200==req.status&&((\"INPUT\"!=document.activeElement.tagName||\"number\"!=document.activeElement.type&&\"color\"!=document.activeElement.type)&&(getElement(\"state\").innerHTML=req.responseText),clearTimeout(firstTime),clearTimeout(lastTime),lastTime=setTimeout(showState,3e3))},req.open(\"GET\",\"index?state=1\",!0),req.send(),firstTime=setTimeout(showState,3e3)}function fmtUpTime(e){var t,n,o=Math.floor(e/86400);return e%=86400,t=Math.floor(e/3600),e%=3600,n=Math.floor(e/60),e=e%60,0<o?o+` days, ${t} hours, ${n} minutes and ${e} seconds`:0<t?t+` hours, ${n} minutes and ${e} seconds`:0<n?n+` minutes and ${e} seconds`:`just ${e} seconds`}function updateOnlineFor(){onlineForEl.textContent=fmtUpTime(++onlineFor)}function onLoad(){(onlineForEl=getElement(\"onlineFor\"))&&(onlineFor=parseInt(onlineForEl.dataset.initial,10))&&setInterval(updateOnlineFor,1e3),showState()}window.addEventListener(\"load\",onLoad),history.pushState(null,\"\",\"index\"),setTimeout(()=>{getElement(\"changed\").innerHTML=\"\"},5e3);</script>";
|
||||
//region_end pageScript
|
||||
|
||||
//region_start ha_discovery_script
|
||||
const char ha_discovery_script[]="<script type='text/javascript'>function send_ha_disc(){var e=new XMLHttpRequest;e.open(\"GET\",\"/ha_discovery?prefix=\"+document.getElementById(\"ha_disc_topic\").value,!1),e.onload=function(){200===e.status?alert(\"MQTT discovery queued\"):404===e.status&&alert(\"Error invoking ha_discovery\")},e.onerror=function(){alert(\"Error invoking ha_discovery\")},e.send()}</script>";
|
||||
//region_end ha_discovery_script
|
||||
@ -18,6 +18,7 @@ extern const char *g_build_str;
|
||||
|
||||
extern const char htmlHeadStyle[];
|
||||
extern const char pageScript[];
|
||||
extern const char ha_discovery_script[];
|
||||
|
||||
#define HTTP_RESPONSE_OK 200
|
||||
#define HTTP_RESPONSE_NOT_FOUND 404
|
||||
|
||||
@ -1,27 +1,80 @@
|
||||
var firstTime, lastTime, req=null;
|
||||
eb=s=>document.getElementById(s);
|
||||
var firstTime,
|
||||
lastTime,
|
||||
req = null;
|
||||
var onlineFor;
|
||||
var onlineForEl = null;
|
||||
|
||||
var getElement = (id) => document.getElementById(id);
|
||||
|
||||
// refresh status section every 3 seconds
|
||||
function showState() {
|
||||
function showState() {
|
||||
clearTimeout(firstTime);
|
||||
clearTimeout(lastTime);
|
||||
if (req!=null) { req.abort() }
|
||||
req=new XMLHttpRequest();
|
||||
req.onreadystatechange=()=>{
|
||||
if(req.readyState==4 && req.status==200){
|
||||
if (!(document.activeElement.tagName=='INPUT' &&
|
||||
(document.activeElement.type=='number' || document.activeElement.type=='color'))) {
|
||||
var s=req.responseText;
|
||||
eb('state').innerHTML=s;
|
||||
if (req != null) {
|
||||
req.abort();
|
||||
}
|
||||
req = new XMLHttpRequest();
|
||||
req.onreadystatechange = () => {
|
||||
if (req.readyState == 4 && req.status == 200) {
|
||||
if (
|
||||
!(
|
||||
document.activeElement.tagName == "INPUT" &&
|
||||
(document.activeElement.type == "number" || document.activeElement.type == "color")
|
||||
)
|
||||
) {
|
||||
getElement("state").innerHTML = req.responseText;
|
||||
}
|
||||
clearTimeout(firstTime);
|
||||
clearTimeout(lastTime);
|
||||
lastTime=setTimeout(showState, 3e3);
|
||||
}};
|
||||
req.open('GET','index?state=1', true);
|
||||
lastTime = setTimeout(showState, 3e3);
|
||||
}
|
||||
};
|
||||
req.open("GET", "index?state=1", true);
|
||||
req.send();
|
||||
firstTime=setTimeout(showState, 3e3);
|
||||
firstTime = setTimeout(showState, 3e3);
|
||||
}
|
||||
window.addEventListener('load', showState);
|
||||
history.pushState(null, '', 'index'); // drop actions like 'toggle' from URL
|
||||
setTimeout(()=>{eb('changed').innerHTML=''}, 5e3); // hide change info
|
||||
|
||||
function fmtUpTime(totalSeconds) {
|
||||
var days, hours, minutes, seconds;
|
||||
|
||||
days = Math.floor(totalSeconds / (24 * 60 * 60));
|
||||
totalSeconds = totalSeconds % (24 * 60 * 60);
|
||||
hours = Math.floor(totalSeconds / (60 * 60));
|
||||
totalSeconds = totalSeconds % (60 * 60);
|
||||
minutes = Math.floor(totalSeconds / 60);
|
||||
seconds = totalSeconds % 60;
|
||||
|
||||
if (days > 0) {
|
||||
return `${days} days, ${hours} hours, ${minutes} minutes and ${seconds} seconds`;
|
||||
}
|
||||
if (hours > 0) {
|
||||
return `${hours} hours, ${minutes} minutes and ${seconds} seconds`;
|
||||
}
|
||||
if (minutes > 0) {
|
||||
return `${minutes} minutes and ${seconds} seconds`;
|
||||
}
|
||||
return `just ${seconds} seconds`;
|
||||
}
|
||||
|
||||
function updateOnlineFor() {
|
||||
onlineForEl.textContent = fmtUpTime(++onlineFor);
|
||||
}
|
||||
|
||||
function onLoad() {
|
||||
onlineForEl = getElement("onlineFor");
|
||||
if (onlineForEl) {
|
||||
onlineFor = parseInt(onlineForEl.dataset.initial, 10); //We have some valid value
|
||||
if (onlineFor) {
|
||||
setInterval(updateOnlineFor, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
showState();
|
||||
}
|
||||
|
||||
window.addEventListener("load", onLoad);
|
||||
history.pushState(null, "", "index"); // drop actions like 'toggle' from URL
|
||||
|
||||
setTimeout(() => {
|
||||
getElement("changed").innerHTML = "";
|
||||
}, 5e3); // hide change info
|
||||
|
||||
19
src/httpserver/script_ha_discovery.js
Normal file
19
src/httpserver/script_ha_discovery.js
Normal file
@ -0,0 +1,19 @@
|
||||
function send_ha_disc() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open(
|
||||
"GET",
|
||||
"/ha_discovery?prefix=" + document.getElementById("ha_disc_topic").value,
|
||||
false
|
||||
);
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
alert("MQTT discovery queued");
|
||||
} else if (xhr.status === 404) {
|
||||
alert("Error invoking ha_discovery");
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
alert("Error invoking ha_discovery");
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
Reference in New Issue
Block a user