package com.sunzee.thread.advanceparameter;

import android.util.Log;

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;

/**
 * 进阶参数任务
 */
public class AdvanceParameterTask 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 AdvanceParameterTask(int type) {
        clearPlcQuee();
        this.type = type;
    }
    public AdvanceParameterTask(int type, String address, String data) {
        clearPlcQuee();
        this.type = type;
        this.address=address;
        this.data = data;
    }
    public AdvanceParameterTask(int type, String address, int id) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
        this.id = id;
    }


    public AdvanceParameterTask(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 AdvanceParameterThreadType.runD426:
                runReadD426();
                break;
            case AdvanceParameterThreadType.runD427:
                runReadD427();
                break;
            case AdvanceParameterThreadType.runD428:
                runReadD428();
                break;
            case AdvanceParameterThreadType.runD429:
                runReadD429();
                break;
            case AdvanceParameterThreadType.runD430:
                runReadD430();
                break;
            case AdvanceParameterThreadType.runD431:
                runReadD431();
                break;
            case AdvanceParameterThreadType.runWrite:
                runWrite();
                break;

        }
    }

    private static final String TAG = "AdvanceParameterTask";
    private void runWrite() {
        if (getIsRun()){
            Log.d(TAG, "runWrite: ");
            SerialPortManager.getInstance().sendCommandFrame(new WriteCommandFrame(address, data), address);
        }
    }

    private void runReadD426() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D426", 2), 17, "D426");
        }
    }

    private void runReadD427() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D427", 2), 17, "D427");
        }
    }

    private void runReadD428() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D428", 2), 17, "D428");
        }
    }

    private void runReadD429() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D429", 2), 17, "D429");
        }
    }

    private void runReadD430() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D430", 2), 17, "D430");
        }
    }

    private void runReadD431() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("D431", 2), 17, "D431");
        }
    }


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