SendDeviceRule.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\DeviceInfo;
  4. use App\Model\LockInfo;
  5. use App\Server\DeviceServer;
  6. use Doctrine\DBAL\Schema\Schema;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Log;
  9. class SendDeviceRule extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'SDR {type : 发送命令类型 } {id : 设备编号}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '发送设备规则';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $arguments = $this->arguments();
  40. Log::info('执行了自动下发协议');
  41. $id = $arguments['id'];
  42. $type = $arguments['type'];
  43. Log::info('typeis '.$arguments['type']);
  44. if($arguments['type'] == 1){
  45. $this->sendDevice($id);
  46. } else if($type == 2){
  47. $this->sendBox($id);
  48. } else if ($type == 3){
  49. $this->cronSend();
  50. }
  51. dd('ok');
  52. }
  53. public function cronSend(){
  54. //找出需要重新下发的设备
  55. $device_info = LockInfo::where(['status'=>1,'send_status'=>1])->distinct('device_id')->pluck('device_id');
  56. foreach ( $device_info as $device_id){
  57. //找出要重新下发的锁位
  58. $lock_info = LockInfo::where(['device_id'=>$device_id,'status'=>1,'send_status'=>1])->distinct('box_type')->pluck('box_type');
  59. $device_name = DeviceInfo::where(['id'=>$device_id])->value('device_name');
  60. //没有设备名称跳出
  61. if(empty($device_name)) continue;
  62. foreach ($lock_info as $box_type){
  63. //没有锁协议跳出
  64. $lock = LockInfo::where(['device_id'=>$device_id,'box_type'=>$box_type])->get();
  65. if(empty($lock)) continue;
  66. $send_rule = [];
  67. foreach ($lock as $protol){
  68. $send_rule["box".$protol->box_type][] = [
  69. 'open'=>$protol->open_time,
  70. 'close'=>$protol->close_time,
  71. 'start'=>$protol['start_time'],
  72. 'type'=>$protol['type'],
  73. 'value'=>$protol['value']
  74. ];
  75. }
  76. $send_rule['query'] = 3600;
  77. LockInfo::where(['id'=>$protol->id])->update(['send_status'=>2]);
  78. $res = (new DeviceServer())->sendMsg($device_name ,json_encode($send_rule));
  79. sleep('20');
  80. if($res['Success'] == 'success') {
  81. LockInfo::where(['device_id'=>$device_id,'box_type'=>$box_type])->update(['send_status'=>2]);
  82. }
  83. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  84. }
  85. }
  86. dd('command is runing end');
  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. }