UpdateHkList.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_ext.is_hak',MineEnum::IS_HAK_TD)
  81. ->where('mine_list_ext.ip','!=','')
  82. ->where('mine_list_ext.port','!=','')
  83. ->where('mine_list_ext.key','!=','')
  84. ->where('mine_list_ext.secret','!=','')
  85. ->where('mine_list_ext.deleted_at',null)->get();
  86. if(sizeof($mine_list_ext)>0){
  87. for($i=0;$i<sizeof($mine_list_ext);$i++){//循环同步所有天地伟业矿区
  88. Input::replace(
  89. [
  90. 'parentOrgIds' => $mine_list_ext[$i]->sOrgId,
  91. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  92. 'username' => $mine_list_ext[$i]->key,
  93. 'password' => $mine_list_ext[$i]->secret,
  94. 'sysId' => 'PG',
  95. ]
  96. );
  97. $tdwy = new TdwyController();
  98. $region = $tdwy->getRegionByOrgId();
  99. $mine_id = $mine_list_ext[$i]->mine_id;
  100. //更新区域
  101. $result['data']['list'] = $tdwy->saveTrees($region, $mine_id);
  102. }
  103. }
  104. //天地区域同步结束时间
  105. // $syn['td_area_end_time'] = date('Y-m-d H:i:s');
  106. // DB::table('syn')->insert($syn);
  107. }
  108. public function updateCameraList()
  109. {
  110. //海康
  111. //查询存在index_code列表
  112. $index_code_list = MineList::where('index_code', '!=', null)->get();
  113. //不在线矿区
  114. $mine_list_ext = DB::table('mine_list_ext')->where('deleted_at',null)->where('ip','!=',null)->get();
  115. for($i=0;$i<count($mine_list_ext);$i++){
  116. $ip = explode('://',$mine_list_ext[$i]->ip)[1];
  117. $port = $mine_list_ext[$i]->port;
  118. if($fp = @fsockopen($ip,$port,$errno,$errstr,1)){
  119. fclose($fp);
  120. }else{
  121. $arr[] = $mine_list_ext[$i]->mine_id;
  122. }
  123. }
  124. //去掉不在线矿区
  125. foreach($index_code_list as $k=>$v){
  126. $degree = explode('|',$v->degree);
  127. if(isset($arr)){
  128. for($i=0;$i<count($arr);$i++){
  129. if($degree[0] == $arr[$i]){
  130. unset($index_code_list[$k]);
  131. }
  132. }
  133. }
  134. }
  135. //去掉洗选
  136. // foreach($index_code_list as $k=>$v){
  137. // $degree = explode('|',$v->degree);
  138. // if($degree[0] == 66){
  139. // unset($index_code_list[$k]);
  140. // }
  141. // }
  142. //天地伟业矿区
  143. $mine_ext = DB::table('mine_list_ext')
  144. ->select('mine_list_ext.*','mine_list.parent_id')
  145. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  146. ->where('mine_list.parent_id',0)
  147. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  148. ->where('mine_list_ext.ip','!=','')
  149. ->where('mine_list_ext.port','!=','')
  150. ->where('mine_list_ext.key','!=','')
  151. ->where('mine_list_ext.secret','!=','')
  152. ->where('mine_list_ext.deleted_at',null)->get();
  153. $mine_id_list = [];
  154. foreach($mine_ext as $k=>$v){
  155. $mine_id_list[$k] = $v->mine_id;
  156. }
  157. //清除天地伟业的区域
  158. if(count($mine_id_list) > 0){
  159. foreach($index_code_list as $k=>$v){
  160. if(in_array(explode('|',$v->degree)[0],$mine_id_list)){
  161. unset($index_code_list[$k]);
  162. }
  163. }
  164. }
  165. //海康摄像头同步开始时间
  166. // $syn_id = DB::table('syn')->orderBy('id','desc')->pluck('id')->first();
  167. // $syn1['hk_camera_start_time'] = date('Y-m-d H:i:s');
  168. // DB::table('syn')->where('id',$syn_id)->update($syn1);
  169. // $nm_camera_time = 0;
  170. foreach ($index_code_list as $key => $val) {
  171. //宁煤同步摄像头开始时间
  172. // $nm_start_time = time();
  173. // $timetotal['mine_id'] = $val->id;
  174. // $timetotal['interface_start_time'] = date('Y-m-d H:i:s');
  175. $degree = explode('|', $val->degree);
  176. // $trans_result = CameraServices::getHaiKangTransList($degree[0]);
  177. $result = CameraServices::getHaiKangCamera($degree[0], $val->index_code, CameraEnum::CAMERA_TYPE_ALL);
  178. // $info = DB::table('syn')->select('interface','handle')->orderBy('id','desc')->first();
  179. // $syn2['interface'] = $info->interface . $val->id . ':' . (time() - $nm_start_time) . ';';
  180. // $timetotal['interface_end_time'] = date('Y-m-d H:i:s');
  181. // $timetotal['handle_start_time'] = date('Y-m-d H:i:s');
  182. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  183. // $handle_start = time();
  184. if ($result['status']) {
  185. $trans_arr = [];
  186. // foreach ($trans_result['data'] as $tk => $tv) {
  187. // $trans_arr[$tv['indexCode']]['ip'] = $tv['ip'];
  188. // }
  189. $index_code_arr = [];
  190. // $arr_count = count($result['data']);
  191. foreach ($result['data'] as $k => $v) {
  192. $ip = '';
  193. $port = '';
  194. $com_number = '';
  195. // if (isset($trans_arr[$v['trans_code']])) {
  196. // $ip = $trans_arr[$v['trans_code']]['ip'];
  197. // $port = CameraEnum::HAK_DEFAULT_PORT;
  198. // $com_number = $v['com_number'];
  199. // }
  200. //当前摄像头index_code数组
  201. $index_code_arr[$k] = $v['camera_id'];
  202. //更新时间距当前时间天数
  203. $update_time = strtotime($v['updateTime']);
  204. $update_time = number_format((time() - $update_time)/(60*60*24),2);
  205. $params = [
  206. 'mine_id' => $val->id,
  207. 'camera_name' => $this->transformCameraName($v['camera_name']),
  208. 'index_code' => $v['camera_id'],
  209. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  210. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  211. 'ip' => $ip,
  212. 'port' => $port,
  213. 'com_number' => $com_number,
  214. //'sort' => $arr_count - $k,
  215. // 'camera_status' => $v['camera_status'],
  216. ];
  217. if($result['is_update'] == true){
  218. CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  219. }elseif($update_time < 10){
  220. CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  221. }
  222. }
  223. //删除不存在的摄像头
  224. CameraList::where('mine_id', $val->id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $index_code_arr)->delete();
  225. }
  226. // $syn2['handle'] = $info->handle . $val->id . ':' . (time() - $handle_start) . ';';
  227. // $timetotal['handle_end_time'] = date('Y-m-d H:i:s');
  228. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  229. // DB::table('timetotal')->insert($timetotal);
  230. // if($val->parent_id == 968){
  231. // //宁煤同步摄像头时间
  232. // $nm_camera_time += time() - $nm_start_time;
  233. // }
  234. }
  235. // $syn['nm_camera_time'] = $nm_camera_time;//宁煤单个区域同步时间
  236. // DB::table('syn')->where('id',$syn_id)->update($syn);
  237. //海康摄像头同步结束时间
  238. // $syn['hk_camera_end_time'] = date('Y-m-d H:i:s');
  239. // DB::table('syn')->where('id',$syn_id)->update($syn);
  240. //天地伟业需要同步的矿区
  241. $mine_list_ext = DB::table('mine_list_ext')
  242. ->select('mine_list_ext.*','mine_list.parent_id')
  243. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  244. ->where('mine_list.parent_id',1)//0,先不同步天地伟业摄像头
  245. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  246. ->where('mine_list_ext.ip','!=','')
  247. ->where('mine_list_ext.port','!=','')
  248. ->where('mine_list_ext.key','!=','')
  249. ->where('mine_list_ext.secret','!=','')
  250. ->where('mine_list_ext.deleted_at',null)->get();
  251. //天地摄像头同步开始时间
  252. // $syn['td_camera_start_time'] = date('Y-m-d H:i:s');
  253. // DB::table('syn')->where('id',$syn_id)->update($syn);
  254. if(sizeof($mine_list_ext)>0){
  255. for($i=0;$i<sizeof($mine_list_ext);$i++){//循环同步所有天地伟业矿区
  256. //矿区下所有区域
  257. $mine_list = DB::table('mine_list')
  258. ->where('degree','like',$mine_list_ext[$i]->mine_id.'|%')
  259. ->where('index_code','!=',null)->get();
  260. foreach($mine_list as $k=>$v){
  261. $mine_id = $v->id;
  262. Input::replace(
  263. [
  264. 'parentOrgIds' => $v->index_code,
  265. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  266. 'username' => $mine_list_ext[$i]->key,
  267. 'password' => $mine_list_ext[$i]->secret,
  268. 'sysId' => 'PG',
  269. ]
  270. );
  271. //获取区域下的子区域
  272. $tdwy = new TdwyController();
  273. $region = $tdwy->getRegionByOrgId();
  274. //获取最底层区域摄像头列表
  275. if (empty($region['content'])) {
  276. Input::replace(
  277. [
  278. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  279. 'username' => $mine_list_ext[$i]->key,
  280. 'password' => $mine_list_ext[$i]->secret,
  281. 'sysId' => 'PG',
  282. 'sOrgId' => $v->index_code,
  283. 'ip' => $mine_list_ext[$i]->ip,
  284. 'port' => $mine_list_ext[$i]->port,
  285. ]
  286. );
  287. //获取设备列表
  288. $tdwy = new TdwyController();
  289. $device_list = $tdwy->queryDeviceList();
  290. $device_index_code = [];
  291. //删除区域下所有摄像头
  292. // DB::table('camera_list')->where('mine_id',$v->id)->delete();
  293. if(isset($device_list['content']) && sizeof($device_list['content'])>0){
  294. for($j=0;$j<sizeof($device_list['content']);$j++){//循环添加区域下所有摄像头
  295. if(isset($device_list['content'][$j]['sId'])){//筛选有sId的摄像头
  296. if($device_list['content'][$j]['onlineStatus'] == 0){
  297. $device_list['content'][$j]['onlineStatus'] = 2;
  298. }
  299. $params = [
  300. 'mine_id' => $v->id,
  301. 'ip' => $mine_list_ext[$i]->ip,
  302. 'port' => 1554,
  303. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  304. 'sort' => sizeof($device_list['content']) - $j,
  305. 'camera_name' => $device_list['content'][$j]['sName'],
  306. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  307. 'index_code' => $device_list['content'][$j]['sId'],
  308. 'video_recorder'=> CameraEnum::VIDEO_RECORDER_TD,
  309. 'camera_status' => $device_list['content'][$j]['onlineStatus'],
  310. ];
  311. CameraList::updateOrCreate(['index_code' => $device_list['content'][$j]['sId']], $params);
  312. }
  313. $device_index_code[$j] = $device_list['content'][$j]['sId'];
  314. }
  315. }
  316. //删除不存在的摄像头
  317. CameraList::where('mine_id', $mine_id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $device_index_code)->delete();
  318. }
  319. }
  320. }
  321. }
  322. //天地摄像头同步结束时间
  323. // $syn['td_camera_end_time'] = date('Y-m-d H:i:s');
  324. // DB::table('syn')->where('id',$syn_id)->update($syn);
  325. }
  326. //转义摄像头名称中的特殊字符
  327. public function transformCameraName($camera_name)
  328. {
  329. $camera_name = trim($camera_name);
  330. $camera_name = str_replace('#', '号', $camera_name);
  331. $camera_name = str_replace(' ', '-', $camera_name);
  332. $camera_name = str_replace('+', '', $camera_name);
  333. return $camera_name;
  334. }
  335. }