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.PlcParamAddressEnum
import com.hboxs.serialport.plc.frame.ReadCommandFrame
import com.hboxs.serialport.plc.frame.WriteCommandFrame
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.utils.commons.StringUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ThreadCoreParameter : LifecycleObserver {

    private val TAG = "ThreadGeneralParam"


    var jobWriteData: Job? = null
        get() = field

    var isActiveWriteData: Boolean = true
        get() = field
        set(value) {
            field = value
        }


    var jobdata01: Job? = null
        get() = field
    var isdata01: Boolean = true
        get() = field
        set(value) {
            field = value
        }

    var jobdata02: Job? = null
        get() = field
    var isdata02: Boolean = true
        get() = field
        set(value) {
            field = value
        }
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun startRead() {


    }

    public fun startData1(dataPosition: Int) {
        closeData1()
        var position = StringUtils.intTo4DigitHexStrict(dataPosition)
        jobdata01 = CoroutineScope(Dispatchers.Main).launch {
            while (isdata01) {
                Log.d("backinfo","发送的命令：" + "00000101" + position)
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_GETDATAOB, "00000101" + position))
                // 执行定时任务的操作
                Log.d(TAG, "jobdata01")
                delay(500) // 每隔2秒重复执行
            }
        }
    }

    public fun startData2(dataPosition: Int) {
        closeData2()
        var position = StringUtils.intTo4DigitHexStrict(dataPosition)
        jobdata02 = CoroutineScope(Dispatchers.Main).launch {
            while (isdata02) {
                Log.d("backinfo","发送的命令：" + "00000101" + position)
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_GETDATATWO, "00000101" + position))
                // 执行定时任务的操作
                Log.d(TAG, "jobdata01")
                delay(500) // 每隔2秒重复执行
            }
        }
    }
    public fun closeData2() {
        jobdata02?.cancel()
    }

    public fun closeData1() {
        jobdata01?.cancel()
    }

    fun writeData(data: String) {
        closeWriteData()
        jobWriteData = CoroutineScope(Dispatchers.Main).launch {
            while (isActiveWriteData) {
                Log.d("backinfo", "发生的数据：" + data)
                VboxSerialPortSendQueue.getInstance()
                    .sendCommand(VboxWriteCommand(VboxCommand.CMD_WRITEDATAOB, data))
                delay(1000) // 每隔2秒重复执行
            }
        }
    }

    fun closeWriteData() {
        jobWriteData?.cancel()
    }


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


}