|
|
|
@ -16,6 +16,7 @@ |
|
|
|
package org.thingsboard.server.service.transport; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
@ -27,6 +28,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.cache.ota.OtaPackageDataCache; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
import org.thingsboard.server.common.data.ApiUsageState; |
|
|
|
import org.thingsboard.server.common.data.DataConstants; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
@ -56,7 +58,6 @@ import org.thingsboard.server.common.data.ota.OtaPackageUtil; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
|
|
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
import org.thingsboard.server.common.msg.EncryptionUtil; |
|
|
|
@ -96,10 +97,8 @@ import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|
|
|
import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|
|
|
import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
import org.thingsboard.server.service.resource.TbResourceService; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
@ -138,6 +137,10 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
|
|
|
|
private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
private static boolean checkIsMqttCredentials(DeviceCredentials credentials) { |
|
|
|
return credentials != null && DeviceCredentialsType.MQTT_BASIC.equals(credentials.getCredentialsType()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> handle(TbProtoQueueMsg<TransportApiRequestMsg> tbProtoQueueMsg) { |
|
|
|
TransportApiRequestMsg transportApiRequestMsg = tbProtoQueueMsg.getValue(); |
|
|
|
@ -234,10 +237,6 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
return getEmptyTransportApiResponseFuture(); |
|
|
|
} |
|
|
|
|
|
|
|
private static boolean checkIsMqttCredentials(DeviceCredentials credentials) { |
|
|
|
return credentials != null && DeviceCredentialsType.MQTT_BASIC.equals(credentials.getCredentialsType()); |
|
|
|
} |
|
|
|
|
|
|
|
private DeviceCredentials checkMqttCredentials(TransportProtos.ValidateBasicMqttCredRequestMsg clientCred, String credId) { |
|
|
|
return checkMqttCredentials(clientCred, deviceCredentialsService.findDeviceCredentialsByCredentialsId(credId)); |
|
|
|
} |
|
|
|
@ -286,6 +285,9 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
device.setCustomerId(gateway.getCustomerId()); |
|
|
|
DeviceProfile deviceProfile = deviceProfileCache.findOrCreateDeviceProfile(gateway.getTenantId(), requestMsg.getDeviceType()); |
|
|
|
device.setDeviceProfileId(deviceProfile.getId()); |
|
|
|
ObjectNode additionalInfo = JacksonUtil.newObjectNode(); |
|
|
|
additionalInfo.put(DataConstants.LAST_CONNECTED_GATEWAY, gatewayId.toString()); |
|
|
|
device.setAdditionalInfo(additionalInfo); |
|
|
|
Device savedDevice = deviceService.saveDevice(device); |
|
|
|
tbClusterService.onDeviceUpdated(savedDevice, null); |
|
|
|
device = savedDevice; |
|
|
|
@ -303,18 +305,17 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
ObjectNode entityNode = mapper.valueToTree(device); |
|
|
|
TbMsg tbMsg = TbMsg.newMsg(DataConstants.ENTITY_CREATED, deviceId, customerId, metaData, TbMsgDataType.JSON, mapper.writeValueAsString(entityNode)); |
|
|
|
tbClusterService.pushMsgToRuleEngine(tenantId, deviceId, tbMsg, null); |
|
|
|
} |
|
|
|
|
|
|
|
List<EntityRelation> currentLastConnectedGatewayRelationList = relationService.findByFromAndType(TenantId.SYS_TENANT_ID, device.getId(), DataConstants.LAST_CONNECTED_GATEWAY, RelationTypeGroup.COMMON); |
|
|
|
EntityRelation lastConnectedGatewayRelation; |
|
|
|
if (!currentLastConnectedGatewayRelationList.isEmpty()) { |
|
|
|
lastConnectedGatewayRelation = currentLastConnectedGatewayRelationList.get(0); |
|
|
|
lastConnectedGatewayRelation.setTo(gateway.getId()); |
|
|
|
} else { |
|
|
|
lastConnectedGatewayRelation = new EntityRelation(device.getId(), gateway.getId(), DataConstants.LAST_CONNECTED_GATEWAY); |
|
|
|
JsonNode deviceAdditionalInfo = device.getAdditionalInfo(); |
|
|
|
if (deviceAdditionalInfo == null) { |
|
|
|
deviceAdditionalInfo = JacksonUtil.newObjectNode(); |
|
|
|
} |
|
|
|
if (!deviceAdditionalInfo.has(DataConstants.LAST_CONNECTED_GATEWAY) || !gatewayId.toString().equals(deviceAdditionalInfo.get(DataConstants.LAST_CONNECTED_GATEWAY).asText())) { |
|
|
|
ObjectNode newDeviceAdditionalInfo = (ObjectNode) deviceAdditionalInfo; |
|
|
|
newDeviceAdditionalInfo.put(DataConstants.LAST_CONNECTED_GATEWAY, gatewayId.toString()); |
|
|
|
deviceService.saveDevice(device); |
|
|
|
} |
|
|
|
} |
|
|
|
relationService.saveRelationAsync(TenantId.SYS_TENANT_ID, lastConnectedGatewayRelation); |
|
|
|
|
|
|
|
GetOrCreateDeviceFromGatewayResponseMsg.Builder builder = GetOrCreateDeviceFromGatewayResponseMsg.newBuilder() |
|
|
|
.setDeviceInfo(getDeviceInfoProto(device)); |
|
|
|
DeviceProfile deviceProfile = deviceProfileCache.get(device.getTenantId(), device.getDeviceProfileId()); |
|
|
|
@ -359,7 +360,8 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
private TransportApiResponseMsg getTransportApiResponseMsg(DeviceCredentials deviceCredentials, TransportProtos.ResponseStatus status) { |
|
|
|
private TransportApiResponseMsg getTransportApiResponseMsg(DeviceCredentials |
|
|
|
deviceCredentials, TransportProtos.ResponseStatus status) { |
|
|
|
if (!status.equals(TransportProtos.ResponseStatus.SUCCESS)) { |
|
|
|
return TransportApiResponseMsg.newBuilder().setProvisionDeviceResponseMsg(TransportProtos.ProvisionDeviceResponseMsg.newBuilder().setStatus(status).build()).build(); |
|
|
|
} |
|
|
|
@ -602,7 +604,8 @@ public class DefaultTransportApiService implements TransportApiService { |
|
|
|
.build()); |
|
|
|
} |
|
|
|
|
|
|
|
private ListenableFuture<TransportApiResponseMsg> handleRegistration(TransportProtos.LwM2MRegistrationRequestMsg msg) { |
|
|
|
private ListenableFuture<TransportApiResponseMsg> handleRegistration |
|
|
|
(TransportProtos.LwM2MRegistrationRequestMsg msg) { |
|
|
|
TenantId tenantId = new TenantId(UUID.fromString(msg.getTenantId())); |
|
|
|
String deviceName = msg.getEndpoint(); |
|
|
|
Lock deviceCreationLock = deviceCreationLocks.computeIfAbsent(deviceName, id -> new ReentrantLock()); |
|
|
|
|