Pārlūkot izejas kodu

天地伟业接口修改

任敬轩 2 gadi atpakaļ
vecāks
revīzija
a1340dd5e2

+ 1 - 1
Modules/Camera/Http/Controllers/Api/CameraApiController.php

@@ -192,7 +192,7 @@ class CameraApiController extends BaseController
             $mine_res = DB::table('mine_list')->where('id',$parent_id)->get();
             //天地伟业
             if(isset($mine_res[0]->title) && in_array($mine_res[0]->title,config('mine'))){
-                $result = CameraServices::getRtspTianDi($camera_id,$parent_id);
+                $result = CameraServices::getRtspTianDiEasy($camera_id,$parent_id);
             //宁煤集团
             }elseif(isset($mine_res[0]->title) && in_array($mine_res[0]->title,config('mine_hls'))){
                 $result = CameraServices::getHkHls($camera_id,$parent_id);

+ 173 - 0
Modules/Camera/Services/CameraServices.php

@@ -779,6 +779,179 @@ class CameraServices
     }
 
     //根据摄像头id获取摄像头播放链接
+    public static function getRtspTianDiEasy($camera_id,$parent_id)
+    {
+
+        $result['status'] = true;
+        $result['msg']    = ApiEnum::RETURN_SUCCESS;
+
+        $query = CameraList::where('id', $camera_id)->first();
+
+        if (!$query) {
+            $result['status'] = false;
+            $result['msg'] = ApiEnum::NO_CAMERA_URL;
+            return $result;
+        }
+
+        $degree = MineList::where('id', $query->mine_id)->value('degree');
+
+        $mine_list = MineList::whereIn('id', explode('|', $degree))->select(['title'])->get()->toArray();
+
+        $path = '';
+        foreach ($mine_list as $key => $val) {
+            $path .= '/' . $val['title'];
+        }
+        $new_camera_name = str_replace('-','',$query->camera_name);
+        $new_camera_name = str_replace('#','号',$new_camera_name);
+        $path .= '/' . $new_camera_name;
+        // dd($path);
+        //标记摄像头为在线状态
+        CameraList::where('id', $camera_id)->update(['camera_status' => CameraEnum::CAMERA_STATUS_ONLINE]);
+
+        //已有拉流id,直接返回拉流链接
+        if($query->revert_id != 'NullId'){
+            $result['data'] = [
+                'camera_id' => $camera_id,
+                'url' => env('EASY_DARWIN_JF_RTSP') .  $path
+            ];
+            return  $result;
+        }
+
+
+
+        $mine_ext = MineListExt::where('mine_id', $parent_id)->first();
+
+        Input::replace(
+            [
+                'url'      => trim($mine_ext->ip, '/') . ':' . $mine_ext->port,
+                'username' => $mine_ext->key,
+                'password' => $mine_ext->secret,
+                'sysId'    => 'PG',
+                'sId'      => $query->index_code,
+                'ip'       => trim(trim($query->ip, 'http://'), 'https://'),
+                'port'     => $query->port,
+            ]
+        );
+
+        $tdwy = new TdwyController();
+        $result_td = $tdwy->getRtspById();
+
+
+        $data = [
+            'url'        => $result_td['content'][0]['url'],
+            'customPath' => $path,
+        ];
+        // dd($data);
+        Log::info('请求流媒体服务器参数------------');
+        Log::info($data);
+
+        $curl = env('EASY_DARWIN_JF_URL') . CameraEnum::API_STREAM_START . '?' . http_build_query($data);
+
+        //请求流媒体服务器拉流
+        $curl_res = curl_request($curl);
+
+        //访问记录
+        $log['mine_id'] = $query->mine_id;
+        $log['camera_id'] = $camera_id;
+        $log['log'] = $curl_res;
+        if(strpos($curl_res,' ')){
+            $log['status'] = 1;//异常
+        }else{
+            $log['status'] = 0;//正常
+        }
+        $log['created_at'] = date('Y-m-d H:i:s');
+        $log['updated_at'] = date('Y-m-d H:i:s');
+        DB::table('log')->insert($log);
+
+        // DB::table('camera_list')->where('id',$camera_id)->update($res);
+        // dd($res);
+        Log::info('请求流媒体服务器result------------');
+        Log::info($curl_res);
+
+        if (strpos($curl_res,' ')) {
+            //标记摄像头为异常状态
+            CameraList::where('id', $camera_id)->update(['camera_status' => CameraEnum::CAMERA_STATUS_ERROR]);
+
+            $result['status'] = false;
+            $result['msg']    = ApiEnum::EASY_DAWIN_REQUEST_FAIL;
+            return $result;
+        }
+
+        //更新拉流id
+        CameraList::where('id', $camera_id)->update(
+            [
+                'revert_id'  => trim($curl_res, '"'),
+                'updated_at' => date('Y-m-d H:i:s'),
+            ]
+        );
+
+        $result['data'] = [
+            'camera_id' => $camera_id,
+            'url' => env('EASY_DARWIN_JF_RTSP') . $path
+        ];
+
+        //查询是否存在封面图
+        $cover_picture = CameraList::where('id', $camera_id)->value('cover_picture');
+
+        if (!$cover_picture) {
+            if (!is_dir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path)) {
+                mkdir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path, 0777, true);
+            }
+            // dd($result_td['content'][0]['url']);
+            $exec = 'nohup /usr/bin/ffmpeg -i "' . $result_td['content'][0]['url'] . '" -vcodec copy -acodec copy -vbsf h264_mp4toannexb -f hls -hls_flags delete_segments -segment_list_size 10 -hls_list_size 2 /www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path . '/' . CameraEnum::M3U8_FILE_NAME . ' >/dev/null &';
+
+            shell_exec($exec);
+
+            //sleep10秒生成文件
+            $i = 1;
+            while ($i <= 10) {
+                //判断m3u8文件是否存在
+                $file_exists = file_exists(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path . '/' . CameraEnum::M3U8_FILE_NAME);
+
+                //判断ts文件是否存在
+                $ts_exists = glob(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . CameraEnum::TS_FILE_SUFFIX);
+
+                if ($file_exists && $ts_exists) {
+
+                    //获取第一帧作为封面图
+                    $file_name = time() . CameraEnum::COVER_PICTURE_NAME;
+
+                    $cover_exec = 'ffmpeg -i ' . public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . CameraEnum::M3U8_FILE_NAME . ' -vf "select=between(mod(n\, 25)\, 0\, 0), setpts=N/24/TB" ' . public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . $file_name;
+                    exec($cover_exec);
+
+                    //判断图片是否存在
+                    $picture_exists = file_exists(public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/' . $file_name);
+
+                    if ($picture_exists) {
+                        $cms = new CameraServices();
+
+                        //上传图片到oss并更新数据库
+                        $picture_path = $cms->uploadOssFile($file_name, public_path() . '/' . CameraEnum::M3U8_FILE_PATH . $path  . '/');
+
+                        CameraList::where('id', $camera_id)->update(['cover_picture' => $picture_path]);
+                    }
+
+                    return $result;
+                } else {
+                    sleep(1);
+                }
+
+                $i++;
+            }
+        }else{
+            return $result;
+        }
+
+        //标记摄像头为异常状态
+        CameraList::where('id', $camera_id)->update(['camera_status' => CameraEnum::CAMERA_STATUS_ERROR]);
+
+        $result['status'] = false;
+        $result['msg']    = ApiEnum::EASY_DAWIN_REQUEST_FAIL;
+
+        return $result;
+    }
+
+    //根据摄像头id获取摄像头播放链接
     public static function getRtspTianDi($camera_id,$parent_id)
     {