Mercurial > epgrec.yaz
comparison Reservation.class.php @ 159:66eabfc1b118
revoke all whitespace modifications
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Wed, 31 Mar 2010 06:42:51 +0900 |
parents | 3a6ec345ab0c |
children | 3d4df1fe4166 |
comparison
equal
deleted
inserted
replaced
158:be39a7efcc87 | 159:66eabfc1b118 |
---|---|
7 | 7 |
8 | 8 |
9 // 予約クラス | 9 // 予約クラス |
10 | 10 |
11 class Reservation { | 11 class Reservation { |
12 | 12 |
13 public static function simple( $program_id , $autorec = 0, $mode = 0) { | 13 public static function simple( $program_id , $autorec = 0, $mode = 0) { |
14 $settings = Settings::factory(); | 14 $settings = Settings::factory(); |
15 $rval = 0; | 15 $rval = 0; |
16 try { | 16 try { |
17 $prec = new DBRecord( PROGRAM_TBL, "id", $program_id ); | 17 $prec = new DBRecord( PROGRAM_TBL, "id", $program_id ); |
18 | 18 |
19 $rval = self::custom( | 19 $rval = self::custom( |
20 $prec->starttime, | 20 $prec->starttime, |
21 $prec->endtime, | 21 $prec->endtime, |
22 $prec->channel_id, | 22 $prec->channel_id, |
23 $prec->title, | 23 $prec->title, |
24 $prec->description, | 24 $prec->description, |
25 $prec->category_id, | 25 $prec->category_id, |
26 $program_id, | 26 $program_id, |
27 $autorec, | 27 $autorec, |
28 $mode ); | 28 $mode ); |
29 | 29 |
30 } | 30 } |
31 catch( Exception $e ) { | 31 catch( Exception $e ) { |
32 throw $e; | 32 throw $e; |
33 } | 33 } |
34 return $rval; | 34 return $rval; |
35 } | 35 } |
36 | 36 |
37 public static function custom( | 37 public static function custom( |
38 $starttime, // 開始時間Datetime型 | 38 $starttime, // 開始時間Datetime型 |
39 $endtime, // 終了時間Datetime型 | 39 $endtime, // 終了時間Datetime型 |
40 $channel_id, // チャンネルID | 40 $channel_id, // チャンネルID |
41 $title = "none", // タイトル | 41 $title = "none", // タイトル |
50 $settings = Settings::factory(); | 50 $settings = Settings::factory(); |
51 | 51 |
52 // 時間を計算 | 52 // 時間を計算 |
53 $start_time = toTimestamp( $starttime ); | 53 $start_time = toTimestamp( $starttime ); |
54 $end_time = toTimestamp( $endtime ) + $settings->extra_time; | 54 $end_time = toTimestamp( $endtime ) + $settings->extra_time; |
55 | 55 |
56 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組 | 56 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組 |
57 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する | 57 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する |
58 } | 58 } |
59 $at_start = $start_time - PADDING_TIME; | 59 $at_start = $start_time - PADDING_TIME; |
60 $sleep_time = PADDING_TIME - $settings->former_time; | 60 $sleep_time = PADDING_TIME - $settings->former_time; |
61 $rec_start = $start_time - $settings->former_time; | 61 $rec_start = $start_time - $settings->former_time; |
62 | 62 |
63 // durationを計算しておく | 63 // durationを計算しておく |
64 $duration = $end_time - $rec_start; | 64 $duration = $end_time - $rec_start; |
65 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く | 65 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く |
66 throw new Exception( "終わりつつある/終わっている番組です" ); | 66 throw new Exception( "終わりつつある/終わっている番組です" ); |
67 } | 67 } |
68 | 68 |
69 $rrec = null; | 69 $rrec = null; |
70 try { | 70 try { |
71 // 同一番組予約チェック | 71 // 同一番組予約チェック |
72 if( $program_id ) { | 72 if( $program_id ) { |
73 $num = DBRecord::countRecords( RESERVE_TBL, "WHERE program_id = '".$program_id."'" ); | 73 $num = DBRecord::countRecords( RESERVE_TBL, "WHERE program_id = '".$program_id."'" ); |
74 if( $num ) { | 74 if( $num ) { |
75 throw new Exception("同一の番組が録画予約されています"); | 75 throw new Exception("同一の番組が録画予約されています"); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 $crec = new DBRecord( CHANNEL_TBL, "id", $channel_id ); | 79 $crec = new DBRecord( CHANNEL_TBL, "id", $channel_id ); |
80 | 80 |
81 // 既存予約数 = TUNER番号 | 81 // 既存予約数 = TUNER番号 |
82 $tuners = ($crec->type == "GR") ? (int)($settings->gr_tuners) : (int)($settings->bs_tuners); | 82 $tuners = ($crec->type == "GR") ? (int)($settings->gr_tuners) : (int)($settings->bs_tuners); |
83 $type_str = ($crec->type == "GR") ? "type = 'GR' " : "(type = 'BS' OR type = 'CS') "; | 83 $type_str = ($crec->type == "GR") ? "type = 'GR' " : "(type = 'BS' OR type = 'CS') "; |
84 | 84 |
85 $battings = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". | 85 $battings = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". |
86 "AND ".$type_str. | 86 "AND ".$type_str. |
87 "AND starttime < '".toDatetime($end_time) ."' ". | 87 "AND starttime < '".toDatetime($end_time) ."' ". |
88 "AND endtime > '".toDatetime($rec_start)."'" | 88 "AND endtime > '".toDatetime($rec_start)."'" |
89 ); | 89 ); |
90 | 90 |
91 if( $battings >= $tuners ) { | 91 if( $battings >= $tuners ) { |
92 // 重複を発見した | 92 // 重複を発見した |
93 if( $settings->force_cont_rec == 1 ) { | 93 if( $settings->force_cont_rec == 1 ) { |
94 // 解消可能な重複かどうかを調べる | 94 // 解消可能な重複かどうかを調べる |
95 // 前後の予約数 | 95 // 前後の予約数 |
96 $nexts = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". | 96 $nexts = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". |
97 "AND ".$type_str. | 97 "AND ".$type_str. |
98 "AND starttime = '".toDatetime($end_time - $settings->former_time)."'"); | 98 "AND starttime = '".toDatetime($end_time - $settings->former_time)."'"); |
99 | 99 |
100 $prevs = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". | 100 $prevs = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ". |
101 "AND ".$type_str. | 101 "AND ".$type_str. |
102 "AND endtime = '".$starttime."'" ); | 102 "AND endtime = '".$starttime."'" ); |
103 | 103 |
104 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理 | 104 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理 |
105 if( ($battings - $nexts - $prevs) >= $tuners ) | 105 if( ($battings - $nexts - $prevs) >= $tuners ) |
106 throw new Exception( "重複予約を解消できません" ); | 106 throw new Exception( "重複予約を解消できません" ); |
107 | 107 |
108 // 直後の番組はあるか? | 108 // 直後の番組はあるか? |
109 if( $nexts ) { | 109 if( $nexts ) { |
110 // この番組の終わりをちょっとだけ早める | 110 // この番組の終わりをちょっとだけ早める |
111 $end_time = $end_time - $settings->former_time - $settings->rec_switch_time; | 111 $end_time = $end_time - $settings->former_time - $settings->rec_switch_time; |
112 $duration = $end_time - $rec_start; // durationを計算しなおす | 112 $duration = $end_time - $rec_start; // durationを計算しなおす |
113 } | 113 } |
114 $battings -= $nexts; | 114 $battings -= $nexts; |
115 | 115 |
116 // 直前の録画予約を見付ける | 116 // 直前の録画予約を見付ける |
117 $trecs = DBRecord::createRecords(RESERVE_TBL, "WHERE complete = '0' ". | 117 $trecs = DBRecord::createRecords(RESERVE_TBL, "WHERE complete = '0' ". |
118 "AND ".$type_str. | 118 "AND ".$type_str. |
119 "AND endtime = '".$starttime."'" ); | 119 "AND endtime = '".$starttime."'" ); |
120 // 直前の番組をずらす | 120 // 直前の番組をずらす |
138 if( $prev_start_time > (time() + PADDING_TIME + $settings->former_time) ) { | 138 if( $prev_start_time > (time() + PADDING_TIME + $settings->former_time) ) { |
139 // 開始時刻を元に戻す | 139 // 開始時刻を元に戻す |
140 $prev_starttime = toDatetime( $prev_start_time + $settings->former_time ); | 140 $prev_starttime = toDatetime( $prev_start_time + $settings->former_time ); |
141 // 終わりをちょっとだけずらす | 141 // 終わりをちょっとだけずらす |
142 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - $settings->former_time - $settings->rec_switch_time ); | 142 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - $settings->former_time - $settings->rec_switch_time ); |
143 | 143 |
144 // tryのネスト | 144 // tryのネスト |
145 try { | 145 try { |
146 // いったん予約取り消し | 146 // いったん予約取り消し |
147 self::cancel( $prev_id ); | 147 self::cancel( $prev_id ); |
148 // 再予約 | 148 // 再予約 |
149 self::custom( | 149 self::custom( |
150 $prev_starttime, // 開始時間Datetime型 | 150 $prev_starttime, // 開始時間Datetime型 |
151 $prev_endtime, // 終了時間Datetime型 | 151 $prev_endtime, // 終了時間Datetime型 |
152 $prev_channel_id, // チャンネルID | 152 $prev_channel_id, // チャンネルID |
153 $prev_title, // タイトル | 153 $prev_title, // タイトル |
154 $prev_description, // 概要 | 154 $prev_description, // 概要 |
174 throw new Exception( "重複予約があります" ); | 174 throw new Exception( "重複予約があります" ); |
175 } | 175 } |
176 } | 176 } |
177 // チューナー番号 | 177 // チューナー番号 |
178 $tuner = $battings; | 178 $tuner = $battings; |
179 | 179 |
180 // 改めてdurationをチェックしなおす | 180 // 改めてdurationをチェックしなおす |
181 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く | 181 if( $duration < ($settings->former_time + 60) ) { // 60秒以下の番組は弾く |
182 throw new Exception( "終わりつつある/終わっている番組です" ); | 182 throw new Exception( "終わりつつある/終わっている番組です" ); |
183 } | 183 } |
184 | 184 |
240 $filename = mb_convert_encoding( $filename, FILESYSTEM_ENCODING, "UTF-8" ); | 240 $filename = mb_convert_encoding( $filename, FILESYSTEM_ENCODING, "UTF-8" ); |
241 } | 241 } |
242 | 242 |
243 $filename .= $RECORD_MODE["$mode"]['suffix']; | 243 $filename .= $RECORD_MODE["$mode"]['suffix']; |
244 $thumbname = $filename.".jpg"; | 244 $thumbname = $filename.".jpg"; |
245 | 245 |
246 // サムネール | 246 // サムネール |
247 $gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh"; | 247 $gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh"; |
248 if( defined("GEN_THUMBNAIL") ) | 248 if( defined("GEN_THUMBNAIL") ) |
249 $gen_thumbnail = GEN_THUMBNAIL; | 249 $gen_thumbnail = GEN_THUMBNAIL; |
250 | 250 |
251 // ファイル名生成終了 | 251 // ファイル名生成終了 |
252 | 252 |
253 // 予約レコードを埋める | 253 // 予約レコードを埋める |
254 $rrec = new DBRecord( RESERVE_TBL ); | 254 $rrec = new DBRecord( RESERVE_TBL ); |
255 $rrec->channel_disc = $crec->channel_disc; | 255 $rrec->channel_disc = $crec->channel_disc; |
256 $rrec->channel_id = $crec->id; | 256 $rrec->channel_id = $crec->id; |
257 $rrec->program_id = $program_id; | 257 $rrec->program_id = $program_id; |
264 $rrec->endtime = toDatetime( $end_time ); | 264 $rrec->endtime = toDatetime( $end_time ); |
265 $rrec->path = $filename; | 265 $rrec->path = $filename; |
266 $rrec->autorec = $autorec; | 266 $rrec->autorec = $autorec; |
267 $rrec->mode = $mode; | 267 $rrec->mode = $mode; |
268 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) ); | 268 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) ); |
269 | 269 |
270 // 予約実行 | 270 // 予約実行 |
271 $cmdline = $settings->at." ".date("H:i m/d/Y", $at_start); | 271 $cmdline = $settings->at." ".date("H:i m/d/Y", $at_start); |
272 $descriptor = array( 0 => array( "pipe", "r" ), | 272 $descriptor = array( 0 => array( "pipe", "r" ), |
273 1 => array( "pipe", "w" ), | 273 1 => array( "pipe", "w" ), |
274 2 => array( "pipe", "w" ), | 274 2 => array( "pipe", "w" ), |
282 "THUMB" => INSTALL_PATH.$settings->thumbs."/".$thumbname, | 282 "THUMB" => INSTALL_PATH.$settings->thumbs."/".$thumbname, |
283 "FORMER" => "".$settings->former_time, | 283 "FORMER" => "".$settings->former_time, |
284 "FFMPEG" => "".$settings->ffmpeg, | 284 "FFMPEG" => "".$settings->ffmpeg, |
285 "SID" => $crec->sid, | 285 "SID" => $crec->sid, |
286 ); | 286 ); |
287 | 287 |
288 // ATで予約する | 288 // ATで予約する |
289 $process = proc_open( $cmdline , $descriptor, $pipes, INSTALL_PATH.$settings->spool, $env ); | 289 $process = proc_open( $cmdline , $descriptor, $pipes, INSTALL_PATH.$settings->spool, $env ); |
290 if( is_resource( $process ) ) { | 290 if( is_resource( $process ) ) { |
291 fwrite($pipes[0], $settings->sleep." ".$sleep_time."\n" ); | 291 fwrite($pipes[0], $settings->sleep." ".$sleep_time."\n" ); |
292 fwrite($pipes[0], DO_RECORD . "\n" ); | 292 fwrite($pipes[0], DO_RECORD . "\n" ); |
295 fwrite($pipes[0], $gen_thumbnail."\n" ); | 295 fwrite($pipes[0], $gen_thumbnail."\n" ); |
296 } | 296 } |
297 fclose($pipes[0]); | 297 fclose($pipes[0]); |
298 // 標準エラーを取る | 298 // 標準エラーを取る |
299 $rstring = stream_get_contents( $pipes[2]); | 299 $rstring = stream_get_contents( $pipes[2]); |
300 | 300 |
301 fclose( $pipes[2] ); | 301 fclose( $pipes[2] ); |
302 proc_close( $process ); | 302 proc_close( $process ); |
303 } | 303 } |
304 else { | 304 else { |
305 $rrec->delete(); | 305 $rrec->delete(); |
335 } | 335 } |
336 throw $e; | 336 throw $e; |
337 } | 337 } |
338 } | 338 } |
339 // custom 終了 | 339 // custom 終了 |
340 | 340 |
341 // 取り消し | 341 // 取り消し |
342 public static function cancel( $reserve_id = 0, $program_id = 0 ) { | 342 public static function cancel( $reserve_id = 0, $program_id = 0 ) { |
343 $settings = Settings::factory(); | 343 $settings = Settings::factory(); |
344 $rec = null; | 344 $rec = null; |
345 | 345 |
346 try { | 346 try { |
347 if( $reserve_id ) { | 347 if( $reserve_id ) { |
348 $rec = new DBRecord( RESERVE_TBL, "id" , $reserve_id ); | 348 $rec = new DBRecord( RESERVE_TBL, "id" , $reserve_id ); |
349 } | 349 } |
350 else if( $program_id ) { | 350 else if( $program_id ) { |