UpdateCameraStatus.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Modules\Camera\Entities\CameraList;
  5. use Modules\Camera\Enum\CameraEnum;
  6. use Modules\Camera\Services\CameraServices;
  7. use Modules\Mine\Entities\MineListExt;
  8. class UpdateCameraStatus extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'camera:updatestatus';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '更新摄像头在线状态';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. }
  39. public function updateCameraStatus()
  40. {
  41. //查询海康视频服务器中的所有摄像头
  42. $camera_list = CameraList::join(
  43. 'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
  44. )->where(
  45. [
  46. 'camera_list.camera_source' => CameraEnum::CAMERA_SOURCE_2,
  47. 'camera_list.video_recorder' => CameraEnum::VIDEO_RECORDER_HK
  48. ]
  49. )->select(
  50. [
  51. 'mine_list.degree',
  52. 'camera_list.id'
  53. ]
  54. )->get()->toArray();
  55. //先将所有摄像头标记为在线状态
  56. CameraList::where(
  57. [
  58. 'camera_source' => CameraEnum::CAMERA_SOURCE_2,
  59. 'video_recorder' => CameraEnum::VIDEO_RECORDER_HK,
  60. ]
  61. )->update(['camera_status' => CameraEnum::CAMERA_STATUS_ONLINE]);
  62. //循环取流
  63. $i = 0;
  64. foreach ($camera_list as $key => $val) {
  65. $degree = explode('|', $val->degree);
  66. $result = CameraServices::downloadCameraFiles($degree[0], $val->id);
  67. //取流成功,摄像头标记为在线,失败为离线
  68. if (!$result['status']) {
  69. CameraList::where('id', $val->id)->update(['camera_status' => CameraEnum::CAMERA_STATUS_OFFLINE]);
  70. }
  71. $i++;
  72. //每循环30个停止一次推流
  73. if ($i == 30) {
  74. $i = 0;
  75. //停止推流
  76. CameraServices::stopCameraStream();
  77. //创建目录
  78. // if (!is_dir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path)) {
  79. // mkdir('/www/wwwroot/video.nxjiewei.com/public/' . CameraEnum::M3U8_FILE_PATH . '/' . $path, 0777, true);
  80. // }
  81. //杀掉取流进程
  82. $exec = "kill -9 `ps -ef | grep ffmpeg|awk '{print $2}'`";
  83. shell_exec($exec);
  84. }
  85. }
  86. }
  87. }