UpdateHkList.php 15 KB

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