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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package org.jeecg.modules.iot.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.iot.entity.*;
import org.jeecg.modules.iot.entity.xmlEntity.*;
import org.jeecg.modules.iot.entity.xmlEntity.ControlSystem;
import org.jeecg.modules.iot.service.*;
import org.jeecg.modules.iot.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
 
/**
 * @Description: 设备
 * @Author: cuikaidong
 * @Date: 2024-12-23
 * @Version: V1.0
 */
@Api(tags = "设备")
@RestController
@RequestMapping("/equipment")
@Slf4j
public class EquipmentController extends JeecgController<Equipment, IEquipmentService> {
 
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IServerDeployService serverDeployService;
    @Autowired
    private IParameterGroupService parameterGroupService;
    @Autowired
    private IControlSystemService controlSystemService;
 
    /**
     * 分页列表查询
     *
     * @param equipment
     * @param pageNo
     * @param pageSize
     * @param serverCode
     * @return
     */
    @ApiOperation(value = "设备-分页列表查询", notes = "设备-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Equipment equipment,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "serverCode", required = false) String serverCode,
                                   HttpServletRequest req) {
        QueryWrapper<Equipment> queryWrapper = QueryGenerator.initQueryWrapper(equipment, req.getParameterMap());
        Page<Equipment> page = new Page<>(pageNo, pageSize);
        IPage<Equipment> pageList = equipmentService.page(page, queryWrapper);
        List<Equipment> records = pageList.getRecords();
        return Result.ok(pageList);
    }
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Equipment equipment = equipmentService.getById(id);
        return Result.ok(equipment);
    }
 
    /**
     * 添加
     *
     * @param equipment
     * @return
     */
    @AutoLog(value = "实设备-添加")
    @ApiOperation(value = "实设备-添加", notes = "实设备-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Equipment equipment) {
        // 验证编码名称是否重复
        if (equipmentService.findEquipmentByName(equipment,1)) {
            return Result.error("设备名称已存在!");
        }
        if (equipmentService.findEquipmentByCode(equipment)) {
            return Result.error("设备编号已存在!");
        }
        // 分类
        equipment.setEquipmentType(1);
        // 订阅主题
        ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
        // 验证设备编号和设备名称
        equipmentService.save(equipment);
        equipment.setReadTopic("IOT/" + serverDeploy.getServerCode() + "/ActualDevices/" + equipment.getEqptCode() + "/Read");
        equipment.setWriteTopic("IOT/" + serverDeploy.getServerCode() + "/ActualDevices/" + equipment.getEqptCode() + "/Write");
        equipmentService.updateById(equipment);
        return Result.ok("添加成功!");
    }
 
    /**
     * 虚设备
     *
     * @param equipment
     * @return
     */
    @AutoLog(value = "虚设备-添加")
    @ApiOperation(value = "虚设备-添加", notes = "虚设备-添加")
    @PostMapping(value = "/add/empty")
    public Result<?> addEmpty(@RequestBody Equipment equipment) {
        return equipmentService.addEmpty(equipment);
    }
 
    /**
     * 编辑
     *
     * @param equipment
     * @return
     */
    @AutoLog(value = "设备-编辑")
    @ApiOperation(value = "设备-编辑", notes = "设备-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Equipment equipment) {
        equipment.setCreateTime(new Date());
        // 验证是否是虚设备
        if (equipment.getEquipmentType() == 0) {
            Equipment equipmentById = equipmentService.getById(equipment.getId());
            // 验证是否编辑了虚设备名称
            if (!equipment.getEqptName().equals(equipmentById.getEqptName())){
                ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
                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", equipmentById.getEqptCode());
                HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/DeleteDevicescript", param);
                param.clear();
                // 添加新文件
                param.put("id", equipment.getEqptCode());
                HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/AddDevicescript", param);
            }
        }
        equipmentService.updateById(equipment);
        return Result.ok("编辑成功!");
    }
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "设备-通过id删除")
    @ApiOperation(value = "设备-通过id删除", notes = "设备-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        List<ParameterGroup> parameterGroupList = parameterGroupService.findParameterGroupByServerId(id);
        if (parameterGroupList.size() > 0) {
            return Result.error("删除失败,请先删除参数组!");
        }
        Equipment equipment = equipmentService.getById(id);
        if (equipment.getEquipmentType().equals(0)) {
            ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
            //删除虚设备时调用
            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/DeleteDevicescript", param);
        }
        equipmentService.removeById(id);
        return Result.ok("删除成功!");
    }
 
    /**
     * 根据服务器id查询设备订阅列表
     *
     * @return
     */
    @ApiOperation(value = "根据服务器id查询设备订阅列表", notes = "根据服务器id查询设备订阅列表")
    @GetMapping(value = "/subscribe")
    public Result<?> queryEquipmentSubscribe(@RequestParam(name = "id") String id) {
        Object[] subscribeList = equipmentService.queryEquipmentSubscribeList(id);
        return Result.ok(subscribeList);
    }
 
    /**
     * 查询系统类型
     *
     * @return
     */
    @ApiOperation(value = "查询系统类型", notes = "查询系统类型")
    @GetMapping(value = "/querySystemType")
    public Result<?> querySystemType() {
        List<SystemType> systemTypes = equipmentService.findProjectCode();
        return Result.ok(systemTypes);
    }
 
    /**
     * 根据系统类型查询控制系统列表
     *
     * @return
     */
    @ApiOperation(value = "查询控制系统列表", notes = "查询控制系统列表")
    @GetMapping(value = "/queryControlSystem")
    public Result<?> queryControlSystem(@RequestParam(name = "type") String type) {
        List<ControlSystem> controlSystems = equipmentService.findControlSystems(type);
        return Result.ok(controlSystems);
    }
 
    /**
     * 查询字节顺序列表
     *
     * @return
     */
    @ApiOperation(value = "查询字节顺序列表", notes = "查询字节顺序列表")
    @GetMapping(value = "/queryByteOrder")
    public Result<?> queryByteOrder() {
        List<ByteOrder> byteOrders = equipmentService.findByteOrder();
        return Result.ok(byteOrders);
    }
 
    /**
     * 查询参数类型列表
     *
     * @return
     */
    @ApiOperation(value = "查询参数类型列表", notes = "查询参数类型列表")
    @GetMapping(value = "/querySystemDataType")
    public Result<?> querySystemDataType() {
        List<SystemDataType> systemDataTypeList = equipmentService.findSystemDataTypeList();
        return Result.ok(systemDataTypeList);
    }
 
    /**
     * 根据参数组id查询设备采集参数
     *
     * @return
     */
    @ApiOperation(value = "根据参数组id查询设备采集参数", notes = "根据参数组id查询设备采集参数")
    @GetMapping(value = "/queryParameter")
    public Result<?> queryParameter(@RequestParam(name = "id") String id) {
        List<Parameter> controlSystems = equipmentService.findParameterById(id);
        return Result.ok(controlSystems);
    }
    /**
     * 根据参数组id查询设备自定义采集参数
     *
     * @return
     */
    @ApiOperation(value = "根据参数组id查询设备自定义采集参数", notes = "根据参数组id查询设备自定义采集参数")
    @GetMapping(value = "/customize/queryParameter")
    public Result<?> queryParameterCustomize(@RequestParam(name = "id") String id) {
        List<Parameter> controlSystems = equipmentService.findParameterCustomizeById(id);
        return Result.ok(controlSystems);
    }
 
    /**
     * 根据参数组id查询设备采集参数类型
     *
     * @return
     */
    @ApiOperation(value = "根据参数组id查询设备采集参数类型", notes = "根据参数组id查询设备采集参数类型")
    @GetMapping(value = "/queryDataType")
    public Result<?> queryDataType(@RequestParam(name = "id") String id) {
        List<Parameter> parameters = equipmentService.findDataTypeById(id);
        return Result.ok(parameters);
    }
 
    /**
     * 根据虚设备id查询设备分类树
     *
     * @return projectClassifyList
     */
    @AutoLog(value = "根据虚设备id查询设备分类树")
    @ApiOperation(value = "根据虚设备id查询设备分类树", notes = "查询项目分类树结构")
    @GetMapping(value = "/tree")
    public Result<?> queryPageList(@RequestParam(name = "id") String id) {
        Equipment equipment = equipmentService.getById(id);
        ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
        // 树图标
        Map<String, String> iconIot = new HashMap<>();
        iconIot.put("icon", "iot");
        Map<String, String> iconProject = new HashMap<>();
        iconProject.put("icon", "classify");
        Map<String, String> iconProject1 = new HashMap<>();
        iconProject1.put("icon", "project");
        Map<String, String> iconEmpty = new HashMap<>();
        iconEmpty.put("icon", "");
        Map<String, String> iconSolid = new HashMap<>();
        iconSolid.put("icon", "");
        // 查询数据
        List<Equipment> equipmentList = equipmentService.findEquipmentByServerId(serverDeploy.getId());
        List<ParameterGroup> groupList = parameterGroupService.findParameterGroupByServerId(serverDeploy.getId());
        // 树结构
        List<IotTree> trees1 = new ArrayList<>();
        List<IotTree> treeList = new ArrayList<>();
 
        IotTree iotTree = new IotTree();
        iotTree.setTitle(serverDeploy.getServerName());
        iotTree.setKey("fwq_" + serverDeploy.getServerCode());
        iotTree.setSlots(iconIot);
 
 
        // 遍历设备信息
        equipmentList.forEach(eq -> {
            IotTree emptyEquipment = new IotTree();
            emptyEquipment.setTitle(eq.getEqptName());
            List<IotTree> treeList4 = new ArrayList<>();// 参数组
            // 遍历参数组信息
            groupList.forEach(g -> {
                if (eq.getId().equals(g.getEquipmentId())) {
                    IotTree groupTree = new IotTree();
                    groupTree.setTitle(g.getName());
                    groupTree.setKey("ssb_" + g.getId());
                    groupTree.setSlots(iconSolid);
                    treeList4.add(groupTree);
                }
            });
            if (serverDeploy.getId().equals(eq.getServerId()) && eq.getEquipmentType().equals(1)) {
                emptyEquipment.setSlots(iconSolid);
                emptyEquipment.setKey("group_" + eq.getId());
                emptyEquipment.setChildren(treeList4);
                trees1.add(emptyEquipment);
            }
            iotTree.setChildren(treeList4);
        });
 
        iotTree.setChildren(trees1);
        treeList.add(iotTree);
        return Result.ok(treeList);
    }
 
    /**
     * 根据服务器id查询服务器下设备分类树
     *
     * @return projectClassifyList
     */
    @AutoLog(value = "根据服务器id查询服务器下设备分类树")
    @ApiOperation(value = "根据服务器id查询服务器下设备分类树", notes = "根据服务器id查询服务器下设备分类树")
    @GetMapping(value = "/tree/collect")
    public Result<?> queryCollectTree(@RequestParam(name = "id") String id) {
        ServerDeploy serverDeploy = serverDeployService.getById(id);
        // 树图标
        Map<String, String> iconProject1 = new HashMap<>(); // 服务器图标
        iconProject1.put("icon", "project");
        Map<String, String> iconEmpty = new HashMap<>();
        iconEmpty.put("icon", "empty");
        Map<String, String> iconSolid = new HashMap<>();
        iconSolid.put("icon", "solid");
        Map<String, String> iconParameter = new HashMap<>();
        iconParameter.put("icon", "");
        // 查询数据
        List<Equipment> equipmentList = equipmentService.findEquipmentByServerId(serverDeploy.getId());
        List<ParameterGroup> groupList = parameterGroupService.findParameterGroupByServerId(serverDeploy.getId());
        // 树结构
        List<IotTree> trees2 = new ArrayList<>();
        List<IotTree> treeList = new ArrayList<>();
 
        IotTree iotTree = new IotTree();
        iotTree.setTitle(serverDeploy.getServerName());
        iotTree.setKey("fwq_" + serverDeploy.getServerCode());
        iotTree.setSlots(iconProject1);
        List<IotTree> treeList1 = new ArrayList<>();// 实设备
        List<IotTree> treeList2 = new ArrayList<>(); //虚设备
        // 实设备添加
        IotTree empty = new IotTree();
        empty.setTitle("实设备");
        empty.setKey("solid_" + serverDeploy.getServerCode());
        empty.setSlots(iconParameter);
        // 虚设备添加
        IotTree solid = new IotTree();
        solid.setTitle("虚设备");
        solid.setKey("empty_" + serverDeploy.getServerCode());
        solid.setSlots(iconParameter);
        // 遍历设备信息
        equipmentList.forEach(eq -> {
            IotTree emptyEquipment = new IotTree();
            emptyEquipment.setTitle(eq.getEqptName());
            List<IotTree> treeList4 = new ArrayList<>();// 参数组
            // 遍历参数组信息
            groupList.forEach(g -> {
                if (eq.getId().equals(g.getEquipmentId())) {
                    IotTree groupTree = new IotTree();
                    groupTree.setTitle(g.getName());
                    groupTree.setKey("ssb_" + g.getId());
                    groupTree.setSlots(iconParameter);
                    treeList4.add(groupTree);
                }
            });
            if (serverDeploy.getId().equals(eq.getServerId()) && eq.getEquipmentType().equals(1)) {
                emptyEquipment.setSlots(iconSolid);
                emptyEquipment.setKey("group_" + eq.getId());
                emptyEquipment.setChildren(treeList4);
                treeList1.add(emptyEquipment);
            } else if (serverDeploy.getId().equals(eq.getServerId()) && eq.getEquipmentType().equals(0)) {
                emptyEquipment.setKey("xxb_" + eq.getId());
                emptyEquipment.setSlots(iconEmpty);
                treeList2.add(emptyEquipment);
            }
 
        });
        solid.setChildren(treeList2);
        empty.setChildren(treeList1);
        trees2.add(empty);
        trees2.add(solid);
        iotTree.setChildren(trees2);
        treeList.add(iotTree);
        return Result.ok(treeList);
    }
 
    /**
     * 查询控制系统信息列表
     *
     * @return
     */
    @ApiOperation(value = "查询控制系统信息列表", notes = "查询控制系统信息列表")
    @GetMapping(value = "/findControlSystem")
    public Result<?> findControlSystem() {
        return Result.ok(controlSystemService.list());
    }
}