<?php

namespace App\Listeners;

use App\Model\DeviceBox;
use App\Model\DeviceInfo;
use App\Model\DeviceType;
use App\Model\DeviceTypeBox;
use App\Model\LockInfo;
use App\Model\SystemConfig;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Events\DeviceEvent;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class DeviceEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(DeviceEvent $event)
    {
        $device = $event->device;
        if(!empty($device->device_type)){
            $this->addBox($device);
            Log::info('有数据'.$device->device_type);
        } else {
            Log::info('没数据'.$device->device_type);
        }

        return true;
    }

    public function  addBox($device){
        $lock_config = SystemConfig::get('lock_config');
        DeviceInfo::BOXFOUR ?  $n = 4 : $n = 5;
        $now = date('Y-m-d H:i:s',time());
        for($i=1;$i<=$n;$i++){
            $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['am_open'],'close_time'=>$lock_config['am_close'],'start_time'=>$now];
            $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['pm_open'],'close_time'=>$lock_config['pm_close'],'start_time'=>$now];
        }
        DeviceInfo::where(['id'=>$device->id])->update(['lock_rule'=>json_encode($box)]);
    }
}
