123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Console\Commands;
- use App\Model\DeviceInfo;
- use App\Model\LockInfo;
- use App\Server\DeviceServer;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class SendDeviceRule extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'SDR {type : 发送命令类型 } {id : 设备编号}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '发送设备规则';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $arguments = $this->arguments();
- Log::info('执行了自动下发协议');
- $id = $arguments['id'];
- $type = $arguments['type'];
- Log::info('typeis '.$arguments['type']);
- if($arguments['type'] == 1){
- $this->sendDevice($id);
- } else if($type == 2){
- $this->sendBox($id);
- } else if ($type == 3){
- $this->cronSend($id);
- }
- dd('ok');
- }
- public function cronSend($id){
- //找出需要重新下发的设备
- $device_info = LockInfo::where(['status'=>1,'send_status'=>1])->distinct('device_id')->pluck('device_id');
- foreach ( $device_info as $device_id){
- //找出要重新下发的锁位
- $lock_info = LockInfo::where(['device_id'=>$device_id,'status'=>1,'send_status'=>1])->distinct('box_type')->pluck('box_type');
- $device_name = DeviceInfo::where(['device_name'=>$id])->value('device_name');
- //没有设备名称跳出
- if(empty($device_name)) continue;
- foreach ($lock_info as $box_type){
- //没有锁协议跳出
- $lock = LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->get();
- if(empty($lock)) continue;
- $send_rule = [];
- foreach ($lock as $protol){
- $send_rule["box".$protol->box_type][] = [
- 'open'=>$protol->open_time,
- 'close'=>$protol->close_time,
- 'start'=>$protol['start_time'],
- 'type'=>$protol['type'],
- 'value'=>$protol['value']
- ];
- }
- $send_rule['query'] = 3600;
- LockInfo::where(['id'=>$protol->id])->update(['send_status'=>2]);
- $res = (new DeviceServer())->sendMsg($device_name ,json_encode($send_rule));
- dd($res);
- sleep('20');
- if($res['Success'] == 'success') {
- LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->update(['send_status'=>2]);
- }
- Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
- }
- }
- dd($lock_info->toArray());
- }
- public function sendDevice($id)
- {
- if(empty($id)) return ;
- $lock_info = LockInfo::where(['device_id'=>$id,'status'=>1])->orderBy('box_type','asc')->get()->GroupBy('box_type');
- $device_name = DeviceInfo::where('id',$id)->value('device_name');
- if(empty($device_name)) return;
- foreach ($lock_info as $lock){
- if(empty($lock)) continue;
- $send_rule = [];
- foreach ($lock as $protol){
- $data = [
- 'open'=>$protol->open_time,
- 'close'=>$protol->close_time,
- 'start'=>$protol->start_time,
- 'type'=>$protol['type'],
- ];
- if(empty($protol->value[0])) {
- $data['value']= '';
- } else {
- $data['value']= $protol->value;
- }
- $send_rule["box".$protol->box_type][] = $data;
- }
- if(!empty($send_rule)) {
- $send_rule['query'] = 3600;
- (new DeviceServer())->sendMsg($device_name,json_encode($send_rule));
- sleep('22');
- Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
- }
- }
- }
- }
|