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(); } dd('ok'); } public function cronSend(){ //找出需要重新下发的设备 $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(['id'=>$device_id])->value('device_name'); //没有设备名称跳出 if(empty($device_name)) continue; foreach ($lock_info as $box_type){ //没有锁协议跳出 $lock = LockInfo::where(['device_id'=>$device_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)); sleep('20'); if($res['Success'] == 'success') { LockInfo::where(['device_id'=>$device_id,'box_type'=>$box_type])->update(['send_status'=>2]); } Log::info('执行规则'.json_encode($send_rule).PHP_EOL); } } dd('command is runing end'); } 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); } } } }