ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

萤石云、海康 摄像头对接+云台控制控制

2020-04-29 10:05:58  阅读:490  来源: 互联网

标签:云台 return accessToken url res number 海康 device 摄像头


注:云台控制只支持带有该功能设备,例如萤石云c6系列

支持平台:通用

不建议多人同时观看的项目,适合单人观看,多人建议升级套餐

套餐价格:

 

1.登录萤石云开发平台 获取AppKey、Secret

官网地址:open.ys7.com/

2.添加购买的设备到萤石云账号

3.对接接口

注:本文章已对接功能 关闭设备视频加密、获取设备直播地址、云台控制设备开始转动、云台控制设备停止转动、设备开通直播、设备关闭直播、设备设置预置点、设备调用预置点、设备清除预置点,其他功能还支持例如设备抓拍图片、设配在线添加、配置等功能,其他详细功能可参考下方接口文档

接口地址:open.ys7.com/doc/zh/book…

代码:

 

 

 

<?php
namespace app\common\logic;


use think\Cache;

class Broadcast extends Base
{
    //萤石云 appKey
    protected $appKey = "f777f08665f8438db7871f90899bad10";

    //萤石云 appSecret
    protected $appSecret = "50b5736bd6900001f0bf2db305b26b20";

    //请求头部
    protected $header=['Content-Type: application/x-www-form-urlencoded'];

    //云台控制 转动速度 0-慢,1-适中,2-快,海康设备参数不可为0
    protected $speed = 1;
    /**
     * 获取授权的accessToken
     */
    public function getAccessToken()
    {
        $url = 'https://open.ys7.com/api/lapp/token/get';
        $content = "appKey=$this->appKey&appSecret=$this->appSecret";
        $accessToken = Cache::get('accessToken');
        if(!$accessToken){// 缓存 不存在
            if($accessToken['expireTime'] < time()){//缓存 accessToken 已过期
                $accessToken = $this->httpPost($url,$this->header,$content);
                Cache::set('accessToken',$accessToken['data']);
                return $accessToken['data']['accessToken'];
            }
        }

        return $accessToken['accessToken'];

    }

    /**
     * 关闭设备视频加密
     */
    public function deviceDecrypt($device_number,$verification_code){
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/encrypt/off';

        $content = "accessToken=$accessToken&deviceSerial=$device_number&validateCode=$verification_code";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 获取设备直播地址
     * hlsHd 直播地址
     * @param $device_number 设备号 数组形式
     * @return mixed
     */

    public function getAddress($device_number=[],$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/live/address/get';
        $source = '';
        foreach ($device_number as $key=>$value){
            $source .= $value.':'.$channel_number.',';
        }
        $content = "accessToken=$accessToken&source=$source";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 云台控制设备开始转动
     * @param $device_number 设备序列号
     * @param $direction 操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距
     * @return mixed
     */
    public function startTurn($device_number,$direction)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/ptz/start';
        $content = "accessToken=$accessToken&deviceSerial=$device_number&channelNo=1&direction=$direction&speed=$this->speed";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 云台控制设备停止转动
     * @param $device_number 设备序列号
     * @param $direction 操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距
     * @return mixed
     */
    public function stopTurn($device_number,$direction=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/ptz/stop';
        $content = "accessToken=$accessToken&deviceSerial=$device_number&channelNo=1&direction=$direction&speed=$this->speed";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 设备开通直播
     * @param $device_number 设备号 数组
     * @param int $channel_number
     * @return mixed
     */
    public function openDevice($device_number,$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/live/video/open';
        $source = '';
        foreach ($device_number as $key=>$value){
            $source .= $value.':'.$channel_number.',';
        }
        $content = "accessToken=$accessToken&source=$source";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 设备关闭直播
     * @param $device_number
     * @param int $channel_number
     * @return mixed
     */
    public function closeDevice($device_number,$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/live/video/close';
        $source = '';
        foreach ($device_number as $key=>$value){
            $source .= $value.':'.$channel_number.',';
        }
        $content = "accessToken=$accessToken&source=$source";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * 设备设置预置点
     * @param $device_number
     * @param int $channel_number
     * @return mixed
     */
    public function setPreset($device_number,$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/preset/add';
        $content = "accessToken=$accessToken&deviceSerial=$device_number&channelNo=$channel_number";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }
    /**
     * 设备调用预置点
     * @param $device_number
     * @param int $channel_number
     * @return mixed
     */
    public function callPreset($device_number,$index,$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/preset/move';
        $content = "accessToken=$accessToken&deviceSerial=$device_number&channelNo=$channel_number&index=$index";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }
    /**
     * 设备清除预置点
     * @param $device_number
     * @param int $channel_number
     * @return mixed
     */
    public function clearPreset($device_number,$index,$channel_number=1)
    {
        $accessToken = $this->getAccessToken();
        $url='https://open.ys7.com/api/lapp/device/preset/clear';
        $content = "accessToken=$accessToken&deviceSerial=$device_number&channelNo=$channel_number&index=$index";
        $res=$this->httpPost($url,$this->header,$content);
        return $res;
    }

    /**
     * post 请求
     * @param $url
     * @param $header
     * @param array $data
     * @return mixed
     */
    public function httpPost($url,$header ,$data=array())
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $status = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($status, true);
        return $res;
    }
}

文章转载自:https://www.juchengvi.com/looknews/78

标签:云台,return,accessToken,url,res,number,海康,device,摄像头
来源: https://www.cnblogs.com/jucheng/p/12800257.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有