-
Notifications
You must be signed in to change notification settings - Fork 5
/
epgwakealarm.php
executable file
·105 lines (95 loc) · 3.56 KB
/
epgwakealarm.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/php
<?php
$script_path = dirname( __FILE__ );
chdir( $script_path );
include_once( $script_path . '/config.php');
include_once( INSTALL_PATH . '/DBRecord.class.php' );
include_once( INSTALL_PATH . '/Reservation.class.php' );
include_once( INSTALL_PATH . '/Settings.class.php' );
include_once( INSTALL_PATH . '/recLog.inc.php' );
$acpi_timer_path = "/sys/class/rtc/rtc0/wakealarm"; // ここは書き換える必要があるPCがあるかもしれない
$settings = Settings::factory();
$wakeupvars_text = '<?xml version="1.0" encoding="UTF-8" ?><epgwakeup></epgwakeup>';
if( file_exists( INSTALL_PATH. "/settings/wakeupvars.xml" ) )
$wakeupvars_text = file_get_contents( INSTALL_PATH. "/settings/wakeupvars.xml" );
$wakeupvars = new SimpleXMLElement($wakeupvars_text);
if(count($wakeupvars->getepg_time) == 0) {
$wakeupvars->getepg_time = 0;
}
if( strcasecmp( $argv[1], "start" ) == 0 ) {
try {
// 規定時間以内に予約はあるか
$recstart_time = "00:".sprintf("%02d", intval($settings->wakeup_before) + 5).":00";
$count = DBRecord::countRecords( RESERVE_TBL, " WHERE complete <> '1' AND starttime > now() AND starttime <= addtime(now(), '".$recstart_time."')" );
if( $count > 0 ) {
$wakeupvars->reason = "reserve";
}
else if( (intval($wakeupvars->getepg_time) + intval($settings->getepg_timer) * 3600 ) <= time() ) {
$wakeupvars->reason = "getepg";
exec( INSTALL_PATH."/getepg.php >/dev/null 2>&1" );
}
else {
$wakeupvars->reason = "other";
}
$wakeupvars->asXML(INSTALL_PATH. "/settings/wakeupvars.xml");
chmod(INSTALL_PATH. "/settings/wakeupvars.xml", 0666 );
}
catch( Exception $e ) {
//
}
exit();
}
else if( strcasecmp( $argv[1], "stop" ) == 0 ) {
try {
// 録画中はないか?
$count = DBRecord::countRecords( RESERVE_TBL, " WHERE complete <> '1' AND starttime < now() AND endtime > now()" );
if( $count != 0 ) {
// シャットダウン中止を試みる
exec( $settings->shutdown." -c" );
recLog("予約中にシャットダウンが実行された", EPGREC_WARN );
exit();
}
$waketime = 0;
// 次の予約録画の開始時刻は?
$nextreserves = DBRecord::createRecords( RESERVE_TBL, " WHERE complete <> '1' ORDER BY starttime LIMIT 10" );
$next_rectime = 0;
foreach( $nextreserves as $reserve ) {
$next_rectime = toTimestamp($reserve->starttime);
if( $next_rectime > time() ) break; // 現在より未来であるか?
}
$next_rectime = $next_rectime - 60 * intval($settings->wakeup_before);
if( $next_rectime < time() ) {
// シャットダウン中止を試みる
exec( $settings->shutdown." -c" );
recLog("予約録画開始".$settings->wakeup_before."分以内にシャットダウンが実行された", EPGREC_WARN );
exit();
}
// 次のgetepgの時間は?
$next_getepg_time = 0;
if( intval($wakeupvars->getepg_time) == 0 ) {
$next_getepg_time = time() + intval($settings->getepg_timer) * 3600; // 現在から設定時間後
}
else {
$next_getepg_time = intval($wakeupvars->getepg_time) + intval($settings->getepg_timer) * 3600;
if( $next_getepg_time < time() ) {
$next_getepg_time = time() + intval($settings->getepg_timer) * 3600;
}
}
if( $next_getepg_time < $next_rectime )
$waketime = $next_getepg_time;
else
$waketime = $next_rectime;
// いったんリセットする
$fp = fopen( $acpi_timer_path, "w" );
fwrite($fp , "0");
fclose($fp);
$fp = fopen( $acpi_timer_path, "w" );
fwrite($fp , "".$waketime );
fclose($fp);
}
catch( Exception $e ) {
//
}
exit();
}
?>