package com.mes.config;
|
|
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
import com.mes.common.S7objectMachine;
|
import com.mes.device.PlcDevice;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.stereotype.Component;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* PLC设备配置类
|
*/
|
@Slf4j
|
@Configuration
|
@Component
|
public class DeviceConfig {
|
|
/**
|
* 定义PLC设备列表
|
*/
|
@Bean
|
@ConfigurationProperties(prefix = "plc.devices")
|
public List<PlcDevice> plcDevices() {
|
return new ArrayList<>();
|
}
|
|
/**
|
* 初始化S7objectMachine实例
|
*/
|
@Bean
|
public Map<String, S7objectMachine> s7objectMachines(List<PlcDevice> plcDevices) {
|
Map<String, S7objectMachine> machineMap = new HashMap<>();
|
|
for (PlcDevice device : plcDevices) {
|
try {
|
log.info("初始化PLC设备: {}, IP: {}, Port: {}", device.getName(), device.getIp(), device.getPort());
|
S7objectMachine s7objectMachine = new S7objectMachine(
|
device.getIp(),
|
device.getPort(),
|
device.getJsonFiles(),
|
EPlcType.valueOf(device.getPlcType())
|
);
|
machineMap.put(device.getName(), s7objectMachine);
|
} catch (Exception e) {
|
log.error("初始化PLC设备{}失败: {}", device.getName(), e.getMessage(), e);
|
}
|
}
|
|
return machineMap;
|
}
|
}
|