Search in sources :

Example 11 with DeviceId

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

the class JpaAlarmDaoTest method testFindLatestByOriginatorAndType.

@Test
public void testFindLatestByOriginatorAndType() throws ExecutionException, InterruptedException {
    System.out.println(System.currentTimeMillis());
    UUID tenantId = UUID.fromString("d4b68f40-3e96-11e7-a884-898080180d6b");
    UUID originator1Id = UUID.fromString("d4b68f41-3e96-11e7-a884-898080180d6b");
    UUID originator2Id = UUID.fromString("d4b68f42-3e96-11e7-a884-898080180d6b");
    UUID alarm1Id = UUID.fromString("d4b68f43-3e96-11e7-a884-898080180d6b");
    UUID alarm2Id = UUID.fromString("d4b68f44-3e96-11e7-a884-898080180d6b");
    UUID alarm3Id = UUID.fromString("d4b68f45-3e96-11e7-a884-898080180d6b");
    saveAlarm(alarm1Id, tenantId, originator1Id, "TEST_ALARM");
    saveAlarm(alarm2Id, tenantId, originator1Id, "TEST_ALARM");
    saveAlarm(alarm3Id, tenantId, originator2Id, "TEST_ALARM");
    assertEquals(3, alarmDao.find().size());
    ListenableFuture<Alarm> future = alarmDao.findLatestByOriginatorAndType(new TenantId(tenantId), new DeviceId(originator1Id), "TEST_ALARM");
    Alarm alarm = future.get();
    assertNotNull(alarm);
    assertEquals(alarm2Id, alarm.getId().getId());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 12 with DeviceId

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

the class DeviceAttributesFilterTest method basicMissingAttributesTest.

@Test
public void basicMissingAttributesTest() {
    DeviceAttributesFilter filter = new DeviceAttributesFilter();
    filter.init(wrap("((typeof nonExistingVal === 'undefined') || nonExistingVal == true) && booleanValue == false"));
    List<AttributeKvEntry> clientAttributes = new ArrayList<>();
    clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
    DeviceAttributes attributes = new DeviceAttributes(clientAttributes, new ArrayList<>(), 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) 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)

Example 13 with DeviceId

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

the class DeviceAttributesFilterTest method basicConflictServerAttributesTest.

@Test
public void basicConflictServerAttributesTest() {
    DeviceAttributesFilter filter = new DeviceAttributesFilter();
    filter.init(wrap("cs.doubleValue == 1.0 && cs.booleanValue == true && ss.doubleValue == 0.0 && ss.booleanValue == false"));
    List<AttributeKvEntry> clientAttributes = new ArrayList<>();
    clientAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
    clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", true), 42));
    List<AttributeKvEntry> serverAttributes = new ArrayList<>();
    serverAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 0.0), 42));
    serverAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
    DeviceAttributes attributes = new DeviceAttributes(clientAttributes, 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)

Example 14 with DeviceId

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

the class DeviceController method assignDeviceToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/device/{deviceId}", method = RequestMethod.POST)
@ResponseBody
public Device assignDeviceToPublicCustomer(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(device.getTenantId());
        Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(deviceId, publicCustomer.getId()));
        logEntityAction(deviceId, savedDevice, savedDevice.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strDeviceId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.ASSIGNED_TO_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) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 15 with DeviceId

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

the class DeviceController method getDevicesByIds.

@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/devices", params = { "deviceIds" }, method = RequestMethod.GET)
@ResponseBody
public List<Device> getDevicesByIds(@RequestParam("deviceIds") String[] strDeviceIds) throws ThingsboardException {
    checkArrayParameter("deviceIds", strDeviceIds);
    try {
        SecurityUser user = getCurrentUser();
        TenantId tenantId = user.getTenantId();
        CustomerId customerId = user.getCustomerId();
        List<DeviceId> deviceIds = new ArrayList<>();
        for (String strDeviceId : strDeviceIds) {
            deviceIds.add(new DeviceId(toUUID(strDeviceId)));
        }
        ListenableFuture<List<Device>> devices;
        if (customerId == null || customerId.isNullUid()) {
            devices = deviceService.findDevicesByTenantIdAndIdsAsync(tenantId, deviceIds);
        } else {
            devices = deviceService.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId, customerId, deviceIds);
        }
        return checkNotNull(devices.get());
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) 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)

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