【功能新增】IoT: 重构物联网模块,新增组件支持,更新依赖项,优化代码结构。具体更改包括:将插件模块替换为组件模块,添加 HTTP 和 EMQX 组件,更新相关依赖,优化心跳和下行处理逻辑,增加组件配置属性,完善文档说明。

This commit is contained in:
安浩浩
2025-04-02 21:28:56 +08:00
parent 516e3a2387
commit b5ce269fef
36 changed files with 2700 additions and 96 deletions

View File

@@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.*;
import cn.iocoder.yudao.module.iot.service.device.control.IotDeviceUpstreamService;
import cn.iocoder.yudao.module.iot.service.plugin.IotPluginInstanceService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
@@ -15,6 +16,7 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
*/
@RestController
@Validated
@Primary
public class IoTDeviceUpstreamApiImpl implements IotDeviceUpstreamApi {
@Resource

View File

@@ -9,18 +9,13 @@ import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProduct
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductScriptMapper;
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
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.module.iot.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;
@@ -43,8 +38,8 @@ public class IotProductScriptServiceImpl implements IotProductScriptService {
@Resource
private IotProductService productService;
@Resource
private ScriptService scriptService;
// @Resource
// private ScriptService scriptService;
@Override
public Long createProductScript(IotProductScriptSaveReqVO createReqVO) {
@@ -121,89 +116,90 @@ public class IotProductScriptServiceImpl implements IotProductScriptService {
@Override
public IotProductScriptTestRespVO testProductScript(IotProductScriptTestReqVO testReqVO) {
long startTime = System.currentTimeMillis();
try {
// 验证产品是否存在
validateProductExists(testReqVO.getProductId());
// 根据ID获取已保存的脚本如果有
IotProductScriptDO existingScript = null;
if (testReqVO.getId() != null) {
existingScript = getProductScript(testReqVO.getId());
}
// 创建测试上下文
PluginScriptContext context = new PluginScriptContext();
IotProductDO product = productService.getProduct(testReqVO.getProductId());
// 设置设备上下文(使用产品信息,没有具体设备)
context.withDeviceContext(product.getProductKey(), null);
// 设置输入参数
Map<String, Object> params = new HashMap<>();
params.put("input", testReqVO.getTestInput());
params.put("productKey", product.getProductKey());
params.put("scriptType", testReqVO.getScriptType());
// 根据脚本类型设置特定参数
switch (testReqVO.getScriptType()) {
case 1: // PROPERTY_PARSER
params.put("method", "property");
break;
case 2: // EVENT_PARSER
params.put("method", "event");
params.put("identifier", "default");
break;
case 3: // COMMAND_ENCODER
params.put("method", "command");
break;
default:
// 默认不添加额外参数
}
// 添加所有参数到上下文
for (Map.Entry<String, Object> entry : params.entrySet()) {
context.setParameter(entry.getKey(), entry.getValue());
}
// 执行脚本
Object result = scriptService.executeScript(
testReqVO.getScriptLanguage(),
testReqVO.getScriptContent(),
context);
// 更新测试结果(如果是已保存的脚本)
if (existingScript != null) {
IotProductScriptDO updateObj = new IotProductScriptDO();
updateObj.setId(existingScript.getId());
updateObj.setLastTestTime(LocalDateTime.now());
updateObj.setLastTestResult(1); // 1表示成功
productScriptMapper.updateById(updateObj);
}
long executionTime = System.currentTimeMillis() - startTime;
return IotProductScriptTestRespVO.success(result, executionTime);
} catch (Exception e) {
log.error("[testProductScript][测试脚本异常]", e);
// 如果是已保存的脚本,更新测试失败状态
if (testReqVO.getId() != null) {
try {
IotProductScriptDO updateObj = new IotProductScriptDO();
updateObj.setId(testReqVO.getId());
updateObj.setLastTestTime(LocalDateTime.now());
updateObj.setLastTestResult(0); // 0表示失败
productScriptMapper.updateById(updateObj);
} catch (Exception ex) {
log.error("[testProductScript][更新脚本测试结果异常]", ex);
}
}
long executionTime = System.currentTimeMillis() - startTime;
return IotProductScriptTestRespVO.error(e.getMessage(), executionTime);
}
// long startTime = System.currentTimeMillis();
//
// try {
// // 验证产品是否存在
// validateProductExists(testReqVO.getProductId());
//
// // 根据ID获取已保存的脚本如果有
// IotProductScriptDO existingScript = null;
// if (testReqVO.getId() != null) {
// existingScript = getProductScript(testReqVO.getId());
// }
//
// // 创建测试上下文
// PluginScriptContext context = new PluginScriptContext();
// IotProductDO product = productService.getProduct(testReqVO.getProductId());
//
// // 设置设备上下文(使用产品信息,没有具体设备)
// context.withDeviceContext(product.getProductKey(), null);
//
// // 设置输入参数
// Map<String, Object> params = new HashMap<>();
// params.put("input", testReqVO.getTestInput());
// params.put("productKey", product.getProductKey());
// params.put("scriptType", testReqVO.getScriptType());
//
// // 根据脚本类型设置特定参数
// switch (testReqVO.getScriptType()) {
// case 1: // PROPERTY_PARSER
// params.put("method", "property");
// break;
// case 2: // EVENT_PARSER
// params.put("method", "event");
// params.put("identifier", "default");
// break;
// case 3: // COMMAND_ENCODER
// params.put("method", "command");
// break;
// default:
// // 默认不添加额外参数
// }
//
// // 添加所有参数到上下文
// for (Map.Entry<String, Object> entry : params.entrySet()) {
// context.setParameter(entry.getKey(), entry.getValue());
// }
//
// // 执行脚本
// Object result = scriptService.executeScript(
// testReqVO.getScriptLanguage(),
// testReqVO.getScriptContent(),
// context);
//
// // 更新测试结果(如果是已保存的脚本)
// if (existingScript != null) {
// IotProductScriptDO updateObj = new IotProductScriptDO();
// updateObj.setId(existingScript.getId());
// updateObj.setLastTestTime(LocalDateTime.now());
// updateObj.setLastTestResult(1); // 1表示成功
// productScriptMapper.updateById(updateObj);
// }
//
// long executionTime = System.currentTimeMillis() - startTime;
// return IotProductScriptTestRespVO.success(result, executionTime);
//
// } catch (Exception e) {
// log.error("[testProductScript][测试脚本异常]", e);
//
// // 如果是已保存的脚本,更新测试失败状态
// if (testReqVO.getId() != null) {
// try {
// IotProductScriptDO updateObj = new IotProductScriptDO();
// updateObj.setId(testReqVO.getId());
// updateObj.setLastTestTime(LocalDateTime.now());
// updateObj.setLastTestResult(0); // 0表示失败
// productScriptMapper.updateById(updateObj);
// } catch (Exception ex) {
// log.error("[testProductScript][更新脚本测试结果异常]", ex);
// }
// }
//
// long executionTime = System.currentTimeMillis() - startTime;
// return IotProductScriptTestRespVO.error(e.getMessage(), executionTime);
// }
return null;
}
@Override