Files

129 lines
3.2 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
class CameraController extends Controller
{
public function actionStream($id, $scale = 50)
{
$cms = Yii::$app->params['cameras'];
if (!isset($cms[$id]))
{
throw new NotFoundHttpException();
}
if (!is_numeric($scale)) $scale = 50;
$zm_id = $cms[$id];
$source_url = Yii::$app->params['zmUrl'].'/cgi-bin/nph-zms?scale='.$scale.'&mode=jpeg&maxfps=30&buffer=1000&monitor='.$zm_id.'&connkey='.rand(0, 65536);
$fp = fopen($source_url, 'r', false);
if ($fp)
{
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
// send mjpeg header:
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=ZoneMinderFrame");
// pass data
fpassthru($fp);
fclose($fp);
}
else throw new NotFoundHttpException();
}
public function actionView($id)
{
$cms = Yii::$app->params['cameras'];
if (!isset($cms[$id]))
{
throw new NotFoundHttpException();
}
$archive = $this->getArchive($cms[$id]);
return $this->render('view',
[
'id' => $id,
'archive' => array_reverse($archive),
]);
}
private function getArchive($zm_id) : array
{
$client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'zm/api');
$json = $client->getEvents($zm_id, date("Y-m-d H:i:s", time() - 7 * 24 * 3600));
$r = json_decode($json, true);
$result = [];
foreach ($r['events'] as $e)
{
$result[] = new \app\models\zm\Event($e['Event']);
}
$pages_count = $r['pagination']['pageCount'];
for ($i = 2; $i <= $pages_count; $i++)
{
$json = $client->getEvents($zm_id, date("Y-m-d H:i:s", time() - 7 * 24 * 3600), $i);
$r = json_decode($json, true);
foreach ($r['events'] as $e)
{
$result[] = new \app\models\zm\Event($e['Event']);
}
}
return $result;
}
private function getZMEvent($event_id)
{
$client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'zm/api');
$json = $client->getEvent($event_id);
$r = json_decode($json, true);
$e = new \app\models\zm\Event($r['event']['Event']);
return $e;
}
private function checkCameraAccess($camera_id, $event_id)
{
$cms = Yii::$app->params['cameras'];
if (!isset($cms[$camera_id]))
{
throw new NotFoundHttpException();
}
$e = $this->getZMEvent($event_id);
if ($e->monitor_id !== $camera_id)
{
throw new ForbiddenHttpException();
}
}
public function actionViewArchive($camera_id, $event_id)
{
$this->checkCameraAccess($camera_id, $event_id);
return $this->render('view_archive',
[
'camera_id' => $camera_id,
'id' => $event_id,
'event' => $this->getZMEvent($event_id),
]);
}
public function actionStreamEvent($camera_id, $event_id)
{
$this->checkCameraAccess($camera_id, $event_id);
$source_url = Yii::$app->params['zmUrl'].'/zm/index.php?mode=mpeg&format=h264&eid='.$event_id.'&view=view_video';
$fp = fopen($source_url, 'r', false);
if ($fp)
{
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
// pass data
fpassthru($fp);
fclose($fp);
}
else throw new NotFoundHttpException();
}
}