package com.example.iot_controlhost.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.iot_controlhost.model.MyTask;
import com.example.iot_controlhost.model.thread.QueueIOTask;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * 消毒计划-广播接受者
 */
public class UvTaskReceiver extends BroadcastReceiver {
    /**
     * 开灯Action
     */
    public static final String OPEN_LIGHT = "OPEN_LIGHT";
    /**
     * 关灯Action
     */
    public static final String CLOSE_LIGHT = "CLOSE_LIGHT";

    public void onReceive(Context context, Intent intent) {
        MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
            boolean target = false;
            MyLog.test("收到Action：" + intent.getAction());
            if (OPEN_LIGHT.equals(intent.getAction())) {
                target = true;
            } else if (CLOSE_LIGHT.equals(intent.getAction())) {
                target = false;
            }
            // 操作消毒灯 最多尝试3次
            if (MyUtil.autoControlOperateThird(RoomController.uvLight, target)) {
                MyLog.auto("定时任务：消毒灯" + (target ? "开启" : "关闭") );
            } else {
                MyLog.autoError("定时任务失败：消毒灯" + (target ? "开启" : "关闭") );
            }
        }));
    }
}