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

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

支持平台:通用

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

套餐价格:

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

官网地址:open.ys7.com/

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

3.对接接口

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

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

代码:

<?php
namespace appcommonlogic;


use thinkCache;

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

原文地址:https://www.cnblogs.com/jucheng/p/12800257.html