<?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\Artisan;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use QL\QueryList;

class IndexController extends Controller
{
    public function testOpreation(Request $request)
    {
//        Artisan::call('SDR: 1 1');
//        Artisan::call('SDR', [
//            'id' => 1, 'type' => 1
//        ]);

    //echo strtotime('+60 days');dd('ok');
        $id = $request->get('id');
        $device = DeviceInfo::where(['device_name'=>$id])->first();
        if($device->type == 1){
            $img = SystemConfig::get('lock_config','box_img_four');
        } else {
            $img = SystemConfig::get('lock_config','box_img_five');
        }
        $type = $device->type;
         switch($type) {
             case  DeviceInfo::BOXFOUR;
                 $n = 4;
                 break;
             case DeviceInfo::BOXFIVE;
                 $n = 5;
                 break;
             case DeviceInfo::BOXFOURRECYCLE;
                 $n = 5;
                 break;
             case DeviceInfo::BOXFIVERECYCLE;
                 $n = 6;
                 break;
             default;
                 $n = 5;
                 break;
         }
        for ($i = 1; $i<$n;$i++){
            $device_box[] = $i;
        }
        return view('test_opreation',['img'=>$img,'id'=>$id]);
    }

    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('device_name',$id)->first();
        $cache_key = $id .'rule_type';
        $re_type = Cache::get($cache_key);
//        return json_encode(['code'=>200,'msg'=>'下发命令成功','data'=>[]]);
//dd($re_type);
        $time1 = 180;//3分钟定时
        $opt = 180;
        $cpt = 240;
        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"];
                sleep(6);
                break;
            case 3;
                $open_time = date('H:i',(time() + $opt));
                $close_time = date('H:i',(time() + $cpt));
                $start_time = date('Y-m-d H:i:s',(time() - 60));
                $type_arr = [1=>4,2=>5,3=>5,4=>6];
                for ($i = 1; $i<=$type_arr[$type]; $i++){
                    $rule = ['box'.$i=>[['type'=>1,'start'=>$start_time,'open'=>$open_time,'close'=>$close_time,'value'=>'']],'query'=>3600];
                    $res = (new DeviceServer())->sendMsg($device->device_name ,json_encode($rule));
                    sleep(22);
                }
//                $lock_rule = json_decode($device->lock_rule);
//                foreach ($lock_rule as $box_name){
//                    sleep(22);
//                    $rule = [$box_name=>[['type'=>1,'start'=>$start_time,'open'=>$open_time,'close'=>$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('H:i',(time() + $opt));
                $close_time = date('H:i',(time() + $cpt));
                $start_time = date('Y-m-d H:i',(time() - 60));
                $rule = ['box'.$box_name=>[['type'=>1,'start'=>$start_time,'open'=>$open_time,'close'=>$close_time,'value'=>''],],'query'=>3600];
                break;
        }
//        $rule = ["cmd"=>"stop_test"];
//        dd(json_encode($rule));
        if($type != 3){
            $res = (new DeviceServer())->sendMsg($device->device_name,json_encode($rule));
        }
        if(!empty($res) && $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;
        $img_field_arr = [
            1=>'box_img_four',
            2=>'box_img_five',
            3=>'box_img_refour',
            4=>'box_img_refive',
        ];
        $data['device_image'] = $lock_config[$img_field_arr[$device->device_type]];

        return json_encode(['code'=>200,'msg'=>'ok','data'=>$data]);
    }

}
