SendDeviceRule.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 {type : 发送命令类型 } {id : 设备编号}';
  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. $type = $arguments['type'];
  42. Log::info('typeis '.$arguments['type']);
  43. if($arguments['type'] == 1){
  44. $this->sendDevice($id);
  45. } else if($type == 2){
  46. $this->sendBox($id);
  47. } else if ($type == 3){
  48. $this->cronSend($id);
  49. }
  50. dd('ok');
  51. }
  52. public function cronSend($id){
  53. //找出需要重新下发的设备
  54. $device_info = LockInfo::where(['status'=>1,'send_status'=>1])->distinct('device_id')->pluck('device_id');
  55. foreach ( $device_info as $device_id){
  56. //找出要重新下发的锁位
  57. $lock_info = LockInfo::where(['device_id'=>$device_id,'status'=>1,'send_status'=>1])->distinct('box_type')->pluck('box_type');
  58. $device_name = DeviceInfo::where(['device_name'=>$id])->value('device_name');
  59. //没有设备名称跳出
  60. if(empty($device_name)) continue;
  61. foreach ($lock_info as $box_type){
  62. //没有锁协议跳出
  63. $lock = LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->get();
  64. if(empty($lock)) continue;
  65. $send_rule = [];
  66. foreach ($lock as $protol){
  67. $send_rule["box".$protol->box_type][] = [
  68. 'open'=>$protol->open_time,
  69. 'close'=>$protol->close_time,
  70. 'start'=>$protol['start_time'],
  71. 'type'=>$protol['type'],
  72. 'value'=>$protol['value']
  73. ];
  74. }
  75. $send_rule['query'] = 3600;
  76. LockInfo::where(['id'=>$protol->id])->update(['send_status'=>2]);
  77. $res = (new DeviceServer())->sendMsg($device_name ,json_encode($send_rule));
  78. dd($res);
  79. sleep('20');
  80. if($res['Success'] == 'success') {
  81. LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->update(['send_status'=>2]);
  82. }
  83. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  84. }
  85. }
  86. dd($lock_info->toArray());
  87. }
  88. public function sendDevice($id)
  89. {
  90. if(empty($id)) return ;
  91. $lock_info = LockInfo::where(['device_id'=>$id,'status'=>1])->orderBy('box_type','asc')->get()->GroupBy('box_type');
  92. $device_name = DeviceInfo::where('id',$id)->value('device_name');
  93. if(empty($device_name)) return;
  94. foreach ($lock_info as $lock){
  95. if(empty($lock)) continue;
  96. $send_rule = [];
  97. foreach ($lock as $protol){
  98. $data = [
  99. 'open'=>$protol->open_time,
  100. 'close'=>$protol->close_time,
  101. 'start'=>$protol->start_time,
  102. 'type'=>$protol['type'],
  103. ];
  104. if(empty($protol->value[0])) {
  105. $data['value']= '';
  106. } else {
  107. $data['value']= $protol->value;
  108. }
  109. $send_rule["box".$protol->box_type][] = $data;
  110. }
  111. if(!empty($send_rule)) {
  112. $send_rule['query'] = 3600;
  113. (new DeviceServer())->sendMsg($device_name,json_encode($send_rule));
  114. sleep('22');
  115. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  116. }
  117. }
  118. }
  119. }