-
Notifications
You must be signed in to change notification settings - Fork 21
/
sleeptimer.php
54 lines (45 loc) · 1.64 KB
/
sleeptimer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
const IS_ROMONITOR = true;
// This is required for pcntl_signal to work
pcntl_async_signals(true);
require_once ("includes/vars.php");
require_once ("includes/functions.php");
$opts = getopt('', ['currenthost:', 'sleeptime:', 'fadetime:']);
prefs::set_pref($opts);
$ramping = false;
$volume = 100;
$player = null;
// If we are terminated while ramping we need to reset the volume
pcntl_signal(SIGTERM, "term_handler");
logger::mark("SLEEPTIMER", "Using Player",prefs::currenthost());
logger::info('SLEEPTIMER', 'Sleeping for',format_time(prefs::get_pref('sleeptime')));
sleep((int) prefs::get_pref('sleeptime'));
$fadetime = prefs::get_pref('fadetime') * 60;
logger::mark(prefs::currenthost(), 'Starting Sleep Timer Volume Ramp over', $fadetime, 'seconds');
$player = new base_mpd_player();
$mpd_status = $player->do_command_list(['status']);
$volume = $mpd_status['volume'];
prefs::$database = new timers();
if ($mpd_status['state'] == 'play') {
$ramping = true;
$player->ramp_volume($volume, 0, $fadetime);
// Mark the timer as finished. The UI will react to the state change
// callback when we pause it, which will mark the timer as not running.
prefs::$database->sleep_timer_finished();
$player->do_command_list(['pause']);
sleep(1);
$player->do_command_list(['setvol '.$volume]);
$ramping = false;
} else {
prefs::$database->sleep_timer_finished();
}
prefs::$database->close_database();
function term_handler($signo) {
global $ramping, $volume, $player;
if ($ramping) {
logger::log(prefs::currenthost(), "Sleep Timer Terminated while ramping. Resetting volume to", $volume);
$player->do_command_list(['setvol '.$volume]);
}
exit(0);
}
?>