comparison Reservation.class.php @ 39:8965ef108821

change: modify all scripts for web base setting.
author Sushi-k <epgrec@park.mda.or.jp>
date Tue, 28 Jul 2009 11:50:04 +0900
parents cbbddf99d1cd
children a935b4789aff
comparison
equal deleted inserted replaced
38:2bc96d657ba1 39:8965ef108821
1 <?php 1 <?php
2 include_once('config.php'); 2 include_once('config.php');
3 include_once( INSTALL_PATH . "/DBRecord.class.php" ); 3 include_once( INSTALL_PATH . "/DBRecord.class.php" );
4 include_once( INSTALL_PATH . "/reclib.php" ); 4 include_once( INSTALL_PATH . "/reclib.php" );
5 include_once( INSTALL_PATH . "/Settings.class.php" );
6
5 7
6 // 予約クラス 8 // 予約クラス
7 9
8 class Reservation { 10 class Reservation {
9 11
10 public static function simple( $program_id , $autorec = 0, $mode = 0) { 12 public static function simple( $program_id , $autorec = 0, $mode = 0) {
13 $settings = Settings::factory();
11 $rval = 0; 14 $rval = 0;
12 try { 15 try {
13 $prec = new DBRecord( TBL_PREFIX.PROGRAM_TBL, "id", $program_id ); 16 $prec = new DBRecord( PROGRAM_TBL, "id", $program_id );
14 17
15 $rval = self::custom( 18 $rval = self::custom(
16 $prec->starttime, 19 $prec->starttime,
17 $prec->endtime, 20 $prec->endtime,
18 $prec->channel_id, 21 $prec->channel_id,
40 $program_id = 0, // 番組ID 43 $program_id = 0, // 番組ID
41 $autorec = 0, // 自動録画 44 $autorec = 0, // 自動録画
42 $mode = 0 // 録画モード 45 $mode = 0 // 録画モード
43 ) { 46 ) {
44 global $RECORD_MODE; 47 global $RECORD_MODE;
45 48 $settings = Settings::factory();
49
46 // 時間を計算 50 // 時間を計算
47 $start_time = toTimestamp( $starttime ); 51 $start_time = toTimestamp( $starttime );
48 $end_time = toTimestamp( $endtime ) + EXTRA_TIME; 52 $end_time = toTimestamp( $endtime ) + $settings->extra_time;
49 53
50 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組 54 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組
51 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する 55 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する
52 } 56 }
53 $at_start = $start_time - PADDING_TIME; 57 $at_start = $start_time - PADDING_TIME;
54 $sleep_time = PADDING_TIME - FORMER_TIME; 58 $sleep_time = PADDING_TIME - $settings->former_time;
55 $rec_start = $start_time - FORMER_TIME; 59 $rec_start = $start_time - $settings->former_time;
56 60
57 // durationを計算しておく 61 // durationを計算しておく
58 $duration = $end_time - $rec_start; 62 $duration = $end_time - $rec_start;
59 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く 63 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く
60 throw new Exception( "終わりつつある/終わっている番組です" ); 64 throw new Exception( "終わりつつある/終わっている番組です" );
61 } 65 }
62 66
63 $rrec = null; 67 $rrec = null;
64 try { 68 try {
65 // 同一番組予約チェック 69 // 同一番組予約チェック
66 if( $program_id ) { 70 if( $program_id ) {
67 $num = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE program_id = '".$program_id."'" ); 71 $num = DBRecord::countRecords( RESERVE_TBL, "WHERE program_id = '".$program_id."'" );
68 if( $num ) { 72 if( $num ) {
69 throw new Exception("同一の番組が録画予約されています"); 73 throw new Exception("同一の番組が録画予約されています");
70 } 74 }
71 } 75 }
72 76
73 $crec = new DBRecord( TBL_PREFIX.CHANNEL_TBL, "id", $channel_id ); 77 $crec = new DBRecord( CHANNEL_TBL, "id", $channel_id );
74 78
75 // 既存予約数 = TUNER番号 79 // 既存予約数 = TUNER番号
76 $tuners = ($crec->type == "GR") ? GR_TUNERS : BS_TUNERS; 80 $tuners = ($crec->type == "GR") ? $settings->gr_tuners : $settings->bs_tuners;
77 $battings = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ". 81 $battings = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
78 "AND type = '".$crec->type."' ". 82 "AND type = '".$crec->type."' ".
79 "AND starttime < '".toDatetime($end_time) ."' ". 83 "AND starttime < '".toDatetime($end_time) ."' ".
80 "AND endtime > '".toDatetime($rec_start)."'" 84 "AND endtime > '".toDatetime($rec_start)."'"
81 ); 85 );
82 86
83 if( $battings >= $tuners ) { 87 if( $battings >= $tuners ) {
84 // 重複を発見した 88 // 重複を発見した
85 if( FORCE_CONT_REC ) { 89 if( FORCE_CONT_REC ) {
86 // 解消可能な重複かどうかを調べる 90 // 解消可能な重複かどうかを調べる
87 // 前後の予約数 91 // 前後の予約数
88 $nexts = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ". 92 $nexts = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
89 "AND type = '".$crec->type."' ". 93 "AND type = '".$crec->type."' ".
90 "AND starttime = '".toDatetime($end_time - FORMER_TIME)."'"); 94 "AND starttime = '".toDatetime($end_time - $settings->former_time)."'");
91 95
92 $prevs = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ". 96 $prevs = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
93 "AND type = '".$crec->type."' ". 97 "AND type = '".$crec->type."' ".
94 "AND endtime = '".$starttime."'" ); 98 "AND endtime = '".$starttime."'" );
95 99
96 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理 100 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理
97 if( ($battings - $nexts - $prevs) >= $tuners ) 101 if( ($battings - $nexts - $prevs) >= $tuners )
98 throw new Exception( "重複予約を解消できません" ); 102 throw new Exception( "重複予約を解消できません" );
99 103
100 // 直後の番組はあるか? 104 // 直後の番組はあるか?
101 if( $nexts ) { 105 if( $nexts ) {
102 // この番組の終わりをちょっとだけ早める 106 // この番組の終わりをちょっとだけ早める
103 $end_time = $end_time - FORMER_TIME - REC_SWITCH_TIME; 107 $end_time = $end_time - $settings->former_time - $settings->rec_switch_time;
104 $duration = $end_time - $rec_start; // durationを計算しなおす 108 $duration = $end_time - $rec_start; // durationを計算しなおす
105 } 109 }
106 $battings -= $nexts; 110 $battings -= $nexts;
107 111
108 // 直前の録画予約を見付ける 112 // 直前の録画予約を見付ける
109 $trecs = DBRecord::createRecords(TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ". 113 $trecs = DBRecord::createRecords(RESERVE_TBL, "WHERE complete = '0' ".
110 "AND type = '".$crec->type."' ". 114 "AND type = '".$crec->type."' ".
111 "AND endtime = '".$starttime."'" ); 115 "AND endtime = '".$starttime."'" );
112 // 直前の番組をずらす 116 // 直前の番組をずらす
113 for( $i = 0; $i < count($trecs) ; $i++ ) { 117 for( $i = 0; $i < count($trecs) ; $i++ ) {
114 if( $battings < $tuners ) break; // 解消終了のハズ? 118 if( $battings < $tuners ) break; // 解消終了のハズ?
115 // 予約修正に必要な情報を取り出す 119 // 予約修正に必要な情報を取り出す
116 $prev_id = $trecs[$i]->id; 120 $prev_id = $trecs[$i]->id;
124 $prev_autorec = $trecs[$i]->autorec; 128 $prev_autorec = $trecs[$i]->autorec;
125 $prev_mode = $trecs[$i]->mode; 129 $prev_mode = $trecs[$i]->mode;
126 130
127 $prev_start_time = toTimestamp($prev_starttime); 131 $prev_start_time = toTimestamp($prev_starttime);
128 // 始まっていない予約? 132 // 始まっていない予約?
129 if( $prev_start_time > (time() + PADDING_TIME + FORMER_TIME) ) { 133 if( $prev_start_time > (time() + PADDING_TIME + $settings->former_time) ) {
130 // 開始時刻を元に戻す 134 // 開始時刻を元に戻す
131 $prev_starttime = toDatetime( $prev_start_time + FORMER_TIME ); 135 $prev_starttime = toDatetime( $prev_start_time + $settings->former_time );
132 // 終わりをちょっとだけずらす 136 // 終わりをちょっとだけずらす
133 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - FORMER_TIME - REC_SWITCH_TIME ); 137 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - $settings->former_time - $settings->rec_switch_time );
134 138
135 // tryのネスト 139 // tryのネスト
136 try { 140 try {
137 // いったん予約取り消し 141 // いったん予約取り消し
138 self::cancel( $prev_id ); 142 self::cancel( $prev_id );
166 } 170 }
167 // チューナー番号 171 // チューナー番号
168 $tuner = $battings; 172 $tuner = $battings;
169 173
170 // 改めてdurationをチェックしなおす 174 // 改めてdurationをチェックしなおす
171 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く 175 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く
172 throw new Exception( "終わりつつある/終わっている番組です" ); 176 throw new Exception( "終わりつつある/終わっている番組です" );
173 } 177 }
174 178
175 179
176 // ここからファイル名生成 180 // ここからファイル名生成
190 %SEC% 開始秒 194 %SEC% 開始秒
191 %DURATION% 録画時間(秒) 195 %DURATION% 録画時間(秒)
192 */ 196 */
193 197
194 $day_of_week = array( "日","月","火","水","木","金","土" ); 198 $day_of_week = array( "日","月","火","水","木","金","土" );
195 $filename = "%TYPE%%CH%_%ST%_%ET%"; 199 $filename = $settings->filename_format;
196 if( defined( "FILENAME_FORMAT" ) ) { 200
197 $filename = FILENAME_FORMAT;
198 }
199 // あると面倒くさそうな文字を全部_に 201 // あると面倒くさそうな文字を全部_に
200 $fn_title = mb_ereg_replace("[ \./\*:<>\?\\|()\'\"&]","_", trim($title) ); 202 $fn_title = mb_ereg_replace("[ \./\*:<>\?\\|()\'\"&]","_", trim($title) );
201 203
202 // %TITLE% 204 // %TITLE%
203 $filename = str_replace("%TITLE%", $fn_title, $filename); 205 $filename = str_replace("%TITLE%", $fn_title, $filename);
235 $filename .= $RECORD_MODE[$mode]['suffix']; 237 $filename .= $RECORD_MODE[$mode]['suffix'];
236 238
237 // ファイル名生成終了 239 // ファイル名生成終了
238 240
239 // 予約レコードを埋める 241 // 予約レコードを埋める
240 $rrec = new DBRecord( TBL_PREFIX.RESERVE_TBL ); 242 $rrec = new DBRecord( RESERVE_TBL );
241 $rrec->channel_disc = $crec->channel_disc; 243 $rrec->channel_disc = $crec->channel_disc;
242 $rrec->channel_id = $crec->id; 244 $rrec->channel_id = $crec->id;
243 $rrec->program_id = $program_id; 245 $rrec->program_id = $program_id;
244 $rrec->type = $crec->type; 246 $rrec->type = $crec->type;
245 $rrec->channel = $crec->channel; 247 $rrec->channel = $crec->channel;
252 $rrec->autorec = $autorec; 254 $rrec->autorec = $autorec;
253 $rrec->mode = $mode; 255 $rrec->mode = $mode;
254 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) ); 256 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) );
255 257
256 // 予約実行 258 // 予約実行
257 $cmdline = AT." ".date("H:i m/d/Y", $at_start); 259 $cmdline = $settings->at." ".date("H:i m/d/Y", $at_start);
258 $descriptor = array( 0 => array( "pipe", "r" ), 260 $descriptor = array( 0 => array( "pipe", "r" ),
259 1 => array( "pipe", "w" ), 261 1 => array( "pipe", "w" ),
260 2 => array( "pipe", "w" ), 262 2 => array( "pipe", "w" ),
261 ); 263 );
262 $env = array( "CHANNEL" => $crec->channel, 264 $env = array( "CHANNEL" => $crec->channel,
263 "DURATION" => $duration, 265 "DURATION" => $duration,
264 "OUTPUT" => INSTALL_PATH.SPOOL."/".$filename, 266 "OUTPUT" => INSTALL_PATH.$settings->spool."/".$filename,
265 "TYPE" => $crec->type, 267 "TYPE" => $crec->type,
266 "TUNER" => $tuner, 268 "TUNER" => $tuner,
267 "MODE" => $mode, 269 "MODE" => $mode,
268 ); 270 );
269 271
270 // ATで予約する 272 // ATで予約する
271 $process = proc_open( $cmdline , $descriptor, $pipes, SPOOL, $env ); 273 $process = proc_open( $cmdline , $descriptor, $pipes, INSTALL_PATH.$settings->spool, $env );
272 if( is_resource( $process ) ) { 274 if( is_resource( $process ) ) {
273 fwrite($pipes[0], SLEEP." ".$sleep_time."\n" ); 275 fwrite($pipes[0], $settings->sleep." ".$sleep_time."\n" );
274 fwrite($pipes[0], DO_RECORD . "\n" ); 276 fwrite($pipes[0], DO_RECORD . "\n" );
275 fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" ); 277 fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" );
276 if( USE_THUMBS ) { 278 if( $settings->use_thumbs ) {
277 // サムネール生成 279 // サムネール生成
278 $ffmpeg_cmd = FFMPEG." -i \${OUTPUT} -r 1 -s 160x90 -ss ".(FORMER_TIME+2)." -vframes 1 -f image2 ".INSTALL_PATH.THUMBS."/".$filename.".jpg\n"; 280 $ffmpeg_cmd = $settings->ffmpeg." -i \${OUTPUT} -r 1 -s 160x90 -ss ".($settings->former_time + 2)." -vframes 1 -f image2 ".INSTALL_PATH.$settings->thumbs."/".$filename.".jpg\n";
279 fwrite($pipes[0], $ffmpeg_cmd ); 281 fwrite($pipes[0], $ffmpeg_cmd );
280 } 282 }
281 fclose($pipes[0]); 283 fclose($pipes[0]);
282 // 標準エラーを取る 284 // 標準エラーを取る
283 $rstring = stream_get_contents( $pipes[2]); 285 $rstring = stream_get_contents( $pipes[2]);
319 } 321 }
320 // custom 終了 322 // custom 終了
321 323
322 // 取り消し 324 // 取り消し
323 public static function cancel( $reserve_id = 0, $program_id = 0 ) { 325 public static function cancel( $reserve_id = 0, $program_id = 0 ) {
326 $settings = Settings::factory();
324 $rec = null; 327 $rec = null;
325 328
326 try { 329 try {
327 if( $reserve_id ) { 330 if( $reserve_id ) {
328 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "id" , $reserve_id ); 331 $rec = new DBRecord( RESERVE_TBL, "id" , $reserve_id );
329 } 332 }
330 else if( $program_id ) { 333 else if( $program_id ) {
331 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "program_id" , $program_id ); 334 $rec = new DBRecord( RESERVE_TBL, "program_id" , $program_id );
332 } 335 }
333 if( $rec == null ) { 336 if( $rec == null ) {
334 throw new Exception("IDの指定が無効です"); 337 throw new Exception("IDの指定が無効です");
335 } 338 }
336 if( ! $rec->complete ) { 339 if( ! $rec->complete ) {
337 // 未実行の予約である 340 // 未実行の予約である
338 if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + FORMER_TIME) ) 341 if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + $settings->former_time) )
339 throw new Exception("過去の録画予約です"); 342 throw new Exception("過去の録画予約です");
340 exec( ATRM . " " . $rec->job ); 343 exec( $settings->atrm . " " . $rec->job );
341 } 344 }
342 $rec->delete(); 345 $rec->delete();
343 } 346 }
344 catch( Exception $e ) { 347 catch( Exception $e ) {
345 throw $e; 348 throw $e;