Search in sources :

Example 1 with SaveDeviceWithCredentialsRequest

use of org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest in project thingsboard by thingsboard.

the class RestClient method saveDeviceWithCredentials.

public Optional<Device> saveDeviceWithCredentials(Device device, DeviceCredentials credentials) {
    try {
        SaveDeviceWithCredentialsRequest request = new SaveDeviceWithCredentialsRequest(device, credentials);
        ResponseEntity<Device> deviceOpt = restTemplate.postForEntity(baseURL + "/api/device-with-credentials", request, Device.class);
        return Optional.ofNullable(deviceOpt.getBody());
    } catch (HttpClientErrorException exception) {
        if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
            return Optional.empty();
        } else {
            throw exception;
        }
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) SaveDeviceWithCredentialsRequest(org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest) Device(org.thingsboard.server.common.data.Device)

Example 2 with SaveDeviceWithCredentialsRequest

use of org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest in project thingsboard by thingsboard.

the class Lwm2mController method saveDeviceWithCredentials.

@ApiOperation(hidden = true, value = "Save device with credentials (Deprecated)")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/lwm2m/device-credentials", method = RequestMethod.POST)
@ResponseBody
public Device saveDeviceWithCredentials(@RequestBody Map<Class<?>, Object> deviceWithDeviceCredentials) throws ThingsboardException {
    ObjectMapper mapper = new ObjectMapper();
    Device device = checkNotNull(mapper.convertValue(deviceWithDeviceCredentials.get(Device.class), Device.class));
    DeviceCredentials credentials = checkNotNull(mapper.convertValue(deviceWithDeviceCredentials.get(DeviceCredentials.class), DeviceCredentials.class));
    return deviceController.saveDeviceWithCredentials(new SaveDeviceWithCredentialsRequest(device, credentials));
}
Also used : Device(org.thingsboard.server.common.data.Device) SaveDeviceWithCredentialsRequest(org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Device (org.thingsboard.server.common.data.Device)2 SaveDeviceWithCredentialsRequest (org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ApiOperation (io.swagger.annotations.ApiOperation)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)1