comparison Reservation.class.php @ 1:f5a9f0eb4858

deleted: LICENSE.ja
author Sushi-k <epgrec@park.mda.or.jp>
date Wed, 08 Jul 2009 11:44:50 +0900
parents
children 218d34ec7874
comparison
equal deleted inserted replaced
0:96312e6ab8d4 1:f5a9f0eb4858
1 <?php
2 include_once('config.php');
3 include_once( INSTALL_PATH . "/DBRecord.class.php" );
4 include_once( INSTALL_PATH . "/reclib.php" );
5
6 // 予約クラス
7
8 class Reservation {
9
10 public static function simple( $program_id , $autorec = 0, $mode = 0) {
11 $rval = 0;
12 try {
13 $prec = new DBRecord( TBL_PREFIX.PROGRAM_TBL, "id", $program_id );
14
15 $rval = self::custom(
16 $prec->starttime,
17 $prec->endtime,
18 $prec->channel_id,
19 $prec->title,
20 $prec->description,
21 $prec->category_id,
22 $program_id,
23 $autorec,
24 $mode );
25
26 }
27 catch( Exception $e ) {
28 throw $e;
29 }
30 return $rval;
31 }
32
33 public static function custom(
34 $starttime, // 開始時間Datetime型
35 $endtime, // 終了時間Datetime型
36 $channel_id, // チャンネルID
37 $title = "none", // タイトル
38 $description = "none", // 概要
39 $category_id = 0, // カテゴリID
40 $program_id = 0, // 番組ID
41 $autorec = 0, // 自動録画
42 $mode = 0 // 録画モード
43 ) {
44 global $RECORD_MODE;
45
46 // 時間を計算
47 $start_time = toTimestamp( $starttime );
48 $end_time = toTimestamp( $endtime );
49
50 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組
51 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する
52 }
53 $at_start = $start_time - PADDING_TIME;
54 $sleep_time = PADDING_TIME - FORMER_TIME;
55 $rec_start = $start_time - FORMER_TIME;
56
57 // durationを計算しておく
58 $duration = $end_time - $rec_start;
59 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く
60 throw new Exception( "終わりつつある/終わっている番組です" );
61 }
62
63 $rrec = null;
64 try {
65 // 同一番組予約チェック
66 if( $program_id ) {
67 $num = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE program_id = '".$program_id."'" );
68 if( $num ) {
69 throw new Exception("同一の番組が録画予約されています");
70 }
71 }
72
73 $crec = new DBRecord( TBL_PREFIX.CHANNEL_TBL, "id", $channel_id );
74
75 // 既存予約数 = TUNER番号
76 $tuners = ($crec->type == "GR") ? GR_TUNERS : BS_TUNERS;
77 $battings = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
78 "AND type = '".$crec->type."' ".
79 "AND starttime < '".toDatetime($end_time) ."' ".
80 "AND endtime > '".toDatetime($rec_start)."'"
81 );
82
83 if( $battings >= $tuners ) {
84 // 重複を発見した
85 if( FORCE_CONT_REC ) {
86 // 解消可能な重複かどうかを調べる
87 // 前後の予約数
88 $nexts = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
89 "AND type = '".$crec->type."' ".
90 "AND starttime = '".toDatetime($end_time - FORMER_TIME)."'");
91
92 $prevs = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
93 "AND type = '".$crec->type."' ".
94 "AND endtime = '".$starttime."'" );
95
96 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理
97 if( ($battings - $nexts - $prevs) >= $tuners )
98 throw new Exception( "重複予約を解消できません" );
99
100 // 直後の番組はあるか?
101 if( $nexts ) {
102 // この番組の終わりをちょっとだけ早める
103 $end_time = $end_time - FORMER_TIME - REC_SWITCH_TIME;
104 $duration = $end_time - $rec_start; // durationを計算しなおす
105 }
106 $battings -= $nexts;
107
108 // 直前の録画予約を見付ける
109 $trecs = DBRecord::createRecords(TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
110 "AND type = '".$crec->type."' ".
111 "AND endtime = '".$starttime."'" );
112 // 直前の番組をずらす
113 for( $i = 0; $i < count($trecs) ; $i++ ) {
114 if( $battings < $tuners ) break; // 解消終了のハズ?
115 // 予約修正に必要な情報を取り出す
116 $prev_id = $trecs[$i]->id;
117 $prev_program_id = $trecs[$i]->program_id;
118 $prev_channel_id = $trecs[$i]->channel_id;
119 $prev_title = $trecs[$i]->title;
120 $prev_description = $trecs[$i]->description;
121 $prev_category_id = $trecs[$i]->category_id;
122 $prev_starttime = $trecs[$i]->starttime;
123 $prev_endtime = $trecs[$i]->endtime;
124 $prev_autorec = $trecs[$i]->autorec;
125 $prev_mode = $trecs[$i]->mode;
126
127 $prev_start_time = toTimestamp($prev_starttime);
128 // 始まっていない予約?
129 if( $prev_start_time > (time() + PADDING_TIME + FORMER_TIME) ) {
130 // 開始時刻を元に戻す
131 $prev_starttime = toDatetime( $prev_start_time + FORMER_TIME );
132 // 終わりをちょっとだけずらす
133 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - FORMER_TIME - REC_SWITCH_TIME );
134
135 // tryのネスト
136 try {
137 // いったん予約取り消し
138 self::cancel( $prev_id );
139 // 再予約
140 self::custom(
141 $prev_starttime, // 開始時間Datetime型
142 $prev_endtime, // 終了時間Datetime型
143 $prev_channel_id, // チャンネルID
144 $prev_title, // タイトル
145 $prev_description, // 概要
146 $prev_category_id, // カテゴリID
147 $prev_program_id, // 番組ID
148 $prev_autorec, // 自動録画
149 $prev_mode );
150 }
151 catch( Exception $e ) {
152 throw new Exception( "重複予約を解消できません" );
153 }
154 }
155 else {
156 throw new Exception( "重複予約を解消できません" );
157 }
158 $battings--;
159 }
160 if( $battings < 0 ) $battings = 0;
161 // これで重複解消したはず
162 }
163 else {
164 throw new Exception( "重複予約があります" );
165 }
166 }
167 // チューナー番号
168 $tuner = $battings;
169
170 // 改めてdurationをチェックしなおす
171 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く
172 throw new Exception( "終わりつつある/終わっている番組です" );
173 }
174
175 $filename = "".$crec->type.$crec->channel."_".date("YmdHis", $start_time)."_".date("YmdHis", $end_time).$RECORD_MODE[$mode]['suffix'];
176
177 // 予約レコードを埋める
178 $rrec = new DBRecord( TBL_PREFIX.RESERVE_TBL );
179 $rrec->channel_disc = $crec->channel_disc;
180 $rrec->channel_id = $crec->id;
181 $rrec->program_id = $program_id;
182 $rrec->type = $crec->type;
183 $rrec->channel = $crec->channel;
184 $rrec->title = $title;
185 $rrec->description = $description;
186 $rrec->category_id = $category_id;
187 $rrec->starttime = toDatetime( $rec_start );
188 $rrec->endtime = toDatetime( $end_time );
189 $rrec->path = $filename;
190 $rrec->autorec = $autorec;
191 $rrec->mode = $mode;
192 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) );
193
194 // 予約実行
195 $cmdline = AT." ".date("H:i m/d/Y", $at_start);
196 $descriptor = array( 0 => array( "pipe", "r" ),
197 1 => array( "pipe", "w" ),
198 2 => array( "pipe", "w" ),
199 );
200 $env = array( "CHANNEL" => $crec->channel,
201 "DURATION" => $duration,
202 "OUTPUT" => INSTALL_PATH.SPOOL."/".$filename,
203 "TYPE" => $crec->type,
204 "TUNER" => $tuner,
205 "MODE" => $mode,
206 );
207
208 // ATで予約する
209 $process = proc_open( $cmdline , $descriptor, $pipes, SPOOL, $env );
210 if( is_resource( $process ) ) {
211 fwrite($pipes[0], SLEEP." ".$sleep_time."\n" );
212 fwrite($pipes[0], DO_RECORD . "\n" );
213 fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" );
214 if( USE_THUMBS ) {
215 // サムネール生成
216 $ffmpeg_cmd = FFMPEG." -i \${OUTPUT} -r 1 -s 160x90 -ss ".(FORMER_TIME+2)." -vframes 1 -f image2 ".INSTALL_PATH.THUMBS."/".$filename.".jpg\n";
217 fwrite($pipes[0], $ffmpeg_cmd );
218 }
219 fclose($pipes[0]);
220 // 標準エラーを取る
221 $rstring = stream_get_contents( $pipes[2]);
222
223 fclose( $pipes[2] );
224 proc_close( $process );
225 }
226 else {
227 $rrec->delete();
228 throw new Exception("AT実行エラー");
229 }
230 // job番号を取り出す
231 $rarr = array();
232 $tok = strtok( $rstring, " \n" );
233 while( $tok !== false ) {
234 array_push( $rarr, $tok );
235 $tok = strtok( " \n" );
236 }
237 $key = array_search("job", $rarr);
238 if( $key !== false ) {
239 if( is_numeric( $rarr[$key+1]) ) {
240 $rrec->job = $rarr[$key+1];
241 return $rrec->job; // 成功
242 }
243 }
244 // エラー
245 $rrec->delete();
246 throw new Exception( "job番号の取得に失敗" );
247 }
248 catch( Exception $e ) {
249 if( $rrec != null ) {
250 if( $rrec->id ) {
251 // 予約を取り消す
252 $rrec->delete();
253 }
254 }
255 throw $e;
256 }
257 }
258 // custom 終了
259
260 // 取り消し
261 public static function cancel( $reserve_id = 0, $program_id = 0 ) {
262 $rec = null;
263
264 try {
265 if( $reserve_id ) {
266 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "id" , $reserve_id );
267 }
268 else if( $program_id ) {
269 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "program_id" , $program_id );
270 }
271 if( $rec == null ) {
272 throw new Exception("IDの指定が無効です");
273 }
274 if( ! $rec->complete ) {
275 // 未実行の予約である
276 if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + FORMER_TIME) )
277 throw new Exception("過去の録画予約です");
278 exec( ATRM . " " . $rec->job );
279 }
280 $rec->delete();
281 }
282 catch( Exception $e ) {
283 throw $e;
284 }
285 }
286 }
287 ?>