sendRule.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $box_info = LockInfo::where(['device_id'=>$id])->get();
  19. if(!empty($box_info)){
  20. $device_name = DeviceInfo::where(['id'=>$id])->value('device_name');
  21. if(empty($device_name)) return true;
  22. $lock_info = LockInfo::where(['device_id'=>$id,'status'=>1])->orderBy('box_type','asc')->get()->GroupBy('box_type');
  23. foreach ($lock_info as $lock){
  24. foreach ($lock as $protol){
  25. $send_rule["box".$protol->box_type][] = [
  26. 'open'=>$protol->open_time,
  27. 'close'=>$protol->close_time,
  28. 'start'=>$protol['start_time'],
  29. 'type'=>$protol['type'],
  30. 'value'=>$protol['value']
  31. ];
  32. }
  33. $send_rule['query'] = 3600;
  34. $res = (new DeviceServer())->sendMsg(868626044260472 ,json_encode($send_rule));
  35. sleep('20');
  36. Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
  37. }
  38. foreach ($box_info as $box){
  39. $box_name = $box->name;
  40. $lock_info = LockInfo::where(['box_id'=>$box->id,'status'=>1])->get();
  41. $rule = [];
  42. foreach ($lock_info as $val){
  43. $rule[$box_name][] = [
  44. 'open'=>$val->open_time,
  45. 'close'=>$val->close_time,
  46. 'start'=>$val->start_time,
  47. 'type'=>$val->type,
  48. 'value'=>$val->value,
  49. ];
  50. }
  51. if(!empty($rule)){
  52. $rule['query'] = 1800;
  53. Log::info(json_encode($rule).PHP_EOL);
  54. $res = (new DeviceServer())->sendMsg($device_name,json_encode($rule));
  55. if($res['Success'] != true){
  56. return $this->response()->error($box_name.'下发失败');
  57. continue;
  58. }
  59. sleep(22);
  60. }
  61. }
  62. return $this->response()->success('下发成功')->refresh();
  63. }
  64. }
  65. }