package com.sunzee.thread.homepage;

import com.hboxs.serialport.SerialPortManager;
import com.hboxs.serialport.SerialPortSendQueue;
import com.hboxs.serialport.frame.RstCommandFrame;
import com.hboxs.serialport.frame.SetCommandFrame;
import com.hboxs.serialport.frame.WriteCommandFrame;
import com.hboxs.serialport.util.HexUtils;

/**
 * 长时间或者推送要执行的任务
 */
public class HomepageTask 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 HomepageTask(int type) {
        clearPlcQuee();
        this.type = type;
    }

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

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

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

    public HomepageTask(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 HomepageThreadType.runD120:
                runWriteD120();
                break;
            case HomepageThreadType.runM8:
                runSetM8();
                break;
            case HomepageThreadType.runRst:
                runRst();
                break;
            case HomepageThreadType.runSet:
                runSet();
                break;
            case HomepageThreadType.runWrite:
                runWrite();
                break;

        }
    }

    private void runWrite() {
        if (getIsRun()) {
            SerialPortManager.getInstance().sendCommandFrame(new WriteCommandFrame(address, data), address);
        }
    }

    private void runSet() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new SetCommandFrame(address), 18, address);
        }
    }

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

    private void runSetM8() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new SetCommandFrame("M8"), 8, "M8");
        }
    }

    private void runWriteD120() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new WriteCommandFrame(address, HexUtils.hexStr2BinStr(data)), 120, address);
        }
    }

    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;
    }
}
