<?php

namespace App\Admin\Actions\Device;

use App\Model\DeviceBox;
use App\Server\DeviceServer;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class getRule extends RowAction
{
   public $name = '属性上报';


    public function handle(Model $model, Request $request)
    {
        $id = $this->row->id;
        $device_name = $this->row->device_name;
        $box_info = DeviceBox::where(['device_id'=>$id])->get();
        if(!empty($box_info)){
            foreach ($box_info as $box){
                $box_name = $box->name;
                $rule = ['get'=>$box_name];
                if(!empty($rule)){
                    Log::info(json_encode($rule).PHP_EOL);
                    $res = (new DeviceServer())->sendMsg($device_name,json_encode($rule));
                    sleep(12);
                }
            }
            return $this->response()->success('上报属性成功')->refresh();
        }
    }

}
