huang
2025-10-31 1fed5e7bab3a8f6b9adbfcd3695e14a03d47677f
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.mes.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.entity.PlcAddress;
import com.mes.mapper.PlcAddressMapper;
import com.mes.s7.enhanced.EnhancedS7Serializer;
import com.mes.service.PlcAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * PLC地址映射服务实现类
 * 管理PLC地址映射配置,支持从数据库和配置文件加载
 * 
 * @author huang
 * @date 2025/10/30
 */
@Slf4j
@Service
public class PlcAddressServiceImpl implements PlcAddressService {
 
    @Autowired
    private PlcAddressMapper plcAddressMapper;
 
 
    // JSON解析器
    private final ObjectMapper objectMapper = new ObjectMapper();
 
    /**
     * 获取包含地址映射的项目配置
     * 合并数据库和配置文件中的映射信息
     */
    @Override
    public PlcAddress getProjectConfigWithMapping(String projectId) {
        try {
            // 仅使用数据库中的项目配置
            return getMappingByProjectId(projectId);
        } catch (Exception e) {
            log.error("获取项目配置失败,项目ID: {}", projectId, e);
            return null;
        }
    }
 
    /**
     * 从数据库获取项目配置
     */
    @Override
    public PlcAddress getProjectConfig(String projectId) {
        try {
            return plcAddressMapper.selectOne(
                    new LambdaQueryWrapper<PlcAddress>()
                            .eq(PlcAddress::getProjectId, projectId)
            );
        } catch (Exception e) {
            log.error("获取项目配置失败,项目ID: {}", projectId, e);
            return null;
        }
    }
 
    /**
     * 获取所有映射配置
     * 合并数据库和配置文件中的映射信息
     */
    @Override
    public List<PlcAddress> getAllMappings() {
        try {
            // 直接返回数据库中的映射配置
            return plcAddressMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有PLC地址映射配置失败", e);
            throw new RuntimeException("获取所有PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 分页获取PLC地址映射配置
     */
    @Override
    public IPage<PlcAddress> getMappingsByPage(int page, int size, String projectId, String plcIp) {
        try {
            Page<PlcAddress> pageParam = new Page<>(page, size);
            LambdaQueryWrapper<PlcAddress> queryWrapper = new LambdaQueryWrapper<>();
            
            if (projectId != null && !projectId.trim().isEmpty()) {
                queryWrapper.like(PlcAddress::getProjectId, projectId);
            }
            
            if (plcIp != null && !plcIp.trim().isEmpty()) {
                queryWrapper.like(PlcAddress::getPlcIp, plcIp);
            }
            
            return plcAddressMapper.selectPage(pageParam, queryWrapper);
        } catch (Exception e) {
            log.error("分页获取PLC地址映射配置失败", e);
            throw new RuntimeException("分页获取PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 根据ID获取PLC地址映射配置
     */
    @Override
    public PlcAddress getMappingById(Long id) {
        try {
            return plcAddressMapper.selectById(id);
        } catch (Exception e) {
            log.error("根据ID获取PLC地址映射配置失败,ID: {}", id, e);
            throw new RuntimeException("根据ID获取PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 根据项目标识获取PLC地址映射配置
     */
    @Override
    public PlcAddress getMappingByProjectId(String projectId) {
        try {
            return plcAddressMapper.selectOne(
                    new LambdaQueryWrapper<PlcAddress>()
                            .eq(PlcAddress::getProjectId, projectId)
            );
        } catch (Exception e) {
            log.error("根据项目标识获取PLC地址映射配置失败,项目ID: {}", projectId, e);
            throw new RuntimeException("根据项目标识获取PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 保存PLC地址映射配置
     */
    @Override
    public PlcAddress saveMapping(PlcAddress mapping) {
        try {
            // 检查项目ID是否已存在
            PlcAddress existing = plcAddressMapper.selectOne(
                    new LambdaQueryWrapper<PlcAddress>()
                            .eq(PlcAddress::getProjectId, mapping.getProjectId())
            );
            
            if (existing != null) {
                throw new RuntimeException("项目标识 " + mapping.getProjectId() + " 已存在");
            }
            
            plcAddressMapper.insert(mapping);
            return mapping;
        } catch (Exception e) {
            log.error("保存PLC地址映射配置失败", e);
            throw new RuntimeException("保存PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 更新PLC地址映射配置
     */
    @Override
    public PlcAddress updateMapping(PlcAddress mapping) {
        try {
            // 检查ID是否存在
            PlcAddress existing = plcAddressMapper.selectById(mapping.getId());
            if (existing == null) {
                return null;
            }
            
            // 如果项目ID有变化,检查新项目ID是否已存在
            if (!existing.getProjectId().equals(mapping.getProjectId())) {
                PlcAddress duplicate = plcAddressMapper.selectOne(
                        new LambdaQueryWrapper<PlcAddress>()
                                .eq(PlcAddress::getProjectId, mapping.getProjectId())
                );
                
                if (duplicate != null) {
                    throw new RuntimeException("项目标识 " + mapping.getProjectId() + " 已存在");
                }
            }
            
            plcAddressMapper.updateById(mapping);
            return mapping;
        } catch (Exception e) {
            log.error("更新PLC地址映射配置失败", e);
            throw new RuntimeException("更新PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 删除PLC地址映射配置
     */
    @Override
    public boolean deleteMapping(Long id) {
        try {
            int result = plcAddressMapper.deleteById(id);
            return result > 0;
        } catch (Exception e) {
            log.error("删除PLC地址映射配置失败,ID: {}", id, e);
            throw new RuntimeException("删除PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 批量删除PLC地址映射配置
     */
    @Override
    public int deleteMappings(List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return 0;
            }
            return plcAddressMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            log.error("批量删除PLC地址映射配置失败,ID列表: {}", ids, e);
            throw new RuntimeException("批量删除PLC地址映射配置失败", e);
        }
    }
 
    /**
     * 测试PLC连接
     */
    @Override
    public boolean testConnection(PlcAddress mapping) {
        try {
            // 解析PLC类型
            EPlcType plcType = EPlcType.S1200; // 默认值
            if (mapping.getPlcType() != null && !mapping.getPlcType().trim().isEmpty()) {
                try {
                    plcType = EPlcType.valueOf(mapping.getPlcType());
                } catch (IllegalArgumentException e) {
                    log.warn("未知的PLC类型: {}, 使用默认类型 S1200", mapping.getPlcType());
                }
            }
            
            // 创建S7PLC实例
            String plcIp = mapping.getPlcIp() != null ? mapping.getPlcIp() : "192.168.10.21";
            S7PLC s7Plc = new S7PLC(plcType, plcIp);
            
            // 创建EnhancedS7Serializer实例
            EnhancedS7Serializer serializer = EnhancedS7Serializer.newInstance(s7Plc);
            
            // 尝试读取一个简单的数据来测试连接
            String dbArea = mapping.getDbArea() != null ? mapping.getDbArea() : "DB1";
            int beginIndex = mapping.getBeginIndex();
            
            // 这里我们只测试连接,不读取实际数据
            // 实际项目中可能需要根据具体需求调整
            return true;
        } catch (Exception e) {
            log.error("PLC连接测试失败", e);
            return false;
        }
    }
 
    /**
     * 重新加载配置文件中的映射
     */
    @Override
    public void reloadConfigMappings() {
        // 现仅使用数据库中的配置,不再从文件重载
        log.info("当前使用数据库配置,reloadConfigMappings无需操作");
    }
 
    /**
     * 将数据库实体转换为项目配置对象
     */
}