<?php

namespace App\Http\Controllers;

use App\Model\DeviceInfo;
use App\Model\LockInfo;
use App\Model\SystemConfig;
use App\Server\DeviceServer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use QL\QueryList;

class IndexController extends Controller
{
    public function testOpreation(Request $request)
    {

        $id = $request->get('id');
        $device = DeviceInfo::where(['id'=>$id])->first();
        $rule = json_decode($device->lock_rule);
        foreach ($rule as $key=>$box){
            $device_box[] = $key;
        }
        return view('test_opreation',['box'=>$device_box,'device'=>$device]);
    }

    function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = object_array($value);
            }
        }
        return $array;
    }


    //下发协议
    public function send_protocol()
    {
        $type = request('type');
        $id = request('id');
        $device  = DeviceInfo::where('id',$id)->first();
        $cache_key = $device->device_name.'rule_type';
        $re_type = Cache::get($cache_key);
//        return json_encode(['code'=>200,'msg'=>'下发命令成功','data'=>[]]);

        $time1 = 30;//3分钟定时
        if(empty($type) || empty($id)){
            return json_encode(['code'=>601,'msg'=>'缺少必要参数']);
        }
        if(empty($device)){
            return json_encode(['code'=>602,'msg'=>'设备不存在']);
        }
//        echo '缓存类型--'.$re_type.' 当前类型:--'.$type;

        if(empty($re_type)){
            if($type == 4) Cache::put($cache_key,4,$time1);
        } else {
            if($type != $re_type) return json_encode(['code'=>603,'msg'=>'设备下发协议冲突']);
        }
//        return json_encode(['code'=>200,'msg'=>'不重复']);
        switch ($type){
            case 1;
                $rule = ['cmd'=>'start_test'];
                break;
            case 2;
                $rule = ['cmd'=>'stop_test'];
                break;
            case 3;
                $open_time = date('Y-m-d H:i:s',(time() + 180));
                $close_time = date('Y-m-d H:i:s',(time() + 240));
                $start_time = date('Y-m-d H:i:s',(time() - 60));
                $lock_rule = json_decode($device->lock_rule);
                foreach ($lock_rule as $box_name){
                    sleep(22);
                    $rule = [$box_name=>['type'=>1,'start_time'=>$start_time,'open_time'=>$open_time,'close_time'=>$close_time,'value'=>''],'query'=>3600];
                    $res = (new DeviceServer())->sendMsg($device->device_name ,$rule);
                }
                break;
            case 4;
                $box_name = request('box_name');
                if(empty($box_name)){
                    return json_encode(['code'=>601,'msg'=>'缺少必要参数']);
                }
                $open_time = date('Y-m-d H:i:s',(time() + 60));
                $close_time = date('Y-m-d H:i:s',(time() + 120));
                $start_time = date('Y-m-d H:i:s',(time() - 60));
                $rule = ['box'.$box_name=>['type'=>1,'start_time'=>$start_time,'open_time'=>$open_time,'close_time'=>$close_time,'value'=>''],'query'=>3600];
                break;
        }
        //$rule = ['cmd'=>'stop_test'];
        if($type != 3){
             $res = (new DeviceServer())->sendMsg($device->device_name,json_encode($rule));
        }
        if($res['Success'] == true){
            return json_encode(['code'=>200,'msg'=>'下发命令成功']);
        } else {
            return json_encode(['code'=>400,'msg'=>'下发命令失败']);
        }


    }


    //获取设备信息
    public function get_boxname()
    {
        $id = request('id');
        $device  = DeviceInfo::where('device_name',$id)->first();
        if(empty($device)){
            return json_encode(['code'=>200,'msg'=>'ok','data'=>[]]);
        }
        $lock_config = SystemConfig::get('lock_config');

        $data['id'] = $device->id;
        $data['device_name'] = $device->device_name;
        $data['device_type'] = $device->device_type;
        if($device->device_type == 1){
            $data['device_image'] = $lock_config['box_img_four'];
        } else {
            $data['device_image'] = $lock_config['box_img_five'];
        }
        $data['device_image'] = '';
        return json_encode(['code'=>200,'msg'=>'ok','data'=>$data]);
    }

}
