diff --git a/maibu-admin/src/main/java/com/maibu/web/controller/system/SysIotCommDeviceController.java b/maibu-admin/src/main/java/com/maibu/web/controller/system/SysIotCommDeviceController.java index 1562c06..0336d1e 100644 --- a/maibu-admin/src/main/java/com/maibu/web/controller/system/SysIotCommDeviceController.java +++ b/maibu-admin/src/main/java/com/maibu/web/controller/system/SysIotCommDeviceController.java @@ -2,12 +2,16 @@ package com.maibu.web.controller.system; import cn.hutool.core.convert.Convert; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.maibu.core.business.IoTCommonDevice; import com.maibu.core.business.Product; +import com.maibu.core.business.dto.DeviceConfigDTO; import com.maibu.core.business.dto.ProductDTO; import com.maibu.core.controller.BaseController; import com.maibu.core.domain.AjaxResult; import com.maibu.core.page.TableDataInfo; +import com.maibu.memory.GlobalMemory; import com.maibu.service.DeviceProductService; +import com.maibu.service.IotDeviceCommonService; import com.maibu.utils.ServletUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -16,6 +20,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; /** * 厂家模块 @@ -25,6 +32,91 @@ import java.time.LocalDateTime; @RequestMapping("/system/iotDevice") public class SysIotCommDeviceController extends BaseController { + @Autowired + private IotDeviceCommonService deviceService; + /** + * 设备录入:连接测试成功后 → 放入未注册缓存 + */ + @PostMapping("/add") + @ApiOperation("新增设备") + public AjaxResult add(@RequestBody DeviceConfigDTO deviceDTO) { + if (deviceDTO.getDeviceName() == null || deviceDTO.getSn() == null) { + return AjaxResult.error("设备名称和 SN 不能为空"); + } + + try { + // 连接测试 + 放入未注册缓存 + deviceService.addDeviceToUnRegisterCache(deviceDTO); + return AjaxResult.success("设备连接成功,已加入未注册队列"); + } catch (Exception e) { + return AjaxResult.error("设备连接失败:" + e.getMessage()); + } + } + + + /** + * 查询已注册设备列表(分页) + */ + @GetMapping("/list") + @ApiOperation("查询设备列表") + public TableDataInfo list(IoTCommonDevice device) { + Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1); + Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10); + + List allDevices = new ArrayList<>(GlobalMemory.unRegisterCommonDeviceMap.values()); + + if (device.getDeviceName() != null && !device.getDeviceName().isEmpty()) { + allDevices = allDevices.stream() + .filter(d -> d.getDeviceName().contains(device.getDeviceName())) + .collect(Collectors.toList()); + } + + long total = allDevices.size(); + int fromIndex = (pageNum - 1) * pageSize; + int toIndex = Math.min(fromIndex + pageSize, (int) total); + + List pageData = (fromIndex < total) ? allDevices.subList(fromIndex, toIndex) : new ArrayList<>(); + + return getDataTable(pageData, total); + } + + /** + * 删除设备 + */ + @DeleteMapping("/{id}") + @ApiOperation("删除设备") + public AjaxResult remove(@PathVariable Long id) { + return toAjax(deviceService.removeById(id) ? 1 : 0); + } + + + /** + * 编辑设备 + */ + @PostMapping("/update") + @ApiOperation("编辑未注册设备") + public AjaxResult edit(@RequestBody DeviceConfigDTO deviceDTO) { + if (deviceDTO.getSn() == null) { + return AjaxResult.error("设备 SN 不能为空"); + } + + // 1. 检查缓存中是否存在 + IoTCommonDevice cacheDevice = deviceService.getFromUnRegisterCache(deviceDTO.getSn()); + if (cacheDevice == null) { + return AjaxResult.error("设备不在未注册缓存中"); + } + + // 2. 字段映射更新 (DTO -> Cache Object) + // 注意:根据 DTO 中有值的字段覆盖缓存 + if (deviceDTO.getDeviceName() != null) cacheDevice.setDeviceName(deviceDTO.getDeviceName()); + if (deviceDTO.getProductId() != null) cacheDevice.setProductId(deviceDTO.getProductId()); + // ... 其他需要更新的字段 + + // 3. 写回 Redis + deviceService.updateUnRegisterCache(cacheDevice); + + return AjaxResult.success("缓存更新成功"); + } }