<?php

namespace App\Listeners;

use App\Events\LockEvent;
use App\Model\DeviceBox;
use App\Model\LockInfo;
use App\Server\DeviceServer;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class LockEventListener
{


    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(LockEvent $lock)
    {

        $lock = $lock->lock ;
        $box_id = $lock->boxs->id;
        $device_name = $lock->boxs->devices->device_name;


        if(empty($box_id) || empty($device_name)){
            return fasle;
        }
        echo $box_id.'设备名称'.$device_name;
        $lock_info = LockInfo::where(['box_id'=>$box_id,'status'=>1])->get();

        $rule = [];
        foreach ($lock_info as $val){
            $rule[$lock->boxs->name][] =  [
                'open'=>$val->open_time,
                'close'=>$val->close_time,
                'start'=>$val->start_time,
                'type'=>$val->type,
                'value'=>$val->value,
            ];
        }
        if(!empty($rule)){
            $rule['query'] = 1200;
            //发送命令
//            (new DeviceServer())->sendMsg($device_name,json_encode($rule));

            Log::info('设备'.$device_name.'盒子'.$lock->boxs->name.'更新规则'.PHP_EOL);
        } else {
            Log::info('设备'.$device_name.'盒子'.$lock->boxs->name.'无规则'.PHP_EOL);
        }

    }
}
