<?php

namespace app\api\controller\reservoir\hkws;

use app\common\controller\Api;
use think\Request;
use think\Db;
use isc\Api as Iscapi;

/**
 * Class Displacement
 * @package app\api\controller
 */
class Isc extends Api
{
    protected $noNeedLogin = ['playbackUrl','CameraUrlall','getCameraUrl', 'getcamera_search','getCameraUrl', 'getTestTalkUrl', 'take_pic', 'voice_zhanyong_username'];
    protected $noNeedRight = '*';
    public $api = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->api = new Iscapi();
    }

    public function getCameraUrl()
    {
        $cameraIndexCode = input('post.cameraIndexCode');
        $protocol = input('post.protocol', 'rtsp');
        $streamType = input('post.streamType', 0);
        if ($streamType == 0) {
            $stype = 1;
        } else {
            $stype = 0;
        }
        $res = $this->api->getPreviewUrl($cameraIndexCode, $protocol, $stype);
        //获得监控信息,经纬度查询天气预报
        $info = Db::name('reservoir_hkws_hardware')
            ->where(['indexCode' => $cameraIndexCode])
            ->field('id,latitude,longitude,indexCode')
            ->find();
        $res['data']['latitude'] = $info['latitude'];
        $res['data']['longitude'] = $info['longitude'];

        $data['camera'] = $res;
        $res = $this->getTalkUrlFunc($cameraIndexCode);
        $data['talk'] = $res;

        $this->success('', $data);
    }

    /**
     * 回放功能
     * @return void
     */
    public function playbackUrl()
    {
        $cameraIndexCode = input('cameraIndexCode');
        $begin_time = input('beginTime');
        $end_time = input('endTime');
        //时间要为DATE_ISO8601格式 这个等接接口的时候看前端传什么格式的时间再调整
        if($begin_time>time()){
            $this->error("时间选择错误");
        }
        if($end_time>time()){
            $this->error("时间选择错误");
        }
        $beginTime = date("Y-m-d\TH:i:s",$begin_time).".000+08:00";
        $endTime = date("Y-m-d\TH:i:s",$end_time).".000+08:00";
        $res = $this->api->playbackUrl($cameraIndexCode,$beginTime,$endTime);
        if($res['code']=="0"){
            foreach ($res['data']['list'] as $k=>$v){
                $res['data']['list'][$k]['beginTime']=date("Y-m-d H:i:s",strtotime($res['data']['list'][$k]['beginTime']));
                $res['data']['list'][$k]['endTime']=date("Y-m-d H:i:s",strtotime($res['data']['list'][$k]['endTime']));
            }
            $this->success('请求成功', $res);
        }else{
            $data=['cameraIndexCode'=>$cameraIndexCode,'begin_time'=>$begin_time,'end_time'=>$end_time];
            $errdata=[
                'data'=>$data,
                'res'=>$res
            ];
            $this->error('请求失败',$errdata);
        }

    }
    public function CameraUrlall(){
        $protocol = $this->request->param('protocol');
        $hardware=Db::name("reservoir_hkws_hardware")->where('reservoir_id',27)->limit(16)->select();
        $streamType = "1";
        $return_data=[];
        foreach ($hardware as $k=>$v){
            $cameraIndexCode = $hardware[$k]['indexCode'];
            $res = $this->api->getPreviewUrl($cameraIndexCode,$protocol,$streamType);
            $return_data[$k]=$res;
            $return_data[$k]['name']=$hardware[$k]['name '];
        }
        $this->success('',$return_data);
    }
    public function getTalkUrl()
    {
        $cameraIndexCode = input('post.cameraIndexCode');
        $res = $this->api->getTalkUrl($cameraIndexCode);
        $this->success('', $res);
    }

    public function getTalkUrlFunc($cameraIndexCode)
    {
        $res = $this->api->getTalkUrl($cameraIndexCode);
        return $res;
    }

    public function getTestTalkUrl()
    {
        $cameraIndexCode = input('get.cameraIndexCode');
        $res = $this->api->testgetPreviewUrl($cameraIndexCode);
        $this->success('', $res);
    }
    public function getcamera_search()
    {
        $res = $this->api->testgetcamera_searchUrl();
        $this->success('', $res);
    }

    public function yuntai()
    {
        $cameraIndexCode = input('post.cameraIndexCode');
        $action = input('post.action');
        $command = input('post.command');
        $speed = input('post.speed');
        $res = $this->api->controlling($cameraIndexCode, $action, $command, $speed);
        $this->success('', $res);
    }

    /**
     * 手动抓图
     */
    public function take_pic()
    {
        $cameraIndexCode = input('post.cameraIndexCode');
        $res = $this->api->manualCapture($cameraIndexCode);
        $this->success('', $res);
    }

    /**
     * 15. 语音通道占用与解除占用
     */
    public function operate_voice()
    {
        $param = $this->request->param();
        if (empty($param['cameraIndexCode'])) {
            $this->error("设备编码不能为空");
        }
        if (empty($param['type'])) {
            $this->error("操作类型不能为空");
        }
        $hardware = Db::name("reservoir_hkws_hardware")->where("indexCode", $param['cameraIndexCode'])->find();
        if (empty($hardware)) {
            $this->error("未找到有效设备");
        }
        if ($param['type'] == 1) {
            //开启占用
            Db::name("reservoir_hkws_hardware")->where("indexCode", $param['cameraIndexCode'])->update(["intercom_occupancy_userid" => $this->auth->id]);
            $uid = $this->auth->id;
        } elseif ($param['type'] == 2) {
            //解除占用
            Db::name("reservoir_hkws_hardware")->where("indexCode", $param['cameraIndexCode'])->update(["intercom_occupancy_userid" => ""]);
            $uid = "";
        }
        $this->success("操作成功", $uid);
    }

    /**
     * 16. 获取语音对讲占用人
     */
    public function voice_zhanyong_username()
    {
        $param = $this->request->param();
        if (empty($param['cameraIndexCode'])) {
            $this->error("设备编码不能为空");
        }

        $userid = Db::name("reservoir_hkws_hardware")->where("indexCode", $param['cameraIndexCode'])->value("intercom_occupancy_userid");
        if (empty($userid)) {
            $this->error("无人占用");
        } else {
            $staff_name = Db::name("inspection_staff")->where("user_id", $userid)->value("staff_name");
            $this->success("占用人姓名获取成功", $staff_name);
        }
    }
}