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 { @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 queryWrapper = QueryGenerator.initQueryWrapper(equipment, req.getParameterMap()); Page page = new Page<>(pageNo, pageSize); IPage pageList = equipmentService.page(page, queryWrapper); List 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 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 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 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 systemTypes = equipmentService.findProjectCode(); return Result.ok(systemTypes); } /** * 根据系统类型查询控制系统列表 * * @return */ @ApiOperation(value = "查询控制系统列表", notes = "查询控制系统列表") @GetMapping(value = "/queryControlSystem") public Result queryControlSystem(@RequestParam(name = "type") String type) { List controlSystems = equipmentService.findControlSystems(type); return Result.ok(controlSystems); } /** * 查询字节顺序列表 * * @return */ @ApiOperation(value = "查询字节顺序列表", notes = "查询字节顺序列表") @GetMapping(value = "/queryByteOrder") public Result queryByteOrder() { List byteOrders = equipmentService.findByteOrder(); return Result.ok(byteOrders); } /** * 查询参数类型列表 * * @return */ @ApiOperation(value = "查询参数类型列表", notes = "查询参数类型列表") @GetMapping(value = "/querySystemDataType") public Result querySystemDataType() { List 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 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 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 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 iconIot = new HashMap<>(); iconIot.put("icon", "iot"); Map iconProject = new HashMap<>(); iconProject.put("icon", "classify"); Map iconProject1 = new HashMap<>(); iconProject1.put("icon", "project"); Map iconEmpty = new HashMap<>(); iconEmpty.put("icon", ""); Map iconSolid = new HashMap<>(); iconSolid.put("icon", ""); // 查询数据 List equipmentList = equipmentService.findEquipmentByServerId(serverDeploy.getId()); List groupList = parameterGroupService.findParameterGroupByServerId(serverDeploy.getId()); // 树结构 List trees1 = new ArrayList<>(); List 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 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 iconProject1 = new HashMap<>(); // 服务器图标 iconProject1.put("icon", "project"); Map iconEmpty = new HashMap<>(); iconEmpty.put("icon", "empty"); Map iconSolid = new HashMap<>(); iconSolid.put("icon", "solid"); Map iconParameter = new HashMap<>(); iconParameter.put("icon", ""); // 查询数据 List equipmentList = equipmentService.findEquipmentByServerId(serverDeploy.getId()); List groupList = parameterGroupService.findParameterGroupByServerId(serverDeploy.getId()); // 树结构 List trees2 = new ArrayList<>(); List treeList = new ArrayList<>(); IotTree iotTree = new IotTree(); iotTree.setTitle(serverDeploy.getServerName()); iotTree.setKey("fwq_" + serverDeploy.getServerCode()); iotTree.setSlots(iconProject1); List treeList1 = new ArrayList<>();// 实设备 List 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 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()); } }