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 = "
Authorization completed!";
$("#confbtn").html("
Saved");
- var res=this.responseText.split("_");
- outputShutdown(res[1],act);
+ var res=JSON.parse(data.responseText.split("true_")[1]);
+ outputShutdown(res.date,res.act);
setTimeout(function(){
$("#exampleModalCenter").modal("hide");
document.getElementById("pwrform").reset();
$("#pwrform input, select").prop("disabled","");
$("#confbtn").prop("disabled","");
document.getElementById("currentState").innerHTML = "";
- $("#confbtn").html("Confirm identity");
+ $("#confbtn").html("Confirm identity");
},3000);
- }else if(this.responseText=="wrongCredentials"){
- document.getElementById("currentState").innerHTML = "
Authorization failed!
";
+ }else if(data.responseText=="wrongCredentials"){
+ document.getElementById("currentState").innerHTML = "
Authorization failed!
";
}else{
document.getElementById("currentState").innerHTML = "
Error!
";
}
@@ -88,12 +88,14 @@ function authorize() {
function checkShutdown(callback) {
document.getElementById("currentState").innerHTML='
Checking for power events...
';
var vReq = new ntwReq("backend/serv.php?checkShutdown", function (data) {
- if(this.responseText==""){
+ console.log(data.responseText);
+ var res=JSON.parse(data.responseText);
+ if( (res.act=="") || (res.date==null) ){
document.getElementById("sys2").innerHTML="";
shutdownCurrent=false;
}else{
shutdownCurrent=true;
- outputShutdown(this.responseText,"unknown");
+ outputShutdown(res.date,res.act);
}
if(callback !== undefined){
callback();
@@ -115,8 +117,8 @@ function cancelShutdown(force) {
return;
}
var vReq = new ntwReq("backend/serv.php?cancelShutdown", function (data) {
- console.log(this.responseText);
- if(this.responseText==""){
+ console.log(data.responseText);
+ if(data.responseText==""){
console.log("Cancel response is empty");
mdtoast('
Power event was cancelled!', { type: 'success'});
checkShutdown();
@@ -129,12 +131,21 @@ function cancelShutdown(force) {
var dobj={Mon: "Monday", Tue: "Tuesday", Wed: "Wednesday", Thu: "Thursday", Fri: "Friday", Sat: "Saturday", Sun: "Sunday"};
function outputShutdown(data,act) {
- var toParse=data.split(" CEST ")[0];
- var day=data.substring(0,3);
- var s = data.replace(day,dobj[day]);
- s=s.split(" ")[0]+" "+s.split(" ")[1]+" "+s.split(" ")[2]+", "+s.split(" ")[5]+" "+s.split(" ")[3];
- scheduled=Date.parse(s);
- d = new Date(scheduled);
+ if(typeof data !== "number"){ // for compatibility reasons
+ data=data.replace("\n","");
+ console.log("Trying to process old info...");
+ var day=data.substring(0,3);
+ var s = data.replace(day,dobj[day]);
+ console.log(s);
+ s=s.split(" ")[0]+", "+s.split(" ")[3]+" "+s.split(" ")[1]+", "+s.split(" ")[6]+" "+s.split(" ")[4];
+ console.log(s);
+ scheduled=Date.parse(s);
+ d = new Date(scheduled);
+ }else{
+ d = new Date(data);
+ }
+
+
var restd = Math.floor((d.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
var resth = Math.floor((d.getTime() - Date.now()) / (1000 * 60 * 60)) % 24;
var restm = Math.floor((d.getTime() - Date.now()) / (1000 * 60)) % 60;
@@ -150,12 +161,7 @@ function outputShutdown(data,act) {
}
if(str==""){ str="
< 1 min"; }
console.log(str);
- var action = (act=="1") ? "shutdown" : "reboot";
- if(act=="unknown"){
- action="shutdown/reboot";
- }
- var c =toParse.split(" ");
- document.getElementById("sys2").innerHTML='
Planned to '+action+' at '+c[3]+' on '+c[0]+', '+c[1]+' '+c[2]+'
Remaining time: '+str+'
';
+ document.getElementById("sys2").innerHTML='
Scheduled power event: '+act+'
Remaining time: '+str+'
';
}
function shutdown(){