<?php

namespace App\Admin\Actions\Lock;

use App\Model\LockInfo;
use App\Server\DeviceServer;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;

class sendRule extends RowAction
{
    public $name = '更新规则';
    public function handle(Request $request)
    {
        $rule = '{
                    "box1": [
                        {"open":"09:33", "close":"09:34", "type":2, "start":"2020-09-01 10:00:00","value":""},
                        {"open":"09:35", "close":"09:36", "type":2, "start":"2020-09-01 10:00:00","value":""},
                        {"open":"09:37", "close":"09:38", "type":3, "start":"2020-09-01 10:00:00","value":["6","7"]}
                    ],
                    "query":600
                } ';
        $locks = LockInfo::where(['device_id'=>$this->row->id])->get()->groupBy('box_name');

        foreach ($locks as $lock ){

        }
        $res = (new DeviceServer())->sendMsg(868626044260522,$rule);
        if($res['Success'] == true){
            return $this->response()->success('更新设备规则成功')->refresh();
        } else {
            return $this->response()->error('更新命令失败');
        }
    }

}
