package com.sunzee.thread.debugparameter;

import com.hboxs.serialport.SerialPortSendQueue;
import com.hboxs.serialport.frame.ReadCommandFrame;
import com.hboxs.serialport.frame.RstCommandFrame;
import com.hboxs.serialport.frame.SetCommandFrame;

import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runRst;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runSet;

/**
 * 调试页面参数任务
 */
public class DebugParameterTask implements Runnable {

    private int type;
    private volatile String address;

    public String getAddress() {
        return address;
    }

    public int getId() {
        return id;
    }

    private volatile int id;
    private volatile boolean isRun;
    private String name;
    private String data;

    public DebugParameterTask(int type) {
        clearPlcQuee();
        this.type = type;
    }

    public DebugParameterTask(int type, String address) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
    }

    public DebugParameterTask(int type, String address, int id) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
        this.id = id;
    }


    public DebugParameterTask(int type, String address, String data, int id, String name) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
        this.data = data;
        this.id = id;
        this.name = name;
    }

    private void clearPlcQuee() {
        setIsRun(true);
        SerialPortSendQueue.clear();
    }

    public void setIsRun(boolean isRun) {
        this.isRun = isRun;
    }

    public boolean getIsRun() {
        return isRun;
    }

    @Override
    public void run() {
        switch (type) {
            case DebugParameterThreadType.runM311:
                runReadM311();
                break;
            case runRst:
                runRst();
                break;
            case runSet:
                runSet();
                break;

        }
    }

    private void runReadM311() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M311", 2), 311, "M311");
        }
    }

    private void runRst() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new RstCommandFrame(getAddress()), 311, getAddress());
        }
    }

    private void runSet() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new SetCommandFrame(getAddress()), 311, getAddress());

        }
    }

    public void setAddressAndId(String address, int id) {
        this.address = address;
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
