view Reservation.class.php @ 164:254d8a21bf36

fix: ͽÌóºÑ¤ß¤Î̤¼Â¹ÔÏ¿²è¤Ërecomplete.php¤¬É¬ÍפʤΤÇÌᤷ¤¿
author Sushi-k <epgrec@park.mda.or.jp>
date Tue, 06 Apr 2010 11:03:45 +0900
parents 84220382fea8
children 3d4df1fe4166 e379552d084c
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" );
include_once( INSTALL_PATH . "/recLog.inc.php" );

// 後方互æ›æ€§
if( !defined( "RECORDER_CMD" ) ) {
	define( "RECORDER_CMD", INSTALL_PATH."/recorder.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,				// 録画モード
		$dirty = 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") ? (int)($settings->gr_tuners) : (int)($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_dirty        = $trecs[$i]->dirty;
						
						$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,
									$prev_dirty );
							}
							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;
			
			// %TITLE%
			$filename = mb_str_replace("%TITLE%", trim($title), $filename);
			// %ST%	開始日時
			$filename = mb_str_replace("%ST%",date("YmdHis", $start_time), $filename );
			// %ET%	終了日時
			$filename = mb_str_replace("%ET%",date("YmdHis", $end_time), $filename );
			// %TYPE%	GR/BS
			$filename = mb_str_replace("%TYPE%",$crec->type, $filename );
			// %CH%	ãƒãƒ£ãƒ³ãƒãƒ«ç•ªå·
			$filename = mb_str_replace("%CH%","".$crec->channel, $filename );
			// %DOW%	曜日(Sun-Mon)
			$filename = mb_str_replace("%DOW%",date("D", $start_time), $filename );
			// %DOWJ%	曜日(日-土)
			$filename = mb_str_replace("%DOWJ%",$day_of_week[(int)date("w", $start_time)], $filename );
			// %YEAR%	開始年
			$filename = mb_str_replace("%YEAR%",date("Y", $start_time), $filename );
			// %MONTH%	開始月
			$filename = mb_str_replace("%MONTH%",date("m", $start_time), $filename );
			// %DAY%	開始日
			$filename = mb_str_replace("%DAY%",date("d", $start_time), $filename );
			// %HOUR%	開始時
			$filename = mb_str_replace("%HOUR%",date("H", $start_time), $filename );
			// %MIN%	開始分
			$filename = mb_str_replace("%MIN%",date("i", $start_time), $filename );
			// %SEC%	開始秒
			$filename = mb_str_replace("%SEC%",date("s", $start_time), $filename );
			// %DURATION%	録画時間(秒)
			$filename = mb_str_replace("%DURATION%","".$duration, $filename );
			
			// ã‚ã‚‹ã¨é¢å€’ãã•ãã†ãªæ–‡å­—を全部_ã«
			$filename = preg_replace("/[ \.\/\*:<>\?\\|()\'\"&]/u","_", trim($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], RECORDER_CMD." ".$rrec->id."\n" );
				fclose($pipes[0]);
				// 標準エラーをå–ã‚‹
				$rstring = stream_get_contents($pipes[2]);
				
			    fclose( $pipes[2] );
			    fclose( $pipes[1] );
			    proc_close( $process );
			}
			else {
				$rrec->delete();
				reclog( "Reservation::custom atã®å®Ÿè¡Œã«å¤±æ•—ã—ãŸæ¨¡æ§˜", EPGREC_ERROR);
				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];
					reclog( "Reservation::custom ジョブ番å·".$rrec->job."ã«éŒ²ç”»ã‚¸ãƒ§ãƒ–を登録");
					return $rrec->job;			// æˆåŠŸ
				}
			}
			// エラー
			$rrec->delete();
			reclog( "Reservation::custom job番å·ã®å–å¾—ã«å¤±æ•—",EPGREC_ERROR );
			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) ) {
					reclog("Reservation::cancel 実行中ã®äºˆç´„ID".$rec->id."ã®å–り消ã—ãŒå®Ÿè¡Œã•ã‚ŒãŸ" );
					
					// recorderã¨ã®é€šä¿¡ã‚’試ã¿ã‚‹
					$ipc_key = ftok( RECORDER_CMD, "R" );
					
					/* php 5.3以é™ã˜ã‚ƒãªã„ã¨msg_queue_existsã¯ä½¿ãˆãªã„
					if( ! msg_queue_exists( $ipc_key ) ) {
						// メッセージキューãŒãªã„
						reclog( "Reservation::cancel 実行中ã¨æŽ¨æ¸¬ã•ã‚Œã‚‹äºˆç´„".$rec->id."ãŒå®Ÿè¡Œã•ã‚Œã¦ã„ãªã„", EPGREC_ERROR );
						$rec->complete = 1;
						throw new RecException( "Reserve:: 実行中ã¨æŽ¨æ¸¬ã•ã‚Œã‚‹äºˆç´„ãŒå®Ÿè¡Œã•ã‚Œã¦ã„ã¾ã›ã‚“。å†åº¦ã€å‰Šé™¤ã‚’試ã¿ã¦ãã ã•ã„。", EPGREC_ERROR );
					}
					else {
					*/
						$msgh_r = msg_get_queue( $ipc_key );
						$ipc_key = ftok( RECORDER_CMD, "W" );
						$msgh_w = msg_get_queue( $ipc_key );
						
						// 終了を指示
						msg_send( $msgh_r, (int)$rec->id, "terminate" );
						sleep(1);
						for( $i = 0; $i < 60; $i++ ) {
							$r = msg_receive($msgh_w, (int)$rec->id , $msgtype, 1024, $message, TRUE, MSG_IPC_NOWAIT | MSG_NOERROR);
							if( $r ) {
								if( $message == "success" ) {
									reclog( "Reserve:: 実行中ã®äºˆç´„ID".$rec->id."ã®å–り消ã—ã«æˆåŠŸã—ãŸæ¨¡æ§˜" );
									break;
								}
								else if( $message == "error" ){
									reclog( "Reserve:: 実行中ã®äºˆç´„ID".$rec->id."ã®å–り消ã—ã«å¤±æ•—", EPGREC_ERROR );
									throw new RecException("実行中ã®äºˆç´„å–り消ã—ã«å¤±æ•—ã—ã¾ã—ãŸã€‚ã—ã°ã‚‰ã時間をãŠã„ã¦ã‹ã‚‰å†åº¦ã€å–り消ã—ã¦ãã ã•ã„", EPGREC_ERROR );
								}
								// ãれ以外ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã¯ç„¡è¦–ã—ã¦å¾…ã¤
							}
							sleep(1);
						}
						if( $i >= 60 ) throw new RecException("実行中ã®äºˆç´„å–り消ã—ã«å¤±æ•—ã—ã¾ã—ãŸã€‚ã—ã°ã‚‰ã時間をãŠã„ã¦ã‹ã‚‰å†åº¦ã€å–り消ã—ã¦ãã ã•ã„", EPGREC_ERROR );
//					}
				}
				else {
					// ã¾ã å®Ÿè¡Œã•ã‚Œã¦ã„ãªã„予約ãªã‚‰atを削除ã—ã¨ã
					exec( $settings->atrm . " " . $rec->job );
					reclog("Reservation::cancel ジョブ番å·".$rec->job."を削除");
					$rec->delete();
				}
			}
			else {
				// 録画済ã¿äºˆç´„ãªã‚‰ãŸã æ¶ˆã™
				$rec->delete();
			}
		}
		catch( Exception $e ) {
			reclog("Reservation::cancel 予約キャンセルã§DB接続ã¾ãŸã¯ã‚¢ã‚¯ã‚»ã‚¹ã«å¤±æ•—ã—ãŸæ¨¡æ§˜", EPGREC_ERROR );
			throw $e;
		}
	}
}
?>