Commit 685de4e23858f9c6e5ef40878235579628ee7d51

Authored by 云中非
1 parent 0d1f25d6

refactor: 脚本启用禁用时,刷新相关设备配置的解析脚本。

@@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.yunteng.dto.TkDeviceScriptDTO; @@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.yunteng.dto.TkDeviceScriptDTO;
38 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum; 38 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
39 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; 39 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
40 import org.thingsboard.server.common.msg.queue.ServiceQueue; 40 import org.thingsboard.server.common.msg.queue.ServiceQueue;
  41 +import org.thingsboard.server.common.yunteng.script.YtScriptFactory;
41 import org.thingsboard.server.common.yunteng.script.YtScriptInvokeService; 42 import org.thingsboard.server.common.yunteng.script.YtScriptInvokeService;
42 import org.thingsboard.server.common.yunteng.script.YtScriptType; 43 import org.thingsboard.server.common.yunteng.script.YtScriptType;
43 import org.thingsboard.server.controller.BaseController; 44 import org.thingsboard.server.controller.BaseController;
@@ -73,16 +74,7 @@ public class TkDeviceScriptController extends BaseController { @@ -73,16 +74,7 @@ public class TkDeviceScriptController extends BaseController {
73 String tenantId = getCurrentUser().getCurrentTenantId(); 74 String tenantId = getCurrentUser().getCurrentTenantId();
74 scriptDTO.setTenantId(tenantId); 75 scriptDTO.setTenantId(tenantId);
75 scriptService.validateFormdata(scriptDTO, created); 76 scriptService.validateFormdata(scriptDTO, created);
76 - /** 业务流程 1/3.编辑时同步刷新相关的设备配置 2/3.处理TB业务逻辑 3/3.处理业务平台的业务逻辑 */  
77 - if (!created) {  
78 - List<DeviceProfileDTO> usedProfiles = ytDeviceProfileService.findDeviceProfile(tenantId, id);  
79 - for (DeviceProfileDTO profile : usedProfiles) {  
80 - DeviceProfile tbDeviceProfile =  
81 - buildTbDeviceProfileFromDeviceProfileDTO(  
82 - profile, scriptDTO.getTenantId(), scriptDTO.getConvertJs());  
83 - updateTbDeviceProfile(tbDeviceProfile);  
84 - }  
85 - } 77 +
86 78
87 String creator = getCurrentUser().getCurrentUserId(); 79 String creator = getCurrentUser().getCurrentUserId();
88 scriptDTO.setCreator(creator); 80 scriptDTO.setCreator(creator);
@@ -96,9 +88,31 @@ public class TkDeviceScriptController extends BaseController { @@ -96,9 +88,31 @@ public class TkDeviceScriptController extends BaseController {
96 public ResponseEntity<Boolean> updateScriptStatus( 88 public ResponseEntity<Boolean> updateScriptStatus(
97 @PathVariable("id") String id, @PathVariable("status") Integer status) 89 @PathVariable("id") String id, @PathVariable("status") Integer status)
98 throws ThingsboardException { 90 throws ThingsboardException {
  91 + /** 业务流程 1/3.编辑时同步刷新相关的设备配置 2/3.处理TB业务逻辑 3/3.处理业务平台的业务逻辑 */
  92 + String tenantId = getCurrentUser().getCurrentTenantId();
  93 + TkDeviceScriptDTO scriptDTO = scriptService.getDeviceScript(tenantId,id).get();
  94 + if(scriptDTO ==null || scriptDTO.getStatus().equals(status)){
  95 + return ResponseEntity.ok(false);
  96 + }
  97 + String convertJs = YtScriptFactory.INCLUD_ORIGINAL_DATA;
  98 +
  99 + if(1 == scriptDTO.getStatus()){
  100 + if(scriptDTO.isSaveOriginalData()){
  101 + convertJs += scriptDTO.getConvertJs();
  102 + }else{
  103 + convertJs = scriptDTO.getConvertJs();
  104 + }
99 105
100 - return ResponseEntity.ok(  
101 - scriptService.updateScriptStatus(getCurrentUser().getCurrentTenantId(), id, status)); 106 + }
  107 + List<DeviceProfileDTO> usedProfiles = ytDeviceProfileService.findDeviceProfile(tenantId, id);
  108 + for (DeviceProfileDTO profile : usedProfiles) {
  109 + DeviceProfile tbDeviceProfile =
  110 + buildTbDeviceProfileFromDeviceProfileDTO(
  111 + profile, tenantId, convertJs);
  112 + updateTbDeviceProfile(tbDeviceProfile);
  113 + }
  114 + scriptService.updateScriptStatus(tenantId, id, status);
  115 + return ResponseEntity.ok(true);
102 } 116 }
103 117
104 /** 118 /**
@@ -21,6 +21,7 @@ public class YtScriptFactory { @@ -21,6 +21,7 @@ public class YtScriptFactory {
21 21
22 public static final String RULE_NODE_FUNCTION_NAME = "tcpTransportFunc"; 22 public static final String RULE_NODE_FUNCTION_NAME = "tcpTransportFunc";
23 23
  24 + public static final String INCLUD_ORIGINAL_DATA = "out.source=params;";
24 private static final String JS_WRAPPER_PREFIX_TEMPLATE = "function %s(params) { " + 25 private static final String JS_WRAPPER_PREFIX_TEMPLATE = "function %s(params) { " +
25 " var out = new Object(); " + 26 " var out = new Object(); " +
26 " return JSON.stringify(%s(params));" + 27 " return JSON.stringify(%s(params));" +