SendDeviceRule.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\DeviceInfo;
  4. use App\Model\LockInfo;
  5. use App\Server\DeviceServer;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class SendDeviceRule extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'SDR:{id} {type}';
  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. $arguments = $this->arguments();
  39. Log::info('执行了自动下发协议');
  40. $id = $arguments['id'];
  41. if($arguments['type'] == 1){
  42. $this->sendDevice($id);
  43. } else {
  44. $this->sendBox($id);
  45. }
  46. dd('ok');
  47. }
  48. public function sendDevice($id){
  49. //找出需要重新下发的设备
  50. $device_info = LockInfo::where(['status'=>1,'send_status'=>1])->distinct('device_id')->pluck('device_id');
  51. foreach ( $device_info as $device_id){
  52. //找出要重新下发的锁位
  53. $lock_info = LockInfo::where(['device_id'=>$device_id,'status'=>1,'send_status'=>1])->distinct('box_type')->pluck('box_type');
  54. $device_name = DeviceInfo::where(['id'=>$id])->value('device_name');
  55. //没有设备名称跳出
  56. if(empty($device_name)) continue;
  57. foreach ($lock_info as $box_type){
  58. //没有锁协议跳出
  59. $lock = LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->get();
  60. if(empty($lock)) continue;
  61. $send_rule = [];
  62. foreach ($lock as $protol){
  63. $send_rule["box".$protol->box_type][] = [
  64. 'open'=>$protol->open_time,
  65. 'close'=>$protol->close_time,
  66. 'start'=>$protol['start_time'],
  67. 'type'=>$protol['type'],
  68. 'value'=>$protol['value']
  69. ];
  70. }
  71. $send_rule['query'] = 3600;
  72. LockInfo::where(['id'=>$protol->id])->update(['send_status'=>2]);
  73. $res = (new DeviceServer())->sendMsg($device_name ,json_encode($send_rule));
  74. dd($res);
  75. sleep('20');
  76. if($res['Success'] == 'success') {
  77. LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->update(['send_status'=>2]);
  78. }
  79. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  80. }
  81. }
  82. dd($lock_info->toArray());
  83. }
  84. }