Created
May 7, 2021 18:03
-
-
Save agran/5a947f888c8de4c4b29b4d4775d81989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: red; icon-glyph: magic; | |
let req = new Request('http://192.168.1.111/agranAlarm/alarmTime.txt?rand='+Math.random()) | |
var wakeUpTimeOld = await req.loadString(); | |
var data = []; | |
var message = ''; | |
if(typeof wakeUpTimeOld === 'undefined' || wakeUpTimeOld == ''){ | |
wakeUpTimeOld = (new Date()).getTime(); | |
message = "Будильник отключён. На какое время установить?"; | |
}else{ | |
console.log(timeStringToDate(wakeUpTimeOld)); | |
console.log((new Date()).getTime()); | |
var tmDiff = timeStringToDate(wakeUpTimeOld) - (new Date()).getTime(); | |
tmDiff = secondsToTime(tmDiff/1000); | |
var tmDiffStr = tmDiff.h + "ч " + addZero(tmDiff.m)+'м'; | |
message = "Включён будильник на " + wakeUpTimeOld + ".\n" + "Cработает через " + tmDiffStr + ". На какое время установить?"; | |
wakeUpTimeOld = timeStringToDate(wakeUpTimeOld); | |
data.push("Отключить!"); | |
} | |
data.push(getButton(0.25, wakeUpTimeOld)); | |
data.push(getButton(0.5, wakeUpTimeOld)); | |
for (let i = 1; i < 13; i++) { | |
data.push(getButton(i, wakeUpTimeOld)); | |
} | |
Script.setShortcutOutput( {'res': data, 'message': message} ); | |
Script.complete(); | |
function getButton(plusHours, wakeUpTimeOld){ | |
if(plusHours<1){ | |
var newTime = getHoursMinutes(timePlusHours(wakeUpTimeOld, plusHours)); | |
return "+"+(plusHours*60)+"м (" + newTime + ")"; | |
}else{ | |
var newTime = getHoursMinutes(nowPlusHours(plusHours)); | |
return "+"+(plusHours)+"ч (" + newTime+ ")"; | |
} | |
} | |
function getHoursMinutes(tmInt){ | |
var tmbuf = new Date(tmInt); | |
return tmbuf.getHours()+':'+addZero(tmbuf.getMinutes()); | |
} | |
function timePlusHours(tmInt, plusHours){ | |
var tmbuf = new Date(tmInt); | |
tmbuf.setTime(tmbuf.getTime() + plusHours * 3600 * 1000); | |
return tmbuf.getTime(); | |
} | |
function nowPlusHours(plusHours){ | |
var tmbuf = new Date(); | |
return timePlusHours(tmbuf.getTime(), plusHours); | |
} | |
function timeStringToDate(timeStr){ | |
var today = new Date(); | |
var times = timeStr.split(':'); | |
var tmbuf = new Date(today.getFullYear(), today.getMonth(), today.getDate(), parseInt(times[0]), parseInt(times[1]), 0); | |
if (tmbuf.getTime() < (new Date()).getTime()) { | |
tmbuf.setTime(tmbuf.getTime() + 1000 * 60 * 60 * 24); | |
} | |
return tmbuf.getTime(); | |
} | |
function secondsToTime(secs) | |
{ | |
var hours = Math.floor(secs / (60 * 60)); | |
var divisor_for_minutes = secs % (60 * 60); | |
var minutes = Math.floor(divisor_for_minutes / 60); | |
var divisor_for_seconds = divisor_for_minutes % 60; | |
var seconds = Math.ceil(divisor_for_seconds); | |
var obj = { | |
"h": hours, | |
"m": minutes, | |
"s": seconds | |
}; | |
return obj; | |
} | |
function addZero(n) { | |
return (n < 10 ? "0" : "") + (n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment