huang
2025-05-22 16b32f4511aa90b95b13a15751850cf6db0829e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
    }