[fix]:code review

This commit is contained in:
alwayssuper 2025-01-23 16:54:45 +08:00
parent 2b27085ec2
commit 4f962bd1f7
12 changed files with 68 additions and 31 deletions

View File

@ -30,6 +30,7 @@ public interface ErrorCodeConstants {
ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在"); ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在");
ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备"); ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备");
ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!"); ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!");
ErrorCode DEVICE_DATA_CONTENT_JSON_PARSE_ERROR = new ErrorCode(1_050_003_007, "导入设备数据格式错误!");
// ========== 产品分类 1-050-004-000 ========== // ========== 产品分类 1-050-004-000 ==========
ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在"); ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在");

View File

@ -55,11 +55,10 @@ public class IotDeviceDataController {
// TODO:数据权限 // TODO:数据权限
@PostMapping("/simulator") @PostMapping("/simulator")
@Operation(summary = "模拟设备") @Operation(summary = "模拟设备")
public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulcatorReqVO) { public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
//TODO:生成一下设备日志 后续完善模拟设备代码逻辑 //TODO:使用 IotDeviceDataSimulatorSaveReqVO 另外content里数据类型的效验前端也没做后端应该要要效验一下这块后续看看怎么安排
// TODO @super应该 deviceDataService 里面有个 simulatorDevice然后里面去 insert 日志 // TODO @super应该 deviceDataService 里面有个 simulatorDevice然后里面去 insert 日志
IotDevicePropertyReportReqDTO simulatorReqVO = new IotDevicePropertyReportReqDTO(); deviceDataService.simulatorSend(simulatorReqVO);
deviceDataService.simulateSend(simulatorReqVO);
return success(true); return success(true);
} }

View File

@ -38,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO {
@NotEmpty(message = "数据内容不能为空") @NotEmpty(message = "数据内容不能为空")
private String content; private String content;
// TODO @芋艿需要讨论下reportTime 到底以那个为准
@Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED)
private Long reportTime; private Long reportTime;

View File

@ -30,7 +30,9 @@ public class DeviceConsumer {
@Async @Async
public void onMessage(ThingModelMessage message) { public void onMessage(ThingModelMessage message) {
log.info("[onMessage][消息内容({})]", message); log.info("[onMessage][消息内容({})]", message);
deviceDataService.saveDeviceDataTest(message); // 设备数据记录
// deviceDataService.saveDeviceDataTest(message);
// 设备日志记录
iotDeviceLogDataService.saveDeviceLog(message); iotDeviceLogDataService.saveDeviceLog(message);
} }

View File

@ -1,9 +0,0 @@
package cn.iocoder.yudao.module.iot.mq.consumer.simulatesend;
/**
* TODO @alwayssuper记得实现还有类注释哈
*
* @author alwayssuper
*/
public class SimulateSendConsumer {
}

View File

@ -37,7 +37,11 @@ public interface IotDeviceLogDataService {
*/ */
PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO); PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
/**
* 插入设备日志
*
* @param msg 设备数据
*/
void saveDeviceLog(ThingModelMessage msg); void saveDeviceLog(ThingModelMessage msg);
} }

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.json.JSONUtil;
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;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
@ -12,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -79,7 +81,19 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
@Override @Override
public void saveDeviceLog(ThingModelMessage msg) { public void saveDeviceLog(ThingModelMessage msg) {
// 1. 构建设备日志对象
IotDeviceLogDO iotDeviceLogDO = IotDeviceLogDO.builder()
.id(msg.getId()) // 消息ID
.deviceKey(msg.getDeviceKey()) // 设备标识
.productKey(msg.getProductKey()) // 产品标识
.type(msg.getMethod()) // 消息类型使用method作为类型
.subType("property") // TODO:这块先写死后续优化
.content(JSONUtil.toJsonStr(msg)) // TODO:后续优化
.reportTime(msg.getTime()) // 上报时间
.build();
// 2. 插入设备日志
iotDeviceLogDataMapper.insert(iotDeviceLogDO);
} }
} }

View File

@ -46,7 +46,7 @@ public interface IotDevicePropertyDataService {
* @param simulatorReqVO 设备数据 * @param simulatorReqVO 设备数据
*/ */
void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO); void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
/** /**
* 获得设备属性最新数据 * 获得设备属性最新数据

View File

@ -3,8 +3,10 @@ package cn.iocoder.yudao.module.iot.service.device;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
@ -39,8 +41,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
/** /**
* IoT 设备属性数据 Service 实现类 * IoT 设备属性数据 Service 实现类
@ -164,24 +168,33 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
thingModelMessageService.saveThingModelMessage(device, thingModelMessage); thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
} }
//TODO: copy了saveDeviceData的逻辑后续看看这块怎么优化 //TODO: copy saveDeviceData 的逻辑后续看看这块怎么优化
@Override @Override
public void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO) { public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
// 1. 根据产品 key 和设备名称获得设备信息 // 1. 根据设备 key 获得设备信息
IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName()); IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
// 2. 解析消息保存数据
JSONObject jsonObject = new JSONObject(simulatorReqVO.getParams()); // 2. 解析 content JSON 对象
log.info("[saveDeviceData][productKey({}) deviceName({}) data({})]", simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName(), jsonObject); JSONObject contentJson;
try {
contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
} catch (Exception e) {
throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
}
// 3. 构建物模型消息
ThingModelMessage thingModelMessage = ThingModelMessage.builder() ThingModelMessage thingModelMessage = ThingModelMessage.builder()
.id(jsonObject.getStr("id")) .id(IdUtil.fastSimpleUUID()) // TODO:后续优化
.sys(jsonObject.get("sys")) .sys(null)// TODO:这块先写死后续优化
.method(jsonObject.getStr("method")) .method("thing.event.property.post") // TODO:这块先写死后续优化
.params(jsonObject.get("params")) .params(contentJson) // content 作为 params
.time(jsonObject.getLong("time") == null ? System.currentTimeMillis() : jsonObject.getLong("time")) .time(simulatorReqVO.getReportTime()) // 使用上报时间
.productKey(simulatorReqVO.getProductKey()) .productKey(simulatorReqVO.getProductKey())
.deviceName(simulatorReqVO.getDeviceName()) .deviceName(device.getDeviceName())
.deviceKey(device.getDeviceKey()) .deviceKey(device.getDeviceKey())
.build(); .build();
// 4. 发送模拟消息
simulateSendProducer.sendSimulateMessage(thingModelMessage); simulateSendProducer.sendSimulateMessage(thingModelMessage);
} }

View File

@ -72,6 +72,15 @@ public interface IotDeviceService {
*/ */
IotDeviceDO getDevice(Long id); IotDeviceDO getDevice(Long id);
/**
* 根据设备 key 获得设备
*
* @param deviceKey 编号
* @return IoT 设备
*/
IotDeviceDO getDeviceByDeviceKey(String deviceKey);
/** /**
* <EFBFBD><EFBFBD>得设备分页 * <EFBFBD><EFBFBD>得设备分页
* *

View File

@ -197,6 +197,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
return deviceMapper.selectById(id); return deviceMapper.selectById(id);
} }
@Override
public IotDeviceDO getDeviceByDeviceKey(String deviceKey) {
return deviceMapper.selectByDeviceKey(deviceKey);
}
@Override @Override
public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) { public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) {
return deviceMapper.selectPage(pageReqVO); return deviceMapper.selectPage(pageReqVO);

View File

@ -27,7 +27,7 @@
<!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 --> <!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 -->
<insert id="insert"> <insert id="insert">
INSERT INTO device_log_${log.deviceKey} (id, product_key, type, subType, content, report_time) INSERT INTO device_log_${log.deviceKey} (ts, id, product_key, type, subType, content, report_time)
USING device_log USING device_log
TAGS ('${log.deviceKey}') TAGS ('${log.deviceKey}')
VALUES ( VALUES (