| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');include_once(FCPATH . 'application/controllers/Base_Controller.php');class Video extends Base_Controller {	public function __construct() {        parent::__construct();        $this->load->model('Videomonitor_model');        $this->load->model('Project_model');        $this->load->model('Network_model');        $this->load->model('System_config_model');    }    // 视屏监控列表    public function getlist()    {    	$userid = $this->get_user_info('id');    	$role = $this->get_user_info('role');    	$companyid = $this->get_user_info('companyid');    	$page = $this->input->post('page',true);    	$count = $this->input->post('count',true);        $page = empty($page) ? 1 : $page;        $count = empty($count) ? 16 : $count;        $project_id = intval($this->input->post('project_id',true));        // $network_id = $this->input->post('network_id',true);        // $lamp_id = $this->input->post('lamp_id',true);        // $action = $this->input->post('action',true);        $keyword = $this->input->post('keyword',true);        $filter = [];        if ($project_id > 0) $filter['projectid'] = $project_id;    	$field = "V.name as video_name,                    V.address,                    V.id,                    V.channel,                    V.devid,                    V.password,                    V.type,                    V.image";        if (!empty($keyword)) {            $filter['keyword'] = $keyword;        }        $accessToken = $this->get_accessToken();        $total = $this->Videomonitor_model->getTotal($filter,$role,$userid,$companyid,$project_id);        $data = $this->Videomonitor_model->get_list_by_role($role,$userid,$companyid,$field,$page,$count,$project_id,$filter);        foreach ($data as &$v) {            if (!empty($v['image'])) {                $v['image'] = base_url($v['image']);            }        }        json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>ceil($total/$count),'accessToken'=>$accessToken));        // if (empty($action)) {  // (视屏监控页)带分页的列表                   // }else{  // (项目管理页)不带分页的列表                    //     if ($network_id > 0) $filter['networkid'] = $network_id;        //     if ($lamp_id > 0) $filter['lampid'] = $lamp_id;        //     $data = $this->Videomonitor_model->get_list_by_filter($filter,$field);        //     json_result('0000',$this->response['0000'],array('video_list'=>$data));        // }    }    // 获取萤石云accessToken    private function get_accessToken(){        $accessToken = $this->System_config_model->get_system_config('YSYAccessToken');        $expireTime = $this->System_config_model->get_system_config('YSYAccessTokenExpireTime');        if (empty($accessToken) || empty($expireTime) || strtotime($expireTime) < time()) {            // token不存在或者token过期,重新获取token            $url = 'https://open.ys7.com/api/lapp/token/get';            $appKey = $this->config->config['YSYAppkey'];            $appSecret = $this->config->config['YXYSecret'];            $resData = request_post($url,array('appKey'=>$appKey,'appSecret'=>$appSecret));            $resData = json_decode($resData);            if ($resData->code != 200) {                exit(json_result('0814',$this->response['0814'],array()));            }            // 获取token成功,记录token值与过期时间            $accessToken = $resData->data->accessToken;            $this->System_config_model->set_system_config('YSYAccessToken',$resData->data->accessToken);            $expireTime = floor($resData->data->expireTime/1000) - 24*3600;            $expireDate = date('Y-m-d H:i:s',$expireTime);            $this->System_config_model->set_system_config('YSYAccessTokenExpireTime',$expireDate);        }        return $accessToken;    }    // 翻译提示信息    private function translation_info($code){        if (!isset($code) || empty($code)) {            return 'Unknown error';        }        $data = array(            '20002' => 'Equipment does not exist',            '20007' => 'Equipment is not online',            '20010' => 'Device verification code error',            '20011' => 'Device add failure',            '20013' => 'The equipment has been added to others',            '20014' => 'DeviceSerial illegality',            '20017' => 'The device has been added to its own',            '10004' => 'User does not exist',            '20018' => 'The user does not have the device',            '20032' => 'The user\'s lower channel does not exist',            '60020' => 'The command is not supported',            '60061' => 'Account flow exceeded or not purchased, limit opening',            '60060' => 'Unbound address',            '49999' => 'Data anomaly',        );         return empty($data['code']) ? 'Unknown error' : $data['code'];    }    // 添加/编辑视频监控    public function update(){        $where['id'] = $this->input->post('id',true);        $role = $this->get_user_info('role');        $data['name'] = $this->input->post('name',true);        $data['projectid'] = $this->input->post('projectid',true);        $data['devid'] = $this->input->post('devid',true);        $data['password'] = $this->input->post('password',true);        $data['type'] = intval($this->input->post('type',true));        $address = $this->input->post('address',true);        $channel = $this->input->post('channel',true);        $longitude = $this->input->post('longitude',true);        $latitude = $this->input->post('latitude',true);        if ($data['type'] == 0) {            $data['channel'] = 1;        }else{            $data['channel'] = $channel;        }                if(isset($address)) $data['address'] = $address;        if(isset($longitude)) $data['longitude'] = $longitude;        if(isset($latitude)) $data['latitude'] = $latitude;                if(empty($data['name'])) exit(json_result('0803',$this->response['0803'],array()));        if(empty($data['projectid'])) exit(json_result('0308',$this->response['0308'],array()));        if(empty($data['devid'])) exit(json_result('0808',$this->response['0808'],array()));        if(empty($data['password'])) exit(json_result('0809',$this->response['0809'],array()));        if($data['type'] < 0) exit(json_result('0810',$this->response['0810'],array()));        if($data['type'] == 1 && empty($channel)) exit(json_result('0811',$this->response['0811'],array()));        // 获取accessToken        $data['devid'] = strtoupper($data['devid']);        $accessToken = $this->get_accessToken();        $version = $this->session->userdata('version');                if (empty($where['id'])) {  // 添加数据            if ($this->Videomonitor_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']))) {                exit(json_result('0801',$this->response['0801'],array()));            }            if ($data['type'] == 1 && $this->Videomonitor_model->getDataCount(array('devid'=>$data['devid'],'channel'=>$data['channel']))) {                exit(json_result('0812',$this->response['0812'],array()));            }            //1. 添加设备            $url = 'https://open.ys7.com/api/lapp/device/add';            $res1 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'validateCode'=>$data['password']));            $res1 = json_decode($res1);            if ($res1->code != 200) {                if (empty($version)) {                    exit(json_result('other',$res1->msg,array()));                }else{                    exit(json_result('other',$this->translation_info($res1->code),array()));                }                            }            // 关闭视频加密            $url = 'https://open.ys7.com/api/lapp/device/encrypt/off';            request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'validateCode'=>$data['password']));            // 修改设备名称            $url = 'https://open.ys7.com/api/lapp/device/name/update';            request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'deviceName'=>$data['name']));            // 2.开通直播            $url = 'https://open.ys7.com/api/lapp/live/video/open';            $res2 = request_post($url,array('accessToken'=>$accessToken,'source'=>$data['devid'].':'.$data['channel']));            $res2 = json_decode($res2);            if ($res2->code != 200) {                if (empty($version)) {                    exit(json_result('other',$res1->msg,array()));                }else{                    exit(json_result('other',$this->translation_info($res1->code),array()));                }            }else{                if ($res2->data[0]->ret != 200 && $res2->data[0]->ret != 60062) {                    if (empty($version)) {                        exit(json_result('other',$res2->data[0]->desc,array()));                    }else{                        exit(json_result('other',$this->translation_info($res2->data[0]->ret),array()));                    }                }            }            // 3.获取播放地址            $url = 'https://open.ys7.com/api/lapp/live/address/get';            $res3 = request_post($url,array('accessToken'=>$accessToken,'source'=>$data['devid'].':'.$data['channel']));            $res3 = json_decode($res3);            if ($res3->code != 200) {                if (empty($version)) {                    exit(json_result('other',$res3->msg,array()));                }else{                    exit(json_result('other',$this->translation_info($res3->code),array()));                }            }            $res = $res3->data[0];            $data['hls'] = $res->hls;            $data['hlsHd'] = $res->hlsHd;            $data['rtmp'] = $res->rtmp;            $data['rtmpHd'] = $res->rtmpHd;                        // 4.获取图片            $url = 'https://open.ys7.com/api/lapp/device/capture';            $res4 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'channelNo'=>$data['channel']));            $res4 = json_decode($res4);            if ($res4->code == 200) {                $image = file_get_contents($res4->data->picUrl);                $filename = FCPATH.'file/image/C24816636_'.date('YmdHis').'.jpg';                $file = fopen($filename, 'w');                fwrite($file, $image);                fclose($file);                $data['image'] = 'api/file/image/C24816636_'.date('YmdHis').'.jpg';            }            $data['createtime'] = date('Y-m-d H-i-s',time());            $id = $this->Videomonitor_model->insert($data);            $data['count'] = 1;            // 设置项目跟网络的监控数            $this->Project_model->add_monitor_count($data);            $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');            $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下添加视屏监控,监控名称\"{$data['name']}\"",0);            $this->add_operation_log('insert',"Add video monitor.Monitor name:\"{$data['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);        }else{   // 编辑数据            if ($this->Videomonitor_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']),$where['id'])) {                exit(json_result('0801',$this->response['0801'],array()));            }            if ($data['type'] == 1 && $this->Videomonitor_model->getDataCount(array('devid'=>$data['devid'],'channel'=>$data['channel']),$where['id'])) {                exit(json_result('0812',$this->response['0812'],array()));            }            $oldData = $this->Videomonitor_model->getOne($where['id']);            $this->Videomonitor_model->update(array('id'=>$where['id']),$data);            $data['count'] = 1;            $oldData['count'] = 1;            if ($oldData['projectid'] != $data['projectid']) {                $this->Project_model->add_monitor_count($data);                $this->Project_model->minus_monitor_count($oldData);            }            if ($oldData['name'] != $data['name']) {                $url = 'https://open.ys7.com/api/lapp/device/name/update';                request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'deviceName'=>$data['name']));            }            $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');            $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的视屏监控,监控名称\"{$data['name']}\"",0);            $this->add_operation_log('update',"Update video monitor.Monitor name:\"{$data['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);            $id = $where['id'];        }        exit(json_result('0000',$this->response['0000'],array('id'=>$id)));    }    // 删除视频监控(支持批量操作)    public function del(){        $videoIds = $this->input->post('id',true);        $role = $this->get_user_info('role');        if (empty($videoIds)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $ids = explode(',', $videoIds);        $data = $this->Videomonitor_model->get_list_by_filter(array('id'=>$ids),'V.id,V.projectid,V.devid,V.name,V.image');        $url = 'https://open.ys7.com/api/lapp/device/delete';        $accessToken = $this->get_accessToken();        foreach ($data as $v) {            @unlink($v['image']);            // 删除萤石云上监控            request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$v['devid']));                        $v['count'] = 1;            $this->Project_model->minus_monitor_count($v);            $projectData = $this->Project_model->getData(array('id'=>$v['projectid']),'projectname');            $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目下的视屏监控,监控名称\"{$v['name']}\"",0);            $this->add_operation_log('delete',"Delete video monitor.Monitor name:\"{$v['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);        }        $this->Videomonitor_model->delData(array('id'=>$ids));        // 删除视频日志信息        $this->Videomonitor_model->del_video_info_log(array('videoid'=>$ids));                exit(json_result('0000',$this->response['0000'],array()));    }    // 视屏监控详情    public function video_info(){        $id = $this->input->post('id',true);        if (empty($id)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $field = "id,name,address,type,projectid,devid,password,channel,status,longitude,latitude,hls,hlsHD,rtmp,rtmpHD,image";        $data = $this->Videomonitor_model->getOne($id,$field);        $data['accessToken'] = $this->get_accessToken();        exit(json_result('0000',$this->response['0000'],$data));    }    // 播放视频监控    public function video_play(){        $id = $this->input->post('id',true);        if (empty($id)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $field = "id,name,address,type,projectid,devid,password,channel,status,longitude,latitude,hls,hlsHD,rtmp,rtmpHD,image";        $data = $this->Videomonitor_model->getOne($id,$field);        // 4.获取图片        $accessToken = $this->get_accessToken();        $url = 'https://open.ys7.com/api/lapp/device/capture';        $res4 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'channelNo'=>$data['channel']));        $res4 = json_decode($res4);        if ($res4->code == 200) {            $image = file_get_contents($res4->data->picUrl);            $filename = FCPATH.'file/image/C24816636_'.date('YmdHis').'.jpg';            $file = fopen($filename, 'w');            fwrite($file, $image);            fclose($file);            $image = 'api/file/image/C24816636_'.date('YmdHis').'.jpg';            $this->Videomonitor_model->update(array('id'=>$id),array('image'=>$image));            @unlink($data['image']);            $data['image'] = $image;        }        $data['accessToken'] = $this->get_accessToken();        exit(json_result('0000',$this->response['0000'],$data));    }    // 云台控制    public function yun_start(){        $deviceSerial = $this->input->post('deviceSerial',true);        $channelNo = $this->input->post('channelNo',true);        $direction = intval($this->input->post('direction',true));        $speed = intval($this->input->post('speed',true));        if (empty($channelNo) || empty($deviceSerial)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $accessToken = $this->get_accessToken();        $version = $this->session->userdata('version');        // 开始云台控制        $url1 = 'https://open.ys7.com/api/lapp/device/ptz/start';        $request_data = array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction,'speed'=>$speed);        // var_dump($request_data);        $data = request_post($url1,$request_data);        $data = json_decode($data);        if ($data->code != '200') {            if (empty($version)) {                exit(json_result('other',$data->msg,array()));            }else{                exit(json_result('other',$this->translation_info($data->msg),array()));            }        }        // sleep(1);        // var_dump($data);        // 停止云台控制        // $url2 = 'https://open.ys7.com/api/lapp/device/ptz/stop';        // $data2 = request_post($url2,array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction));        // $data2 = json_decode($data2);                exit(json_result('0000',$this->response['0000'],array()));    }    public function yun_end(){        $deviceSerial = $this->input->post('deviceSerial',true);        $channelNo = $this->input->post('channelNo',true);        $direction = intval($this->input->post('direction',true));        $accessToken = $this->get_accessToken();        $version = $this->session->userdata('version');        if (empty($channelNo) || empty($deviceSerial)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $url2 = 'https://open.ys7.com/api/lapp/device/ptz/stop';        $data2 = request_post($url2,array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction));        $data2 = json_decode($data2);        if ($data2->code != 200) {            if (empty($version)) {                exit(json_result('other',$data2->msg,array()));            }else{                exit(json_result('other',$this->translation_info($data2->msg),array()));            }        }        exit(json_result('0000',$this->response['0000'],array()));    }} ?>
 |