12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Admin\Actions\Device;
- use App\Model\DeviceBox;
- use App\Model\DeviceInfo;
- use App\Model\LockInfo;
- use App\Server\DeviceServer;
- use Encore\Admin\Actions\RowAction;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class sendRule extends RowAction
- {
- public $name = '下发协议';
- public function handle(Model $model, Request $request)
- {
- $id = $this->row->id;
- $device_name = $this->row->device_name;
- $box_info = LockInfo::where(['device_id'=>$id])->get();
- if(!empty($box_info)){
- $device_name = DeviceInfo::where(['id'=>$id])->value('device_name');
- if(empty($device_name)) return true;
- $lock_info = LockInfo::where(['device_id'=>$id,'status'=>1])->orderBy('box_type','asc')->get()->GroupBy('box_type');
- foreach ($lock_info as $lock){
- 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;
- $res = (new DeviceServer())->sendMsg(868626044260472 ,json_encode($send_rule));
- sleep('20');
- Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
- }
- foreach ($box_info as $box){
- $box_name = $box->name;
- $lock_info = LockInfo::where(['box_id'=>$box->id,'status'=>1])->get();
- $rule = [];
- foreach ($lock_info as $val){
- $rule[$box_name][] = [
- 'open'=>$val->open_time,
- 'close'=>$val->close_time,
- 'start'=>$val->start_time,
- 'type'=>$val->type,
- 'value'=>$val->value,
- ];
- }
- if(!empty($rule)){
- $rule['query'] = 1800;
- Log::info(json_encode($rule).PHP_EOL);
- $res = (new DeviceServer())->sendMsg($device_name,json_encode($rule));
- if($res['Success'] != true){
- return $this->response()->error($box_name.'下发失败');
- continue;
- }
- sleep(22);
- }
- }
- return $this->response()->success('下发成功')->refresh();
- }
- }
- }
|