StartupCommandLineRunner.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.gzlh.startup;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.thread.ThreadUtil;
  4. import cn.hutool.json.JSONObject;
  5. import cn.hutool.json.XML;
  6. import com.gzlh.background.client.BackgroundClientNetty;
  7. import com.gzlh.background.properties.BackgroundPropertiesConfig;
  8. import com.gzlh.dto.ApplicationConfigDTO;
  9. import com.gzlh.event.EventBus;
  10. import com.gzlh.led.client.LedNettyConfig;
  11. import com.gzlh.led.properties.LedPropertiesConfig;
  12. import com.gzlh.infrared.client.RedNettyConfig;
  13. import com.gzlh.infrared.config.RedPropertiesConfig;
  14. import com.gzlh.socket.TestClientNetty;
  15. import com.gzlh.weighbridge.client.WeighbridgeNettyConfig;
  16. import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig;
  17. import javafx.scene.layout.Background;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.boot.CommandLineRunner;
  20. import org.springframework.stereotype.Component;
  21. import javax.annotation.Resource;
  22. import java.io.File;
  23. import java.util.Properties;
  24. @Component
  25. @Slf4j
  26. public class StartupCommandLineRunner implements CommandLineRunner {
  27. @Resource
  28. private EventBus eventBus;
  29. @Resource
  30. private WeighbridgeNettyConfig weighbridgeNettyConfig;
  31. @Resource
  32. private WeighbridgePropertiesConfig propertiesConfig;
  33. @Resource
  34. private LedNettyConfig ledNettyConfig;
  35. @Resource
  36. private LedPropertiesConfig ledConfig;
  37. @Resource
  38. private RedNettyConfig redNettyConfig;
  39. @Resource
  40. private RedPropertiesConfig redConfig;
  41. @Resource
  42. private BackgroundPropertiesConfig backgroundPropertiesConfig;
  43. @Resource
  44. private BackgroundClientNetty backgroundClientNetty;
  45. @Override
  46. public void run(String... args) throws Exception {
  47. initConfig();
  48. if (propertiesConfig.isEnable()) {
  49. ThreadUtil.execute(() -> weighbridgeNettyConfig.connect());
  50. }
  51. if (ledConfig.isEnable()) {
  52. ThreadUtil.execute(() -> ledNettyConfig.connect());
  53. }
  54. if (redConfig.isEnable()) {
  55. ThreadUtil.execute(() -> redNettyConfig.connect());
  56. }
  57. if (backgroundPropertiesConfig.isEnable()) {
  58. ThreadUtil.execute(() -> backgroundClientNetty.connect());
  59. }
  60. }
  61. private void initConfig() {
  62. Properties properties = System.getProperties();
  63. String rootPath = properties.getProperty("user.dir");
  64. String separator = File.separator;
  65. String configFile = rootPath + separator + "config.xml";
  66. if (!FileUtil.exist(configFile)) {
  67. log.error("配置文件不存在:{}", configFile);
  68. return;
  69. }
  70. String content = FileUtil.readUtf8String(configFile);
  71. JSONObject result = XML.toJSONObject(content);
  72. ApplicationConfigDTO configDTO = result.toBean(ApplicationConfigDTO.class);
  73. eventBus.setEventDTOList(configDTO.getConfig().getEventList().getEvent());
  74. }
  75. }