cuikaidong
2025-06-12 44e283b774bb1168d0c17dfe5070a1ca8e2274cd
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package org.jeecg.modules.iot.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.iot.depository.DbConfig;
import org.jeecg.modules.iot.depository.InfluxdbTest;
import org.jeecg.modules.iot.depository.MysqlDataWriter;
import org.jeecg.modules.iot.entity.Equipment;
import org.jeecg.modules.iot.entity.ParameterGroup;
import org.jeecg.modules.iot.entity.ServerDeploy;
import org.jeecg.modules.iot.entity.xmlEntity.*;
import org.jeecg.modules.iot.entity.xmlEntity.script.FunctionEntity;
import org.jeecg.modules.iot.entity.xmlEntity.script.Functions;
import org.jeecg.modules.iot.mapper.EquipmentMapper;
import org.jeecg.modules.iot.service.IEquipmentService;
import org.jeecg.modules.iot.service.IParameterGroupService;
import org.jeecg.modules.iot.service.IServerDeployService;
import org.jeecg.modules.iot.util.HttpClientUtil;
import org.jeecg.modules.iot.util.JaxbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.sql.DataSource;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;
 
/**
 * @Description: 设备
 * @Author: cuikaidong
 * @Date: 2024-12-23
 * @Version: V1.0
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {
 
    @Autowired
    private IParameterGroupService parameterGroupService;
    @Autowired
    private IServerDeployService serverDeployService;
    @Autowired
    private DataSource dataSource;
 
    @Value("${drive}")
    private String drive;
 
    @Override
    public List<SystemType> findProjectCode() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getSystemTypeList();
    }
 
    @Override
    public Object[] queryEquipmentSubscribeList(String id) {
        Object[] strings = new Object[0];
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, id).list();
        if (CollectionUtil.isNotEmpty(equipmentList)) {
            Object[] objects = new Object[equipmentList.size()];
            for (int i = 0; i < equipmentList.size(); i++) {
                objects[i] = equipmentList.get(i).getReadTopic();
            }
            return objects;
        }
        return strings;
    }
 
    @Override
    public List<ControlSystem> findControlSystems(String type) {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        return controlSystemList.stream().filter(c -> c.getType().equals(type)).collect(Collectors.toList());
    }
 
    @Override
    public List<ByteOrder> findByteOrder() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getByteOrderList();
    }
 
    @Override
    public List<SystemDataType> findSystemDataTypeList() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getSystemDataTypeList();
    }
 
    @Override
    public List<Parameter> findParameterById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).defaultParameterList.get(0).parameterList;
    }
 
    @Override
    public List<Parameter> findParameterCustomizeById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).registerTypeList.get(0).parameterList;
    }
 
    @Override
    public List<Parameter> findDataTypeById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).dataTypeList.get(0).parameterList;
    }
 
    @Override
    public List<Equipment> findEquipmentByServerId(String id) {
        return new LambdaQueryChainWrapper<>(baseMapper).eq(Equipment::getServerId, id).list();
    }
 
    @Override
    public Boolean findEquipmentByName(Equipment equipment, Integer type) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, equipment.getServerId())
                .eq(Equipment::getEquipmentType, type)
                .eq(Equipment::getEqptName, equipment.getEqptName())
                .list();
        return equipmentList.size() > 0;
    }
 
    @Override
    public Boolean findEquipmentByCode(Equipment equipment) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, equipment.getServerId())
                .eq(Equipment::getEqptCode, equipment.getEqptCode())
                .list();
        return equipmentList.size() > 0;
    }
 
    @Override
    public List<FunctionEntity> findFunctionInformationList(String functionType) {
        Functions entity = (Functions) JaxbUtil.xmlToBean("F:\\项目\\IOT\\IOT文件\\FunctionConfig.xml", Functions.class);
        switch (functionType) {
            case "Convert":
                return entity.getConvertEntity().getFunctionList();
            case "Math":
                return entity.getMathEntity().getFunctionEntityList();
            case "String":
                return entity.getStringEntity().getFunctionEntityList();
            case "File":
                return entity.getFileEntity().getFunctionEntityList();
            case "Time":
                return entity.getTimeEntity().getFunctionEntityList();
            case "Database":
                return entity.getDatabaseEntity().getFunctionEntityList();
            case "Custom":
                return entity.getCustomEntity().getFunctionEntityList();
            default:
                return null;
        }
    }
 
    @Override
    public void createEmptyEquipmentTable(Date deployDate, String serverId) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, serverId)
                .eq(Equipment::getEquipmentType, 0)
                .ge(Equipment::getCreateTime, deployDate)
                .list();
        equipmentList.forEach(equipment -> {
            createSqlServerTable(equipment.getControlSystem() + '_' + equipment.getEqptCode(),
                    "CollectTime DATETIME DEFAULT GETDATE() NOT NULL",
                    "EquipmentID NVARCHAR(50) NOT NULL",
                    "PRIMARY KEY (CollectTime)"
            );
        });
    }
 
    /**
     * 创建实时数据表
     *
     * @param tableName
     * @param columns
     */
    private void createSqlServerTable(String tableName, String... columns) {
        String tableSql = String.format(
                "IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = '%s' ) " +
                        "CREATE TABLE [%s] (%s)",
                tableName, tableName, String.join(", ", columns)
        );
        try (Connection conn = dataSource.getConnection()) {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(tableSql);
            System.out.println("表创建成功: " + tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> addEmpty(Equipment equipment) {
        // 验证编码名称是否重复
        if (this.findEquipmentByName(equipment, 0)) {
            return Result.error("设备名称已存在!");
        }
        if (this.findEquipmentByCode(equipment)) {
            return Result.error("设备编号已存在!");
        }
        // 分类
        equipment.setEquipmentType(0);
        ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
        this.save(equipment);
        equipment.setReadTopic("IOT/" + serverDeploy.getServerCode() + "/VirtualDevices/" + equipment.getEqptCode() + "/Read");
        equipment.setWriteTopic("IOT." + serverDeploy.getServerCode() + ".VirtualDevices." + equipment.getEqptCode());
        this.updateById(equipment);
        // 新增虚设备时调用
        // 新建项目目录时或者项目载入时调用
        Map<String, String> param = new HashMap<>();
        param.put("path", "D:/iot/" + serverDeploy.getServerCode() + "/script");
        HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/ProjectPath", param);
        param.clear();
        param.put("id", equipment.getEqptCode());
        HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/AddDevicescript", param);
        return Result.ok("添加成功!");
    }
 
    @Override
    public Equipment findEquipmentByCode(String code) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getEqptCode, code)
                .one();
    }
 
    @Override
    public void insertMysqlData(String tableName, String[] columns, Object[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("字段与值的数量不匹配");
        }
 
        // 转义表名和列名(SQL Server使用方括号[])
        String escapedTableName = "[" + tableName.replace("]", "]]") + "]";
 
        // 构建列名和占位符
        StringBuilder columnBuilder = new StringBuilder();
        StringBuilder placeholderBuilder = new StringBuilder();
 
        for (int i = 0; i < columns.length; i++) {
            if (i > 0) {
                columnBuilder.append(", ");
                placeholderBuilder.append(", ");
            }
            // 转义列名,处理可能包含的特殊字符
            String escapedColumn = "[" + columns[i].replace("]", "]]") + "]";
            columnBuilder.append(escapedColumn);
            placeholderBuilder.append("?");
        }
 
        String sql = String.format("INSERT INTO %s (%s) VALUES (%s)",
                escapedTableName,
                columnBuilder.toString(),
                placeholderBuilder.toString());
 
//        log.debug("构建SQL: {}", sql);
 
        try (Connection conn = dataSource.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
 
            // 设置参数,根据值类型选择合适的set方法
            for (int i = 0; i < values.length; i++) {
                Object value = values[i];
                String columnName = columns[i];
 
//                log.debug("设置参数 {}: 列名={}, 值={}, 类型={}",
//                        i+1, columnName, value, value != null ? value.getClass().getName() : "NULL");
 
                if (value == null) {
                    // 对于NULL值,需要指定类型
                    if (columnName.toLowerCase().contains("bool")) {
                        pstmt.setNull(i + 1, Types.BIT);
                    } else if (columnName.toLowerCase().contains("int")) {
                        pstmt.setNull(i + 1, Types.INTEGER);
                    } else if (columnName.toLowerCase().contains("time") ||
                            columnName.toLowerCase().contains("date")) {
                        pstmt.setNull(i + 1, Types.TIMESTAMP);
                    } else {
                        pstmt.setNull(i + 1, Types.VARCHAR);
                    }
                } else if (value instanceof String) {
                    pstmt.setString(i + 1, (String) value);
                } else if (value instanceof Integer) {
                    pstmt.setInt(i + 1, (Integer) value);
                } else if (value instanceof Long) {
                    pstmt.setLong(i + 1, (Long) value);
                } else if (value instanceof Boolean) {
                    pstmt.setBoolean(i + 1, (Boolean) value);
                } else if (value instanceof java.util.Date) {
                    // 将java.util.Date转换为java.sql.Timestamp
                    pstmt.setTimestamp(i + 1, new java.sql.Timestamp(((java.util.Date) value).getTime()));
                } else if (value instanceof java.util.Calendar) {
                    pstmt.setTimestamp(i + 1, new java.sql.Timestamp(
                            ((java.util.Calendar) value).getTimeInMillis()));
                } else if (value instanceof byte[]) {
                    pstmt.setBytes(i + 1, (byte[]) value);
                } else if (value instanceof boolean[]) {
                    // 布尔数组处理:转换为逗号分隔的字符串
                    boolean[] boolArray = (boolean[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < boolArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(boolArray[j] ? "1" : "0");
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else if (value instanceof int[]) {
                    // 整数数组处理:转换为逗号分隔的字符串
                    int[] intArray = (int[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < intArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(intArray[j]);
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else {
                    LocalDateTime localDateTime = LocalDateTime.parse(values[i].toString());
                    pstmt.setTimestamp(i + 1, Timestamp.valueOf(localDateTime));
                }
            }
 
            pstmt.executeUpdate();
//            log.debug("数据插入成功: {}", tableName);
        } catch (SQLException e) {
            log.error("SQL Server插入数据失败: " + sql, e);
            throw new RuntimeException("SQL Server插入数据失败", e);
        }
    }
}