package com.sunzee.thread.homepageA.homepage;

import com.hboxs.serialport.SerialPortManager;
import com.hboxs.serialport.SerialPortSendQueue;
import com.hboxs.serialport.frame.ReadCommandFrame;
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 HomepageATask 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 HomepageATask(int type) {
        clearPlcQuee();
        this.type = type;
    }

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

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

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

    public HomepageATask(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 HomepageAThreadType.runD169:
                runRead(2,169,"D169");
                break;
            case HomepageAThreadType.runD170:
                runRead(2,170,"D170");
                break;
            case HomepageAThreadType.runD172:
                runRead(2,172,"D172");
                break;

        }
    }


    private void runRead(int datLength,int id,String address) {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame(address, datLength), id, 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;
    }
}
