UpdateHkList.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Input;
  5. use Modules\Camera\Entities\CameraList;
  6. use Modules\Camera\Enum\CameraEnum;
  7. use Modules\Camera\Services\CameraServices;
  8. use Modules\Camera\Http\Controllers\Api\TdwyController;
  9. use Modules\Mine\Entities\MineList;
  10. use Modules\Mine\Entities\MineListExt;
  11. use Modules\Mine\Enum\MineEnum;
  12. use Modules\Mine\Services\MineServices;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class UpdateHkList extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'hklist:update';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '同步海康区域及摄像头列表';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. $start_time = time();
  46. echo '开始时间:'.date('Y-m-d H:i:s');
  47. $this->updateAreaList();
  48. $this->updateCameraList();
  49. echo '时长:'.number_format(((time()-$start_time)/60),2).'分钟';
  50. }
  51. //同步区域列表
  52. public function updateAreaList()
  53. {
  54. //查询使用海康服务器的矿区id
  55. $mine_id_list = MineListExt::where('is_hak', 1)->pluck('mine_id')->all();
  56. //海康区域同步开始时间
  57. // $syn['hk_area_start_time'] = date('Y-m-d H:i:s');
  58. foreach ($mine_id_list as $key => $val) {
  59. // if($val == 968){
  60. // //宁煤区域同步开始时间
  61. // $syn['nm_area_start_time'] = date('Y-m-d H:i:s');
  62. //
  63. // MineServices::getHaiKangArea($val, CameraEnum::REQUEST_TYPE_LOCAL);
  64. //
  65. // //宁煤区域同步结束时间
  66. // $syn['nm_area_end_time'] = date('Y-m-d H:i:s');
  67. // }else{
  68. MineServices::getHaiKangArea($val, CameraEnum::REQUEST_TYPE_LOCAL);
  69. // }
  70. }
  71. //海康区域同步结束时间
  72. // $syn['hk_area_end_time'] = date('Y-m-d H:i:s');
  73. //天地区域同步开始时间
  74. // $syn['td_area_start_time'] = date('Y-m-d H:i:s');
  75. //同步天地伟业矿区区域
  76. $mine_list_ext = DB::table('mine_list_ext')
  77. ->select('mine_list_ext.*','mine_list.parent_id')
  78. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  79. // ->where('mine_list.parent_id',0)
  80. ->where('mine_list.parent_id',123456)
  81. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  82. ->where('mine_list_ext.ip','!=','')
  83. ->where('mine_list_ext.port','!=','')
  84. ->where('mine_list_ext.key','!=','')
  85. ->where('mine_list_ext.secret','!=','')
  86. ->where('mine_list_ext.deleted_at',null)->get();
  87. if(sizeof($mine_list_ext)>0){
  88. for($i=0;$i<sizeof($mine_list_ext);$i++){//循环同步所有天地伟业矿区
  89. Input::replace(
  90. [
  91. 'parentOrgIds' => $mine_list_ext[$i]->sOrgId,
  92. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  93. 'username' => $mine_list_ext[$i]->key,
  94. 'password' => $mine_list_ext[$i]->secret,
  95. 'sysId' => 'PG',
  96. ]
  97. );
  98. $tdwy = new TdwyController();
  99. $region = $tdwy->getRegionByOrgId();
  100. $mine_id = $mine_list_ext[$i]->mine_id;
  101. //更新区域
  102. $result['data']['list'] = $tdwy->saveTrees($region, $mine_id);
  103. }
  104. }
  105. //天地区域同步结束时间
  106. // $syn['td_area_end_time'] = date('Y-m-d H:i:s');
  107. // DB::table('syn')->insert($syn);
  108. }
  109. public function updateCameraList()
  110. {
  111. //海康
  112. //查询存在index_code列表
  113. $index_code_list = MineList::where('index_code', '!=', null)->get();
  114. //不在线矿区
  115. $mine_list_ext = DB::table('mine_list_ext')->where('deleted_at',null)->where('ip','!=',null)->get();
  116. for($i=0;$i<count($mine_list_ext);$i++){
  117. $ip = explode('://',$mine_list_ext[$i]->ip)[1];
  118. $port = $mine_list_ext[$i]->port;
  119. if($fp = @fsockopen($ip,$port,$errno,$errstr,1)){
  120. fclose($fp);
  121. }else{
  122. $arr[] = $mine_list_ext[$i]->mine_id;
  123. }
  124. }
  125. //去掉不在线矿区
  126. foreach($index_code_list as $k=>$v){
  127. $degree = explode('|',$v->degree);
  128. if(isset($arr)){
  129. for($i=0;$i<count($arr);$i++){
  130. if($degree[0] == $arr[$i]){
  131. unset($index_code_list[$k]);
  132. }
  133. }
  134. }
  135. }
  136. //去掉洗选
  137. // foreach($index_code_list as $k=>$v){
  138. // $degree = explode('|',$v->degree);
  139. // if($degree[0] == 66){
  140. // unset($index_code_list[$k]);
  141. // }
  142. // }
  143. //天地伟业矿区
  144. $mine_ext = DB::table('mine_list_ext')
  145. ->select('mine_list_ext.*','mine_list.parent_id')
  146. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  147. // ->where('mine_list.parent_id',0)
  148. ->where('mine_list.parent_id',123456)
  149. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  150. ->where('mine_list_ext.ip','!=','')
  151. ->where('mine_list_ext.port','!=','')
  152. ->where('mine_list_ext.key','!=','')
  153. ->where('mine_list_ext.secret','!=','')
  154. ->where('mine_list_ext.deleted_at',null)->get();
  155. $mine_id_list = [];
  156. foreach($mine_ext as $k=>$v){
  157. $mine_id_list[$k] = $v->mine_id;
  158. }
  159. //清除天地伟业的区域
  160. if(count($mine_id_list) > 0){
  161. foreach($index_code_list as $k=>$v){
  162. if(in_array(explode('|',$v->degree)[0],$mine_id_list)){
  163. unset($index_code_list[$k]);
  164. }
  165. }
  166. }
  167. //海康摄像头同步开始时间
  168. // $syn_id = DB::table('syn')->orderBy('id','desc')->pluck('id')->first();
  169. // $syn1['hk_camera_start_time'] = date('Y-m-d H:i:s');
  170. // DB::table('syn')->where('id',$syn_id)->update($syn1);
  171. // $nm_camera_time = 0;
  172. foreach ($index_code_list as $key => $val) {
  173. //宁煤同步摄像头开始时间
  174. // $nm_start_time = time();
  175. // $timetotal['mine_id'] = $val->id;
  176. // $timetotal['interface_start_time'] = date('Y-m-d H:i:s');
  177. $degree = explode('|', $val->degree);
  178. // $trans_result = CameraServices::getHaiKangTransList($degree[0]);
  179. if($degree[0] == 6356){//洗选风险作业执法记录仪
  180. $degree[0] = $degree[1];
  181. }
  182. $result = CameraServices::getHaiKangCamera($degree[0], $val->index_code, CameraEnum::CAMERA_TYPE_ALL);
  183. // $info = DB::table('syn')->select('interface','handle')->orderBy('id','desc')->first();
  184. // $syn2['interface'] = $info->interface . $val->id . ':' . (time() - $nm_start_time) . ';';
  185. // $timetotal['interface_end_time'] = date('Y-m-d H:i:s');
  186. // $timetotal['handle_start_time'] = date('Y-m-d H:i:s');
  187. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  188. // $handle_start = time();
  189. if ($result['status']) {
  190. $trans_arr = [];
  191. // foreach ($trans_result['data'] as $tk => $tv) {
  192. // $trans_arr[$tv['indexCode']]['ip'] = $tv['ip'];
  193. // }
  194. $index_code_arr = [];
  195. // $arr_count = count($result['data']);
  196. foreach ($result['data'] as $k => $v) {
  197. $ip = '';
  198. $port = '';
  199. $com_number = '';
  200. // if (isset($trans_arr[$v['trans_code']])) {
  201. // $ip = $trans_arr[$v['trans_code']]['ip'];
  202. // $port = CameraEnum::HAK_DEFAULT_PORT;
  203. // $com_number = $v['com_number'];
  204. // }
  205. //当前摄像头index_code数组
  206. $index_code_arr[$k] = $v['camera_id'];
  207. //更新时间距当前时间天数
  208. // $update_time = strtotime($v['updateTime']);
  209. // $update_time = number_format((time() - $update_time)/(60*60*24),2);
  210. $params = [
  211. 'mine_id' => $val->id,
  212. 'camera_name' => $this->transformCameraName($v['camera_name']),
  213. 'index_code' => $v['camera_id'],
  214. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  215. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  216. 'ip' => $ip,
  217. 'port' => $port,
  218. 'com_number' => $com_number,
  219. //'sort' => $arr_count - $k,
  220. // 'camera_status' => $v['camera_status'],
  221. ];
  222. // if($result['is_update'] == true){
  223. CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  224. // }elseif($update_time < 10){
  225. // CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  226. // }
  227. }
  228. //删除不存在的摄像头
  229. CameraList::where('mine_id', $val->id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $index_code_arr)->delete();
  230. }
  231. // $syn2['handle'] = $info->handle . $val->id . ':' . (time() - $handle_start) . ';';
  232. // $timetotal['handle_end_time'] = date('Y-m-d H:i:s');
  233. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  234. // DB::table('timetotal')->insert($timetotal);
  235. // if($val->parent_id == 968){
  236. // //宁煤同步摄像头时间
  237. // $nm_camera_time += time() - $nm_start_time;
  238. // }
  239. }
  240. // $syn['nm_camera_time'] = $nm_camera_time;//宁煤单个区域同步时间
  241. // DB::table('syn')->where('id',$syn_id)->update($syn);
  242. //海康摄像头同步结束时间
  243. // $syn['hk_camera_end_time'] = date('Y-m-d H:i:s');
  244. // DB::table('syn')->where('id',$syn_id)->update($syn);
  245. //天地伟业需要同步的矿区
  246. $mine_list_ext = DB::table('mine_list_ext')
  247. ->select('mine_list_ext.*','mine_list.parent_id')
  248. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  249. // ->where('mine_list.parent_id',1)//0,先不同步天地伟业摄像头
  250. ->where('mine_list.parent_id',123456)//0,先不同步天地伟业摄像头
  251. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  252. ->where('mine_list_ext.ip','!=','')
  253. ->where('mine_list_ext.port','!=','')
  254. ->where('mine_list_ext.key','!=','')
  255. ->where('mine_list_ext.secret','!=','')
  256. ->where('mine_list_ext.deleted_at',null)->get();
  257. //天地摄像头同步开始时间
  258. // $syn['td_camera_start_time'] = date('Y-m-d H:i:s');
  259. // DB::table('syn')->where('id',$syn_id)->update($syn);
  260. if(sizeof($mine_list_ext)>0){
  261. for($i=0;$i<sizeof($mine_list_ext);$i++){//循环同步所有天地伟业矿区
  262. //矿区下所有区域
  263. $mine_list = DB::table('mine_list')
  264. ->where('degree','like',$mine_list_ext[$i]->mine_id.'|%')
  265. ->where('index_code','!=',null)->get();
  266. foreach($mine_list as $k=>$v){
  267. $mine_id = $v->id;
  268. Input::replace(
  269. [
  270. 'parentOrgIds' => $v->index_code,
  271. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  272. 'username' => $mine_list_ext[$i]->key,
  273. 'password' => $mine_list_ext[$i]->secret,
  274. 'sysId' => 'PG',
  275. ]
  276. );
  277. //获取区域下的子区域
  278. $tdwy = new TdwyController();
  279. $region = $tdwy->getRegionByOrgId();
  280. //获取最底层区域摄像头列表
  281. if (empty($region['content'])) {
  282. Input::replace(
  283. [
  284. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  285. 'username' => $mine_list_ext[$i]->key,
  286. 'password' => $mine_list_ext[$i]->secret,
  287. 'sysId' => 'PG',
  288. 'sOrgId' => $v->index_code,
  289. 'ip' => $mine_list_ext[$i]->ip,
  290. 'port' => $mine_list_ext[$i]->port,
  291. ]
  292. );
  293. //获取设备列表
  294. $tdwy = new TdwyController();
  295. $device_list = $tdwy->queryDeviceList();
  296. $device_index_code = [];
  297. //删除区域下所有摄像头
  298. // DB::table('camera_list')->where('mine_id',$v->id)->delete();
  299. if(isset($device_list['content']) && sizeof($device_list['content'])>0){
  300. for($j=0;$j<sizeof($device_list['content']);$j++){//循环添加区域下所有摄像头
  301. if(isset($device_list['content'][$j]['sId'])){//筛选有sId的摄像头
  302. if($device_list['content'][$j]['onlineStatus'] == 0){
  303. $device_list['content'][$j]['onlineStatus'] = 2;
  304. }
  305. $params = [
  306. 'mine_id' => $v->id,
  307. 'ip' => $mine_list_ext[$i]->ip,
  308. 'port' => 1554,
  309. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  310. 'sort' => sizeof($device_list['content']) - $j,
  311. 'camera_name' => $device_list['content'][$j]['sName'],
  312. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  313. 'index_code' => $device_list['content'][$j]['sId'],
  314. 'video_recorder'=> CameraEnum::VIDEO_RECORDER_TD,
  315. 'camera_status' => $device_list['content'][$j]['onlineStatus'],
  316. ];
  317. CameraList::updateOrCreate(['index_code' => $device_list['content'][$j]['sId']], $params);
  318. }
  319. $device_index_code[$j] = $device_list['content'][$j]['sId'];
  320. }
  321. }
  322. //删除不存在的摄像头
  323. CameraList::where('mine_id', $mine_id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $device_index_code)->delete();
  324. }
  325. }
  326. }
  327. }
  328. //天地摄像头同步结束时间
  329. // $syn['td_camera_end_time'] = date('Y-m-d H:i:s');
  330. // DB::table('syn')->where('id',$syn_id)->update($syn);
  331. //同步大华视频服务器摄像头
  332. $tdwy = new TdwyController();
  333. $tdwy->dahuaCamera();
  334. }
  335. //转义摄像头名称中的特殊字符
  336. public function transformCameraName($camera_name)
  337. {
  338. $camera_name = trim($camera_name);
  339. $camera_name = str_replace('#', '号', $camera_name);
  340. $camera_name = str_replace(' ', '-', $camera_name);
  341. $camera_name = str_replace('+', '', $camera_name);
  342. return $camera_name;
  343. }
  344. }