【代码评审】IOT:设备的 review

This commit is contained in:
YunaiV 2024-09-21 10:09:30 +08:00
parent bd18e73052
commit 6b9cca0b79
14 changed files with 145 additions and 440 deletions

View File

@ -1,19 +1,25 @@
package cn.iocoder.yudao.module.iot.enums.device; package cn.iocoder.yudao.module.iot.enums.device;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.Getter; import lombok.Getter;
import java.util.Arrays;
/** /**
* IoT 设备状态枚举 * IoT 设备状态枚举
* 设备状态0 - 未激活1 - 在线2 - 离线3 - 已禁用 *
* @author haohao
*/ */
@Getter @Getter
public enum IotDeviceStatusEnum { public enum IotDeviceStatusEnum implements IntArrayValuable {
INACTIVE(0, "未激活"), INACTIVE(0, "未激活"),
ONLINE(1, "在线"), ONLINE(1, "在线"),
OFFLINE(2, "离线"), OFFLINE(2, "离线"),
DISABLED(3, "已禁用"); DISABLED(3, "已禁用");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotDeviceStatusEnum::getStatus).toArray();
/** /**
* 状态 * 状态
*/ */
@ -40,4 +46,10 @@ public enum IotDeviceStatusEnum {
public static boolean isValidStatus(Integer status) { public static boolean isValidStatus(Integer status) {
return fromStatus(status) != null; return fromStatus(status) != null;
} }
@Override
public int[] array() {
return ARRAYS;
}
} }

View File

@ -37,14 +37,14 @@ public class IotDeviceController {
private IotDeviceService deviceService; private IotDeviceService deviceService;
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建IoT 设备") @Operation(summary = "创建设备")
@PreAuthorize("@ss.hasPermission('iot:device:create')") @PreAuthorize("@ss.hasPermission('iot:device:create')")
public CommonResult<Long> createDevice(@Valid @RequestBody IotDeviceSaveReqVO createReqVO) { public CommonResult<Long> createDevice(@Valid @RequestBody IotDeviceSaveReqVO createReqVO) {
return success(deviceService.createDevice(createReqVO)); return success(deviceService.createDevice(createReqVO));
} }
@PutMapping("/update-status") @PutMapping("/update-status")
@Operation(summary = "更新IoT 设备状态") @Operation(summary = "更新设备状态")
@Parameter(name = "id", description = "编号", required = true) @Parameter(name = "id", description = "编号", required = true)
@Parameter(name = "status", description = "状态", required = true, example = "1") @Parameter(name = "status", description = "状态", required = true, example = "1")
@PreAuthorize("@ss.hasPermission('iot:device:update')") @PreAuthorize("@ss.hasPermission('iot:device:update')")
@ -55,7 +55,7 @@ public class IotDeviceController {
} }
@PutMapping("/update") @PutMapping("/update")
@Operation(summary = "更新IoT 设备") @Operation(summary = "更新设备")
@PreAuthorize("@ss.hasPermission('iot:device:update')") @PreAuthorize("@ss.hasPermission('iot:device:update')")
public CommonResult<Boolean> updateDevice(@Valid @RequestBody IotDeviceSaveReqVO updateReqVO) { public CommonResult<Boolean> updateDevice(@Valid @RequestBody IotDeviceSaveReqVO updateReqVO) {
deviceService.updateDevice(updateReqVO); deviceService.updateDevice(updateReqVO);
@ -63,7 +63,7 @@ public class IotDeviceController {
} }
@DeleteMapping("/delete") @DeleteMapping("/delete")
@Operation(summary = "删除IoT 设备") @Operation(summary = "删除设备")
@Parameter(name = "id", description = "编号", required = true) @Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:device:delete')") @PreAuthorize("@ss.hasPermission('iot:device:delete')")
public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) { public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) {
@ -72,7 +72,7 @@ public class IotDeviceController {
} }
@GetMapping("/get") @GetMapping("/get")
@Operation(summary = "获得IoT 设备") @Operation(summary = "获得设备")
@Parameter(name = "id", description = "编号", required = true, example = "1024") @Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('iot:device:query')") @PreAuthorize("@ss.hasPermission('iot:device:query')")
public CommonResult<IotDeviceRespVO> getDevice(@RequestParam("id") Long id) { public CommonResult<IotDeviceRespVO> getDevice(@RequestParam("id") Long id) {
@ -81,7 +81,7 @@ public class IotDeviceController {
} }
@GetMapping("/page") @GetMapping("/page")
@Operation(summary = "获得IoT 设备分页") @Operation(summary = "获得设备分页")
@PreAuthorize("@ss.hasPermission('iot:device:query')") @PreAuthorize("@ss.hasPermission('iot:device:query')")
public CommonResult<PageResult<IotDeviceRespVO>> getDevicePage(@Valid IotDevicePageReqVO pageReqVO) { public CommonResult<PageResult<IotDeviceRespVO>> getDevicePage(@Valid IotDevicePageReqVO pageReqVO) {
PageResult<IotDeviceDO> pageResult = deviceService.getDevicePage(pageReqVO); PageResult<IotDeviceDO> pageResult = deviceService.getDevicePage(pageReqVO);
@ -89,7 +89,7 @@ public class IotDeviceController {
} }
@GetMapping("/export-excel") @GetMapping("/export-excel")
@Operation(summary = "导出IoT 设备 Excel") @Operation(summary = "导出设备 Excel")
@PreAuthorize("@ss.hasPermission('iot:device:export')") @PreAuthorize("@ss.hasPermission('iot:device:export')")
@ApiAccessLog(operateType = EXPORT) @ApiAccessLog(operateType = EXPORT)
public void exportDeviceExcel(@Valid IotDevicePageReqVO pageReqVO, public void exportDeviceExcel(@Valid IotDevicePageReqVO pageReqVO,

View File

@ -1,10 +1,15 @@
package cn.iocoder.yudao.module.iot.controller.admin.device.vo; package cn.iocoder.yudao.module.iot.controller.admin.device.vo;
import lombok.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import java.math.BigDecimal; import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ -15,28 +20,32 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
@ToString(callSuper = true) @ToString(callSuper = true)
public class IotDevicePageReqVO extends PageParam { public class IotDevicePageReqVO extends PageParam {
@Schema(description = "设备唯一标识符,全局唯一,用于识别设备") // TODO @芋艿需要去掉一些多余的字段
@Schema(description = "设备唯一标识符", example = "24602")
private String deviceKey; private String deviceKey;
@Schema(description = "设备名称,在产品内唯一,用于标识设备", example = "王五") @Schema(description = "设备名称", example = "王五")
private String deviceName; private String deviceName;
@Schema(description = "产品 ID关联 iot_product 表的 id", example = "26202") @Schema(description = "产品编号", example = "26202")
private Long productId; private Long productId;
@Schema(description = "产品 Key关联 iot_product 表的 product_key") @Schema(description = "产品标识")
private String productKey; private String productKey;
@Schema(description = "设备类型0 - 直连设备1 - 网关子设备2 - 网关设备", example = "1") @Schema(description = "设备类型", example = "1")
// TODO @haohao需要有个设备类型的枚举
private Integer deviceType; private Integer deviceType;
@Schema(description = "设备备注名称,供用户自定义备注", example = "张三") @Schema(description = "备注名称", example = "张三")
private String nickname; private String nickname;
@Schema(description = "网关设备 ID,子设备需要关联的网关设备 ID", example = "16380") @Schema(description = "网关设备 ID", example = "16380")
private Long gatewayId; private Long gatewayId;
@Schema(description = "设备状态0 - 未激活1 - 在线2 - 离线3 - 已禁用", example = "1") @Schema(description = "设备状态", example = "1")
@InEnum(IotDeviceStatusEnum.class)
private Integer status; private Integer status;
@Schema(description = "设备状态最后更新时间") @Schema(description = "设备状态最后更新时间")

View File

@ -13,7 +13,7 @@ import java.time.LocalDateTime;
@ExcelIgnoreUnannotated @ExcelIgnoreUnannotated
public class IotDeviceRespVO { public class IotDeviceRespVO {
@Schema(description = "设备 ID主键自增", requiredMode = Schema.RequiredMode.REQUIRED, example = "177") @Schema(description = "设备编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "177")
private Long id; private Long id;
@Schema(description = "设备唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "设备唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED)
@ -24,11 +24,11 @@ public class IotDeviceRespVO {
@ExcelProperty("设备名称备") @ExcelProperty("设备名称备")
private String deviceName; private String deviceName;
@Schema(description = "产品 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
@ExcelProperty("产品 ID") @ExcelProperty("产品 ID")
private Long productId; private Long productId;
@Schema(description = "产品 Key", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("产品 Key") @ExcelProperty("产品 Key")
private String productKey; private String productKey;
@ -72,7 +72,7 @@ public class IotDeviceRespVO {
@ExcelProperty("设备的固件版本") @ExcelProperty("设备的固件版本")
private String firmwareVersion; private String firmwareVersion;
@Schema(description = "设备密钥,用于设备认证,需安全存储") @Schema(description = "设备密钥,用于设备认证")
@ExcelProperty("设备密钥") @ExcelProperty("设备密钥")
private String deviceSecret; private String deviceSecret;
@ -92,16 +92,17 @@ public class IotDeviceRespVO {
@ExcelProperty("认证类型(如一机一密、动态注册)") @ExcelProperty("认证类型(如一机一密、动态注册)")
private String authType; private String authType;
@Schema(description = "设备位置的纬度,范围 -90.000000 ~ 90.000000") // TODO @haohao经纬度可能 double 就够啦
@ExcelProperty("设备位置的纬度,范围 -90.000000 ~ 90.000000") @Schema(description = "设备位置的纬度,范围")
@ExcelProperty("设备位置的纬度")
private BigDecimal latitude; private BigDecimal latitude;
@Schema(description = "设备位置的经度,范围 -180.000000 ~ 180.000000") @Schema(description = "设备位置的经度")
@ExcelProperty("设备位置的经度,范围 -180.000000 ~ 180.000000") @ExcelProperty("设备位置的经度")
private BigDecimal longitude; private BigDecimal longitude;
@Schema(description = "地区编码,符合国家地区编码标准,关联地区表", example = "16995") @Schema(description = "地区编码", example = "16995")
@ExcelProperty("地区编码,符合国家地区编码标准,关联地区表") @ExcelProperty("地区编码")
private Integer areaId; private Integer areaId;
@Schema(description = "设备详细地址") @Schema(description = "设备详细地址")

View File

@ -7,16 +7,16 @@ import lombok.Data;
@Data @Data
public class IotDeviceSaveReqVO { public class IotDeviceSaveReqVO {
@Schema(description = "设备 ID主键自增", requiredMode = Schema.RequiredMode.REQUIRED, example = "177") @Schema(description = "设备编号", example = "177")
private Long id; private Long id;
@Schema(description = "设备名称,在产品内唯一,用于标识设备", requiredMode = Schema.RequiredMode.REQUIRED, example = "王五") @Schema(description = "设备名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "王五")
private String deviceName; private String deviceName;
@Schema(description = "设备备注名称,供用户自定义备注", example = "张三") @Schema(description = "备注名称", example = "张三")
private String nickname; private String nickname;
@Schema(description = "产品 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
private Long productId; private Long productId;
} }

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.dal.dataobject.device;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum;
import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
@ -13,7 +14,7 @@ import java.time.LocalDateTime;
/** /**
* IoT 设备 DO * IoT 设备 DO
* *
* @author 芋道源码 * @author haohao
*/ */
@TableName("iot_device") @TableName("iot_device")
@KeySequence("iot_device_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写 @KeySequence("iot_device_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@ -39,33 +40,48 @@ public class IotDeviceDO extends BaseDO {
*/ */
private String deviceName; private String deviceName;
/** /**
* 产品 ID关联 iot_product 表的 id * 设备备注名称
*/
private String nickname;
/**
* 设备序列号
*/
private String serialNumber;
/**
* 产品编号
*
* 关联 {@link IotProductDO#getId()} * 关联 {@link IotProductDO#getId()}
*/ */
private Long productId; private Long productId;
/** /**
* 产品 Key关联 iot_product 表的 product_key * 产品标识
* 关联 {@link IotProductDO#getProductKey()} *
* 冗余 {@link IotProductDO#getProductKey()}
*/ */
private String productKey; private String productKey;
/** /**
* 设备类型0 - 直连设备1 - 网关子设备2 - 网关设备 * 设备类型
* 关联 {@link IotProductDO#getDeviceType()} *
* 冗余 {@link IotProductDO#getDeviceType()}
*/ */
private Integer deviceType; private Integer deviceType;
/** /**
* 设备备注名称供用户自定义备注 * 设备状态
*/ *
private String nickname; * 枚举 {@link IotDeviceStatusEnum}
/**
* 网关设备 ID子设备需要关联的网关设备 ID
*/
private Long gatewayId;
/**
* 设备状态0 - 未激活1 - 在线2 - 离线3 - 已禁用
* 关联 {@link cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum}
*/ */
private Integer status; private Integer status;
/**
* 网关设备编号
*
* 子设备需要关联的网关设备 ID
*
* 关联 {@link IotDeviceDO#getId()}
*/
private Long gatewayId;
/** /**
* 设备状态最后更新时间 * 设备状态最后更新时间
*/ */
@ -82,6 +98,7 @@ public class IotDeviceDO extends BaseDO {
* 设备激活时间 * 设备激活时间
*/ */
private LocalDateTime activeTime; private LocalDateTime activeTime;
/** /**
* 设备的 IP 地址 * 设备的 IP 地址
*/ */
@ -90,6 +107,7 @@ public class IotDeviceDO extends BaseDO {
* 设备的固件版本 * 设备的固件版本
*/ */
private String firmwareVersion; private String firmwareVersion;
/** /**
* 设备密钥用于设备认证需安全存储 * 设备密钥用于设备认证需安全存储
*/ */
@ -109,26 +127,26 @@ public class IotDeviceDO extends BaseDO {
/** /**
* 认证类型如一机一密动态注册 * 认证类型如一机一密动态注册
*/ */
// TODO @haohao是不是要枚举哈
private String authType; private String authType;
/** /**
* 设备位置的纬度范围 -90.000000 ~ 90.000000 * 设备位置的纬度
*/ */
private BigDecimal latitude; private BigDecimal latitude;
/** /**
* 设备位置的经度范围 -180.000000 ~ 180.000000 * 设备位置的经度
*/ */
private BigDecimal longitude; private BigDecimal longitude;
/** /**
* 地区编码符合国家地区编码标准关联地区表 * 地区编码
*
* 关联 Area id
*/ */
private Integer areaId; private Integer areaId;
/** /**
* 设备详细地址 * 设备详细地址
*/ */
private String address; private String address;
/**
* 设备序列号
*/
private String serialNumber;
} }

View File

@ -53,4 +53,5 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
default long selectCountByGatewayId(Long id) { default long selectCountByGatewayId(Long id) {
return selectCount(IotDeviceDO::getGatewayId, id); return selectCount(IotDeviceDO::getGatewayId, id);
} }
} }

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.service.device; package cn.iocoder.yudao.module.iot.service.device;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -34,6 +35,7 @@ public class DeviceServiceImpl implements IotDeviceService {
@Resource @Resource
private IotDeviceMapper deviceMapper; private IotDeviceMapper deviceMapper;
// TODO @haohao不直接调用 productmapper通过 productservice每一个模型不直接使用对方的
@Resource @Resource
private IotProductMapper productMapper; private IotProductMapper productMapper;
@ -46,40 +48,33 @@ public class DeviceServiceImpl implements IotDeviceService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Long createDevice(IotDeviceSaveReqVO createReqVO) { public Long createDevice(IotDeviceSaveReqVO createReqVO) {
// 1. 转换 VO DO // 1.1 校验产品是否存在
IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class);
// 2. 根据产品 ID 查询产品信息
IotProductDO product = productMapper.selectById(createReqVO.getProductId()); IotProductDO product = productMapper.selectById(createReqVO.getProductId());
if (product == null) { if (product == null) {
throw exception(PRODUCT_NOT_EXISTS); throw exception(PRODUCT_NOT_EXISTS);
} }
device.setProductKey(product.getProductKey()); // 1.2 校验设备名称在同一产品下是否唯一
device.setDeviceType(product.getDeviceType()); if (StrUtil.isBlank(createReqVO.getDeviceName())) {
createReqVO.setDeviceName(generateUniqueDeviceName(product.getProductKey()));
// 3. DeviceName 可以为空当为空时自动生成产品下的唯一标识符作为 DeviceName } else {
if (StrUtil.isBlank(device.getDeviceName())) { validateDeviceNameUnique(product.getProductKey(), createReqVO.getDeviceName());
device.setDeviceName(generateUniqueDeviceName(createReqVO.getProductId()));
} }
// 4. 校验设备名称在同一产品下是否唯一 // 2.1 转换 VO DO
validateDeviceNameUnique(device.getProductKey(), device.getDeviceName()); IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class)
.setProductKey(product.getProductKey())
// 5. 生成并设置必要的字段 .setDeviceType(product.getDeviceType());
// 2.2 生成并设置必要的字段
device.setDeviceKey(generateUniqueDeviceKey()); device.setDeviceKey(generateUniqueDeviceKey());
device.setDeviceSecret(generateDeviceSecret()); device.setDeviceSecret(generateDeviceSecret());
device.setMqttClientId(generateMqttClientId()); device.setMqttClientId(generateMqttClientId());
device.setMqttUsername(generateMqttUsername(device.getDeviceName(), device.getProductKey())); device.setMqttUsername(generateMqttUsername(device.getDeviceName(), device.getProductKey()));
device.setMqttPassword(generateMqttPassword()); device.setMqttPassword(generateMqttPassword());
// 2.3 设置设备状态为未激活
// 6. 设置设备状态为未激活
device.setStatus(IotDeviceStatusEnum.INACTIVE.getStatus()); device.setStatus(IotDeviceStatusEnum.INACTIVE.getStatus());
device.setStatusLastUpdateTime(LocalDateTime.now()); device.setStatusLastUpdateTime(LocalDateTime.now());
// 2.4 插入到数据库
// 7. 插入到数据库
deviceMapper.insert(device); deviceMapper.insert(device);
// 8. 返回生成的设备 ID
return device.getId(); return device.getId();
} }
@ -111,7 +106,7 @@ public class DeviceServiceImpl implements IotDeviceService {
* @return 生成的 deviceSecret * @return 生成的 deviceSecret
*/ */
private String generateDeviceSecret() { private String generateDeviceSecret() {
// 32 位随机字符串 // TODO @haohaoreturn IdUtil.fastSimpleUUID()
return UUID.randomUUID().toString().replace("-", ""); return UUID.randomUUID().toString().replace("-", "");
} }
@ -141,39 +136,25 @@ public class DeviceServiceImpl implements IotDeviceService {
* @return 生成的 MQTT Password * @return 生成的 MQTT Password
*/ */
private String generateMqttPassword() { private String generateMqttPassword() {
// 在实际应用中建议使用更安全的方法生成 MQTT Password如加密或哈希 // TODO @haohao后续优化在实际应用中建议使用更安全的方法生成 MQTT Password如加密或哈希
return UUID.randomUUID().toString(); return UUID.randomUUID().toString();
} }
/** /**
* 生成唯一的 DeviceName * 生成唯一的 DeviceName
* *
* @param productId 产品 ID * @param productKey 产品标识
* @return 生成的唯一 DeviceName * @return 生成的唯一 DeviceName
*/ */
private String generateUniqueDeviceName(Long productId) { private String generateUniqueDeviceName(String productKey) {
// 实现逻辑以在产品下生成唯一的设备名称 // TODO @haohao业务逻辑里尽量避免 while true万一 bug = =虽然这个不会哈我先改了下
String deviceName; for (int i = 0; i < Short.MAX_VALUE; i++) {
String productKey = getProductKey(productId); String deviceName = IdUtil.fastSimpleUUID().substring(0, 20);
do { if (deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName) != null) {
// 20 位随机字符串 return deviceName;
deviceName = UUID.randomUUID().toString().replace("-", "").substring(0, 20); }
} while (deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName) != null);
return deviceName;
}
/**
* 获取产品 Key
*
* @param productId 产品 ID
* @return 产品 Key
*/
private String getProductKey(Long productId) {
IotProductDO product = productMapper.selectById(productId);
if (product == null) {
throw exception(PRODUCT_NOT_EXISTS);
} }
return product.getProductKey(); throw new IllegalArgumentException("生成 DeviceName 失败");
} }
@Override @Override
@ -183,6 +164,7 @@ public class DeviceServiceImpl implements IotDeviceService {
IotDeviceDO existingDevice = validateDeviceExists(updateReqVO.getId()); IotDeviceDO existingDevice = validateDeviceExists(updateReqVO.getId());
// 设备名称 产品 ID 不能修改 // 设备名称 产品 ID 不能修改
// TODO @haohao这种直接设置为 null 就不会更新了忽略前端的传参
if (updateReqVO.getDeviceName() != null && !updateReqVO.getDeviceName().equals(existingDevice.getDeviceName())) { if (updateReqVO.getDeviceName() != null && !updateReqVO.getDeviceName().equals(existingDevice.getDeviceName())) {
throw exception(DEVICE_NAME_CANNOT_BE_MODIFIED); throw exception(DEVICE_NAME_CANNOT_BE_MODIFIED);
} }
@ -200,18 +182,14 @@ public class DeviceServiceImpl implements IotDeviceService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteDevice(Long id) { public void deleteDevice(Long id) {
// 校验存在 // 1.1 校验存在
IotDeviceDO iotDeviceDO = validateDeviceExists(id); IotDeviceDO device = validateDeviceExists(id);
// 1.2 如果是网关设备检查是否有子设备
// 如果是网关设备检查是否有子设备 if (device.getGatewayId() != null && deviceMapper.selectCountByGatewayId(id) > 0) {
if (iotDeviceDO.getGatewayId() != null) { throw exception(DEVICE_HAS_CHILDREN);
long childCount = deviceMapper.selectCountByGatewayId(id);
if (childCount > 0) {
throw exception(DEVICE_HAS_CHILDREN);
}
} }
// 删除设备 // 2. 删除设备
deviceMapper.deleteById(id); deviceMapper.deleteById(id);
} }
@ -222,11 +200,11 @@ public class DeviceServiceImpl implements IotDeviceService {
* @return 设备对象 * @return 设备对象
*/ */
private IotDeviceDO validateDeviceExists(Long id) { private IotDeviceDO validateDeviceExists(Long id) {
IotDeviceDO iotDeviceDO = deviceMapper.selectById(id); IotDeviceDO device = deviceMapper.selectById(id);
if (iotDeviceDO == null) { if (device == null) {
throw exception(DEVICE_NOT_EXISTS); throw exception(DEVICE_NOT_EXISTS);
} }
return iotDeviceDO; return device;
} }
@Override @Override
@ -244,21 +222,20 @@ public class DeviceServiceImpl implements IotDeviceService {
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void updateDeviceStatus(Long id, Integer status) { public void updateDeviceStatus(Long id, Integer status) {
// 校验存在 // 校验存在
validateDeviceExists(id); validateDeviceExists(id);
// TODO @haohao这个可以直接用 swagger 注解哈
// 校验状态是否合法 // 校验状态是否合法
if (!IotDeviceStatusEnum.isValidStatus(status)) { if (!IotDeviceStatusEnum.isValidStatus(status)) {
throw exception(DEVICE_INVALID_DEVICE_STATUS); throw exception(DEVICE_INVALID_DEVICE_STATUS);
} }
// 更新状态和更新时间 // 更新状态和更新时间
IotDeviceDO updateObj = new IotDeviceDO() IotDeviceDO updateObj = new IotDeviceDO().setId(id).setStatus(status)
.setId(id)
.setStatus(status)
.setStatusLastUpdateTime(LocalDateTime.now()); .setStatusLastUpdateTime(LocalDateTime.now());
deviceMapper.updateById(updateObj); deviceMapper.updateById(updateObj);
} }
} }

View File

@ -13,7 +13,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
public interface IotDeviceService { public interface IotDeviceService {
/** /**
* 创建IoT 设备 * 创建设备
* *
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
@ -21,21 +21,21 @@ public interface IotDeviceService {
Long createDevice(@Valid IotDeviceSaveReqVO createReqVO); Long createDevice(@Valid IotDeviceSaveReqVO createReqVO);
/** /**
* 更新IoT 设备 * 更新设备
* *
* @param updateReqVO 更新信息 * @param updateReqVO 更新信息
*/ */
void updateDevice(@Valid IotDeviceSaveReqVO updateReqVO); void updateDevice(@Valid IotDeviceSaveReqVO updateReqVO);
/** /**
* 删除IoT 设备 * 删除设备
* *
* @param id 编号 * @param id 编号
*/ */
void deleteDevice(Long id); void deleteDevice(Long id);
/** /**
* 获得IoT 设备 * 获得设备
* *
* @param id 编号 * @param id 编号
* @return IoT 设备 * @return IoT 设备
@ -43,7 +43,7 @@ public interface IotDeviceService {
IotDeviceDO getDevice(Long id); IotDeviceDO getDevice(Long id);
/** /**
* 获得IoT 设备分页 * 获得设备分页
* *
* @param pageReqVO 分页查询 * @param pageReqVO 分页查询
* @return IoT 设备分页 * @return IoT 设备分页
@ -51,10 +51,11 @@ public interface IotDeviceService {
PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO); PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO);
/** /**
* 更新IoT 设备状态 * 更新设备状态
* *
* @param id 编号 * @param id 编号
* @param status 状态 * @param status 状态
*/ */
void updateDeviceStatus(Long id, Integer status); void updateDeviceStatus(Long id, Integer status);
} }

View File

@ -170,7 +170,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3.1 使用 diffList 方法比较新旧列表 // 3.1 使用 diffList 方法比较新旧列表
List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList, List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList,
// TODO @haohao是不是用 id 比较相同就 ok 如果可以的化下面的 update 可以更简单
// 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象 // 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象
(oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier())
&& Objects.equals(oldFunc.getType(), newFunc.getType())); && Objects.equals(oldFunc.getType(), newFunc.getType()));
@ -191,6 +190,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
thinkModelFunctionMapper.updateById(updateFunc); thinkModelFunctionMapper.updateById(updateFunc);
} }
} }
// TODO @haohaoseckillProductMapper.updateBatch(diffList.get(1)); 可以直接类似这么操作哇
} }
if (CollUtil.isNotEmpty(deleteList)) { if (CollUtil.isNotEmpty(deleteList)) {
Set<Long> idsToDelete = CollectionUtils.convertSet(deleteList, IotThinkModelFunctionDO::getId); Set<Long> idsToDelete = CollectionUtils.convertSet(deleteList, IotThinkModelFunctionDO::getId);

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.device.IotDeviceMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>

View File

@ -1,219 +0,0 @@
package cn.iocoder.yudao.module.iot.service.device;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import jakarta.annotation.Resource;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.dal.mysql.device.IotDeviceMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.springframework.context.annotation.Import;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link DeviceServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(DeviceServiceImpl.class)
public class DeviceServiceImplTest extends BaseDbUnitTest {
@Resource
private DeviceServiceImpl deviceService;
@Resource
private IotDeviceMapper deviceMapper;
@Test
public void testCreateDevice_success() {
// 准备参数
IotDeviceSaveReqVO createReqVO = randomPojo(IotDeviceSaveReqVO.class).setId(null);
// 调用
Long deviceId = deviceService.createDevice(createReqVO);
// 断言
assertNotNull(deviceId);
// 校验记录的属性是否正确
IotDeviceDO device = deviceMapper.selectById(deviceId);
assertPojoEquals(createReqVO, device, "id");
}
@Test
public void testUpdateDevice_success() {
// mock 数据
IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class);
deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据
// 准备参数
IotDeviceSaveReqVO updateReqVO = randomPojo(IotDeviceSaveReqVO.class, o -> {
o.setId(dbDevice.getId()); // 设置更新的 ID
});
// 调用
deviceService.updateDevice(updateReqVO);
// 校验是否更新正确
IotDeviceDO device = deviceMapper.selectById(updateReqVO.getId()); // 获取最新的
assertPojoEquals(updateReqVO, device);
}
@Test
public void testUpdateDevice_notExists() {
// 准备参数
IotDeviceSaveReqVO updateReqVO = randomPojo(IotDeviceSaveReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> deviceService.updateDevice(updateReqVO), DEVICE_NOT_EXISTS);
}
@Test
public void testDeleteDevice_success() {
// mock 数据
IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class);
deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbDevice.getId();
// 调用
deviceService.deleteDevice(id);
// 校验数据不存在了
assertNull(deviceMapper.selectById(id));
}
@Test
public void testDeleteDevice_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> deviceService.deleteDevice(id), DEVICE_NOT_EXISTS);
}
@Test
@Disabled // TODO 请修改 null 为需要的值然后删除 @Disabled 注解
public void testGetDevicePage() {
// mock 数据
IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class, o -> { // 等会查询到
o.setDeviceKey(null);
o.setDeviceName(null);
o.setProductId(null);
o.setProductKey(null);
o.setDeviceType(null);
o.setNickname(null);
o.setGatewayId(null);
o.setStatus(null);
o.setStatusLastUpdateTime(null);
o.setLastOnlineTime(null);
o.setLastOfflineTime(null);
o.setActiveTime(null);
o.setIp(null);
o.setFirmwareVersion(null);
o.setDeviceSecret(null);
o.setMqttClientId(null);
o.setMqttUsername(null);
o.setMqttPassword(null);
o.setAuthType(null);
o.setLatitude(null);
o.setLongitude(null);
o.setAreaId(null);
o.setAddress(null);
o.setSerialNumber(null);
o.setCreateTime(null);
});
deviceMapper.insert(dbDevice);
// 测试 deviceKey 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceKey(null)));
// 测试 deviceName 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceName(null)));
// 测试 productId 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setProductId(null)));
// 测试 productKey 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setProductKey(null)));
// 测试 deviceType 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceType(null)));
// 测试 nickname 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setNickname(null)));
// 测试 gatewayId 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setGatewayId(null)));
// 测试 status 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setStatus(null)));
// 测试 statusLastUpdateTime 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setStatusLastUpdateTime(null)));
// 测试 lastOnlineTime 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLastOnlineTime(null)));
// 测试 lastOfflineTime 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLastOfflineTime(null)));
// 测试 activeTime 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setActiveTime(null)));
// 测试 ip 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setIp(null)));
// 测试 firmwareVersion 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setFirmwareVersion(null)));
// 测试 deviceSecret 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceSecret(null)));
// 测试 mqttClientId 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttClientId(null)));
// 测试 mqttUsername 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttUsername(null)));
// 测试 mqttPassword 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttPassword(null)));
// 测试 authType 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAuthType(null)));
// 测试 latitude 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLatitude(null)));
// 测试 longitude 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLongitude(null)));
// 测试 areaId 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAreaId(null)));
// 测试 address 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAddress(null)));
// 测试 serialNumber 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setSerialNumber(null)));
// 测试 createTime 不匹配
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setCreateTime(null)));
// 准备参数
IotDevicePageReqVO reqVO = new IotDevicePageReqVO();
reqVO.setDeviceKey(null);
reqVO.setDeviceName(null);
reqVO.setProductId(null);
reqVO.setProductKey(null);
reqVO.setDeviceType(null);
reqVO.setNickname(null);
reqVO.setGatewayId(null);
reqVO.setStatus(null);
reqVO.setStatusLastUpdateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
reqVO.setLastOnlineTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
reqVO.setLastOfflineTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
reqVO.setActiveTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
reqVO.setIp(null);
reqVO.setFirmwareVersion(null);
reqVO.setDeviceSecret(null);
reqVO.setMqttClientId(null);
reqVO.setMqttUsername(null);
reqVO.setMqttPassword(null);
reqVO.setAuthType(null);
reqVO.setLatitude(null);
reqVO.setLongitude(null);
reqVO.setAreaId(null);
reqVO.setAddress(null);
reqVO.setSerialNumber(null);
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
// 调用
PageResult<IotDeviceDO> pageResult = deviceService.getDevicePage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbDevice, pageResult.getList().get(0));
}
}

View File

@ -1,71 +0,0 @@
package cn.iocoder.yudao.module.iot.service.thinkmodelfunction;
import org.junit.jupiter.api.Test;
import jakarta.annotation.Resource;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper;
import org.springframework.context.annotation.Import;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link IotThinkModelFunctionServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(IotThinkModelFunctionServiceImpl.class)
public class IotThinkModelFunctionServiceImplTest extends BaseDbUnitTest {
@Resource
private IotThinkModelFunctionServiceImpl thinkModelFunctionService;
@Resource
private IotThinkModelFunctionMapper thinkModelFunctionMapper;
@Test
public void testCreateThinkModelFunction_success() {
// 准备参数
IotThinkModelFunctionSaveReqVO createReqVO = randomPojo(IotThinkModelFunctionSaveReqVO.class);
// 调用
Long thinkModelFunctionId = thinkModelFunctionService.createThinkModelFunction(createReqVO);
// 断言
assertNotNull(thinkModelFunctionId);
// 校验记录的属性是否正确
IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionMapper.selectById(thinkModelFunctionId);
assertPojoEquals(createReqVO, thinkModelFunction, "id");
}
@Test
public void testDeleteThinkModelFunction_success() {
// mock 数据
IotThinkModelFunctionDO dbThinkModelFunction = randomPojo(IotThinkModelFunctionDO.class);
thinkModelFunctionMapper.insert(dbThinkModelFunction);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbThinkModelFunction.getId();
// 调用
thinkModelFunctionService.deleteThinkModelFunction(id);
// 校验数据不存在了
assertNull(thinkModelFunctionMapper.selectById(id));
}
@Test
public void testDeleteThinkModelFunction_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> thinkModelFunctionService.deleteThinkModelFunction(id), THINK_MODEL_FUNCTION_NOT_EXISTS);
}
}