package com.example.iot_controlhost.model.sensor;

import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.utils.DataUtils;
import com.example.iot_controlhost.utils.MyUtil;

/**
 * 地面温度传感器
 */
public class FloorTSensor extends Sensor {
    /**
     * 温度
     */
    protected double temperature;

    public FloorTSensor(String name) {
        super(name);
    }

    @Override
    public boolean collectSensorData(String model, String address) {
        //获得指令
        String str = address + "0300000001";
        String crc = getCRC(str);
        str = str + crc;
        //利用指令获得数据
        String data = serialPortUtil.sendAndReceiveSerialData(this, str);
        //解析数据
        double wd;
        if (StringUtils.isEmpty(data)) {
            temperature = 0;
            return false;
        }
        String[] temp = substring(data, 2);
        if (DataUtils.CRC16Check(temp)) {
            wd = WDTransform(temp[3] + temp[4]);
        } else {
            return false;
        }
        temperature = wd > 100 ? 0 : wd;
        return true;
    }

    @Override
    public String[] getModels() {
        return new String[]{"RSDS19"};
    }


    public double getTemperature() {
        return temperature;
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }

    @Override
    public String toString() {
        return "温度：" + temperature + "℃";
    }

    /**
     * 温湿度转换
     */
    protected double WDTransform(String sixteen) {
        int ten = Integer.parseInt(sixteen, 16);
        return (double) ten / 10.0;
    }

    @Override
    public boolean setAddressToNewAddress(String model, String oldAddress,String oldBaud, String newAddress) {
        //修改地址指令
        String editAddress = oldAddress + "06000100" + newAddress;
        editAddress = editAddress + getCRC(editAddress);
        if (editAddress.equals(serialPortUtil.sendAndReceiveSerialData(this, editAddress))) {
            //重启设备以应用最新地址与波特率
            String reboot = newAddress + "06002555AA";
            reboot = reboot + getCRC(reboot);
            serialPortUtil.sendAndReceiveSerialData(this, reboot);
            return true;
        }
        return false;
    }

    @Override
    public boolean setBaudToNewBaud(String model, String oldAddress, String newBaud) {//修改波特率指令
        //从 0 开始，例如：数字为 5 时波特率为 9600
        //300,600,1200,2400,4800,9600,19200,38400,43000,56000,57600,115200
        switch (newBaud) {
            case "300":
                newBaud = "00";
                break;
            case "600":
                newBaud = "01";
                break;
            case "1200":
                newBaud = "02";
                break;
            case "2400":
                newBaud = "03";
                break;
            case "4800":
                newBaud = "04";
                break;
            case "9600":
                newBaud = "05";
                break;
            case "19200":
                newBaud = "06";
                break;
            case "38400":
                newBaud = "07";
                break;
            case "43000":
                newBaud = "08";
                break;
            case "56000":
                newBaud = "09";
                break;
            case "57600":
                newBaud = "0A";
                break;
            case "115200":
                newBaud = "0B";
                break;
            default:
                return false;
        }
        String editBaud = oldAddress + "06000300" + newBaud;
        editBaud = editBaud + getCRC(editBaud);
        if ( editBaud.equals(serialPortUtil.sendAndReceiveSerialData(this, editBaud))) {
            //重启设备以应用最新地址与波特率
            String reboot = oldAddress + "06002555AA";
            reboot = reboot + getCRC(reboot);
            serialPortUtil.sendAndReceiveSerialData(this, reboot);
            return true;
        }
        return false;
    }
}
