diff --git a/commands/TestController.php b/commands/TestController.php index 5860df5..da7bb81 100644 --- a/commands/TestController.php +++ b/commands/TestController.php @@ -9,7 +9,14 @@ class TestController extends Controller { public function actionIndex() { - $client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'/zm/api'); + $client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'zm/api'); print_r($client->getVersion()); } + + public function actionEvents($id) + { + $client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'zm/api'); + //print_r($client->getAllEvents($id)); + print_r($client->getEvents($id, date("Y-m-d H:i:s", strtotime('-10 days')))); + } } diff --git a/components/ApiClient.php b/components/ApiClient.php index 113f31d..2684e14 100644 --- a/components/ApiClient.php +++ b/components/ApiClient.php @@ -29,7 +29,6 @@ class ApiClient { $this->http_client->setOption(CURLOPT_POSTFIELDS, $json); } - $result = $this->http_client->$method($url); if ($this->http_client->responseCode !== 200) { @@ -44,4 +43,15 @@ class ApiClient return $this->sendRequest('host/getVersion.json'); } + public function getAllEvents($id) + { + return $this->sendRequest('events/index/MonitorId:'.$id.'.json'); + } + + public function getEvents($id, $start_time, $page = 1) + { + return $this->sendRequest('events/index/MonitorId:'.$id.'/'. + urlencode('StartTime >=:'.$start_time).'.json?page='.$page); + } + } \ No newline at end of file diff --git a/controllers/CameraController.php b/controllers/CameraController.php index b6ef515..c332a42 100644 --- a/controllers/CameraController.php +++ b/controllers/CameraController.php @@ -41,9 +41,43 @@ class CameraController extends Controller { throw new NotFoundHttpException(); } + $archive = $this->getArchive($cms[$id]); return $this->render('view', [ - 'id' => $id + '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; + } + + public function actionViewArchive($event_id) + { + return $this->render('view_archive', + [ + 'id' => $event_id, ]); } } + diff --git a/models/zm/Event.php b/models/zm/Event.php new file mode 100644 index 0000000..6e93b9f --- /dev/null +++ b/models/zm/Event.php @@ -0,0 +1,19 @@ +id = $json['Id']; + $this->monitor_id = $json['MonitorId']; + $this->start_time = $json['StartDateTime']; + $this->end_time = $json['EndDateTime']; + } +} \ No newline at end of file diff --git a/views/camera/view.php b/views/camera/view.php index 0a18b1c..4b63aab 100644 --- a/views/camera/view.php +++ b/views/camera/view.php @@ -2,6 +2,7 @@ /** @var yii\web\View $this */ use yii\helpers\Url; +use yii\helpers\Html; $this->title = 'Камера '.$id; ?> @@ -12,7 +13,12 @@ $this->title = 'Камера '.$id; $id]); ?>'>
- Архив записей: + Архив записей:
+
diff --git a/views/camera/view_archive.php b/views/camera/view_archive.php new file mode 100644 index 0000000..4a7ddfa --- /dev/null +++ b/views/camera/view_archive.php @@ -0,0 +1,13 @@ +title = 'Архив, запись #'.$id; +?> +
+
+
+ TODO! +
+