TdwyController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 3/17/22
  6. * Time: 7:01 PM
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Api\BaseController;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Input;
  14. use Modules\Camera\Entities\CameraList;
  15. use Modules\Camera\Enum\CameraEnum;
  16. use Modules\Mine\Entities\MineList;
  17. use Modules\Mine\Enum\MineEnum;
  18. use Modules\Mine\Services\MineServices;
  19. class TdwyController extends BaseController
  20. {
  21. protected $pre_url; // 请求地址
  22. protected $username; // 请求username
  23. protected $password; // 请求password
  24. protected $sysId; // 请求sysId
  25. protected $token; // 请求sysId
  26. public function __construct()
  27. {
  28. $this->pre_url = Input::get('url');
  29. $this->username = Input::get('username');
  30. $this->password = Input::get('password');
  31. $this->sysId = Input::get('sysId');
  32. //先获取token
  33. $this->token = $this->loginUser();
  34. }
  35. //登录获取token
  36. protected function loginUser()
  37. {
  38. $url = '/pangu/sdkServer/user/loginUser';
  39. $params = [
  40. 'username' => $this->username,
  41. 'password' => $this->password,
  42. 'sysId' => $this->sysId,
  43. ];
  44. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  45. $result = json_decode($result, true);
  46. $token = '';
  47. if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) {
  48. $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE);
  49. return $token;
  50. }
  51. if(isset($result['statusCode'])){
  52. if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) {
  53. $token = $result['content']['token'];
  54. Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440);
  55. }
  56. }
  57. return $token;
  58. }
  59. //查询设备列表
  60. public function queryDeviceList()
  61. {
  62. $url = '/pangu/sdkServer/device/queryDeviceList';
  63. $sOrgId = Input::get('sOrgId', '');
  64. $iDeviceTypes = Input::get('iDeviceTypes', [5]);
  65. $rootCodes = Input::get('rootCodes', ["BH-0001"]);
  66. $isOrgTree = Input::get('isOrgTree', true);
  67. $orgTypeIds = Input::get('orgTypeIds', []);
  68. $needPage = Input::get('needPage', true);
  69. $pageSize = Input::get('pageSize', 50);
  70. $currentPage = Input::get('currentPage', 1);
  71. $params = [
  72. 'sOrgId' => $sOrgId,
  73. 'iDeviceTypes' => $iDeviceTypes,
  74. 'rootCodes' => $rootCodes,
  75. 'isOrgTree' => $isOrgTree,
  76. 'orgTypeIds' => $orgTypeIds,
  77. 'needPage' => $needPage,
  78. 'pageSize' => $pageSize,
  79. 'currentPage' => $currentPage,
  80. ];
  81. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  82. $result = json_decode($result, true);
  83. return $result;
  84. }
  85. //查询设备rtsp流
  86. public function getRtspById()
  87. {
  88. $url = '/pangu/sdkServer/videoStreaming/getRtspById';
  89. $sId = Input::get('sId', []);
  90. $ip = Input::get('ip', '');
  91. $port = Input::get('port', '');
  92. $type = Input::get('type', 0);
  93. $params = [
  94. 'sId' => $sId,
  95. 'ip' => $ip,
  96. 'port' => $port,
  97. 'type' => $type
  98. ];
  99. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  100. $result = json_decode($result, true);
  101. return $result;
  102. }
  103. //查询区域列表
  104. public function getRegionByOrgId(){
  105. $url = '/pangu/sdkServer/OrgManager/queryOrgList';
  106. $parentOrgIds = Input::get('parentOrgIds', []);
  107. $params = [
  108. 'querySingleType' => true,
  109. 'needPage' => false,
  110. 'currentPage' => 1,
  111. 'pageSize' => 10,
  112. 'parentOrgIds' => $parentOrgIds,
  113. 'queryType' => 0,
  114. ];
  115. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  116. $result = json_decode($result, true);
  117. return $result;
  118. }
  119. //天地伟业获取区域列表
  120. public function saveTrees($regions, $parent_id)
  121. {
  122. $arr = [];
  123. if (empty($regions['content'])) {
  124. return [];
  125. }
  126. $mineService = new MineServices();
  127. $mineService->initMineList();
  128. $arr_count = $regions['count'];
  129. foreach ($regions['content'][0]['data'] as $key => $value) {
  130. $arr[$key]['parentOrgId'] = $value['parentOrgId'];
  131. $arr[$key]['orgId'] = $value['orgId'];
  132. $arr[$key]['name'] = str_replace('#', '号', $value['orgName']);
  133. $id = MineList::where('index_code', $value['orgId'])->value('id');
  134. $params = [
  135. 'id' => $id,
  136. 'parent_id' => $parent_id,
  137. 'title' => $value['orgName'],
  138. 'sort' => $arr_count - $key,
  139. 'index_code' => $value['orgId'],
  140. ];
  141. $result = $mineService->add($params);
  142. $mine_id = $result->id;
  143. Input::replace(
  144. [
  145. 'parentOrgIds' => $value['orgId'],
  146. 'url' => Input::get('url', ''),
  147. 'username' => Input::get('username', ''),
  148. 'password' => Input::get('password', ''),
  149. 'sysId' => 'PG',
  150. ]
  151. );
  152. $region_child = $this->getRegionByOrgId();
  153. $arr[$key]['children'] = self::saveTrees($region_child, $mine_id);
  154. if (count($arr[$key]['children']) == 0) {
  155. unset($arr[$key]['children']);
  156. }
  157. }
  158. return array_values($arr);
  159. }
  160. /**
  161. * 发送请求
  162. * @param string $url
  163. * @param string $postData
  164. * @return bool|string
  165. */
  166. public function curlPost($url = '', $postData = '')
  167. {
  168. if (is_array($postData)) {
  169. $postData = http_build_query($postData);
  170. }
  171. $ch = curl_init();
  172. curl_setopt($ch, CURLOPT_URL, $url);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  174. curl_setopt($ch, CURLOPT_POST, 1);
  175. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  176. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  177. curl_setopt_array($ch, array(
  178. CURLOPT_HTTPHEADER => array(
  179. "Accept:" . '*/*',
  180. "Content-Type:" . 'application/json',
  181. "token:" . $this->token,
  182. )
  183. ));
  184. //https请求 不验证证书和host
  185. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  186. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  187. $data = curl_exec($ch);
  188. curl_close($ch);
  189. return $data;
  190. }
  191. //同步大华视频服务器摄像头
  192. public function dahuaCamera(){
  193. //大华需要同步的区域
  194. $dahua_ext = DB::table('mine_list_ext')
  195. ->where('is_hak',MineEnum::IS_HAK_DH)
  196. ->where('deleted_at',null)->get();
  197. if(count($dahua_ext) > 0){
  198. for($i=0;$i<count($dahua_ext);$i++){
  199. $ip = $dahua_ext[$i]->ip.':'.$dahua_ext[$i]->port;
  200. //获取public_key
  201. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  202. $result = $this->httpRequest($ip.$url);
  203. if($result['data']['publicKey']){
  204. $public_key = $result['data']['publicKey'];
  205. }else{
  206. return;
  207. }
  208. //获取access_token
  209. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  210. $username_password = explode('|',$dahua_ext[$i]->sOrgId);
  211. if(count($username_password) != 2){
  212. return;
  213. }
  214. $password = $this->rsaEncode($username_password[1],$public_key);
  215. $params = [
  216. 'grant_type'=>'password',
  217. 'username'=>$username_password[0],
  218. 'password'=>$password,
  219. 'client_id'=>$dahua_ext[$i]->key,
  220. 'client_secret'=>$dahua_ext[$i]->secret,
  221. 'public_key'=>$public_key
  222. ];
  223. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  224. if(isset($result2['data']['access_token'])){
  225. $access_token = $result2['data']['access_token'];
  226. }else{
  227. return;
  228. }
  229. //获取摄像头列表
  230. $url3 = '/evo-apigw/evo-brm/1.2.0/device/channel/subsystem/page';
  231. $params2 = [
  232. 'pageNum'=>1,
  233. 'pageSize'=>1000,
  234. 'sortType'=>'ASC',
  235. 'sort'=>'channelSn',
  236. ];
  237. $result3 = $this->httpRequest($ip.$url3,'post',$params2,$access_token);
  238. // DB::table('camera_list')->where('mine_id',$dahua_ext[$i]->mine_id)->delete();
  239. if($result3['data']['pageData']){
  240. $camera_list = $result3['data']['pageData'];
  241. if(count($camera_list) > 0){
  242. for($j=0;$j<count($camera_list);$j++){
  243. $param['mine_id'] = $dahua_ext[$i]->mine_id;
  244. $param['revert_id'] = 'NullId';
  245. $param['sort'] = 1;
  246. $param['camera_name'] = $camera_list[$j]['channelName'];
  247. $param['camera_source'] = 2;
  248. $param['index_code'] = $camera_list[$j]['channelCode'];
  249. $param['video_recorder'] = 2;
  250. // $param['created_at'] = date('Y-m-d H:i:s');
  251. // $param['updated_at'] = date('Y-m-d H:i:s');
  252. // DB::table('camera_list')->insert($param);
  253. CameraList::updateOrCreate(['index_code' => $camera_list[$j]['channelCode']], $param);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //大华rtsp
  261. public function dahuaRtsp($camera_id,$parent_id){
  262. $dahua_ext = DB::table('mine_list_ext')
  263. ->where('mine_id',$parent_id)
  264. ->where('deleted_at',null)->get();
  265. if(count($dahua_ext) > 0){
  266. $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port;
  267. //获取public_key
  268. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  269. $result1 = $this->httpRequest($ip.$url);
  270. if($result1['data']['publicKey']){
  271. $public_key = $result1['data']['publicKey'];
  272. }else{
  273. return;
  274. }
  275. //获取access_token
  276. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  277. $username_password = explode('|',$dahua_ext[0]->sOrgId);
  278. if(count($username_password) != 2){
  279. return;
  280. }
  281. $password = $this->rsaEncode($username_password[1],$public_key);
  282. $params = [
  283. 'grant_type'=>'password',
  284. 'username'=>$username_password[0],
  285. 'password'=>$password,
  286. 'client_id'=>$dahua_ext[0]->key,
  287. 'client_secret'=>$dahua_ext[0]->secret,
  288. 'public_key'=>$public_key
  289. ];
  290. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  291. if($result2['data']['access_token']){
  292. $access_token = $result2['data']['access_token'];
  293. }else{
  294. return;
  295. }
  296. $camera = DB::table('camera_list')->where('id',$camera_id)->get();
  297. $url4 = '/evo-apigw/admin/API/MTS/Video/StartVideo';
  298. $params3['data'] = [
  299. 'channelId'=>$camera[0]->index_code,
  300. 'dataType'=>'1',
  301. 'streamType'=>'1'
  302. ];
  303. $result4 = $this->httpRequest($ip.$url4,'post',$params3,$access_token);
  304. if($result4['data']['url'] && $result4['data']['token']){
  305. $rtsp = explode('|',$result4['data']['url']);
  306. if($rtsp[1]){
  307. $result['data'] = [
  308. 'camera_id' => $camera_id,
  309. 'url' => $rtsp[1].'?token='.$result4['data']['token']
  310. ];
  311. return $result;
  312. }
  313. }
  314. }
  315. }
  316. //大华停止语音对讲
  317. public function talkStop($camera_id,$parent_id,$device_code,$session){
  318. $dahua_ext = DB::table('mine_list_ext')
  319. ->where('mine_id',$parent_id)
  320. ->where('deleted_at',null)->get();
  321. if(count($dahua_ext) > 0){
  322. $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port;
  323. //获取public_key
  324. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  325. $result1 = $this->httpRequest($ip.$url);
  326. if($result1['data']['publicKey']){
  327. $public_key = $result1['data']['publicKey'];
  328. }else{
  329. return;
  330. }
  331. //获取access_token
  332. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  333. $username_password = explode('|',$dahua_ext[0]->sOrgId);
  334. if(count($username_password) != 2){
  335. return;
  336. }
  337. $password = $this->rsaEncode($username_password[1],$public_key);
  338. $params = [
  339. 'grant_type'=>'password',
  340. 'username'=>$username_password[0],
  341. 'password'=>$password,
  342. 'client_id'=>$dahua_ext[0]->key,
  343. 'client_secret'=>$dahua_ext[0]->secret,
  344. 'public_key'=>$public_key
  345. ];
  346. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  347. if($result2['data']['access_token']){
  348. $access_token = $result2['data']['access_token'];
  349. }else{
  350. return;
  351. }
  352. //语音对讲
  353. $url5 = '/evo-apigw/admin/API/MTS/Audio/StopTalk';
  354. $params4['data'] = [
  355. "deviceCode"=>$device_code,
  356. "talkType"=>"1",
  357. "session"=>$session,
  358. "channelSeq"=>""
  359. ];
  360. $result = $this->httpRequest($ip.$url5,'post',$params4,$access_token);
  361. return $result;
  362. }
  363. }
  364. //大华语音对讲
  365. public function talkUrl($camera_id,$parent_id){
  366. $dahua_ext = DB::table('mine_list_ext')
  367. ->where('mine_id',$parent_id)
  368. ->where('deleted_at',null)->get();
  369. if(count($dahua_ext) > 0){
  370. $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port;
  371. //获取public_key
  372. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  373. $result1 = $this->httpRequest($ip.$url);
  374. if($result1['data']['publicKey']){
  375. $public_key = $result1['data']['publicKey'];
  376. }else{
  377. return;
  378. }
  379. //获取access_token
  380. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  381. $username_password = explode('|',$dahua_ext[0]->sOrgId);
  382. if(count($username_password) != 2){
  383. return;
  384. }
  385. $password = $this->rsaEncode($username_password[1],$public_key);
  386. $params = [
  387. 'grant_type'=>'password',
  388. 'username'=>$username_password[0],
  389. 'password'=>$password,
  390. 'client_id'=>$dahua_ext[0]->key,
  391. 'client_secret'=>$dahua_ext[0]->secret,
  392. 'public_key'=>$public_key
  393. ];
  394. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  395. if($result2['data']['access_token']){
  396. $access_token = $result2['data']['access_token'];
  397. }else{
  398. return;
  399. }
  400. $camera = DB::table('camera_list')->where('id',$camera_id)->get();
  401. $result['data']['camera_id'] = $camera_id;
  402. $result['data']['deviceCode'] = explode('$',$camera[0]->index_code)[0];
  403. //语音对讲
  404. $url5 = '/evo-apigw/admin/API/MTS/Audio/StartTalk';
  405. $params4['data'] = [
  406. "deviceCode"=>explode('$',$camera[0]->index_code)[0],
  407. "talkType"=>"1",
  408. "audioBit"=>"16",
  409. "audioType"=>"2",
  410. "broadcastChannels"=>"",
  411. "sampleRate"=>"8000",
  412. "talkmode"=>"",
  413. "channelSeq"=>"0"
  414. ];
  415. $result5 = $this->httpRequest($ip.$url5,'post',$params4,$access_token);
  416. if(isset($result5['data']['url'])){
  417. $result['data']['url'] = explode('|',$result5['data']['url'])[1].'?token='.$result5['data']['token'];
  418. $result['data']['session'] = $result5['data']['session'];
  419. $result['data']['id'] = $camera[0]->index_code;
  420. }else{
  421. $result['msg'] = $result5['desc'];
  422. }
  423. return $result;
  424. }
  425. }
  426. //接口第三方调用
  427. public function httpRequest($url, $format = 'get', $data = null, $token = null){
  428. //设置头信息
  429. $headerArray =array("Content-type:application/json;","Accept:application/json");
  430. if ($token) {
  431. $headerArray[] = "Authorization:bearer " . $token;
  432. }
  433. $curl=curl_init();
  434. curl_setopt($curl, CURLOPT_URL, $url);
  435. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  436. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  437. if ($format == 'post') {
  438. //post传值设置post传参
  439. curl_setopt($curl, CURLOPT_POST, 1);
  440. if ($data) {
  441. $data = json_encode($data);
  442. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  443. }
  444. }
  445. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  446. curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
  447. $data=json_decode(curl_exec($curl), true);
  448. curl_close($curl);
  449. //返回接口返回数据
  450. return $data;
  451. }
  452. public function rsaEncode($password,$rsa_public_key)
  453. {
  454. // 要执行的代码
  455. $rsa_public = "-----BEGIN PUBLIC KEY-----\n";
  456. $rsa_public = $rsa_public.$rsa_public_key;
  457. $rsa_public = $rsa_public."\n-----END PUBLIC KEY-----";
  458. $key = openssl_pkey_get_public($rsa_public);
  459. if (!$key) {
  460. echo "公钥不可用\n";
  461. echo $rsa_public;
  462. }
  463. //openssl_public_encrypt 第一个参数只能是string
  464. //openssl_public_encrypt 第二个参数是处理后的数据
  465. //openssl_public_encrypt 第三个参数是openssl_pkey_get_public返回的资源类型
  466. $return_en = openssl_public_encrypt($password, $crypted, $key);
  467. if (!$return_en) {
  468. echo "加密失败,请检查RSA秘钥";
  469. }
  470. return base64_encode($crypted);
  471. }
  472. }