【功能优化】IoT: 注释掉脚本服务相关代码,移除不必要的依赖,简化产品脚本测试逻辑

This commit is contained in:
安浩浩 2025-03-24 16:25:11 +08:00
parent 9b2389356f
commit a9dc654b36
2 changed files with 88 additions and 89 deletions

View File

@ -70,11 +70,11 @@
</dependency>
<!-- 脚本插件相关 -->
<dependency>
<!--<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-iot-plugin-script</artifactId>
<version>${revision}</version>
</dependency>
</dependency>-->
<!-- 消息队列相关 -->
<dependency>

View File

@ -9,8 +9,6 @@ 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;
@ -42,8 +40,8 @@ public class IotProductScriptServiceImpl implements IotProductScriptService {
@Resource
private IotProductService productService;
@Resource
private ScriptService scriptService;
// @Resource
// private ScriptService scriptService;
@Override
public Long createProductScript(IotProductScriptSaveReqVO createReqVO) {
@ -120,89 +118,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