package com.sunzee.thread.myservice;

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 MyserviceTask 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 MyserviceTask(int type) {
        clearPlcQuee();
        this.type = type;
    }

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

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

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

    public MyserviceTask(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 MyserviceThreadType.runM600:
                runReadM600();
                break;
            case MyserviceThreadType.runM8:
                runSetM8();
                break;
            case MyserviceThreadType.runM17:
                runSetM17();
                break;
            case MyserviceThreadType.runRst:
                runRst();
                break;
            case MyserviceThreadType.runSet:
                runSet();
                break;
            case MyserviceThreadType.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 runSetM17() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new SetCommandFrame("M17"), 17, "M17");
        }
    }

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

    private void runReadM600() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M600", 8), 600, "M600");
        }
    }

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