IotService.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.ruoyi.sim.service.my;
  2. import org.slf4j.LoggerFactory;
  3. import org.slf4j.Logger;
  4. import org.springframework.stereotype.Service;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.Socket;
  9. import static com.ruoyi.sim.service.my.IotService.Const.*;
  10. /**
  11. * 硬件通信
  12. */
  13. @Service
  14. public class IotService {
  15. interface Const {
  16. String IP = "127.0.0.1";
  17. int PORT = 9000;
  18. /**
  19. * 报文长度
  20. */
  21. int MSG_LENGTH = 20;
  22. String PREFIX = "AA";
  23. String SUFFIX = "55";
  24. /**
  25. * orientation
  26. */
  27. String ORN_SEND = "01";
  28. /**
  29. * orientation
  30. */
  31. String ORN_RECEIVE = "02";
  32. String CMD_DATA_PLACE_HOLDER = "00000000";
  33. String CMD_READ = "03";
  34. String CMD_GET_SIM_TYPE = "B1";
  35. String CMD_SET_FAULT = "01";
  36. String CMD_READ_SIM_FAULT_STATE = "03";
  37. String CMD_CLEAR_SIM_FAULT = "02";
  38. }
  39. private static final Logger logger = LoggerFactory.getLogger(IotService.class);
  40. private Socket socket = null;
  41. /**
  42. * 检查所有模拟器状态
  43. */
  44. public void checkAllSim() {
  45. }
  46. /**
  47. * 设备类型读取
  48. *
  49. * @param simNum 设备编号
  50. */
  51. public void getSimType(final String simNum) {
  52. // todo:check
  53. //
  54. StringBuffer m = new StringBuffer();
  55. m.append(PREFIX);
  56. m.append(simNum);
  57. m.append(ORN_SEND);
  58. m.append(CMD_READ);
  59. m.append(CMD_GET_SIM_TYPE);
  60. m.append(CMD_DATA_PLACE_HOLDER);
  61. m.append(SUFFIX);
  62. final String sendMsg = m.toString();
  63. final String receiveMsg = send(sendMsg);
  64. }
  65. /**
  66. * 故障下发
  67. *
  68. * @param simNum 设备编号
  69. * @param faultType 故障类型
  70. */
  71. public void setFault(final String simNum, final String faultType) {
  72. // todo:check
  73. //
  74. StringBuffer m = new StringBuffer();
  75. m.append(PREFIX);
  76. m.append(simNum);
  77. m.append(ORN_SEND);
  78. m.append(CMD_SET_FAULT);
  79. m.append(faultType);
  80. m.append(CMD_DATA_PLACE_HOLDER);
  81. m.append(SUFFIX);
  82. //
  83. //
  84. final String receiveMsg = send(m.toString());
  85. //
  86. }
  87. /**
  88. * 状态读取
  89. *
  90. * @param simNum 设备编号
  91. * @param faultType 故障类型
  92. */
  93. public void getSimStatus(final String simNum, final String faultType) {
  94. // todo:check
  95. //
  96. StringBuffer m = new StringBuffer();
  97. m.append(PREFIX);
  98. m.append(simNum);
  99. m.append(ORN_SEND);
  100. m.append(CMD_READ_SIM_FAULT_STATE);
  101. m.append(faultType);
  102. m.append(CMD_DATA_PLACE_HOLDER);
  103. m.append(SUFFIX);
  104. //
  105. //
  106. final String receiveMsg = send(m.toString());
  107. //
  108. }
  109. /**
  110. * 故障清清除
  111. *
  112. * @param simNum 设备编号
  113. * @param faultType 故障类型
  114. */
  115. public void clearSimStatus(final String simNum, final String faultType) {
  116. // todo:check
  117. //
  118. StringBuffer m = new StringBuffer();
  119. m.append(PREFIX);
  120. m.append(simNum);
  121. m.append(ORN_SEND);
  122. m.append(CMD_CLEAR_SIM_FAULT);
  123. m.append(faultType);
  124. m.append(CMD_DATA_PLACE_HOLDER);
  125. m.append(SUFFIX);
  126. //
  127. //
  128. final String receiveMsg = send(m.toString());
  129. //
  130. }
  131. /**
  132. * send hex message
  133. *
  134. * @param sendMsg
  135. * @return
  136. */
  137. public String send(final String sendMsg) {
  138. logger.info("sendMsg");
  139. logger.info(sendMsg);
  140. String receiveMsg = null;
  141. try {
  142. if (socket == null) {
  143. socket = new Socket(IP, PORT);
  144. }
  145. InputStream is = socket.getInputStream();
  146. OutputStream os = socket.getOutputStream();
  147. os.write(hexStrToByteArrs(sendMsg));
  148. byte[] buffer = new byte[1024];
  149. int length = is.read(buffer);
  150. StringBuffer sbHex = new StringBuffer();
  151. for (int i = 0; i < length; i++) {
  152. sbHex.append(String.format("%02X", buffer[i]));
  153. }
  154. receiveMsg = sbHex.toString();
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. }
  158. logger.info("receiveMsg");
  159. logger.info(receiveMsg);
  160. return receiveMsg;
  161. }
  162. /**
  163. *
  164. */
  165. public void close() {
  166. try {
  167. if (socket != null) {
  168. socket.close();
  169. }
  170. } catch (IOException e) {
  171. throw new RuntimeException(e);
  172. } finally {
  173. socket = null;
  174. }
  175. }
  176. public byte[] hexStrToByteArrs(String hexString) {
  177. // if (StringUtils.isEmpty(hexString)) {
  178. // return null;
  179. // }
  180. hexString = hexString.replaceAll(" ", "");
  181. int len = hexString.length();
  182. int index = 0;
  183. byte[] bytes = new byte[len / 2];
  184. while (index < len) {
  185. String sub = hexString.substring(index, index + 2);
  186. bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
  187. index += 2;
  188. }
  189. return bytes;
  190. }
  191. }