HaiKangController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 2021-04-28
  6. * Time: 20:59
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use App\Enum\ApiEnum;
  10. use App\Http\Controllers\Api\BaseController;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Input;
  13. use Illuminate\Support\Facades\Log;
  14. use Modules\Camera\Entities\CameraList;
  15. use Modules\Camera\Enum\CameraEnum;
  16. use Modules\Mine\Entities\MineList;
  17. use Modules\Mine\Services\MineServices;
  18. class HaiKangController extends BaseController
  19. {
  20. protected $pre_url; // 海康接口地址
  21. protected $app_key; // 海康app_key
  22. protected $app_secret; // 海康app_secret
  23. protected $sign;// 签名
  24. protected $time;// 时间戳
  25. protected $artemis;// OpenAPI接口的上下文
  26. public function __construct()
  27. {
  28. // if ($request->has('mine')) {
  29. // $this->pre_url = config('haikang.' . $request->input('mine') . '.pre_url'); // https://120.253.79.51:4433
  30. // $this->app_key = config('haikang.' . $request->input('mine') . '.app_key'); // 25720460
  31. // $this->app_secret = config('haikang.' . $request->input('mine') . '.app_secret'); // qqP7NLcIDwO9MgtYmp8L
  32. // $this->artemis = config('haikang.' . $request->input('mine') . '.artemis'); // /artemis
  33. // } else {
  34. // $this->pre_url = env('HAI_KANG_URL'); // https://120.253.79.51:4433
  35. // $this->app_key = env('HAI_KANG_APP_KEY'); // 25720460
  36. // $this->app_secret = env('HAI_KANG_APP_SECRET'); // qqP7NLcIDwO9MgtYmp8L
  37. // $this->artemis = env('HAI_KANG_ARTEMIS_PATH'); // /artemis
  38. // }
  39. // $this->pre_url = 'https://10.71.252.64:4433'; // https://120.253.79.51:4433
  40. // $this->app_key = '25720460'; // 25720460
  41. // $this->app_secret = 'qqP7NLcIDwO9MgtYmp8L'; // qqP7NLcIDwO9MgtYmp8L
  42. // $this->artemis = '/artemis'; // /artemis
  43. $this->pre_url = Input::get('url'); // https://120.253.79.51:4433
  44. $this->app_key = Input::get('key'); // 25720460
  45. $this->app_secret = Input::get('secret'); // qqP7NLcIDwO9MgtYmp8L
  46. $this->artemis = '/artemis'; // /artemis
  47. list($msec, $sec) = explode(' ', microtime());
  48. $this->time = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  49. }
  50. /**
  51. * 获取区域列表
  52. * @return bool|mixed|string
  53. */
  54. public function getRegionsList()
  55. {
  56. $url = $this->artemis . '/api/resource/v1/regions';
  57. //请求参数
  58. $params = [];
  59. // $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  60. // $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  61. $params['pageNo'] = 1;
  62. $params['pageSize'] = 1000;
  63. $this->sign = $this->get_sign($url);
  64. $type = Input::get('type');
  65. $mine_id = Input::get('mine_id');
  66. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  67. $result = json_decode($result, true);
  68. $area_list = [];
  69. if (isset($result['code']) && $result['code'] == 0) {
  70. $list = $result['data']['list'];
  71. foreach ($list as $key => $item) {
  72. $area_list[] = $item['indexCode'];
  73. if ($item['parentIndexCode'] == '-1') {
  74. unset($list[$key]);
  75. }
  76. }
  77. $result['data']['list'] = array_values($list);
  78. }
  79. if ($type == CameraEnum::REQUEST_TYPE_API) {
  80. $result['data']['list'] = $this->regionsTree($result['data']['list']);
  81. } else {
  82. //先删除不存在的区域
  83. if ($area_list) {
  84. MineList::where('degree', 'like', $mine_id . '|%')->whereNotIn('index_code', $area_list)->delete();
  85. }
  86. //更新区域
  87. $result['data']['list'] = $this->saveTree($result['data']['list'], $mine_id);
  88. }
  89. return $result;
  90. }
  91. protected function regionsTree($regions, $pid = 'root000000')
  92. {
  93. $arr = [];
  94. if (empty($regions)) {
  95. return [];
  96. }
  97. foreach ($regions as $key => $value) {
  98. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  99. $arr[$key]['indexCode'] = $value['indexCode'];
  100. $arr[$key]['mine_id'] = $value['indexCode'];
  101. $arr[$key]['name'] = $value['name'];
  102. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  103. $arr[$key]['treeCode'] = $value['treeCode'];
  104. $arr[$key]['children'] = array_values(self::regionsTree($regions, $value['indexCode']));
  105. unset($arr[$key]['indexCode']);
  106. unset($arr[$key]['parentIndexCode']);
  107. unset($arr[$key]['treeCode']);
  108. if (count($arr[$key]['children']) == 0) {
  109. unset($arr[$key]['children']);
  110. }
  111. }
  112. }
  113. return array_values($arr);
  114. }
  115. protected function saveTree($regions, $parent_id, $pid = 'root000000')
  116. {
  117. $arr = [];
  118. if (empty($regions)) {
  119. return [];
  120. }
  121. $mineService = new MineServices();
  122. $mineService->initMineList();
  123. $arr_count = count($regions);
  124. foreach ($regions as $key => $value) {
  125. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  126. $arr[$key]['indexCode'] = $value['indexCode'];
  127. $arr[$key]['mine_id'] = $value['indexCode'];
  128. $arr[$key]['name'] = str_replace('#', '号', $value['name']);
  129. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  130. $arr[$key]['treeCode'] = $value['treeCode'];
  131. $id = MineList::where('index_code', $value['indexCode'])->value('id');
  132. $params = [
  133. 'id' => $id,
  134. 'parent_id' => $parent_id,
  135. 'title' => $value['name'],
  136. 'sort' => $arr_count - $key,
  137. 'index_code' => $value['indexCode'],
  138. ];
  139. $result = $mineService->add($params);
  140. $mine_id = $result->id;
  141. $arr[$key]['children'] = array_values(self::saveTree($regions, $mine_id, $value['indexCode']));
  142. if (count($arr[$key]['children']) == 0) {
  143. unset($arr[$key]['children']);
  144. }
  145. }
  146. }
  147. return array_values($arr);
  148. }
  149. /**
  150. * 获取区域监控列表
  151. * @return bool|mixed|string
  152. */
  153. public function getCamerasList()
  154. {
  155. if (!Input::has('indexCode')) {
  156. return $this->error(1, '缺少必要参数');
  157. }
  158. $url = $this->artemis . '/api/resource/v1/regions/regionIndexCode/cameras';
  159. //请求参数
  160. $params = [];
  161. $params['regionIndexCode'] = Input::get('indexCode');
  162. // $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  163. // $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  164. $params['pageNo'] = 1;
  165. $params['pageSize'] = 1000;
  166. $this->sign = $this->get_sign($url);
  167. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  168. $result = json_decode($result, true);
  169. return $result;
  170. }
  171. /**
  172. * 获取摄像头码流url
  173. * @return array|mixed
  174. */
  175. public function getCamerasUrl()
  176. {
  177. if (!Input::has('cameraIndexCode')) {
  178. return $this->error(1, '缺少必要参数');
  179. }
  180. $protocol = Input::get('protocol', 'hls');
  181. $streamType = Input::get('streamType', 1);
  182. $url = $this->artemis . '/api/video/v1/cameras/previewURLs';
  183. // $cameras_info = json_decode($this->getCamerasInfo(Input::all()), true);
  184. //请求参数
  185. $params = [];
  186. // $params['regionIndexCode'] = $request->input('regionIndexCode');
  187. $params['cameraIndexCode'] = Input::get('cameraIndexCode');
  188. $params['streamType'] = $streamType; //0主码流 1子码流 2第三码流
  189. $params['protocol'] = $protocol;
  190. $params['transmode'] = 1; //0:UDP 1:TCP
  191. $params['expand'] = 'transcode=1&systemformat=ps&videotype=h264';
  192. $this->sign = $this->get_sign($url);
  193. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  194. $result = json_decode($result, true);
  195. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  196. $video_url = $result['data']['url'];
  197. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  198. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  199. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  200. }
  201. return $result;
  202. }
  203. /**
  204. * 获取摄像头详情
  205. * @param array $data
  206. * @return array|bool|string
  207. */
  208. public function getCamerasInfo($data)
  209. {
  210. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  211. //请求参数
  212. $params = [];
  213. $params['cameraIndexCode'] = $data['cameraIndexCode'];
  214. $this->sign = $this->get_sign($url);
  215. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  216. return $result;
  217. }
  218. //获取编码设备信息
  219. public function getTranscodeInfo()
  220. {
  221. $url = $this->artemis . '/api/resource/v1/encodeDevice/get';
  222. //请求参数
  223. $params = [];
  224. $params['pageNo'] = 1;
  225. $params['pageSize'] = 1;
  226. $this->sign = $this->get_sign($url);
  227. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  228. return $result;
  229. }
  230. //根据区域获取下级编码设备列表
  231. public function getTest()
  232. {
  233. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  234. //请求参数
  235. $params = [];
  236. $params['pageNo'] = 1;
  237. $params['pageSize'] = 1000;
  238. $params['regionIndexCode'] = Input::get('indexCode');
  239. $this->sign = $this->get_sign($url);
  240. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  241. $result = json_decode($result, true);
  242. return $result;
  243. }
  244. //获取编码设备列表
  245. public function getTranscodeList()
  246. {
  247. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  248. //请求参数
  249. $params['regionIndexCode'] = 'root000000';
  250. $params['pageNo'] = 1;
  251. $params['pageSize'] = 100;
  252. $this->sign = $this->get_sign($url);
  253. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  254. $result = json_decode($result, true);
  255. return $result;
  256. }
  257. public function getRegionsInfo()
  258. {
  259. $url = $this->artemis . '/api/resource/v1/region/regionCatalog/regionInfo';
  260. //请求参数
  261. $params = [];
  262. $indexCodes = Input::get('indexCodes');
  263. $params['indexCodes'] = $indexCodes;
  264. $this->sign = $this->get_sign($url);
  265. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  266. $result = json_decode($result, true);
  267. // if (isset($result['code']) && $result['code'] == 0) {
  268. // }
  269. return $result;
  270. }
  271. //海康easy_drawin获取rtsp并推流
  272. public function getRtspByCameraId(){
  273. $result['status'] = true;
  274. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  275. $query = CameraList::where('id',Input::get('camera_id'))->first();
  276. if (!$query) {
  277. $result['status'] = false;
  278. $result['msg'] = ApiEnum::NO_CAMERA_URL;
  279. return $result;
  280. }
  281. $degree = MineList::where('id', $query->mine_id)->value('degree');
  282. $mine_list = MineList::whereIn('id', explode('|', $degree))->select(['title'])->get()->toArray();
  283. $path = '';
  284. foreach ($mine_list as $key => $val) {
  285. $path .= '/' . $val['title'];
  286. }
  287. $new_camera_name = str_replace('-','',$query->camera_name);
  288. $new_camera_name = str_replace('#','号',$new_camera_name);
  289. $path .= '/' . $new_camera_name;
  290. //标记摄像头为在线状态
  291. CameraList::where('id', Input::get('camera_id'))->update(['camera_status' => CameraEnum::CAMERA_STATUS_ONLINE]);
  292. //已有拉流id,直接返回拉流链接
  293. if($query->revert_id != 'NullId'){
  294. $result['data'] = [
  295. 'camera_id' => Input::get('camera_id'),
  296. 'url' => env('EASY_DARWIN_JF_RTSP') . $path
  297. ];
  298. return $result;
  299. }
  300. $url = $this->artemis.'/api/video/v1/cameras/previewURLs';
  301. $params['cameraIndexCode'] = $query->index_code;
  302. $params['streamType'] = 1; //0主码流 1子码流 2第三码流
  303. $params['protocol'] = 'rtsp';
  304. $params['transmode'] = 1; //0:UDP 1:TCP
  305. $params['expand'] = 'streamform=rtp';
  306. $this->sign = $this->get_sign($url);
  307. $rtsp = $this->curlPost($this->pre_url . $url, json_encode($params));
  308. $rtsp = json_decode($rtsp, true);
  309. $data = [
  310. 'url' => $rtsp['data']['url'],
  311. 'customPath' => $path,
  312. ];
  313. // dd($data);
  314. Log::info('请求流媒体服务器参数------------');
  315. Log::info($data);
  316. $curl = env('EASY_DARWIN_JF_URL') . CameraEnum::API_STREAM_START . '?' . http_build_query($data);
  317. //请求流媒体服务器拉流
  318. $curl_res = curl_request($curl);
  319. // DB::table('camera_list')->where('id',$camera_id)->update($res);
  320. // dd($res);
  321. Log::info('请求流媒体服务器result------------');
  322. Log::info($curl_res);
  323. if (strpos($curl_res,' ')) {
  324. //切换码流重新获取rtsp
  325. $params['streamType'] = 0; //0主码流 1子码流 2第三码流
  326. $rtsp = $this->curlPost($this->pre_url . $url, json_encode($params));
  327. $rtsp = json_decode($rtsp, true);
  328. $data = [
  329. 'url' => $rtsp['data']['url'],
  330. 'customPath' => $path,
  331. ];
  332. // dd($data);
  333. Log::info('请求流媒体服务器参数------------');
  334. Log::info($data);
  335. $curl = env('EASY_DARWIN_JF_URL') . CameraEnum::API_STREAM_START . '?' . http_build_query($data);
  336. //请求流媒体服务器拉流
  337. $curl_res = curl_request($curl);
  338. if(strpos($curl_res,' ')){
  339. //标记摄像头为异常状态
  340. CameraList::where('id', Input::get('camera_id'))->update(['camera_status' => CameraEnum::CAMERA_STATUS_ERROR]);
  341. $result['status'] = false;
  342. $result['msg'] = ApiEnum::EASY_DAWIN_REQUEST_FAIL;
  343. return $result;
  344. }
  345. }
  346. CameraList::where('id', Input::get('camera_id'))->update(['revert_id' => $curl_res]);
  347. $result['data'] = [
  348. 'camera_id' => Input::get('camera_id'),
  349. 'url' => env('EASY_DARWIN_JF_RTSP') . $path
  350. ];
  351. return $result;
  352. }
  353. /**
  354. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  355. */
  356. protected function get_sign($url)
  357. {
  358. $sign_str = $this->get_sign_str($url); //签名字符串
  359. $priKey = $this->app_secret;
  360. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  361. $result = base64_encode($sign);
  362. return $result;
  363. }
  364. /**
  365. * 生成签名
  366. * @param $url
  367. * @return string
  368. */
  369. protected function get_sign_str($url)
  370. {
  371. // $next = "\n";
  372. $next = "\n";
  373. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  374. $str .= "x-ca-key:" . $this->app_key . $next;
  375. $str .= "x-ca-timestamp:" . $this->time . $next;
  376. $str .= $url;
  377. return $str;
  378. }
  379. /**
  380. * 发送请求
  381. * @param string $url
  382. * @param string $postData
  383. * @param array $options
  384. * @return bool|string
  385. */
  386. public function curlPost($url = '', $postData = '', $options = array())
  387. {
  388. if (is_array($postData)) {
  389. $postData = http_build_query($postData);
  390. }
  391. $ch = curl_init();
  392. curl_setopt($ch, CURLOPT_URL, $url);
  393. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  394. curl_setopt($ch, CURLOPT_POST, 1);
  395. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  396. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  397. curl_setopt_array($ch, array(
  398. CURLOPT_HTTPHEADER => array(
  399. "Accept:" . '*/*',
  400. "Content-Type:" . 'application/json',
  401. "x-Ca-Key:" . $this->app_key,
  402. "X-Ca-Signature:" . $this->sign,
  403. "X-Ca-Timestamp:" . $this->time,
  404. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  405. )
  406. ));
  407. //https请求 不验证证书和host
  408. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  409. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  410. $data = curl_exec($ch);
  411. curl_close($ch);
  412. return $data;
  413. }
  414. }