huang
2 天以前 14763d895151f3ddad09906f2233057b8b967881
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.mes.plc.client.impl;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.mes.device.entity.DeviceConfig;
import com.mes.plc.client.PlcClient;
import com.mes.service.PlcDynamicDataService;
import com.mes.s7.enhanced.EnhancedS7Serializer;
import lombok.extern.slf4j.Slf4j;
 
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * S7协议PLC客户端实现
 * <p>
 * 基于现有的S7库实现PlcClient接口
 * </p>
 *
 * @author huang
 * @date 2025/12/19
 */
@Slf4j
public class S7PlcClient implements PlcClient {
    
    // PLC类型
    private final EPlcType plcType;
    
    // PLC IP地址
    private final String plcIp;
    
    // PLC端口
    private final int plcPort;
    
    // 设备配置
    private final DeviceConfig device;
    
    // 模块名称
    private final String moduleName;
    
    // S7 PLC实例
    private S7PLC s7Plc;
    
    // S7序列化器
    private EnhancedS7Serializer s7Serializer;
    
    // 连接状态
    private boolean connected = false;
    
    // 超时时间(毫秒)
    private int timeout = 5000;
    
    // PLC类型映射
    private static final Map<String, EPlcType> PLC_TYPE_MAP = new ConcurrentHashMap<>();
    
    // PLC动态数据服务
    private PlcDynamicDataService plcDynamicDataService;
    
    /**
     * 设置PLC动态数据服务
     *
     * @param plcDynamicDataService PLC动态数据服务实例
     */
    public void setPlcDynamicDataService(PlcDynamicDataService plcDynamicDataService) {
        this.plcDynamicDataService = plcDynamicDataService;
    }
    
    static {
        // 初始化PLC类型映射
        PLC_TYPE_MAP.put("S7_1200", EPlcType.S1200);
        PLC_TYPE_MAP.put("S7_1500", EPlcType.S1500);
        PLC_TYPE_MAP.put("S7_200", EPlcType.S200);
        PLC_TYPE_MAP.put("S7_300", EPlcType.S300);
        PLC_TYPE_MAP.put("S7_400", EPlcType.S400);
    }
    
    /**
     * 构造函数
     *
     * @param device 设备配置
     * @param s7Serializer S7序列化器
     */
    public S7PlcClient(DeviceConfig device, EnhancedS7Serializer s7Serializer) {
        this.device = device;
        this.plcIp = device.getPlcIp();
        this.plcPort = device.getPlcPort() != null ? device.getPlcPort() : 102;
        this.moduleName = device.getModuleName() != null ? device.getModuleName() : "DB1";
        this.s7Serializer = s7Serializer;
        
        // 解析PLC类型
        String plcTypeValue = device.getPlcType();
        this.plcType = PLC_TYPE_MAP.getOrDefault(plcTypeValue, EPlcType.S1200);
    }
    
    @Override
    public boolean connect() {
        try {
            if (s7Plc != null && isConnected()) {
                return true;
            }
            
            // 创建S7PLC实例
            this.s7Plc = new S7PLC(this.plcType, this.plcIp, this.plcPort);
  
            
            // 连接PLC
            this.s7Plc.connect();
            this.connected = true;
            log.info("S7 PLC连接成功: {}:{},类型: {}", this.plcIp, this.plcPort, this.plcType);
            return true;
        } catch (Exception e) {
            log.error("S7 PLC连接失败: {}:{}", this.plcIp, this.plcPort, e);
            this.connected = false;
            return false;
        }
    }
    
    @Override
    public void disconnect() {
        try {
            if (this.s7Plc != null) {
          
                log.info("S7 PLC断开连接: {}:{}", this.plcIp, this.plcPort);
            }
        } catch (Exception e) {
            log.error("S7 PLC断开连接失败: {}:{}", this.plcIp, this.plcPort, e);
        } finally {
            this.connected = false;
            this.s7Plc = null;
        }
    }
    
    @Override
    public Map<String, Object> readAllData() {
        if (!isConnected() && !connect()) {
            log.error("S7 PLC未连接,无法读取数据: {}:{}", this.plcIp, this.plcPort);
            return Collections.emptyMap();
        }
        
        try {
            if (this.plcDynamicDataService == null) {
                log.error("PLC动态数据服务未初始化,无法读取数据: {}:{}", this.plcIp, this.plcPort);
                return Collections.emptyMap();
            }
            
            // 使用PlcDynamicDataService读取所有PLC数据
            Map<String, Object> data = this.plcDynamicDataService.readAllPlcData(this.device, this.s7Serializer);
            if (data == null) {
                log.warn("PLC动态数据服务返回空数据: {}:{}", this.plcIp, this.plcPort);
                return new HashMap<>();
            }
            
            log.info("S7 PLC读取所有数据成功: {}:{}, dataSize={}", this.plcIp, this.plcPort, data.size());
            return data;
        } catch (Exception e) {
            log.error("S7 PLC读取所有数据失败: {}:{}", this.plcIp, this.plcPort, e);
            this.connected = false;
            return Collections.emptyMap();
        }
    }
    
    @Override
    public Map<String, Object> readData(String... fields) {
        if (!isConnected() && !connect()) {
            log.error("S7 PLC未连接,无法读取数据: {}:{}", this.plcIp, this.plcPort);
            return Collections.emptyMap();
        }
        
        try {
            if (this.plcDynamicDataService == null) {
                log.error("PLC动态数据服务未初始化,无法读取数据: {}:{}", this.plcIp, this.plcPort);
                return Collections.emptyMap();
            }
            
            if (fields == null || fields.length == 0) {
                return readAllData();
            }
            
            // 使用PlcDynamicDataService读取指定字段PLC数据
            Map<String, Object> data = this.plcDynamicDataService.readPlcData(this.device, java.util.Arrays.asList(fields), this.s7Serializer);
            if (data == null) {
                log.warn("PLC动态数据服务返回空数据: {}:{}", this.plcIp, this.plcPort);
                return new HashMap<>();
            }
            
            log.info("S7 PLC读取指定字段数据成功: {}:{}, fields={}", this.plcIp, this.plcPort, java.util.Arrays.toString(fields));
            return data;
        } catch (Exception e) {
            log.error("S7 PLC读取数据失败: {}:{}", this.plcIp, this.plcPort, e);
            this.connected = false;
            return Collections.emptyMap();
        }
    }
    
    @Override
    public boolean writeData(Map<String, Object> data) {
        if (!isConnected() && !connect()) {
            log.error("S7 PLC未连接,无法写入数据: {}:{}", this.plcIp, this.plcPort);
            return false;
        }
        
        try {
            if (this.plcDynamicDataService == null) {
                log.error("PLC动态数据服务未初始化,无法写入数据: {}:{}", this.plcIp, this.plcPort);
                return false;
            }
            
            if (data == null || data.isEmpty()) {
                log.warn("写入数据为空,跳过操作: {}:{}", this.plcIp, this.plcPort);
                return true;
            }
            
            // 使用PlcDynamicDataService写入PLC数据
            this.plcDynamicDataService.writePlcData(this.device, data, this.s7Serializer);
            log.info("S7 PLC写入数据成功: {}:{}, fields={}", this.plcIp, this.plcPort, data.keySet());
            return true;
        } catch (Exception e) {
            log.error("S7 PLC写入数据失败: {}:{}", this.plcIp, this.plcPort, e);
            this.connected = false;
            return false;
        }
    }
    
    @Override
    public boolean isConnected() {
        try {
            // S7PLC不支持直接检查连接状态,使用内部标志
            return this.connected && this.s7Plc != null;
        } catch (Exception e) {
            this.connected = false;
            return false;
        }
    }
    
    @Override
    public String getPlcType() {
        return this.plcType.name();
    }
    
    @Override
    public int getTimeout() {
        return this.timeout;
    }
    
    @Override
    public void setTimeout(int timeout) {
        this.timeout = timeout;
   
    }
}