|
3 | 3 | import java.io.File; |
4 | 4 | import java.io.FileInputStream; |
5 | 5 | import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.time.Instant; |
6 | 8 | import java.util.ArrayList; |
7 | 9 | import java.util.HashMap; |
8 | 10 | import java.util.List; |
|
12 | 14 | import javax.servlet.http.HttpServletRequest; |
13 | 15 |
|
14 | 16 | import org.apache.commons.collections.CollectionUtils; |
| 17 | +import org.apache.commons.lang3.StringUtils; |
| 18 | +import org.apache.http.Header; |
15 | 19 | import org.slf4j.Logger; |
16 | 20 | import org.slf4j.LoggerFactory; |
17 | 21 | import org.springframework.beans.factory.annotation.Autowired; |
|
28 | 32 | import org.springframework.web.bind.annotation.RequestHeader; |
29 | 33 | import org.springframework.web.bind.annotation.RequestMapping; |
30 | 34 | import org.springframework.web.bind.annotation.RequestMethod; |
| 35 | +import org.springframework.web.bind.annotation.RequestParam; |
31 | 36 | import org.springframework.web.bind.annotation.RestController; |
32 | 37 |
|
33 | 38 | import com.fasterxml.jackson.core.type.TypeReference; |
@@ -67,31 +72,30 @@ public KitController() { |
67 | 72 | } |
68 | 73 |
|
69 | 74 | /** |
70 | | - * Sample End point which returns a Welcome Message |
| 75 | + * Method that returns the devices for an admin or for a user if userId is |
| 76 | + * passed |
71 | 77 | * |
72 | 78 | * @param request |
73 | 79 | * - |
74 | 80 | * @param authorization |
75 | 81 | * - |
76 | | - * |
77 | | - * @param echo |
78 | | - * - the string to echo back |
79 | 82 | * @return - |
80 | 83 | */ |
81 | 84 | @SuppressWarnings({ "resource" }) |
82 | 85 | @RequestMapping(value = "/device", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, |
83 | 86 | MediaType.APPLICATION_XHTML_XML_VALUE }) |
84 | 87 | public ResponseEntity<?> getDevices(HttpServletRequest request, |
85 | 88 | @RequestHeader("Authorization") String authorization) { |
| 89 | + // log.info("calling /device method API call"); |
86 | 90 | List<RegisterDevice> devices = null; |
87 | 91 | String userId = getUserId(request); |
88 | 92 | Boolean isAdmin = (Boolean) request.getAttribute("isAdmin"); //$NON-NLS-1$ |
89 | 93 |
|
90 | 94 | if (isAdmin) { |
91 | 95 | devices = this.deviceManager.getAllAdminDevices(); |
92 | | - } else { |
| 96 | + } else |
93 | 97 | devices = this.deviceManager.getDevices(userId); |
94 | | - } |
| 98 | + |
95 | 99 | String contentType = request.getHeader("Content-Type"); //$NON-NLS-1$ |
96 | 100 |
|
97 | 101 | if (contentType != null && MediaType.APPLICATION_OCTET_STREAM_VALUE.equals(contentType)) { |
@@ -137,13 +141,15 @@ public ResponseEntity<?> registerDevice(@RequestBody RegisterDevice device, Http |
137 | 141 | return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); |
138 | 142 | } |
139 | 143 | // continue to register device |
140 | | - // RegisterDevice originalDevice = this.deviceManager.getDevice("device", userId, device.getDeviceAddress());//$NON-NLS-1$ |
| 144 | + // RegisterDevice originalDevice = |
| 145 | + // this.deviceManager.getDevice("device", |
| 146 | + // userId, device.getDeviceAddress());//$NON-NLS-1$ |
141 | 147 | // removing the user check adding check per deviceIdentifer |
142 | 148 | RegisterDevice originalDevice = this.deviceManager.getDevice("device", null, device.getDeviceAddress());//$NON-NLS-1$ |
143 | 149 | try { |
144 | 150 | if (originalDevice == null) { |
145 | 151 | // device not found. register it. |
146 | | - log.info("Registrating device with address " + device.getDeviceAddress()); //$NON-NLS-1$ |
| 152 | + log.info("Registering device with address " + device.getDeviceAddress()); //$NON-NLS-1$ |
147 | 153 | this.deviceManager.registerDevice(device, userId); |
148 | 154 | } else { |
149 | 155 | log.info("This is a registered device with address " + device.getDeviceAddress()); //$NON-NLS-1$ |
@@ -308,23 +314,15 @@ private String getUserId(HttpServletRequest request) { |
308 | 314 | return this.deviceManager.getUserId(accessToken); |
309 | 315 | } |
310 | 316 |
|
311 | | - |
312 | | - |
313 | 317 | /** |
314 | 318 | * Details about each Device |
315 | 319 | * |
316 | 320 | * @param deviceId |
317 | 321 | * - |
318 | | - * @param model |
319 | | - * - |
320 | 322 | * @param request |
321 | 323 | * - |
322 | 324 | * @param authorization |
323 | 325 | * - |
324 | | - * @param result |
325 | | - * - |
326 | | - * @param echo |
327 | | - * - |
328 | 326 | * @return - |
329 | 327 | */ |
330 | 328 | @RequestMapping(value = "/device/{deviceId}", method = RequestMethod.GET) |
@@ -383,24 +381,20 @@ public ResponseEntity<?> getDevice(@PathVariable String deviceId, HttpServletReq |
383 | 381 | file.delete(); |
384 | 382 | } |
385 | 383 | } |
386 | | - return new ResponseEntity<RegisterDevice>(device, HttpStatus.OK); } |
387 | | - |
388 | | - |
| 384 | + return new ResponseEntity<RegisterDevice>(device, HttpStatus.OK); |
| 385 | + } |
389 | 386 |
|
390 | 387 | /** |
391 | 388 | * This method resets the Device settings. |
392 | 389 | * |
393 | 390 | * @param deviceId |
394 | 391 | * - |
395 | | - * @param device |
396 | | - * - |
397 | 392 | * @param request |
398 | 393 | * - |
399 | | - * @param result |
400 | | - * - |
401 | 394 | * @param authorization |
402 | 395 | * - |
403 | 396 | * @return - |
| 397 | + * |
404 | 398 | */ |
405 | 399 | @RequestMapping(value = "/device/reset/{deviceId}", method = RequestMethod.PUT) |
406 | 400 | public ResponseEntity<?> resetRegisterDevice(@PathVariable String deviceId, HttpServletRequest request, |
@@ -428,8 +422,17 @@ public ResponseEntity<?> resetRegisterDevice(@PathVariable String deviceId, Http |
428 | 422 | // continue with get device without user |
429 | 423 | RegisterDevice device = this.deviceManager.getDevice(deviceId, null); |
430 | 424 | if (device != null) { |
| 425 | + |
431 | 426 | Boolean isAdmin = (Boolean) request.getAttribute("isAdmin"); //$NON-NLS-1$ |
432 | | - this.deviceManager.resetDeviceActivation(device, isAdmin, userId); |
| 427 | + |
| 428 | + try { |
| 429 | + this.deviceManager.resetDeviceActivation(device, isAdmin, userId); |
| 430 | + } catch (IOException e) { |
| 431 | + log.error("KitController: Asset model could not be reset. ", e); |
| 432 | + throw new RuntimeException("KitController: Exception when resetting the device", e); |
| 433 | + // return new ResponseEntity<>(eventErrors, HttpStatus.); |
| 434 | + } |
| 435 | + |
433 | 436 | } |
434 | 437 |
|
435 | 438 | } catch (DeviceRegistrationError e) { |
|
0 commit comments