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