UpdateHkList.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.parent_id',123456)
  148. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  149. ->where('mine_list_ext.ip','!=','')
  150. ->where('mine_list_ext.port','!=','')
  151. ->where('mine_list_ext.key','!=','')
  152. ->where('mine_list_ext.secret','!=','')
  153. ->where('mine_list_ext.deleted_at',null)->get();
  154. $mine_id_list = [];
  155. foreach($mine_ext as $k=>$v){
  156. $mine_id_list[$k] = $v->mine_id;
  157. }
  158. //清除天地伟业的区域
  159. if(count($mine_id_list) > 0){
  160. foreach($index_code_list as $k=>$v){
  161. if(in_array(explode('|',$v->degree)[0],$mine_id_list)){
  162. unset($index_code_list[$k]);
  163. }
  164. }
  165. }
  166. //海康摄像头同步开始时间
  167. // $syn_id = DB::table('syn')->orderBy('id','desc')->pluck('id')->first();
  168. // $syn1['hk_camera_start_time'] = date('Y-m-d H:i:s');
  169. // DB::table('syn')->where('id',$syn_id)->update($syn1);
  170. // $nm_camera_time = 0;
  171. foreach ($index_code_list as $key => $val) {
  172. //宁煤同步摄像头开始时间
  173. // $nm_start_time = time();
  174. // $timetotal['mine_id'] = $val->id;
  175. // $timetotal['interface_start_time'] = date('Y-m-d H:i:s');
  176. $degree = explode('|', $val->degree);
  177. // $trans_result = CameraServices::getHaiKangTransList($degree[0]);
  178. if($degree[0] == 6356){//洗选风险作业执法记录仪
  179. $degree[0] = $degree[1];
  180. }
  181. $result = CameraServices::getHaiKangCamera($degree[0], $val->index_code, CameraEnum::CAMERA_TYPE_ALL);
  182. // $info = DB::table('syn')->select('interface','handle')->orderBy('id','desc')->first();
  183. // $syn2['interface'] = $info->interface . $val->id . ':' . (time() - $nm_start_time) . ';';
  184. // $timetotal['interface_end_time'] = date('Y-m-d H:i:s');
  185. // $timetotal['handle_start_time'] = date('Y-m-d H:i:s');
  186. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  187. // $handle_start = time();
  188. if ($result['status']) {
  189. $trans_arr = [];
  190. // foreach ($trans_result['data'] as $tk => $tv) {
  191. // $trans_arr[$tv['indexCode']]['ip'] = $tv['ip'];
  192. // }
  193. $index_code_arr = [];
  194. // $arr_count = count($result['data']);
  195. foreach ($result['data'] as $k => $v) {
  196. $ip = '';
  197. $port = '';
  198. $com_number = '';
  199. // if (isset($trans_arr[$v['trans_code']])) {
  200. // $ip = $trans_arr[$v['trans_code']]['ip'];
  201. // $port = CameraEnum::HAK_DEFAULT_PORT;
  202. // $com_number = $v['com_number'];
  203. // }
  204. //当前摄像头index_code数组
  205. $index_code_arr[$k] = $v['camera_id'];
  206. //更新时间距当前时间天数
  207. // $update_time = strtotime($v['updateTime']);
  208. // $update_time = number_format((time() - $update_time)/(60*60*24),2);
  209. $params = [
  210. 'mine_id' => $val->id,
  211. 'camera_name' => $this->transformCameraName($v['camera_name']),
  212. 'index_code' => $v['camera_id'],
  213. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  214. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  215. 'ip' => $ip,
  216. 'port' => $port,
  217. 'com_number' => $com_number,
  218. //'sort' => $arr_count - $k,
  219. // 'camera_status' => $v['camera_status'],
  220. ];
  221. // if($result['is_update'] == true){
  222. CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  223. // }elseif($update_time < 10){
  224. // CameraList::updateOrCreate(['index_code' => $v['camera_id']], $params);
  225. // }
  226. }
  227. //删除不存在的摄像头
  228. CameraList::where('mine_id', $val->id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $index_code_arr)->delete();
  229. }
  230. // $syn2['handle'] = $info->handle . $val->id . ':' . (time() - $handle_start) . ';';
  231. // $timetotal['handle_end_time'] = date('Y-m-d H:i:s');
  232. // DB::table('syn')->where('id',$syn_id)->update($syn2);
  233. // DB::table('timetotal')->insert($timetotal);
  234. // if($val->parent_id == 968){
  235. // //宁煤同步摄像头时间
  236. // $nm_camera_time += time() - $nm_start_time;
  237. // }
  238. }
  239. // $syn['nm_camera_time'] = $nm_camera_time;//宁煤单个区域同步时间
  240. // DB::table('syn')->where('id',$syn_id)->update($syn);
  241. //海康摄像头同步结束时间
  242. // $syn['hk_camera_end_time'] = date('Y-m-d H:i:s');
  243. // DB::table('syn')->where('id',$syn_id)->update($syn);
  244. //天地伟业需要同步的矿区
  245. $mine_list_ext = DB::table('mine_list_ext')
  246. ->select('mine_list_ext.*','mine_list.parent_id')
  247. ->leftJoin('mine_list','mine_list.id','=','mine_list_ext.mine_id')
  248. // ->where('mine_list.parent_id',1)//0,先不同步天地伟业摄像头
  249. ->where('mine_list.parent_id',123456)//0,先不同步天地伟业摄像头
  250. ->where('mine_list_ext.is_hak',MineEnum::IS_HAK_TD)
  251. ->where('mine_list_ext.ip','!=','')
  252. ->where('mine_list_ext.port','!=','')
  253. ->where('mine_list_ext.key','!=','')
  254. ->where('mine_list_ext.secret','!=','')
  255. ->where('mine_list_ext.deleted_at',null)->get();
  256. //天地摄像头同步开始时间
  257. // $syn['td_camera_start_time'] = date('Y-m-d H:i:s');
  258. // DB::table('syn')->where('id',$syn_id)->update($syn);
  259. if(sizeof($mine_list_ext)>0){
  260. for($i=0;$i<sizeof($mine_list_ext);$i++){//循环同步所有天地伟业矿区
  261. //矿区下所有区域
  262. $mine_list = DB::table('mine_list')
  263. ->where('degree','like',$mine_list_ext[$i]->mine_id.'|%')
  264. ->where('index_code','!=',null)->get();
  265. foreach($mine_list as $k=>$v){
  266. $mine_id = $v->id;
  267. Input::replace(
  268. [
  269. 'parentOrgIds' => $v->index_code,
  270. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  271. 'username' => $mine_list_ext[$i]->key,
  272. 'password' => $mine_list_ext[$i]->secret,
  273. 'sysId' => 'PG',
  274. ]
  275. );
  276. //获取区域下的子区域
  277. $tdwy = new TdwyController();
  278. $region = $tdwy->getRegionByOrgId();
  279. //获取最底层区域摄像头列表
  280. if (empty($region['content'])) {
  281. Input::replace(
  282. [
  283. 'url' => $mine_list_ext[$i]->ip.":".$mine_list_ext[$i]->port,
  284. 'username' => $mine_list_ext[$i]->key,
  285. 'password' => $mine_list_ext[$i]->secret,
  286. 'sysId' => 'PG',
  287. 'sOrgId' => $v->index_code,
  288. 'ip' => $mine_list_ext[$i]->ip,
  289. 'port' => $mine_list_ext[$i]->port,
  290. ]
  291. );
  292. //获取设备列表
  293. $tdwy = new TdwyController();
  294. $device_list = $tdwy->queryDeviceList();
  295. $device_index_code = [];
  296. //删除区域下所有摄像头
  297. // DB::table('camera_list')->where('mine_id',$v->id)->delete();
  298. if(isset($device_list['content']) && sizeof($device_list['content'])>0){
  299. for($j=0;$j<sizeof($device_list['content']);$j++){//循环添加区域下所有摄像头
  300. if(isset($device_list['content'][$j]['sId'])){//筛选有sId的摄像头
  301. if($device_list['content'][$j]['onlineStatus'] == 0){
  302. $device_list['content'][$j]['onlineStatus'] = 2;
  303. }
  304. $params = [
  305. 'mine_id' => $v->id,
  306. 'ip' => $mine_list_ext[$i]->ip,
  307. 'port' => 1554,
  308. 'revert_id' => CameraEnum::CAMERA_DEFAULT_REVERT_ID,
  309. 'sort' => sizeof($device_list['content']) - $j,
  310. 'camera_name' => $device_list['content'][$j]['sName'],
  311. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  312. 'index_code' => $device_list['content'][$j]['sId'],
  313. 'video_recorder'=> CameraEnum::VIDEO_RECORDER_TD,
  314. 'camera_status' => $device_list['content'][$j]['onlineStatus'],
  315. ];
  316. CameraList::updateOrCreate(['index_code' => $device_list['content'][$j]['sId']], $params);
  317. }
  318. $device_index_code[$j] = $device_list['content'][$j]['sId'];
  319. }
  320. }
  321. //删除不存在的摄像头
  322. CameraList::where('mine_id', $mine_id)->where('index_code', '!=', NULL)->whereNotIn('index_code', $device_index_code)->delete();
  323. }
  324. }
  325. }
  326. }
  327. //天地摄像头同步结束时间
  328. // $syn['td_camera_end_time'] = date('Y-m-d H:i:s');
  329. // DB::table('syn')->where('id',$syn_id)->update($syn);
  330. //同步大华视频服务器摄像头
  331. $tdwy = new TdwyController();
  332. $tdwy->dahuaCamera();
  333. }
  334. //转义摄像头名称中的特殊字符
  335. public function transformCameraName($camera_name)
  336. {
  337. $camera_name = trim($camera_name);
  338. $camera_name = str_replace('#', '号', $camera_name);
  339. $camera_name = str_replace(' ', '-', $camera_name);
  340. $camera_name = str_replace('+', '', $camera_name);
  341. return $camera_name;
  342. }
  343. }