testRule.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Admin\Actions\Device;
  3. use App\Model\DeviceBox;
  4. use App\Server\DeviceServer;
  5. use Encore\Admin\Actions\RowAction;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. class testRule extends RowAction
  10. {
  11. public $name = '测试命令';
  12. public function handle(Model $model, Request $request)
  13. {
  14. set_time_limit(1200);
  15. $id = $this->row->id;
  16. $device_name = $this->row->device_name;
  17. $box_info = DeviceBox::where(['device_id'=>$id])->get();
  18. if(!empty($box_info)){
  19. foreach ($box_info as $box){
  20. $box_name = $box->name;
  21. $test_json = $request->get($box_name);
  22. if(is_null(json_decode($test_json))){
  23. return $this->response()->error($box_name.'下发数据必须是json');
  24. }
  25. if(!empty($test_json) && json_decode($test_json)){
  26. Log::info($test_json.PHP_EOL);
  27. (new DeviceServer())->sendMsg($device_name,$test_json);
  28. sleep(18);
  29. }
  30. }
  31. return $this->response()->success('下发成功')->refresh();
  32. }
  33. }
  34. public function form()
  35. {
  36. $id = $this->row->id;
  37. $box_info = DeviceBox::where(['device_id'=>$id])->get();
  38. if(!empty($box_info)){
  39. foreach ($box_info as $box){
  40. $box_name = $box->name;
  41. $this->textarea($box_name,'请输入'.$box_name.'测试json');
  42. }
  43. }
  44. }
  45. }