Commit c7a79f7f9fe504a07eb151c1cf663fc4246e559b

Authored by 芯火源
1 parent 112adacf

refactor: 业务接口返回TCP解析脚本数据

... ... @@ -61,6 +61,7 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
61 61 import org.thingsboard.server.common.data.security.DeviceCredentials;
62 62 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
63 63 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
  64 +import org.thingsboard.server.common.data.yunteng.dto.TkDeviceScriptDTO;
64 65 import org.thingsboard.server.common.data.yunteng.enums.TkScriptFunctionType;
65 66 import org.thingsboard.server.common.msg.EncryptionUtil;
66 67 import org.thingsboard.server.common.msg.TbMsg;
... ... @@ -76,6 +77,7 @@ import org.thingsboard.server.dao.device.provision.ProvisionResponse;
76 77 import org.thingsboard.server.dao.ota.OtaPackageService;
77 78 import org.thingsboard.server.dao.relation.RelationService;
78 79 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
  80 +import org.thingsboard.server.dao.yunteng.service.TkDeviceScriptService;
79 81 import org.thingsboard.server.dao.yunteng.service.TkDeviceService;
80 82 import org.thingsboard.server.gen.transport.TransportProtos;
81 83 import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto;
... ... @@ -103,6 +105,8 @@ import org.thingsboard.server.service.install.DefaultSystemDataLoaderService;
103 105 import org.thingsboard.server.service.profile.TbDeviceProfileCache;
104 106 import org.thingsboard.server.service.resource.TbResourceService;
105 107
  108 +import java.util.Arrays;
  109 +import java.util.List;
106 110 import java.util.Optional;
107 111 import java.util.UUID;
108 112 import java.util.concurrent.ConcurrentHashMap;
... ... @@ -130,6 +134,7 @@ public class DefaultTransportApiService implements TransportApiService {
130 134 private final TbApiUsageStateService apiUsageStateService;
131 135 private final DeviceService deviceService;
132 136 private final TkDeviceService ytDeviceService;
  137 + private final TkDeviceScriptService scriptService;
133 138 private final RelationService relationService;
134 139 private final DeviceCredentialsService deviceCredentialsService;
135 140 private final DbCallbackExecutorService dbCallbackExecutorService;
... ... @@ -676,12 +681,28 @@ public class DefaultTransportApiService implements TransportApiService {
676 681 private ListenableFuture<TransportApiResponseMsg> handle(TransportProtos.ScriptProto requestMsg) {
677 682
678 683 int type = requestMsg.getFunctionType();
679   - DeviceId deviceId = new DeviceId(new UUID(requestMsg.getScriptIdMSB(), requestMsg.getScriptIdLSB()));
680   - DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(TenantId.SYS_TENANT_ID, deviceId);
681   -
682   - return Futures.immediateFuture(TransportApiResponseMsg.newBuilder()
683   - .setDeviceCredentialsResponseMsg(TransportProtos.GetDeviceCredentialsResponseMsg.newBuilder()
684   - .setDeviceCredentialsData(ByteString.copyFrom(dataDecodingEncodingService.encode(deviceCredentials))))
685   - .build());
  684 + UUID scriptId = getUUID(requestMsg.getScriptIdLSB(),requestMsg.getScriptIdMSB());
  685 +
  686 + List<TkDeviceScriptDTO> allScriptes = scriptService.getScriptes(Arrays.stream(TkScriptFunctionType.values()).filter(f->f.ordinal()==type).findFirst().get(),null,null,scriptId);
  687 + TransportApiResponseMsg.Builder responseBuilder = TransportApiResponseMsg.newBuilder();
  688 + allScriptes.forEach(item->{
  689 + UUID tenantId = UUID.fromString(item.getTenantId());
  690 + UUID id = UUID.fromString(item.getId());
  691 + responseBuilder.addScriptsResponseMsg(TransportProtos.ScriptProto.newBuilder()
  692 + .setConvertJs(item.getConvertJs())
  693 + .setTenantIdLSB(tenantId.getLeastSignificantBits())
  694 + .setTenantIdMSB(tenantId.getMostSignificantBits())
  695 + .setScriptIdLSB(id.getLeastSignificantBits())
  696 + .setScriptIdMSB(id.getMostSignificantBits())
  697 + .setFunctionType(item.getScriptType().ordinal())
  698 + );
  699 + });
  700 + return Futures.immediateFuture(responseBuilder.build());
  701 + }
  702 + private UUID getUUID(long lsb,long msb){
  703 + if(lsb==0 && msb==0){
  704 + return null;
  705 + }
  706 + return new UUID(msb,lsb);
686 707 }
687 708 }
... ...
... ... @@ -698,7 +698,7 @@ message TransportApiResponseMsg {
698 698 GetDeviceCredentialsResponseMsg deviceCredentialsResponseMsg = 10;
699 699
700 700 //Thingskit function
701   - repeated ScriptProto scripts = 11;
  701 + repeated ScriptProto scriptsResponseMsg = 11;
702 702 }
703 703
704 704 /* Messages that are handled by ThingsBoard Core Service */
... ... @@ -783,12 +783,12 @@ message ToOtaPackageStateServiceMsg {
783 783
784 784
785 785
786   -//Thingskit function
  786 +//Thingskit function 请求:functionType、;响应:tenantId、projectId、scriptId、convertJs
787 787 message ScriptProto{
788 788 int64 tenantIdMSB = 1;
789 789 int64 tenantIdLSB = 2;
790   - int64 projectIdMSB = 3;
791   - int64 projectIdLSB = 4;
  790 +// int64 projectIdMSB = 3;
  791 +// int64 projectIdLSB = 4;
792 792 int64 scriptIdMSB = 5;
793 793 int64 scriptIdLSB = 6;
794 794 int32 functionType = 7;
... ...
... ... @@ -1260,7 +1260,7 @@ public class DefaultTransportService implements TransportService {
1260 1260 new TbProtoQueueMsg<>(UUID.randomUUID(), TransportProtos.TransportApiRequestMsg.newBuilder().setScript(msg).build());
1261 1261 try {
1262 1262 TbProtoQueueMsg<TransportApiResponseMsg> response = transportApiRequestTemplate.send(protoMsg).get();
1263   - return response.getValue().getScriptsList();
  1263 + return response.getValue().getScriptsResponseMsgList();
1264 1264 } catch (InterruptedException | ExecutionException e) {
1265 1265 throw new RuntimeException(e);
1266 1266 }
... ...
... ... @@ -18,6 +18,7 @@ import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
18 18 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO;
19 19 import org.thingsboard.server.common.data.yunteng.dto.TkCustomerDeviceDTO;
20 20 import org.thingsboard.server.common.data.yunteng.dto.TkDeviceScriptDTO;
  21 +import org.thingsboard.server.common.data.yunteng.enums.TkScriptFunctionType;
21 22 import org.thingsboard.server.common.data.yunteng.enums.TransportTypeEnum;
22 23 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
23 24 import org.thingsboard.server.dao.yunteng.entities.TkDeviceProfileEntity;
... ... @@ -100,14 +101,22 @@ public class TkDeviceScriptServiceImpl
100 101 }
101 102
102 103 @Override
103   - public String getScriptText(String tenantId, String scriptId) {
  104 + public List<TkDeviceScriptDTO> getScriptes(TkScriptFunctionType type, UUID tenantId, UUID projectId, UUID scriptId){
104 105 LambdaQueryWrapper<TkDeviceScriptEntity> queryWrapper =
105   - new QueryWrapper<TkDeviceScriptEntity>()
106   - .lambda()
107   - .eq(TkDeviceScriptEntity::getTenantId, tenantId)
108   - .eq(TkDeviceScriptEntity::getId, scriptId);
109   - TkDeviceScriptEntity result = baseMapper.selectOne(queryWrapper);
110   - return (result == null || result.getStatus() == 0) ? null : result.getConvertJs();
  106 + new QueryWrapper<TkDeviceScriptEntity>().lambda();
  107 + if(type != null){
  108 + queryWrapper.eq(TkDeviceScriptEntity::getScriptType, type);
  109 + }
  110 + if(tenantId != null){
  111 + queryWrapper.eq(TkDeviceScriptEntity::getTenantId, tenantId.toString());
  112 + }
  113 + if(scriptId != null){
  114 + queryWrapper.eq(TkDeviceScriptEntity::getId, scriptId.toString());
  115 + }
  116 + List<TkDeviceScriptEntity> result = baseMapper.selectList(queryWrapper);
  117 + return result.stream()
  118 + .map(item -> item.getDTO(TkDeviceScriptDTO.class))
  119 + .collect(Collectors.toList());
111 120 }
112 121
113 122 @Override
... ...
1 1 package org.thingsboard.server.dao.yunteng.service;
2 2
3 3 import org.thingsboard.server.common.data.yunteng.dto.TkDeviceScriptDTO;
  4 +import org.thingsboard.server.common.data.yunteng.enums.TkScriptFunctionType;
4 5 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
5 6 import org.thingsboard.server.dao.yunteng.entities.TkDeviceScriptEntity;
6 7
7   -import java.util.List;
8   -import java.util.Map;
9   -import java.util.Optional;
10   -import java.util.Set;
  8 +import java.util.*;
11 9
12 10 public interface TkDeviceScriptService extends BaseService<TkDeviceScriptEntity>{
13 11
... ... @@ -17,7 +15,7 @@ public interface TkDeviceScriptService extends BaseService<TkDeviceScriptEntity>
17 15 * @param scriptId
18 16 * @return
19 17 */
20   - String getScriptText(String tenantId, String scriptId);
  18 + List<TkDeviceScriptDTO> getScriptes(TkScriptFunctionType type, UUID tenantId, UUID projectId, UUID scriptId);
21 19
22 20 TkDeviceScriptDTO insertOrUpdate(TkDeviceScriptDTO deviceDTO);
23 21
... ...