DeviceStateReporter.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.mes.device;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.util.HttpUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.BufferedReader;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Properties;
  10. /**
  11. * 工位客户端设备状态上报(按 gw + lineSn)
  12. */
  13. public class DeviceStateReporter {
  14. public static final Logger log = LoggerFactory.getLogger(DeviceStateReporter.class);
  15. /** 正常运行 */
  16. public static final String STATE_RUNNING = "1";
  17. /** 工位退出/收班 */
  18. public static final String STATE_STOP = "7";
  19. private static DeviceStateReporter instance;
  20. private String serverIp;
  21. private String gw;
  22. private String lineSn;
  23. private DeviceStateReporter() {
  24. }
  25. public static DeviceStateReporter getInstance() {
  26. if (instance == null) {
  27. synchronized (DeviceStateReporter.class) {
  28. if (instance == null) {
  29. instance = new DeviceStateReporter();
  30. }
  31. }
  32. }
  33. return instance;
  34. }
  35. /**
  36. * 从配置文件初始化
  37. */
  38. public void initFromConfig() {
  39. try {
  40. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  41. Properties pro = new Properties();
  42. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  43. pro.load(br);
  44. br.close();
  45. is.close();
  46. init(
  47. pro.getProperty("mes.server_ip", "").trim(),
  48. pro.getProperty("mes.gw", "").trim(),
  49. pro.getProperty("mes.line_sn", "").trim()
  50. );
  51. } catch (Exception e) {
  52. log.error("初始化设备状态上报失败", e);
  53. }
  54. }
  55. public void init(String serverIp, String gw, String lineSn) {
  56. this.serverIp = serverIp;
  57. this.gw = gw;
  58. this.lineSn = lineSn;
  59. log.info("设备状态上报初始化: gw={}, lineSn={}", gw, lineSn);
  60. }
  61. /**
  62. * 异步上报
  63. */
  64. public void reportAsync(final String state, final String content) {
  65. new Thread(new Runnable() {
  66. @Override
  67. public void run() {
  68. reportSync(state, content);
  69. }
  70. }, "device-state-report-thread").start();
  71. }
  72. /**
  73. * 同步上报
  74. */
  75. public boolean reportSync(String state, String content) {
  76. if (isBlank(serverIp) || isBlank(gw) || isBlank(lineSn)) {
  77. log.warn("设备状态上报跳过:配置不完整 serverIp={}, gw={}, lineSn={}", serverIp, gw, lineSn);
  78. return false;
  79. }
  80. try {
  81. String url = "http://" + serverIp + ":8980/js/a/mes/mesDevice/reportState";
  82. JSONObject body = new JSONObject();
  83. body.put("gw", gw);
  84. body.put("lineSn", lineSn);
  85. body.put("state", state);
  86. body.put("content", content == null ? "" : content);
  87. String result = HttpUtils.sendPostRequestJson(url, body.toJSONString());
  88. log.info("设备状态上报: state={}, content={}, result={}", state, content, result);
  89. if (isBlank(result) || "false".equalsIgnoreCase(result)) {
  90. return false;
  91. }
  92. JSONObject retObj = JSONObject.parseObject(result);
  93. return retObj != null && "true".equalsIgnoreCase(String.valueOf(retObj.get("result")));
  94. } catch (Exception e) {
  95. log.error("设备状态上报异常: state={}, content={}", state, content, e);
  96. return false;
  97. }
  98. }
  99. private boolean isBlank(String value) {
  100. return value == null || value.trim().isEmpty();
  101. }
  102. }