TdwyController.php 19 KB

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