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 {


    @Bean
    @ConditionalOnProperty(prefix = "modbus.rtu-master", name = "open", havingValue = "true")
    public ModbusRtuMasterTemplate modbusRtuMasterTemplate(ModbusProperties modbusProperties) {
        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster();
        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
    }

    @Bean
    @ConditionalOnProperty(prefix = "modbus.tcp-master", name = "open", havingValue = "true")
    public ModbusTcpMasterTemplate modbusTcpMasterTemplate(ModbusProperties modbusProperties) {
        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster();
        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
    }
}
