sendRule.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Admin\Actions\Device;
  3. use App\Model\DeviceBox;
  4. use App\Model\DeviceInfo;
  5. use App\Model\LockInfo;
  6. use App\Server\DeviceServer;
  7. use Encore\Admin\Actions\RowAction;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Log;
  11. class sendRule extends RowAction
  12. {
  13. public $name = '下发协议';
  14. public function handle(Model $model, Request $request)
  15. {
  16. $id = $this->row->id;
  17. $device_name = $this->row->device_name;
  18. if(empty($device_name)) return true;
  19. $lock_info = LockInfo::where(['device_id'=>$id,'status'=>1])->orderBy('box_type','asc')->get()->GroupBy('box_type');
  20. foreach ($lock_info as $lock){
  21. if(empty($lock)) continue;
  22. $send_rule = [];
  23. foreach ($lock as $protol){
  24. $data = [
  25. 'open'=>$protol->open_time,
  26. 'close'=>$protol->close_time,
  27. 'start'=>$protol->start_time,
  28. 'type'=>$protol['type'],
  29. ];
  30. if(empty($protol->value[0])) {
  31. $data['value']= '';
  32. } else {
  33. $data['value']= $protol->value;
  34. }
  35. $send_rule["box".$protol->box_type][] = $data;
  36. }
  37. if(!empty($send_rule)) {
  38. $send_rule['query'] = 3600;
  39. $res = (new DeviceServer())->sendMsg($device_name,json_encode($send_rule));
  40. sleep('22');
  41. // if($res['Success'] != true){
  42. // return $this->response()->error($device_name.'下发失败');
  43. // continue;
  44. // }
  45. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  46. }
  47. }
  48. return $this->response()->success('下发成功')->refresh();
  49. }
  50. }