ModbusAutoConfiguration.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.jwk.spring.boot.autoconfigure;
  2. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  3. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * @author JWK
  8. * @version 1.0
  9. * @date 2022/12/2 14:46
  10. */
  11. @EnableConfigurationProperties(ModbusProperties.class)
  12. @Configuration
  13. public class ModbusAutoConfiguration {
  14. /**
  15. * RTU-Master first
  16. * 注入方式@Autowired(required = true) private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
  17. * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
  18. *
  19. * @param modbusProperties
  20. * @return
  21. */
  22. @Bean
  23. @ConditionalOnProperty(prefix = "modbus.rtu-master.first", name = "open", havingValue = "true")
  24. public ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst(ModbusProperties modbusProperties) {
  25. ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFirst();
  26. return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
  27. , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
  28. }
  29. /**
  30. * RTU-Master second
  31. *
  32. * @param modbusProperties
  33. * @return
  34. */
  35. @Bean
  36. @ConditionalOnProperty(prefix = "modbus.rtu-master.second", name = "open", havingValue = "true")
  37. public ModbusRtuMasterTemplate modbusRtuMasterTemplateSecond(ModbusProperties modbusProperties) {
  38. ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getSecond();
  39. return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
  40. , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
  41. }
  42. /**
  43. * RTU-Master third
  44. *
  45. * @param modbusProperties
  46. * @return
  47. */
  48. @Bean
  49. @ConditionalOnProperty(prefix = "modbus.rtu-master.third", name = "open", havingValue = "true")
  50. public ModbusRtuMasterTemplate modbusRtuMasterTemplateThird(ModbusProperties modbusProperties) {
  51. ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getThird();
  52. return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
  53. , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
  54. }
  55. /**
  56. * RTU-Master fourth
  57. *
  58. * @param modbusProperties
  59. * @return
  60. */
  61. @Bean
  62. @ConditionalOnProperty(prefix = "modbus.rtu-master.fourth", name = "open", havingValue = "true")
  63. public ModbusRtuMasterTemplate modbusRtuMasterTemplateFourth(ModbusProperties modbusProperties) {
  64. ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFourth();
  65. return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
  66. , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
  67. }
  68. /**
  69. * RTU-Master fifth
  70. *
  71. * @param modbusProperties
  72. * @return
  73. */
  74. @Bean
  75. @ConditionalOnProperty(prefix = "modbus.rtu-master.fifth", name = "open", havingValue = "true")
  76. public ModbusRtuMasterTemplate modbusRtuMasterTemplateFifth(ModbusProperties modbusProperties) {
  77. ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFifth();
  78. return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
  79. , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
  80. }
  81. /**
  82. * TCP-Master first
  83. *
  84. * @param modbusProperties
  85. * @return
  86. */
  87. @Bean
  88. @ConditionalOnProperty(prefix = "modbus.tcp-master.first", name = "open", havingValue = "true")
  89. public ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst(ModbusProperties modbusProperties) {
  90. ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFirst();
  91. return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
  92. }
  93. /**
  94. * TCP-Master second
  95. *
  96. * @param modbusProperties
  97. * @return
  98. */
  99. @Bean
  100. @ConditionalOnProperty(prefix = "modbus.tcp-master.second", name = "open", havingValue = "true")
  101. public ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond(ModbusProperties modbusProperties) {
  102. ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getSecond();
  103. return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
  104. }
  105. /**
  106. * TCP-Master third
  107. *
  108. * @param modbusProperties
  109. * @return
  110. */
  111. @Bean
  112. @ConditionalOnProperty(prefix = "modbus.tcp-master.third", name = "open", havingValue = "true")
  113. public ModbusTcpMasterTemplate modbusTcpMasterTemplateThird(ModbusProperties modbusProperties) {
  114. ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getThird();
  115. return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
  116. }
  117. /**
  118. * TCP-Master fourth
  119. *
  120. * @param modbusProperties
  121. * @return
  122. */
  123. @Bean
  124. @ConditionalOnProperty(prefix = "modbus.tcp-master.fourth", name = "open", havingValue = "true")
  125. public ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth(ModbusProperties modbusProperties) {
  126. ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFourth();
  127. return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
  128. }
  129. /**
  130. * TCP-Master fifth
  131. *
  132. * @param modbusProperties
  133. * @return
  134. */
  135. @Bean
  136. @ConditionalOnProperty(prefix = "modbus.tcp-master.fifth", name = "open", havingValue = "true")
  137. public ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth(ModbusProperties modbusProperties) {
  138. ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFifth();
  139. return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
  140. }
  141. }