Hardware.php 4.4 KB
<?php

namespace app\api\controller\reservoir\hkws;

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

/**
 * Class Displacement
 * @package app\api\controller
 */
class Hardware extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\reservoir\hkws\Hardware;
    }

    public function getByType()
    {
        $type = input('get.type');
        $id = input('get.id');
        $list = Db::name('reservoir_hkws_hardware')->alias('h')->join('reservoir_list l', 'h.reservoir_id = l.id')->field('l.id,h.indexCode,l.name')->where('h.' . $type . '_id', $id)->select();
        foreach ($list as $k => &$v) {
            $v['rsvr'] = $this->getRsvrById($v['id']);
        }
        $this->success('', $list);
    }

    //根据水库id获取水位信息
    public function getRsvrById($id)
    {
        $info = Db::name('st_rsvr_r')->where('RESERVOIR_ID', $id)->order('TM desc')->limit(1)->find();
        return $info;
    }

    //根据县市id获取cameraindexcode
    public function getListByCounty($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id')->select();
        $ids = [];
        foreach ($reservoirids as $k => $v) {
            $ids[] = $v['id'];
        }
        $list = Db::name('reservoir_hkws_hardware')->field('id,name,indexCode')->where('reservoir_id', 'in', $ids)->select();
        $this->success('', $list);
    }

    //根据水库id获取cameraindexcode
    public function getListByReservoir($id)
    {
        $config = get_addon_config('qiniu');
        $list = Db::name('reservoir_hkws_hardware')
            ->field('id,name,scode as indexCode,image,video_type')
            ->where('reservoir_id', $id)
            ->select();

        foreach ($list as &$v) {
            $v['image'] = $config['cdnurl'] . $v['image'];
            $v['reservoir_name'] = Db::name("reservoir_list")->where("id", $id)->value("name");
            //计算当前设备未读入侵条数
            $monitoringCount = Db::name('reservoir_hkws_alarm_monitoring')
                ->where(['cameraIndexCode' => $v['indexCode'], 'status' => '0'])
                ->count();
            $v['monitor_num'] = $monitoringCount ? $monitoringCount : 0;

            if($v['video_type']==1){
                $cameraUrl=$this->getCameraUrlisc($v['indexCode']);
            }else{
                $cameraUrl=$this->getCameraUrl($v['indexCode']);
            }
            $v['cameraUrl']=$cameraUrl['camera'];

        }

        usort($list, function ($a, $b) {
            return $b['monitor_num'] - $a['monitor_num'];
        });
        $this->success('获取成功', $list);
    }
    public function getCameraUrl($cameraIndexCode){
        $protocol =1;
        $streamType = 1;
        //获得监控视频地址
        $api = new \app\api\controller\Ysyun();
        $token = $api->getaccessToken();
        //请求数据
        $post_data = [
            'accessToken' => $token,
            'deviceSerial' => $cameraIndexCode,
            'expireTime' => 600,//过期时间
            'protocol' => $protocol,//流播放协议,1-ezopen、2-hls、3-rtmp、4-flv,默认为1
            'quality' => $streamType,//视频清晰度,1-高清(主码流)、2-流畅(子码流)
        ];
        $res = $api->getHkAddress($post_data);
        //获得监控信息,经纬度查询天气预报
        $info = Db::name('reservoir_hkws_hardware')
            ->where(['scode' => $cameraIndexCode])
            ->field('id,latitude,longitude,indexCode')
            ->find();
        $res['data']['latitude'] = $info['latitude'];
        $res['data']['longitude'] = $info['longitude'];

        $data['camera'] = $res;
        return $data;
    }
    public function getCameraUrlisc($cameraIndexCode){
        $this->api = new Iscapi();
        $protocol =1;
        $streamType = 1;
        $res = $this->api->getPreviewUrl($cameraIndexCode, $protocol, $streamType);
        $data['talk'] = $res;
        //获得监控信息,经纬度查询天气预报
        $info = Db::name('reservoir_hkws_hardware')
            ->where(['scode' => $cameraIndexCode])
            ->field('id,latitude,longitude,indexCode')
            ->find();
        $res['data']['latitude'] = $info['latitude'];
        $res['data']['longitude'] = $info['longitude'];
        $data['camera'] = $res;
        return $data;
    }
}