package com.jwk.spring.boot.autoconfigure; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author JWK * @version 1.0 * @date 2022/12/2 14:46 */ @EnableConfigurationProperties(ModbusProperties.class) @Configuration public class ModbusAutoConfiguration { /** * RTU-Master first * 注入方式@Autowired(required = true) private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst; * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行. * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.rtu-master.first", name = "open", havingValue = "true") public ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst(ModbusProperties modbusProperties) { ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFirst(); return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits() , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut()); } /** * RTU-Master second * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.rtu-master.second", name = "open", havingValue = "true") public ModbusRtuMasterTemplate modbusRtuMasterTemplateSecond(ModbusProperties modbusProperties) { ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getSecond(); return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits() , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut()); } /** * RTU-Master third * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.rtu-master.third", name = "open", havingValue = "true") public ModbusRtuMasterTemplate modbusRtuMasterTemplateThird(ModbusProperties modbusProperties) { ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getThird(); return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits() , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut()); } /** * RTU-Master fourth * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.rtu-master.fourth", name = "open", havingValue = "true") public ModbusRtuMasterTemplate modbusRtuMasterTemplateFourth(ModbusProperties modbusProperties) { ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFourth(); return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits() , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut()); } /** * RTU-Master fifth * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.rtu-master.fifth", name = "open", havingValue = "true") public ModbusRtuMasterTemplate modbusRtuMasterTemplateFifth(ModbusProperties modbusProperties) { ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFifth(); return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits() , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut()); } /** * TCP-Master first * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.tcp-master.first", name = "open", havingValue = "true") public ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst(ModbusProperties modbusProperties) { ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFirst(); return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort()); } /** * TCP-Master second * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.tcp-master.second", name = "open", havingValue = "true") public ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond(ModbusProperties modbusProperties) { ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getSecond(); return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort()); } /** * TCP-Master third * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.tcp-master.third", name = "open", havingValue = "true") public ModbusTcpMasterTemplate modbusTcpMasterTemplateThird(ModbusProperties modbusProperties) { ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getThird(); return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort()); } /** * TCP-Master fourth * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.tcp-master.fourth", name = "open", havingValue = "true") public ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth(ModbusProperties modbusProperties) { ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFourth(); return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort()); } /** * TCP-Master fifth * * @param modbusProperties * @return */ @Bean @ConditionalOnProperty(prefix = "modbus.tcp-master.fifth", name = "open", havingValue = "true") public ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth(ModbusProperties modbusProperties) { ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFifth(); return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort()); } }