Commit 1bb1f5be8b34a7f1955cc36929f264a3df4d2037

Authored by zbeacon
1 parent 2704696f

Refactoring

... ... @@ -27,8 +27,9 @@ import org.springframework.util.StringUtils;
27 27 import org.thingsboard.server.common.data.DataConstants;
28 28 import org.thingsboard.server.common.data.Device;
29 29 import org.thingsboard.server.common.data.DeviceProfile;
30   -import org.thingsboard.server.common.data.DeviceProfileProvisionType;
31 30 import org.thingsboard.server.common.data.audit.ActionType;
  31 +import org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration;
  32 +import org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration;
32 33 import org.thingsboard.server.common.data.id.CustomerId;
33 34 import org.thingsboard.server.common.data.id.TenantId;
34 35 import org.thingsboard.server.common.data.id.UserId;
... ... @@ -115,11 +116,9 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
115 116 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
116 117 }
117 118
118   - DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionRequestKey, provisionRequestSecret);
  119 + DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKey(provisionRequestKey);
119 120
120   - if (targetProfile == null ||
121   - !(targetProfile.getProvisionType() != DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES ||
122   - targetProfile.getProvisionType() != DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES)) {
  121 + if (targetProfile == null) {
123 122 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
124 123 }
125 124
... ... @@ -127,23 +126,28 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
127 126
128 127 switch(targetProfile.getProvisionType()) {
129 128 case ALLOW_CREATE_NEW_DEVICES:
130   - if (targetDevice != null) {
131   - log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
132   - notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
133   - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
134   - } else {
135   - return createDevice(provisionRequest, targetProfile);
  129 + if (((AllowCreateNewDevicesDeviceProfileProvisionConfiguration) targetProfile.getProfileData().getProvisionConfiguration()).getProvisionDeviceSecret().equals(provisionRequestSecret)){
  130 + if (targetDevice != null) {
  131 + log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
  132 + notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
  133 + return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
  134 + } else {
  135 + return createDevice(provisionRequest, targetProfile);
  136 + }
136 137 }
  138 + break;
137 139 case CHECK_PRE_PROVISIONED_DEVICES:
138   - if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())){
139   - return processProvision(targetDevice, provisionRequest);
140   - } else {
141   - log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
142   - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
  140 + if (((CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration) targetProfile.getProfileData().getProvisionConfiguration()).getProvisionDeviceSecret().equals(provisionRequestSecret)) {
  141 + if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())) {
  142 + return processProvision(targetDevice, provisionRequest);
  143 + } else {
  144 + log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
  145 + return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
  146 + }
143 147 }
144   - default:
145   - throw new RuntimeException("Strategy is not supported - " + targetProfile.getProvisionType().name());
  148 + break;
146 149 }
  150 + return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
147 151 }
148 152
149 153 private ListenableFuture<ProvisionResponse> processProvision(Device device, ProvisionRequest provisionRequest) {
... ...
... ... @@ -38,7 +38,7 @@ public interface DeviceProfileDao extends Dao<DeviceProfile> {
38 38
39 39 DeviceProfileInfo findDefaultDeviceProfileInfo(TenantId tenantId);
40 40
41   - DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret);
  41 + DeviceProfile findByProvisionDeviceKey(String provisionDeviceKey);
42 42
43 43 DeviceProfile findByName(TenantId tenantId, String profileName);
44 44 }
... ...
... ... @@ -56,10 +56,5 @@ public interface DeviceProfileRepository extends PagingAndSortingRepository<Devi
56 56
57 57 DeviceProfileEntity findByTenantIdAndName(UUID id, String profileName);
58 58
59   - @Query(value = "SELECT d.* from device_profile d " +
60   - "WHERE d.provision_device_key = :provisionDeviceKey " +
61   - "AND d.profile_data->'provisionConfiguration'->>'provisionDeviceSecret' = :provisionDeviceSecret",
62   - nativeQuery = true)
63   - DeviceProfileEntity findByProvisionDeviceKeyAndProvisionDeviceSecret(@Param("provisionDeviceKey") String provisionDeviceKey,
64   - @Param("provisionDeviceSecret") String provisionDeviceSecret);
  59 + DeviceProfileEntity findByProvisionDeviceKey(@Param("provisionDeviceKey") String provisionDeviceKey);
65 60 }
... ...
... ... @@ -81,8 +81,8 @@ public class JpaDeviceProfileDao extends JpaAbstractSearchTextDao<DeviceProfileE
81 81 }
82 82
83 83 @Override
84   - public DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret) {
85   - return DaoUtil.getData(deviceProfileRepository.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionDeviceKey, provisionDeviceSecret));
  84 + public DeviceProfile findByProvisionDeviceKey(String provisionDeviceKey) {
  85 + return DaoUtil.getData(deviceProfileRepository.findByProvisionDeviceKey(provisionDeviceKey));
86 86 }
87 87
88 88 @Override
... ...