Mercurial > epgrec.yaz
view Reservation.class.php @ 115:7a64d5e3baee
mod: ¥¡¼¥ï¡¼¥É¸¡º÷¤òÀ°Íý
author | yoneda <epgrec@park.mda.or.jp> |
---|---|
date | Sat, 13 Mar 2010 23:00:51 +0900 |
parents | f6b909678e51 |
children | e2c37cc736db f268d7d10590 |
line wrap: on
line source
<?php include_once('config.php'); include_once( INSTALL_PATH . "/DBRecord.class.php" ); include_once( INSTALL_PATH . "/reclib.php" ); include_once( INSTALL_PATH . "/Settings.class.php" ); // 予約クラス class Reservation { public static function simple( $program_id , $autorec = 0, $mode = 0) { $settings = Settings::factory(); $rval = 0; try { $prec = new DBRecord( PROGRAM_TBL, "id", $program_id ); $rval = self::custom( $prec->starttime, $prec->endtime, $prec->channel_id, $prec->title, $prec->description, $prec->category_id, $program_id, $autorec, $mode ); } catch( Exception $e ) { throw $e; } return $rval; } public static function custom( $starttime, // 開始時間Datetimeåž‹ $endtime, // 終了時間Datetimeåž‹ $channel_id, // ãƒãƒ£ãƒ³ãƒãƒ«ID $title = "none", // タイトル $description = "none", // æ¦‚è¦ $category_id = 0, // カテゴリID $program_id = 0, // 番組ID $autorec = 0, // 自動録画 $mode = 0 // 録画モード ) { global $RECORD_MODE; $settings = Settings::factory(); // 時間を計算 $start_time = toTimestamp( $starttime ); $end_time = toTimestamp( $endtime ) + $settings->extra_time; if( $start_time < (time() + PADDING_TIME + 10) ) { // ç¾åœ¨æ™‚刻より3分先よりå°ã•ã„ï¼ã™ã§ã«é–‹å§‹ã•ã‚Œã¦ã„る番組 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先ã«è¨å®šã™ã‚‹ } $at_start = $start_time - PADDING_TIME; $sleep_time = PADDING_TIME - $settings->former_time; $rec_start = $start_time - $settings->former_time; // durationを計算ã—ã¦ãŠã $duration = $end_time - $rec_start; if( $duration < ($settings->former_time + 60) ) { // 60秒以下ã®ç•ªçµ„ã¯å¼¾ã throw new Exception( "終ã‚ã‚Šã¤ã¤ã‚ã‚‹/終ã‚ã£ã¦ã„る番組ã§ã™" ); } $rrec = null; try { // åŒä¸€ç•ªçµ„予約ãƒã‚§ãƒƒã‚¯ if( $program_id ) { $num = DBRecord::countRecords( RESERVE_TBL, "WHERE program_id = '".$program_id."'" ); if( $num ) { throw new Exception("åŒä¸€ã®ç•ªçµ„ãŒéŒ²ç”»äºˆç´„ã•ã‚Œã¦ã„ã¾ã™"); } } $crec = new DBRecord( CHANNEL_TBL, "id", $channel_id ); // æ—¢å˜äºˆç´„æ•° = TUNERç•ªå· $tuners = ($crec->type == "GR") ? $settings->gr_tuners : $settings->bs_tuners; $type_str = ($crec->type == "GR") ? "type = 'GR' " : "(type = 'BS' OR type = 'CS') "; $battings = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". "AND ".$type_str. "AND starttime < '".toDatetime($end_time) ."' ". "AND endtime > '".toDatetime($rec_start)."'" ); if( $battings >= $tuners ) { // é‡è¤‡ã‚’発見ã—㟠if( $settings->force_cont_rec == 1 ) { // 解消å¯èƒ½ãªé‡è¤‡ã‹ã©ã†ã‹ã‚’調ã¹ã‚‹ // å‰å¾Œã®äºˆç´„æ•° $nexts = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". "AND ".$type_str. "AND starttime = '".toDatetime($end_time - $settings->former_time)."'"); $prevs = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". "AND ".$type_str. "AND endtime = '".$starttime."'" ); // å‰å¾Œã‚’引ã„ã¦ã‚‚ãƒãƒ¥ãƒ¼ãƒŠãƒ¼æ•°ã¨åŒæ•°ä»¥ä¸Šãªã‚‰é‡è¤‡ã®è§£æ¶ˆã¯ç„¡ç† if( ($battings - $nexts - $prevs) >= $tuners ) throw new Exception( "é‡è¤‡äºˆç´„を解消ã§ãã¾ã›ã‚“" ); // 直後ã®ç•ªçµ„ã¯ã‚ã‚‹ã‹? if( $nexts ) { // ã“ã®ç•ªçµ„ã®çµ‚ã‚ã‚Šã‚’ã¡ã‚‡ã£ã¨ã ã‘æ—©ã‚ã‚‹ $end_time = $end_time - $settings->former_time - $settings->rec_switch_time; $duration = $end_time - $rec_start; // durationを計算ã—ãªãŠã™ } $battings -= $nexts; // ç›´å‰ã®éŒ²ç”»äºˆç´„を見付ã‘ã‚‹ $trecs = DBRecord::createRecords(RESERVE_TBL, "WHERE complete = '0' ". "AND ".$type_str. "AND endtime = '".$starttime."'" ); // ç›´å‰ã®ç•ªçµ„ã‚’ãšã‚‰ã™ for( $i = 0; $i < count($trecs) ; $i++ ) { if( $battings < $tuners ) break; // 解消終了ã®ãƒã‚º? // 予約修æ£ã«å¿…è¦ãªæƒ…å ±ã‚’å–り出㙠$prev_id = $trecs[$i]->id; $prev_program_id = $trecs[$i]->program_id; $prev_channel_id = $trecs[$i]->channel_id; $prev_title = $trecs[$i]->title; $prev_description = $trecs[$i]->description; $prev_category_id = $trecs[$i]->category_id; $prev_starttime = $trecs[$i]->starttime; $prev_endtime = $trecs[$i]->endtime; $prev_autorec = $trecs[$i]->autorec; $prev_mode = $trecs[$i]->mode; $prev_start_time = toTimestamp($prev_starttime); // 始ã¾ã£ã¦ã„ãªã„予約? if( $prev_start_time > (time() + PADDING_TIME + $settings->former_time) ) { // 開始時刻を元ã«æˆ»ã™ $prev_starttime = toDatetime( $prev_start_time + $settings->former_time ); // 終ã‚ã‚Šã‚’ã¡ã‚‡ã£ã¨ã ã‘ãšã‚‰ã™ $prev_endtime = toDatetime( toTimestamp($prev_endtime) - $settings->former_time - $settings->rec_switch_time ); // tryã®ãƒã‚¹ãƒˆ try { // ã„ã£ãŸã‚“予約å–り消㗠self::cancel( $prev_id ); // å†äºˆç´„ self::custom( $prev_starttime, // 開始時間Datetimeåž‹ $prev_endtime, // 終了時間Datetimeåž‹ $prev_channel_id, // ãƒãƒ£ãƒ³ãƒãƒ«ID $prev_title, // タイトル $prev_description, // æ¦‚è¦ $prev_category_id, // カテゴリID $prev_program_id, // 番組ID $prev_autorec, // 自動録画 $prev_mode ); } catch( Exception $e ) { throw new Exception( "é‡è¤‡äºˆç´„を解消ã§ãã¾ã›ã‚“" ); } } else { throw new Exception( "é‡è¤‡äºˆç´„を解消ã§ãã¾ã›ã‚“" ); } $battings--; } if( $battings < 0 ) $battings = 0; // ã“ã‚Œã§é‡è¤‡è§£æ¶ˆã—ãŸã¯ãš } else { throw new Exception( "é‡è¤‡äºˆç´„ãŒã‚ã‚Šã¾ã™" ); } } // ãƒãƒ¥ãƒ¼ãƒŠãƒ¼ç•ªå· $tuner = $battings; // 改ã‚ã¦durationã‚’ãƒã‚§ãƒƒã‚¯ã—ãªãŠã™ if( $duration < ($settings->former_time + 60) ) { // 60秒以下ã®ç•ªçµ„ã¯å¼¾ã throw new Exception( "終ã‚ã‚Šã¤ã¤ã‚ã‚‹/終ã‚ã£ã¦ã„る番組ã§ã™" ); } // ã“ã“ã‹ã‚‰ãƒ•ã‚¡ã‚¤ãƒ«åç”Ÿæˆ /* %TITLE% 番組タイトル %ST% 開始日時(ex.200907201830) %ET% 終了日時 %TYPE% GR/BS/CS %CH% ãƒãƒ£ãƒ³ãƒãƒ«ç•ªå· %DOW% 曜日(Sun-Mon) %DOWJ% 曜日(日-土) %YEAR% 開始年 %MONTH% 開始月 %DAY% 開始日 %HOUR% 開始時 %MIN% 開始分 %SEC% 開始秒 %DURATION% 録画時間(秒) */ $day_of_week = array( "æ—¥","月","ç«","æ°´","木","金","土" ); $filename = $settings->filename_format; // ã‚ã‚‹ã¨é¢å€’ãã•ãã†ãªæ–‡å—を全部_ã« $fn_title = mb_ereg_replace("[ \./\*:<>\?\\|()\'\"&]","_", trim($title) ); // %TITLE% $filename = str_replace("%TITLE%", $fn_title, $filename); // %ST% 開始日時 $filename = str_replace("%ST%",date("YmdHis", $start_time), $filename ); // %ET% 終了日時 $filename = str_replace("%ET%",date("YmdHis", $end_time), $filename ); // %TYPE% GR/BS $filename = str_replace("%TYPE%",$crec->type, $filename ); // %CH% ãƒãƒ£ãƒ³ãƒãƒ«ç•ªå· $filename = str_replace("%CH%","".$crec->channel, $filename ); // %DOW% 曜日(Sun-Mon) $filename = str_replace("%DOW%",date("D", $start_time), $filename ); // %DOWJ% 曜日(日-土) $filename = str_replace("%DOWJ%",$day_of_week[(int)date("w", $start_time)], $filename ); // %YEAR% 開始年 $filename = str_replace("%YEAR%",date("Y", $start_time), $filename ); // %MONTH% 開始月 $filename = str_replace("%MONTH%",date("m", $start_time), $filename ); // %DAY% 開始日 $filename = str_replace("%DAY%",date("d", $start_time), $filename ); // %HOUR% 開始時 $filename = str_replace("%HOUR%",date("H", $start_time), $filename ); // %MIN% 開始分 $filename = str_replace("%MIN%",date("i", $start_time), $filename ); // %SEC% 開始秒 $filename = str_replace("%SEC%",date("s", $start_time), $filename ); // %DURATION% 録画時間(秒) $filename = str_replace("%DURATION%","".$duration, $filename ); // æ–‡å—ã‚³ãƒ¼ãƒ‰å¤‰æ› if( defined("FILESYSTEM_ENCODING") ) { $filename = mb_convert_encoding( $filename, FILESYSTEM_ENCODING, "UTF-8" ); } $filename .= $RECORD_MODE["$mode"]['suffix']; $thumbname = $filename.".jpg"; // サムãƒãƒ¼ãƒ« $gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh"; if( defined("GEN_THUMBNAIL") ) $gen_thumbnail = GEN_THUMBNAIL; // ファイルå生æˆçµ‚了 // 予約レコードを埋ã‚ã‚‹ $rrec = new DBRecord( RESERVE_TBL ); $rrec->channel_disc = $crec->channel_disc; $rrec->channel_id = $crec->id; $rrec->program_id = $program_id; $rrec->type = $crec->type; $rrec->channel = $crec->channel; $rrec->title = $title; $rrec->description = $description; $rrec->category_id = $category_id; $rrec->starttime = toDatetime( $rec_start ); $rrec->endtime = toDatetime( $end_time ); $rrec->path = $filename; $rrec->autorec = $autorec; $rrec->mode = $mode; $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) ); // 予約実行 $cmdline = $settings->at." ".date("H:i m/d/Y", $at_start); $descriptor = array( 0 => array( "pipe", "r" ), 1 => array( "pipe", "w" ), 2 => array( "pipe", "w" ), ); $env = array( "CHANNEL" => $crec->channel, "DURATION" => $duration, "OUTPUT" => INSTALL_PATH.$settings->spool."/".$filename, "TYPE" => $crec->type, "TUNER" => $tuner, "MODE" => $mode, "THUMB" => INSTALL_PATH.$settings->thumbs."/".$thumbname, "FORMER" => "".$settings->former_time, "FFMPEG" => "".$settings->ffmpeg, "SID" => $crec->sid, ); // ATã§äºˆç´„ã™ã‚‹ $process = proc_open( $cmdline , $descriptor, $pipes, INSTALL_PATH.$settings->spool, $env ); if( is_resource( $process ) ) { fwrite($pipes[0], $settings->sleep." ".$sleep_time."\n" ); fwrite($pipes[0], DO_RECORD . "\n" ); fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" ); if( $settings->use_thumbs == 1 ) { fwrite($pipes[0], $gen_thumbnail."\n" ); } fclose($pipes[0]); // 標準エラーをå–ã‚‹ $rstring = stream_get_contents( $pipes[2]); fclose( $pipes[2] ); proc_close( $process ); } else { $rrec->delete(); throw new Exception("AT実行エラー"); } // job番å·ã‚’å–り出㙠$rarr = array(); $tok = strtok( $rstring, " \n" ); while( $tok !== false ) { array_push( $rarr, $tok ); $tok = strtok( " \n" ); } $key = array_search("job", $rarr); if( $key !== false ) { if( is_numeric( $rarr[$key+1]) ) { $rrec->job = $rarr[$key+1]; return $rrec->job; // æˆåŠŸ } } // エラー $rrec->delete(); throw new Exception( "job番å·ã®å–å¾—ã«å¤±æ•—" ); } catch( Exception $e ) { if( $rrec != null ) { if( $rrec->id ) { // 予約をå–り消㙠$rrec->delete(); } } throw $e; } } // custom 終了 // å–り消㗠public static function cancel( $reserve_id = 0, $program_id = 0 ) { $settings = Settings::factory(); $rec = null; try { if( $reserve_id ) { $rec = new DBRecord( RESERVE_TBL, "id" , $reserve_id ); } else if( $program_id ) { $rec = new DBRecord( RESERVE_TBL, "program_id" , $program_id ); } if( $rec == null ) { throw new Exception("IDã®æŒ‡å®šãŒç„¡åŠ¹ã§ã™"); } if( ! $rec->complete ) { // 未実行ã®äºˆç´„ã§ã‚ã‚‹ if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + $settings->former_time) ) throw new Exception("éŽåŽ»ã®éŒ²ç”»äºˆç´„ã§ã™"); exec( $settings->atrm . " " . $rec->job ); } $rec->delete(); } catch( Exception $e ) { throw $e; } } } ?>