...
|
...
|
@@ -65,6 +65,7 @@ import org.thingsboard.server.service.state.DeviceStateService; |
65
|
65
|
import java.util.Collections;
|
66
|
66
|
import java.util.List;
|
67
|
67
|
import java.util.Optional;
|
|
68
|
+import java.util.concurrent.ExecutionException;
|
68
|
69
|
import java.util.concurrent.locks.ReentrantLock;
|
69
|
70
|
|
70
|
71
|
|
...
|
...
|
@@ -109,7 +110,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
109
|
110
|
}
|
110
|
111
|
|
111
|
112
|
@Override
|
112
|
|
- public ListenableFuture<ProvisionResponse> provisionDevice(ProvisionRequest provisionRequest) {
|
|
113
|
+ public ProvisionResponse provisionDevice(ProvisionRequest provisionRequest) {
|
113
|
114
|
String provisionRequestKey = provisionRequest.getCredentials().getProvisionDeviceKey();
|
114
|
115
|
String provisionRequestSecret = provisionRequest.getCredentials().getProvisionDeviceSecret();
|
115
|
116
|
|
...
|
...
|
@@ -152,35 +153,24 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
152
|
153
|
throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
|
153
|
154
|
}
|
154
|
155
|
|
155
|
|
- private ListenableFuture<ProvisionResponse> processProvision(Device device, ProvisionRequest provisionRequest) {
|
156
|
|
- ListenableFuture<Optional<AttributeKvEntry>> provisionStateFuture = attributesService.find(device.getTenantId(), device.getId(),
|
157
|
|
- DataConstants.SERVER_SCOPE, DEVICE_PROVISION_STATE);
|
158
|
|
- ListenableFuture<Boolean> provisionedFuture = Futures.transformAsync(provisionStateFuture, optionalAtr -> {
|
159
|
|
- if (optionalAtr != null && optionalAtr.isPresent()) {
|
160
|
|
- String state = optionalAtr.get().getValueAsString();
|
161
|
|
- if (state.equals(PROVISIONED_STATE)) {
|
162
|
|
- return Futures.immediateFuture(true);
|
163
|
|
- } else {
|
164
|
|
- log.error("[{}][{}] Unknown provision state: {}!", device.getName(), DEVICE_PROVISION_STATE, state);
|
165
|
|
- throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
166
|
|
- }
|
167
|
|
- }
|
168
|
|
- return Futures.transform(saveProvisionStateAttribute(device), input -> false, MoreExecutors.directExecutor());
|
169
|
|
- }, MoreExecutors.directExecutor());
|
170
|
|
- if (provisionedFuture.isCancelled()) {
|
171
|
|
- throw new RuntimeException("Unknown provision state!");
|
172
|
|
- }
|
173
|
|
- return Futures.transform(provisionedFuture, provisioned -> {
|
174
|
|
- if (provisioned) {
|
|
156
|
+ private ProvisionResponse processProvision(Device device, ProvisionRequest provisionRequest) {
|
|
157
|
+ try {
|
|
158
|
+ Optional<AttributeKvEntry> provisionState = attributesService.find(device.getTenantId(), device.getId(),
|
|
159
|
+ DataConstants.SERVER_SCOPE, DEVICE_PROVISION_STATE).get();
|
|
160
|
+ if (provisionState != null && provisionState.isPresent() && !provisionState.get().getValueAsString().equals(PROVISIONED_STATE)) {
|
175
|
161
|
notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
176
|
162
|
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
|
163
|
+ } else {
|
|
164
|
+ saveProvisionStateAttribute(device).get();
|
|
165
|
+ notify(device, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
|
177
|
166
|
}
|
178
|
|
- notify(device, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
|
179
|
|
- return new ProvisionResponse(deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId()), ProvisionResponseStatus.SUCCESS);
|
180
|
|
- }, MoreExecutors.directExecutor());
|
|
167
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
168
|
+ throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
|
169
|
+ }
|
|
170
|
+ return new ProvisionResponse(deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId()), ProvisionResponseStatus.SUCCESS);
|
181
|
171
|
}
|
182
|
172
|
|
183
|
|
- private ListenableFuture<ProvisionResponse> createDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
|
173
|
+ private ProvisionResponse createDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
184
|
174
|
deviceCreationLock.lock();
|
185
|
175
|
try {
|
186
|
176
|
return processCreateDevice(provisionRequest, profile);
|
...
|
...
|
@@ -194,22 +184,24 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
194
|
184
|
logAction(device.getTenantId(), device.getCustomerId(), device, success, provisionRequest);
|
195
|
185
|
}
|
196
|
186
|
|
197
|
|
- private ListenableFuture<ProvisionResponse> processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
|
187
|
+ private ProvisionResponse processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
|
198
|
188
|
Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName());
|
199
|
|
- if (device == null) {
|
200
|
|
- Device savedDevice = deviceService.saveDevice(provisionRequest, profile);
|
201
|
|
-
|
202
|
|
- deviceStateService.onDeviceAdded(savedDevice);
|
203
|
|
- pushDeviceCreatedEventToRuleEngine(savedDevice);
|
204
|
|
- notify(savedDevice, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
|
205
|
|
-
|
206
|
|
- return Futures.transform(saveProvisionStateAttribute(savedDevice), input ->
|
207
|
|
- new ProvisionResponse(
|
208
|
|
- getDeviceCredentials(savedDevice),
|
209
|
|
- ProvisionResponseStatus.SUCCESS), MoreExecutors.directExecutor());
|
210
|
|
- } else {
|
211
|
|
- log.warn("[{}] The device is already provisioned!", device.getName());
|
212
|
|
- notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
|
189
|
+ try {
|
|
190
|
+ if (device == null) {
|
|
191
|
+ Device savedDevice = deviceService.saveDevice(provisionRequest, profile);
|
|
192
|
+
|
|
193
|
+ deviceStateService.onDeviceAdded(savedDevice);
|
|
194
|
+ saveProvisionStateAttribute(savedDevice).get();
|
|
195
|
+ pushDeviceCreatedEventToRuleEngine(savedDevice);
|
|
196
|
+ notify(savedDevice, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
|
|
197
|
+
|
|
198
|
+ return new ProvisionResponse(getDeviceCredentials(savedDevice), ProvisionResponseStatus.SUCCESS);
|
|
199
|
+ } else {
|
|
200
|
+ log.warn("[{}] The device is already provisioned!", device.getName());
|
|
201
|
+ notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
|
|
202
|
+ throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
|
203
|
+ }
|
|
204
|
+ } catch (InterruptedException | ExecutionException e) {
|
213
|
205
|
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
|
214
|
206
|
}
|
215
|
207
|
}
|
...
|
...
|
|