[fix]:code review
This commit is contained in:
parent
2b27085ec2
commit
4f962bd1f7
|
@ -30,6 +30,7 @@ public interface ErrorCodeConstants {
|
|||
ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在");
|
||||
ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备");
|
||||
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 ==========
|
||||
ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在");
|
||||
|
|
|
@ -55,11 +55,10 @@ public class IotDeviceDataController {
|
|||
// TODO:数据权限
|
||||
@PostMapping("/simulator")
|
||||
@Operation(summary = "模拟设备")
|
||||
public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulcatorReqVO) {
|
||||
//TODO:先生成一下设备日志 后续完善模拟设备代码逻辑
|
||||
public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
|
||||
//TODO:先使用 IotDeviceDataSimulatorSaveReqVO 另外content里数据类型的效验前端也没做,后端应该要要效验一下,这块后续看看怎么安排
|
||||
// TODO @super:应该 deviceDataService 里面有个 simulatorDevice,然后里面去 insert 日志!
|
||||
IotDevicePropertyReportReqDTO simulatorReqVO = new IotDevicePropertyReportReqDTO();
|
||||
deviceDataService.simulateSend(simulatorReqVO);
|
||||
deviceDataService.simulatorSend(simulatorReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO {
|
|||
@NotEmpty(message = "数据内容不能为空")
|
||||
private String content;
|
||||
|
||||
// TODO @芋艿:需要讨论下,reportTime 到底以那个为准!
|
||||
@Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Long reportTime;
|
||||
|
||||
|
|
|
@ -30,7 +30,9 @@ public class DeviceConsumer {
|
|||
@Async
|
||||
public void onMessage(ThingModelMessage message) {
|
||||
log.info("[onMessage][消息内容({})]", message);
|
||||
deviceDataService.saveDeviceDataTest(message);
|
||||
// 设备数据记录
|
||||
// deviceDataService.saveDeviceDataTest(message);
|
||||
// 设备日志记录
|
||||
iotDeviceLogDataService.saveDeviceLog(message);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package cn.iocoder.yudao.module.iot.mq.consumer.simulatesend;
|
||||
|
||||
/**
|
||||
* TODO @alwayssuper:记得实现,还有类注释哈
|
||||
*
|
||||
* @author alwayssuper
|
||||
*/
|
||||
public class SimulateSendConsumer {
|
||||
}
|
|
@ -37,7 +37,11 @@ public interface IotDeviceLogDataService {
|
|||
*/
|
||||
PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
|
||||
|
||||
|
||||
/**
|
||||
* 插入设备日志
|
||||
*
|
||||
* @param msg 设备数据
|
||||
*/
|
||||
void saveDeviceLog(ThingModelMessage msg);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
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.util.object.BeanUtils;
|
||||
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.validation.annotation.Validated;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -79,7 +81,19 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
|
|||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ public interface IotDevicePropertyDataService {
|
|||
* @param simulatorReqVO 设备数据
|
||||
*/
|
||||
|
||||
void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO);
|
||||
void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
|
||||
|
||||
/**
|
||||
* 获得设备属性最新数据
|
||||
|
|
|
@ -3,8 +3,10 @@ package cn.iocoder.yudao.module.iot.service.device;
|
|||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
|
||||
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.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.filterList;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备【属性】数据 Service 实现类
|
||||
|
@ -164,24 +168,33 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
|
|||
thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
|
||||
}
|
||||
|
||||
//TODO: copy了saveDeviceData的逻辑,后续看看这块怎么优化
|
||||
//TODO: copy 了 saveDeviceData 的逻辑,后续看看这块怎么优化
|
||||
@Override
|
||||
public void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO) {
|
||||
// 1. 根据产品 key 和设备名称,获得设备信息
|
||||
IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName());
|
||||
// 2. 解析消息,保存数据
|
||||
JSONObject jsonObject = new JSONObject(simulatorReqVO.getParams());
|
||||
log.info("[saveDeviceData][productKey({}) deviceName({}) data({})]", simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName(), jsonObject);
|
||||
public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
|
||||
// 1. 根据设备 key ,获得设备信息
|
||||
IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
|
||||
|
||||
// 2. 解析 content 为 JSON 对象
|
||||
JSONObject contentJson;
|
||||
try {
|
||||
contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
|
||||
} catch (Exception e) {
|
||||
throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
|
||||
}
|
||||
|
||||
// 3. 构建物模型消息
|
||||
ThingModelMessage thingModelMessage = ThingModelMessage.builder()
|
||||
.id(jsonObject.getStr("id"))
|
||||
.sys(jsonObject.get("sys"))
|
||||
.method(jsonObject.getStr("method"))
|
||||
.params(jsonObject.get("params"))
|
||||
.time(jsonObject.getLong("time") == null ? System.currentTimeMillis() : jsonObject.getLong("time"))
|
||||
.id(IdUtil.fastSimpleUUID()) // TODO:后续优化
|
||||
.sys(null)// TODO:这块先写死,后续优化
|
||||
.method("thing.event.property.post") // TODO:这块先写死,后续优化
|
||||
.params(contentJson) // 将 content 作为 params
|
||||
.time(simulatorReqVO.getReportTime()) // 使用上报时间
|
||||
.productKey(simulatorReqVO.getProductKey())
|
||||
.deviceName(simulatorReqVO.getDeviceName())
|
||||
.deviceName(device.getDeviceName())
|
||||
.deviceKey(device.getDeviceKey())
|
||||
.build();
|
||||
|
||||
// 4. 发送模拟消息
|
||||
simulateSendProducer.sendSimulateMessage(thingModelMessage);
|
||||
}
|
||||
|
||||
|
|
|
@ -72,6 +72,15 @@ public interface IotDeviceService {
|
|||
*/
|
||||
IotDeviceDO getDevice(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 根据设备 key 获得设备
|
||||
*
|
||||
* @param deviceKey 编号
|
||||
* @return IoT 设备
|
||||
*/
|
||||
IotDeviceDO getDeviceByDeviceKey(String deviceKey);
|
||||
|
||||
/**
|
||||
* <EFBFBD><EFBFBD>得设备分页
|
||||
*
|
||||
|
|
|
@ -197,6 +197,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
|
|||
return deviceMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IotDeviceDO getDeviceByDeviceKey(String deviceKey) {
|
||||
return deviceMapper.selectByDeviceKey(deviceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) {
|
||||
return deviceMapper.selectPage(pageReqVO);
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
<!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 -->
|
||||
<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
|
||||
TAGS ('${log.deviceKey}')
|
||||
VALUES (
|
||||
|
|
Loading…
Reference in New Issue