Refactoring according to comments
This commit is contained in:
parent
6683158c6b
commit
93278f7236
@ -16,6 +16,7 @@
|
||||
package org.thingsboard.server.service.device;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
@ -28,9 +29,6 @@ import org.thingsboard.server.common.data.DataConstants;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.DeviceProfile;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
|
||||
import org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration;
|
||||
import org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.id.UserId;
|
||||
@ -49,6 +47,7 @@ import org.thingsboard.server.dao.device.DeviceDao;
|
||||
import org.thingsboard.server.dao.device.DeviceProfileDao;
|
||||
import org.thingsboard.server.dao.device.DeviceProvisionService;
|
||||
import org.thingsboard.server.dao.device.DeviceService;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponse;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus;
|
||||
@ -115,81 +114,55 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
|
||||
String provisionRequestSecret = provisionRequest.getCredentials().getProvisionDeviceSecret();
|
||||
|
||||
if (StringUtils.isEmpty(provisionRequestKey) || StringUtils.isEmpty(provisionRequestSecret)) {
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
if (provisionRequest.getCredentialsType() != null) {
|
||||
ListenableFuture<ProvisionResponse> error = validateCredentials(provisionRequest);
|
||||
if (error != null) {
|
||||
return error;
|
||||
}
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
|
||||
}
|
||||
|
||||
DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKey(provisionRequestKey);
|
||||
|
||||
if (targetProfile == null) {
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
|
||||
if (targetProfile == null || targetProfile.getProfileData().getProvisionConfiguration() == null ||
|
||||
targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret() == null) {
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
|
||||
}
|
||||
|
||||
Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null);
|
||||
|
||||
switch (targetProfile.getProvisionType()) {
|
||||
case ALLOW_CREATE_NEW_DEVICES:
|
||||
if (((AllowCreateNewDevicesDeviceProfileProvisionConfiguration) targetProfile.getProfileData().getProvisionConfiguration()).getProvisionDeviceSecret().equals(provisionRequestSecret)) {
|
||||
if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
|
||||
if (targetDevice != null) {
|
||||
log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
|
||||
notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
} else {
|
||||
return createDevice(provisionRequest, targetProfile);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CHECK_PRE_PROVISIONED_DEVICES:
|
||||
if (((CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration) targetProfile.getProfileData().getProvisionConfiguration()).getProvisionDeviceSecret().equals(provisionRequestSecret)) {
|
||||
if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
|
||||
if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())) {
|
||||
return processProvision(targetDevice, provisionRequest);
|
||||
} else {
|
||||
log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
private ListenableFuture<ProvisionResponse> validateCredentials(ProvisionRequest provisionRequest) {
|
||||
switch (provisionRequest.getCredentialsType()) {
|
||||
case MQTT_BASIC:
|
||||
if (StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId()) ||
|
||||
StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) ||
|
||||
StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword())) {
|
||||
log.error("Failed to get basic mqtt credentials from credentials data!");
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
|
||||
}
|
||||
break;
|
||||
case X509_CERTIFICATE:
|
||||
if (StringUtils.isEmpty(provisionRequest.getCredentialsData().getHash())) {
|
||||
log.error("Failed to get hash from credentials data!");
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
|
||||
}
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
|
||||
}
|
||||
|
||||
private ListenableFuture<ProvisionResponse> processProvision(Device device, ProvisionRequest provisionRequest) {
|
||||
ListenableFuture<Optional<AttributeKvEntry>> provisionStateFuture = attributesService.find(device.getTenantId(), device.getId(),
|
||||
DataConstants.SERVER_SCOPE, DEVICE_PROVISION_STATE);
|
||||
ListenableFuture<Boolean> provisionedFuture = Futures.transformAsync(provisionStateFuture, optionalAtr -> {
|
||||
if (optionalAtr.isPresent()) {
|
||||
if (optionalAtr != null && optionalAtr.isPresent()) {
|
||||
String state = optionalAtr.get().getValueAsString();
|
||||
if (state.equals(PROVISIONED_STATE)) {
|
||||
return Futures.immediateFuture(true);
|
||||
} else {
|
||||
log.error("[{}][{}] Unknown provision state: {}!", device.getName(), DEVICE_PROVISION_STATE, state);
|
||||
return Futures.immediateCancelledFuture();
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
}
|
||||
}
|
||||
return Futures.transform(saveProvisionStateAttribute(device), input -> false, MoreExecutors.directExecutor());
|
||||
@ -200,7 +173,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
|
||||
return Futures.transform(provisionedFuture, provisioned -> {
|
||||
if (provisioned) {
|
||||
notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
||||
return new ProvisionResponse(null, ProvisionResponseStatus.FAILURE);
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
}
|
||||
notify(device, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
|
||||
return new ProvisionResponse(deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId()), ProvisionResponseStatus.SUCCESS);
|
||||
@ -224,7 +197,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
|
||||
private ListenableFuture<ProvisionResponse> processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
||||
Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName());
|
||||
if (device == null) {
|
||||
Device savedDevice = saveDevice(provisionRequest, profile);
|
||||
Device savedDevice = deviceService.saveDevice(provisionRequest, profile);
|
||||
|
||||
deviceStateService.onDeviceAdded(savedDevice);
|
||||
pushDeviceCreatedEventToRuleEngine(savedDevice);
|
||||
@ -234,10 +207,11 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
|
||||
new ProvisionResponse(
|
||||
getDeviceCredentials(savedDevice),
|
||||
ProvisionResponseStatus.SUCCESS), MoreExecutors.directExecutor());
|
||||
} else {
|
||||
log.warn("[{}] The device is already provisioned!", device.getName());
|
||||
notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
}
|
||||
log.warn("[{}] The device is already provisioned!", device.getName());
|
||||
notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
||||
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
|
||||
}
|
||||
|
||||
private ListenableFuture<List<Void>> saveProvisionStateAttribute(Device device) {
|
||||
@ -246,51 +220,16 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
|
||||
System.currentTimeMillis())));
|
||||
}
|
||||
|
||||
private Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
||||
Device device = new Device();
|
||||
device.setName(provisionRequest.getDeviceName());
|
||||
device.setType(profile.getName());
|
||||
device.setTenantId(profile.getTenantId());
|
||||
Device savedDevice = deviceService.saveDevice(device);
|
||||
if (!StringUtils.isEmpty(provisionRequest.getCredentialsData().getToken()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getHash()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId())) {
|
||||
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getTenantId(), savedDevice.getId());
|
||||
deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
|
||||
switch (provisionRequest.getCredentialsType()) {
|
||||
case ACCESS_TOKEN:
|
||||
deviceCredentials.setDeviceId(savedDevice.getId());
|
||||
deviceCredentials.setCredentialsId(provisionRequest.getCredentialsData().getToken());
|
||||
break;
|
||||
case MQTT_BASIC:
|
||||
BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
|
||||
mqttCredentials.setClientId(provisionRequest.getCredentialsData().getClientId());
|
||||
mqttCredentials.setUserName(provisionRequest.getCredentialsData().getUsername());
|
||||
mqttCredentials.setPassword(provisionRequest.getCredentialsData().getPassword());
|
||||
deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
|
||||
break;
|
||||
case X509_CERTIFICATE:
|
||||
deviceCredentials.setCredentialsValue(provisionRequest.getCredentialsData().getHash());
|
||||
break;
|
||||
}
|
||||
deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
|
||||
deviceCredentialsService.updateDeviceCredentials(savedDevice.getTenantId(), deviceCredentials);
|
||||
}
|
||||
return savedDevice;
|
||||
}
|
||||
|
||||
private DeviceCredentials getDeviceCredentials(Device device) {
|
||||
return deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId());
|
||||
}
|
||||
|
||||
private void pushProvisionEventToRuleEngine(ProvisionRequest request, Device device, String type) {
|
||||
try {
|
||||
ObjectNode entityNode = JacksonUtil.OBJECT_MAPPER.valueToTree(request);
|
||||
TbMsg msg = TbMsg.newMsg(type, device.getId(), createTbMsgMetaData(device), JacksonUtil.OBJECT_MAPPER.writeValueAsString(entityNode));
|
||||
JsonNode entityNode = JacksonUtil.valueToTree(request);
|
||||
TbMsg msg = TbMsg.newMsg(type, device.getId(), createTbMsgMetaData(device), JacksonUtil.toString(entityNode));
|
||||
sendToRuleEngine(device.getTenantId(), msg, null);
|
||||
} catch (JsonProcessingException | IllegalArgumentException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("[{}] Failed to push device action to rule engine: {}", device.getId(), type, e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,13 +50,11 @@ import org.thingsboard.server.dao.device.DeviceProvisionService;
|
||||
import org.thingsboard.server.dao.device.DeviceService;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponse;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus;
|
||||
import org.thingsboard.server.dao.relation.RelationService;
|
||||
import org.thingsboard.server.dao.tenant.TenantProfileService;
|
||||
import org.thingsboard.server.dao.tenant.TenantService;
|
||||
import org.thingsboard.server.dao.util.mapping.JacksonUtil;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.CredentialsType;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg;
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg;
|
||||
@ -70,6 +68,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceTokenR
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceX509CertRequestMsg;
|
||||
import org.thingsboard.server.queue.common.TbProtoQueueMsg;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
|
||||
import org.thingsboard.server.service.executors.DbCallbackExecutorService;
|
||||
import org.thingsboard.server.service.queue.TbClusterService;
|
||||
import org.thingsboard.server.service.state.DeviceStateService;
|
||||
@ -276,30 +275,29 @@ public class DefaultTransportApiService implements TransportApiService {
|
||||
}, dbCallbackExecutorService);
|
||||
}
|
||||
|
||||
|
||||
private ListenableFuture<TransportApiResponseMsg> handle(ProvisionDeviceRequestMsg requestMsg) {
|
||||
ListenableFuture<ProvisionResponse> provisionResponseFuture = null;
|
||||
provisionResponseFuture = deviceProvisionService.provisionDevice(
|
||||
new ProvisionRequest(
|
||||
requestMsg.getDeviceName(),
|
||||
requestMsg.getCredentialsType() != null ? DeviceCredentialsType.valueOf(requestMsg.getCredentialsType().name()) : null,
|
||||
new ProvisionDeviceCredentialsData(requestMsg.getCredentialsDataProto().getValidateDeviceTokenRequestMsg().getToken(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getClientId(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getUserName(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getPassword(),
|
||||
requestMsg.getCredentialsDataProto().getValidateDeviceX509CertRequestMsg().getHash()),
|
||||
new ProvisionDeviceProfileCredentials(
|
||||
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceKey(),
|
||||
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret())));
|
||||
return Futures.transform(provisionResponseFuture, provisionResponse -> {
|
||||
if (provisionResponse.getResponseStatus() == ProvisionResponseStatus.NOT_FOUND) {
|
||||
return getTransportApiResponseMsg(TransportProtos.DeviceCredentialsProto.getDefaultInstance(), TransportProtos.ProvisionResponseStatus.NOT_FOUND);
|
||||
} else if (provisionResponse.getResponseStatus() == ProvisionResponseStatus.FAILURE) {
|
||||
return getTransportApiResponseMsg(TransportProtos.DeviceCredentialsProto.getDefaultInstance(), TransportProtos.ProvisionResponseStatus.FAILURE);
|
||||
} else {
|
||||
return getTransportApiResponseMsg(getDeviceCredentials(provisionResponse.getDeviceCredentials()), TransportProtos.ProvisionResponseStatus.SUCCESS);
|
||||
}
|
||||
}, dbCallbackExecutorService);
|
||||
try {
|
||||
provisionResponseFuture = deviceProvisionService.provisionDevice(
|
||||
new ProvisionRequest(
|
||||
requestMsg.getDeviceName(),
|
||||
requestMsg.getCredentialsType() != null ? DeviceCredentialsType.valueOf(requestMsg.getCredentialsType().name()) : null,
|
||||
new ProvisionDeviceCredentialsData(requestMsg.getCredentialsDataProto().getValidateDeviceTokenRequestMsg().getToken(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getClientId(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getUserName(),
|
||||
requestMsg.getCredentialsDataProto().getValidateBasicMqttCredRequestMsg().getPassword(),
|
||||
requestMsg.getCredentialsDataProto().getValidateDeviceX509CertRequestMsg().getHash()),
|
||||
new ProvisionDeviceProfileCredentials(
|
||||
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceKey(),
|
||||
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret())));
|
||||
} catch (ProvisionFailedException e) {
|
||||
return Futures.immediateFuture(getTransportApiResponseMsg(
|
||||
TransportProtos.DeviceCredentialsProto.getDefaultInstance(),
|
||||
TransportProtos.ProvisionResponseStatus.valueOf(e.getMessage())));
|
||||
}
|
||||
return Futures.transform(provisionResponseFuture, provisionResponse -> getTransportApiResponseMsg(
|
||||
getDeviceCredentials(provisionResponse.getDeviceCredentials()), TransportProtos.ProvisionResponseStatus.SUCCESS),
|
||||
dbCallbackExecutorService);
|
||||
}
|
||||
|
||||
private TransportApiResponseMsg getTransportApiResponseMsg(TransportProtos.DeviceCredentialsProto deviceCredentials, TransportProtos.ProvisionResponseStatus status) {
|
||||
|
||||
@ -16,9 +16,13 @@
|
||||
package org.thingsboard.server.dao.device;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.DeviceProfile;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponse;
|
||||
|
||||
public interface DeviceProvisionService {
|
||||
ListenableFuture<ProvisionResponse> provisionDevice(ProvisionRequest provisionRequest);
|
||||
|
||||
ListenableFuture<ProvisionResponse> provisionDevice(ProvisionRequest provisionRequest) throws ProvisionFailedException;
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.server.dao.device;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.DeviceInfo;
|
||||
import org.thingsboard.server.common.data.DeviceProfile;
|
||||
import org.thingsboard.server.common.data.EntitySubtype;
|
||||
import org.thingsboard.server.common.data.device.DeviceSearchQuery;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
@ -26,6 +27,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -83,4 +85,6 @@ public interface DeviceService {
|
||||
|
||||
Device assignDeviceToTenant(TenantId tenantId, Device device);
|
||||
|
||||
Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile);
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright © 2016-2020 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao.device.provision;
|
||||
|
||||
public class ProvisionFailedException extends RuntimeException {
|
||||
public ProvisionFailedException(String errorMsg) {
|
||||
super(errorMsg);
|
||||
}
|
||||
}
|
||||
@ -20,5 +20,4 @@ public enum ProvisionResponseStatus {
|
||||
SUCCESS,
|
||||
NOT_FOUND,
|
||||
FAILURE
|
||||
|
||||
}
|
||||
|
||||
@ -23,5 +23,5 @@ public class ProvisionDeviceCredentialsData {
|
||||
private final String clientId;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final String hash;
|
||||
private final String x509CertHash;
|
||||
}
|
||||
|
||||
@ -33,7 +33,9 @@ import org.thingsboard.server.common.data.DeviceProfileProvisionType;
|
||||
@JsonSubTypes.Type(value = CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.class, name = "CHECK_PRE_PROVISIONED_DEVICES")})
|
||||
public interface DeviceProfileProvisionConfiguration {
|
||||
|
||||
String getProvisionDeviceSecret();
|
||||
|
||||
@JsonIgnore
|
||||
DeviceProfileProvisionType getType();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,11 +268,6 @@ message ProvisionDeviceRequestMsg {
|
||||
CredentialsDataProto credentialsDataProto = 4;
|
||||
}
|
||||
|
||||
message GatewayProvisionRequestMsg {
|
||||
int32 requestId = 1;
|
||||
ProvisionDeviceRequestMsg provisionDeviceRequestMsg = 2;
|
||||
}
|
||||
|
||||
message ProvisionDeviceCredentialsMsg {
|
||||
string provisionDeviceKey = 1;
|
||||
string provisionDeviceSecret = 2;
|
||||
@ -283,11 +278,6 @@ message ProvisionDeviceResponseMsg {
|
||||
ProvisionResponseStatus provisionResponseStatus = 2;
|
||||
}
|
||||
|
||||
message GatewayProvisionResponseMsg {
|
||||
int32 requestId = 1;
|
||||
ProvisionDeviceResponseMsg provisionDeviceResponseMsg = 2;
|
||||
}
|
||||
|
||||
enum ProvisionResponseStatus {
|
||||
UNKNOWN = 0;
|
||||
SUCCESS = 1;
|
||||
|
||||
@ -72,11 +72,6 @@ message GatewayAttributesMsg {
|
||||
repeated AttributesMsg msg = 1;
|
||||
}
|
||||
|
||||
message GatewayProvisionResponseMsg {
|
||||
string deviceName = 1;
|
||||
transport.ProvisionDeviceResponseMsg provisionDeviceResponseMsg = 2;
|
||||
}
|
||||
|
||||
message GatewayRpcResponseMsg {
|
||||
string deviceName = 1;
|
||||
int32 id = 2;
|
||||
|
||||
@ -40,6 +40,7 @@ import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.EntityView;
|
||||
import org.thingsboard.server.common.data.Tenant;
|
||||
import org.thingsboard.server.common.data.device.DeviceSearchQuery;
|
||||
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
|
||||
import org.thingsboard.server.common.data.device.data.DefaultDeviceConfiguration;
|
||||
import org.thingsboard.server.common.data.device.data.DefaultDeviceTransportConfiguration;
|
||||
import org.thingsboard.server.common.data.device.data.DeviceData;
|
||||
@ -57,6 +58,9 @@ import org.thingsboard.server.common.data.relation.EntitySearchDirection;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
|
||||
import org.thingsboard.server.dao.customer.CustomerDao;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
|
||||
import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus;
|
||||
import org.thingsboard.server.dao.entity.AbstractEntityService;
|
||||
import org.thingsboard.server.dao.entityview.EntityViewService;
|
||||
import org.thingsboard.server.dao.event.EventService;
|
||||
@ -64,6 +68,7 @@ import org.thingsboard.server.dao.exception.DataValidationException;
|
||||
import org.thingsboard.server.dao.service.DataValidator;
|
||||
import org.thingsboard.server.dao.service.PaginatedRemover;
|
||||
import org.thingsboard.server.dao.tenant.TenantDao;
|
||||
import org.thingsboard.server.dao.util.mapping.JacksonUtil;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
@ -466,6 +471,47 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
|
||||
return doSaveDevice(device, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
||||
Device device = new Device();
|
||||
device.setName(provisionRequest.getDeviceName());
|
||||
device.setType(profile.getName());
|
||||
device.setTenantId(profile.getTenantId());
|
||||
Device savedDevice = saveDevice(device);
|
||||
if (!StringUtils.isEmpty(provisionRequest.getCredentialsData().getToken()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getX509CertHash()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword()) ||
|
||||
!StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId())) {
|
||||
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getTenantId(), savedDevice.getId());
|
||||
deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
|
||||
switch (provisionRequest.getCredentialsType()) {
|
||||
case ACCESS_TOKEN:
|
||||
deviceCredentials.setDeviceId(savedDevice.getId());
|
||||
deviceCredentials.setCredentialsId(provisionRequest.getCredentialsData().getToken());
|
||||
break;
|
||||
case MQTT_BASIC:
|
||||
BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
|
||||
mqttCredentials.setClientId(provisionRequest.getCredentialsData().getClientId());
|
||||
mqttCredentials.setUserName(provisionRequest.getCredentialsData().getUsername());
|
||||
mqttCredentials.setPassword(provisionRequest.getCredentialsData().getPassword());
|
||||
deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
|
||||
break;
|
||||
case X509_CERTIFICATE:
|
||||
deviceCredentials.setCredentialsValue(provisionRequest.getCredentialsData().getX509CertHash());
|
||||
break;
|
||||
}
|
||||
deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
|
||||
try {
|
||||
deviceCredentialsService.updateDeviceCredentials(savedDevice.getTenantId(), deviceCredentials);
|
||||
} catch (Exception e) {
|
||||
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
||||
}
|
||||
}
|
||||
return savedDevice;
|
||||
}
|
||||
|
||||
private DataValidator<Device> deviceValidator =
|
||||
new DataValidator<Device>() {
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user