MyserviceTask.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.sunzee.thread.myservice;
  2. import android.util.Log;
  3. import com.hboxs.serialport.SerialPortManager;
  4. import com.hboxs.serialport.SerialPortSendQueue;
  5. import com.hboxs.serialport.frame.ReadCommandFrame;
  6. import com.hboxs.serialport.frame.RstCommandFrame;
  7. import com.hboxs.serialport.frame.SetCommandFrame;
  8. import com.hboxs.serialport.frame.WriteCommandFrame;
  9. import com.hboxs.serialport.util.HexUtils;
  10. import static com.sunzee.thread.myservice.MyserviceThreadType.runX20;
  11. /**
  12. * 长时间或者推送要执行的任务
  13. */
  14. public class MyserviceTask implements Runnable {
  15. private int type;
  16. private volatile String address;
  17. public String getAddress() {
  18. return address;
  19. }
  20. public int getId() {
  21. return id;
  22. }
  23. private volatile int id;
  24. private volatile boolean isRun;
  25. private String name;
  26. private String data;
  27. public MyserviceTask(int type) {
  28. clearPlcQuee();
  29. this.type = type;
  30. }
  31. public MyserviceTask(int type, String address) {
  32. clearPlcQuee();
  33. this.type = type;
  34. this.address = address;
  35. }
  36. public MyserviceTask(int type, String address, int id) {
  37. clearPlcQuee();
  38. this.type = type;
  39. this.address = address;
  40. this.id = id;
  41. }
  42. public MyserviceTask(int type, String address, String data) {
  43. clearPlcQuee();
  44. this.type = type;
  45. this.address = address;
  46. this.data = data;
  47. }
  48. public MyserviceTask(int type, String address, String data, int id, String name) {
  49. clearPlcQuee();
  50. this.type = type;
  51. this.address = address;
  52. this.data = data;
  53. this.id = id;
  54. this.name = name;
  55. }
  56. private void clearPlcQuee() {
  57. setIsRun(true);
  58. SerialPortSendQueue.clear();
  59. }
  60. public void setIsRun(boolean isRun) {
  61. this.isRun = isRun;
  62. }
  63. public boolean getIsRun() {
  64. return isRun;
  65. }
  66. @Override
  67. public void run() {
  68. switch (type) {
  69. case MyserviceThreadType.runM600:
  70. runReadM600();
  71. break;
  72. case MyserviceThreadType.runM8:
  73. runSetM8();
  74. break;
  75. case MyserviceThreadType.runM17:
  76. runSetM17();
  77. break;
  78. case MyserviceThreadType.runRst:
  79. runRst();
  80. break;
  81. case MyserviceThreadType.runSet:
  82. runSet();
  83. break;
  84. case MyserviceThreadType.runWrite:
  85. runWrite();
  86. break;
  87. case MyserviceThreadType.runD120:
  88. runWriteD120();
  89. break;
  90. case MyserviceThreadType.runDD120:
  91. runWriteDD120();
  92. break;
  93. case runX20:
  94. runX20();
  95. break;
  96. }
  97. }
  98. private void runWriteDD120() {
  99. if (getIsRun()) {
  100. SerialPortSendQueue.sendCommand(new WriteCommandFrame(address, HexUtils.hexStr2BinStr(data)), 120, "D"+address);
  101. }
  102. }
  103. private void runX20() {
  104. if (getIsRun()) {
  105. SerialPortSendQueue.sendCommand(new ReadCommandFrame("X20", 4), 20, "X20");
  106. }
  107. }
  108. private void runWrite() {
  109. if (getIsRun()) {
  110. SerialPortManager.getInstance().sendCommandFrame(new WriteCommandFrame(address, data), address);
  111. }
  112. }
  113. private void runSet() {
  114. if (getIsRun()) {
  115. SerialPortSendQueue.sendCommand(new SetCommandFrame(address), 18, address);
  116. }
  117. }
  118. private void runRst() {
  119. if (getIsRun()) {
  120. SerialPortSendQueue.sendCommand(new RstCommandFrame(address), 18, address);
  121. }
  122. }
  123. private void runSetM17() {
  124. if (getIsRun()) {
  125. SerialPortSendQueue.sendCommand(new SetCommandFrame("M17"), 17, "M17");
  126. }
  127. }
  128. private void runWriteD120() {
  129. if (getIsRun()) {
  130. SerialPortSendQueue.sendCommand(new WriteCommandFrame(address, HexUtils.hexStr2BinStr(data)), 120, address);
  131. }
  132. }
  133. private void runSetM8() {
  134. if (getIsRun()) {
  135. SerialPortSendQueue.sendCommand(new SetCommandFrame("M8"), 8, "M8");
  136. }
  137. }
  138. private void runReadM600() {
  139. if (getIsRun()) {
  140. SerialPortSendQueue.sendCommand(new ReadCommandFrame("M600", 8), 600, "M600");
  141. }
  142. }
  143. public void setAddressAndId(String address, int id) {
  144. this.address = address;
  145. this.id = id;
  146. }
  147. public void setAddress(String address) {
  148. this.address = address;
  149. }
  150. public void setId(int id) {
  151. this.id = id;
  152. }
  153. public String getName() {
  154. return name;
  155. }
  156. public void setName(String name) {
  157. this.name = name;
  158. }
  159. public String getData() {
  160. return data;
  161. }
  162. public void setData(String data) {
  163. this.data = data;
  164. }
  165. }