123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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;
- import com.hboxs.serialport.util.HexUtils;
- import static com.sunzee.thread.myservice.MyserviceThreadType.runX20;
- /**
- * 长时间或者推送要执行的任务
- */
- 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;
- case MyserviceThreadType.runD120:
- runWriteD120();
- break;
- case MyserviceThreadType.runDD120:
- runWriteDD120();
- break;
- case runX20:
- runX20();
- break;
- }
- }
- private void runWriteDD120() {
- if (getIsRun()) {
- SerialPortSendQueue.sendCommand(new WriteCommandFrame(address, HexUtils.hexStr2BinStr(data)), 120, "D"+address);
- }
- }
- private void runX20() {
- if (getIsRun()) {
- SerialPortSendQueue.sendCommand(new ReadCommandFrame("X20", 4), 20, "X20");
- }
- }
- 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 runWriteD120() {
- if (getIsRun()) {
- SerialPortSendQueue.sendCommand(new WriteCommandFrame(address, HexUtils.hexStr2BinStr(data)), 120, address);
- }
- }
- 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;
- }
- }
|