Search in sources :

Example 16 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceController method deleteDevice.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/device/{deviceId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteDevice(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        deviceService.deleteDevice(deviceId);
        logEntityAction(deviceId, device, device.getCustomerId(), ActionType.DELETED, null, strDeviceId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.DELETED, e, strDeviceId);
        throw handleException(e);
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceController method unassignDeviceFromCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/device/{deviceId}", method = RequestMethod.DELETE)
@ResponseBody
public Device unassignDeviceFromCustomer(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        if (device.getCustomerId() == null || device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Device isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(device.getCustomerId());
        Device savedDevice = checkNotNull(deviceService.unassignDeviceFromCustomer(deviceId));
        logEntityAction(deviceId, device, device.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strDeviceId, customer.getId().toString(), customer.getName());
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strDeviceId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 18 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceController method assignDeviceToCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/{customerId}/device/{deviceId}", method = RequestMethod.POST)
@ResponseBody
public Device assignDeviceToCustomer(@PathVariable("customerId") String strCustomerId, @PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        Customer customer = checkCustomerId(customerId);
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        checkDeviceId(deviceId);
        Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(deviceId, customerId));
        logEntityAction(deviceId, savedDevice, savedDevice.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strDeviceId, strCustomerId, customer.getName());
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strDeviceId, strCustomerId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) CustomerId(org.thingsboard.server.common.data.id.CustomerId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 19 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceController method getDeviceCredentialsByDeviceId.

@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/device/{deviceId}/credentials", method = RequestMethod.GET)
@ResponseBody
public DeviceCredentials getDeviceCredentialsByDeviceId(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        DeviceCredentials deviceCredentials = checkNotNull(deviceCredentialsService.findDeviceCredentialsByDeviceId(deviceId));
        logEntityAction(deviceId, device, device.getCustomerId(), ActionType.CREDENTIALS_READ, null, strDeviceId);
        return deviceCredentials;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.CREDENTIALS_READ, e, strDeviceId);
        throw handleException(e);
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 20 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceAttributesFilterTest method basicServerAttributesTest.

@Test
public void basicServerAttributesTest() {
    DeviceAttributesFilter filter = new DeviceAttributesFilter();
    filter.init(wrap("doubleValue == 1.0 && booleanValue == false"));
    List<AttributeKvEntry> serverAttributes = new ArrayList<>();
    serverAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
    serverAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
    DeviceAttributes attributes = new DeviceAttributes(new ArrayList<>(), serverAttributes, new ArrayList<>());
    Mockito.when(ruleCtx.getDeviceMetaData()).thenReturn(new DeviceMetaData(new DeviceId(UUID.randomUUID()), "A", "A", attributes));
    Assert.assertTrue(filter.filter(ruleCtx, null));
    filter.stop();
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) DeviceAttributes(org.thingsboard.server.extensions.api.device.DeviceAttributes) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) DeviceMetaData(org.thingsboard.server.extensions.api.device.DeviceMetaData) Test(org.junit.Test)

Aggregations

DeviceId (org.thingsboard.server.common.data.id.DeviceId)42 Test (org.junit.Test)21 Device (org.thingsboard.server.common.data.Device)12 TenantId (org.thingsboard.server.common.data.id.TenantId)10 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)9 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)8 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)8 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)8 ArrayList (java.util.ArrayList)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Event (org.thingsboard.server.common.data.Event)6 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 UUID (java.util.UUID)5 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 DeviceAttributes (org.thingsboard.server.extensions.api.device.DeviceAttributes)5 DeviceMetaData (org.thingsboard.server.extensions.api.device.DeviceMetaData)5 Customer (org.thingsboard.server.common.data.Customer)3 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)3