123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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());
- }
- }
|