package com.hboxs.serialport.plc.thread

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.hboxs.serialport.plc.SerialPortSendQueue
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcDebugAddressEnum
import com.hboxs.serialport.plc.frame.ReadCommandFrame
import com.hboxs.serialport.plc.frame.RstCommandFrame
import com.hboxs.serialport.plc.frame.SetCommandFrame
import com.hboxs.serialport.sbc.VboxSerialPortSendQueue
import com.hboxs.serialport.sbc.frame.VboxCommand
import com.hboxs.serialport.sbc.frame.VboxWriteCommand
import com.quyunshuo.androidbaseframemvvm.base.constant.DebugType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ThreadDebugParamParam : LifecycleObserver {


    var jobSetBtn: Job? = null
        get() = field
    var jobRstBtn: Job? = null
        get() = field


    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun startRead() {
        // 启动协程
    }

    fun setBtn(address: String) {
        VboxSerialPortSendQueue.getInstance()
            .sendCommand(VboxWriteCommand(VboxCommand.CMD_IO, address+"01"))
    }

    fun rstBtn(address: String) {
        VboxSerialPortSendQueue.getInstance()
            .sendCommand(VboxWriteCommand(VboxCommand.CMD_IO, address+"00"))
    }
    fun openClean(address: String, checked: Boolean){
        Log.d("backinfo","发送清洗数据："+address)
        if (checked) {
            VboxSerialPortSendQueue.getInstance()
                .sendCommand(VboxWriteCommand(VboxCommand.CMD_CLEAN_PWM, address + "01"))
        } else {
            VboxSerialPortSendQueue.getInstance()
                .sendCommand(VboxWriteCommand(VboxCommand.CMD_CLEAN_PWM, address + "00"))
        }
    }
    fun stuck(address: String){
        VboxSerialPortSendQueue.getInstance()
            .sendCommand(VboxWriteCommand(VboxCommand.CMD_STUCK_PWM, address))
    }
    fun xReset(address: String){
        VboxSerialPortSendQueue.getInstance()
            .sendCommand(VboxWriteCommand(VboxCommand.CMD_X_RESET_PWM, address))
    }

    fun lutou(address: String, checked: Boolean, type: Int) {
        Log.d("backinfo", "发生炉头信息:checked:" + checked + ",type:" + type)

        if (type == DebugType.PWM1_IO) {
            Log.d("backinfo", "发生炉头信息:checked:" + checked + ",type:" + type)
            if (checked) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "1E"))
            } else {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "00"))
            }
        } else if (type == DebugType.PWM2_IO) {
            Log.d("backinfo", "发生炉头信息:checked:" + checked + ",type:" + type)

            if (checked) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "32"))
            } else {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "00"))
            }

        } else if (type == DebugType.PWM3_IO) {
            Log.d("backinfo", "发生炉头信息:checked:" + checked + ",type:" + type)
            if (checked) {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "64"))
            } else {
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_BURNER_PWM, address + "00"))
            }
        }

    }

    /**
     * 关掉线程池
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun destroy() {
        //避免线程池关不掉

        jobSetBtn?.cancel()
        jobRstBtn?.cancel()

    }


}