package com.example.iot_controlhost.model.sensor

import com.blankj.utilcode.util.StringUtils
import com.example.iot_controlhost.model.Device.getCRC
import com.example.iot_controlhost.model.sensor.Sensor.serialPortUtil
import com.example.iot_controlhost.utils.DataUtils


/**
 * 温湿度传感器
 */
class THSensor(name: String?) : Sensor(name) {
    /**
     * 温度
     */
    @JvmField
    var temperature: Double = 0.0

    /**
     * 湿度
     */
    @JvmField
    var humidity: Double = 0.0

    override fun collectSensorData(model: String, address: String): Boolean {
        var result = false
        //获得指令
        var str = address + "0300000002"
        val crc = getCRC(str)
        str += crc
        //利用指令获得数据
        val data = serialPortUtil.sendAndReceiveSerialData(
            this, str
        )
        //解析数据
        var wd = 0.0
        var sd = 0.0
        if (StringUtils.isEmpty(data)) {
            initData()
        }
        val wsd = substring(data, 2)
        if (wsd.size < 7) {
            initData()
        }
        if (DataUtils.CRC16Check(wsd)) {
            if (model == models[0]) {
                wd = WDTransform(wsd[3] + wsd[4])
                sd = WDTransform(wsd[5] + wsd[6])
            } else if (model == models[1]) {
                // HR-DGWS-485 与 TH10S-B 命令基本一致，只是温湿度解析位置不同
                wd = WDTransform(wsd[5] + wsd[6])
                sd = WDTransform(wsd[3] + wsd[4])
            }
        }
        if (wd > 100 || sd > 100) {
            initData()
        } else {
            temperature = wd
            humidity = sd
            result = true
        }
        return result
    }

    private fun initData() {
        temperature = 0.0
        humidity = 0.0
    }

    override fun getModels(): Array<String> {
        return arrayOf("TH10S-B", "HR-DGWS-485")
    }

    override fun toString(): String {
        return "温度：$temperature℃\t\t湿度：$humidity%"
    }


    //温湿度转换
    protected fun WDTransform(sixteen: String): Double {
        val ten = sixteen.toInt(16)
        return ten.toDouble() / 10.0
    }

    override fun setAddressToNewAddress(
        model: String,
        oldAddress: String,
        oldBaud: String,
        newAddress: String
    ): Boolean {
        //修改地址指令
        var editAddress = if (model == models[0]) {
            oldAddress + "06006400" + newAddress
        } else if (model == models[1]) {
            oldAddress + "06010000" + newAddress
        } else {
            // 不会出现这种
            oldAddress
        }
        editAddress += getCRC(editAddress)
        return editAddress == serialPortUtil.sendAndReceiveSerialData(
            this, editAddress
        )
    }

    override fun setBaudToNewBaud(model: String, oldAddress: String, newBaud: String): Boolean {
        //修改波特率指令
        //0:1200 1:2400 2:4800 3:9600 4:19200
        var newBaud = newBaud
        newBaud = when (newBaud) {
            "1200" -> "00"
            "2400" -> "01"
            "4800" -> "02"
            "9600" -> "03"
            "19200" -> "04"
            else -> return false
        }
        var editBaud = oldAddress + "06006500" + newBaud
        editBaud = editBaud + getCRC(editBaud)
        return editBaud == serialPortUtil.sendAndReceiveSerialData(
            this, editBaud
        )
    }
}
