From 65408fec716a3dff6d712a9577623e00306ec857 Mon Sep 17 00:00:00 2001 From: femto-code <59444046+femto-code@users.noreply.github.com> Date: Sat, 6 Feb 2021 23:00:41 +0100 Subject: [PATCH] Reworked shutdown handling Use new command to retrieve system information about power events in future but keep old command for compatibility reasons Fixed bug concerning authentication :bug: --- backend/serv.php | 23 +++++++++++++++++++-- js/main.js | 52 +++++++++++++++++++++++++++--------------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/backend/serv.php b/backend/serv.php index 4d40a44..b965127 100644 --- a/backend/serv.php +++ b/backend/serv.php @@ -31,8 +31,27 @@ } exit(); } +function getShutdownEventsInfo(){ + // system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)"); + // old command which is not very comfortable in order to retrieve which event is scheduled (poweroff vs reboot) + // For now use old one for compatibility reasons which I dont know so far + $return=array(); + $output2=shell_exec("busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown"); + if($output2==""){ + $output=shell_exec("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)"); + $return["date"]=$output; + $return["act"]="unknown"; + }else{ + $strings=explode(" ", $output2); + // The output specifies the shutdown time as microseconds since the Unix epoch -> divide by 1000 + //echo gettype($strings[2]); + $return["date"]=round(floatval($strings[2]) / 1000); // Unix milliseconds + $return["act"]=explode("\"", $strings[1])[1]; + } + return $return; +} if(isset($_GET["checkShutdown"])){ - system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)"); + echo json_encode(getShutdownEventsInfo()); exit(); }else if(isset($_GET["cancelShutdown"])){ system('sudo /sbin/shutdown -c'); @@ -58,6 +77,6 @@ }else{ echo "false"; } - system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)"); + echo json_encode(getShutdownEventsInfo()); } ?> diff --git a/js/main.js b/js/main.js index da69ae6..23152ea 100644 --- a/js/main.js +++ b/js/main.js @@ -61,22 +61,22 @@ function authorize() { return; } else { var vReq = new ntwReq("backend/serv.php?p=" + pass+"&a="+act+"&time="+time, function (data) { - console.log(this.responseText); - if(this.responseText.indexOf("true") > -1){ + console.log(data.responseText); + if(data.responseText.indexOf("true") > -1){ document.getElementById("currentState").innerHTML = "