1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package com.gzlh.startup;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.thread.ThreadUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.XML;
- import com.gzlh.background.client.BackgroundClientNetty;
- import com.gzlh.background.properties.BackgroundPropertiesConfig;
- import com.gzlh.dto.ApplicationConfigDTO;
- import com.gzlh.event.EventBus;
- import com.gzlh.led.client.LedNettyConfig;
- import com.gzlh.led.properties.LedPropertiesConfig;
- import com.gzlh.infrared.client.RedNettyConfig;
- import com.gzlh.infrared.config.RedPropertiesConfig;
- import com.gzlh.socket.TestClientNetty;
- import com.gzlh.weighbridge.client.WeighbridgeNettyConfig;
- import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig;
- import javafx.scene.layout.Background;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.io.File;
- import java.util.Properties;
- @Component
- @Slf4j
- public class StartupCommandLineRunner implements CommandLineRunner {
- @Resource
- private EventBus eventBus;
- @Resource
- private WeighbridgeNettyConfig weighbridgeNettyConfig;
- @Resource
- private WeighbridgePropertiesConfig propertiesConfig;
- @Resource
- private LedNettyConfig ledNettyConfig;
- @Resource
- private LedPropertiesConfig ledConfig;
- @Resource
- private RedNettyConfig redNettyConfig;
- @Resource
- private RedPropertiesConfig redConfig;
- @Resource
- private BackgroundPropertiesConfig backgroundPropertiesConfig;
- @Resource
- private BackgroundClientNetty backgroundClientNetty;
- @Override
- public void run(String... args) throws Exception {
- initConfig();
- if (propertiesConfig.isEnable()) {
- ThreadUtil.execute(() -> weighbridgeNettyConfig.connect());
- }
- if (ledConfig.isEnable()) {
- ThreadUtil.execute(() -> ledNettyConfig.connect());
- }
- if (redConfig.isEnable()) {
- ThreadUtil.execute(() -> redNettyConfig.connect());
- }
- if (backgroundPropertiesConfig.isEnable()) {
- ThreadUtil.execute(() -> backgroundClientNetty.connect());
- }
- }
- private void initConfig() {
- Properties properties = System.getProperties();
- String rootPath = properties.getProperty("user.dir");
- String separator = File.separator;
- String configFile = rootPath + separator + "config.xml";
- if (!FileUtil.exist(configFile)) {
- log.error("配置文件不存在:{}", configFile);
- return;
- }
- String content = FileUtil.readUtf8String(configFile);
- JSONObject result = XML.toJSONObject(content);
- ApplicationConfigDTO configDTO = result.toBean(ApplicationConfigDTO.class);
- eventBus.setEventDTOList(configDTO.getConfig().getEventList().getEvent());
- }
- }
|