<?php

namespace app\api\controller\reservoir\hkws;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
header('Content-type:text/html; Charset=utf-8');
date_default_timezone_set('PRC');
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 = ['getTestCameraUrl','sendonnly','send','getquery','getCameraUrl','getToken', 'yuntaiStop','yuntaiCenter','getTestTalkUrl', 'take_pic', 'voice_zhanyong_username'];
    protected $noNeedRight = '*';
    public $api = null;

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

    /***
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获得监控地址
     */
    public function getCameraUrl()
    {
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        $protocol = $this->request->param("protocol");
        $streamType = $this->request->param("streamType");
        $channel_no = $this->request->param("channel_no");

        $type=2;
        if($streamType==0){
            $type=2;
        }else {
            $type=1;
        }
        //先查询到设备信息
        $equipment=Db::name("reservoir_hkws_hardware")->where("scode",$cameraIndexCode)->find();
        if (empty($equipment)){
            $this->error('设备不存在');
        }
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'channelNo' => $channel_no,
            'deviceSerial' => $cameraIndexCode,
            'expireTime' => 600,//过期时间
            'protocol' => $protocol,//流播放协议,1-ezopen、2-hls、3-rtmp、4-flv,默认为1
            'quality' => $type,//视频清晰度,1-高清(主码流)、2-流畅(子码流)
        ];
        $res = $api->getHkAddress($post_data);
        //获得监控信息,经纬度查询天气预报
        //获得监控信息,经纬度查询天气预报
        $info = Db::name('reservoir_hkws_hardware')
            ->where(['scode' => $cameraIndexCode])
            ->field('id,latitude,longitude,indexCode,type_child_name')
            ->find();
        $res['data']['latitude'] = $info['latitude'];
        $res['data']['longitude'] = $info['longitude'];
        $res['data']['type_child_name'] = $info['type_child_name'];
        //判断是否在线 200在线
        if($res['code']=="200"){
            $res['data']['status'] = 1;

        }else{
            $res['data']['status'] = 2;

        }
        $data['camera'] = $res;
        //
        if($equipment['play_type']==1){
        $res = $this->getTalkUrlFunc($equipment['haikang_code']);
            
        }
        $data['talk'] = $res;


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



    /***
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获得监控地址
     */
    public function getCameraoff()
    {
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'deviceSerial' => $cameraIndexCode,
        ];
        $res = $api->Cameraoff($post_data);
        $data['camera'] = $res;
        $this->success('', $data);
    }

    /***
     * 云台操控开始
     */
    public function yuntaiCenter()
    {
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        $channel_no = $this->request->param("channel_no");
        $action = input('post.action', 2);
        $speed = input('post.speed', 1);
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'deviceSerial' => $cameraIndexCode,
            'channelNo' => $channel_no,//通道号
            'direction' => $action,//操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距,16-自动控制
            'speed' => $speed,//云台速度:1-适中,2-快
        ];
        $res = $api->ytCenterStart($post_data);

        $this->success('获取成功', $res);
    }
    /***
     * 云台操控结束
     */
    public function yuntaiStop()
    {
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        $action = $this->request->param("action");
        $channel_no = $this->request->param("channel_no");
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'deviceSerial' => $cameraIndexCode,
            'channelNo' => $channel_no,//通道号
            'direction' => $action,//操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距,16-自动控制
        ];
        $res = $api->ytCenterStop($post_data);

        $this->success('获取成功', $res);
    }

    public function gettoken(){
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        $this->success('获取成功', $token);
    }

    public function getTestCameraUrl()
    {
        $cameraIndexCode = input('get.cameraIndexCode');
        $protocol = 'rtsp';
        $streamType = 1;//input('post.streamType', 0);
        $res = $this->api->getPreviewUrl($cameraIndexCode, $protocol, $streamType);
        var_dump($res);
    }

    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->getTalkUrl($cameraIndexCode);
        $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);
        }
    }


    /**
     * 获取下发语音列表
     * @return void
     */
    public function getquery(){
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token
        ];
        $res = $api->getquery($post_data);
        $this->success("获取成功", $res);
    }

    /**
     * 获取下发语音列表
     * @return void
     */
    public function capacity(){
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'deviceSerial' => $cameraIndexCode
        ];

        $res = $api->getquery($post_data);
        $this->success("获取成功", $res);
    }


    /**
     * 下发语音
     * @return void
     */
    public function send(){
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();

        $reservoir_hkws_hardware=Db::name("reservoir_hkws_hardware")->select();
        foreach ($reservoir_hkws_hardware as $k=>$v){
            //请求数据
            $post_data = [
                'accessToken' => $token,
                'deviceSerial' => $v['scode'],
                'channelNo' => $v['channel_no'],
                'fileUrl' => "http://custom-voice-reminder-hn.oss-cn-shenzhen.aliyuncs.com/voice/ee3dfefddfba4b2e91194c13ce19181c.aac?Expires=1711557046&OSSAccessKeyId=LTAI4Fvm85GR68d4cEW63uMy&Signature=M4sJE0juOKlSmbpVe8orBiZOlSM%3D",
            ];
            $res = $api->send($post_data);
//            print_r($res);
//            print_r("===========");
        }

            return $this->success("下发成功");

    }

    /**
     * 下发语音
     * @return void
     */
    public function sendonnly(){
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        $cameraIndexCode = $this->request->param("cameraIndexCode");
        $channel_no = $this->request->param("channel_no");
            //请求数据
            $post_data = [
                'accessToken' => $token,
                'deviceSerial' => $cameraIndexCode,
                'channelNo' => $channel_no,
                'fileUrl' => "http://custom-voice-reminder-hn.oss-cn-shenzhen.aliyuncs.com/voice/ee3dfefddfba4b2e91194c13ce19181c.aac?Expires=1711557046&OSSAccessKeyId=LTAI4Fvm85GR68d4cEW63uMy&Signature=M4sJE0juOKlSmbpVe8orBiZOlSM%3D",
            ];
            $res = $api->send($post_data);

        return $this->success("下发成功",$res);

    }
}