Search in sources :

Example 31 with DeviceProfileId

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

the class LwM2MClientSerDesTest method serializeDeserialize.

@Test
public void serializeDeserialize() {
    LwM2mClient client = new LwM2mClient("nodeId", "testEndpoint");
    TransportDeviceInfo tdi = new TransportDeviceInfo();
    tdi.setPowerMode(PowerMode.PSM);
    tdi.setPsmActivityTimer(10000L);
    tdi.setPagingTransmissionWindow(2000L);
    tdi.setEdrxCycle(3000L);
    tdi.setTenantId(TenantId.fromUUID(UUID.randomUUID()));
    tdi.setCustomerId(new CustomerId(UUID.randomUUID()));
    tdi.setDeviceId(new DeviceId(UUID.randomUUID()));
    tdi.setDeviceProfileId(new DeviceProfileId(UUID.randomUUID()));
    tdi.setDeviceName("testDevice");
    tdi.setDeviceType("testType");
    ValidateDeviceCredentialsResponse credentialsResponse = ValidateDeviceCredentialsResponse.builder().deviceInfo(tdi).build();
    client.init(credentialsResponse, UUID.randomUUID());
    Registration registration = new Registration.Builder("test", "testEndpoint", Identity.unsecure(new InetSocketAddress(1000))).supportedContentFormats().objectLinks(new Link[] { new Link("/") }).build();
    client.setRegistration(registration);
    client.setState(LwM2MClientState.REGISTERED);
    client.getSharedAttributes().put("key1", TransportProtos.TsKvProto.newBuilder().setTs(0).setKv(TransportProtos.KeyValueProto.newBuilder().setStringV("test").build()).build());
    client.getSharedAttributes().put("key2", TransportProtos.TsKvProto.newBuilder().setTs(1).setKv(TransportProtos.KeyValueProto.newBuilder().setDoubleV(1.02).build()).build());
    byte[] bytes = LwM2MClientSerDes.serialize(client);
    Assert.assertNotNull(bytes);
    LwM2mClient desClient = LwM2MClientSerDes.deserialize(bytes);
    Assert.assertEquals(client.getNodeId(), desClient.getNodeId());
    Assert.assertEquals(client.getEndpoint(), desClient.getEndpoint());
    Assert.assertEquals(client.getResources(), desClient.getResources());
    Assert.assertEquals(client.getSharedAttributes(), desClient.getSharedAttributes());
    Assert.assertEquals(client.getKeyTsLatestMap(), desClient.getKeyTsLatestMap());
    Assert.assertEquals(client.getTenantId(), desClient.getTenantId());
    Assert.assertEquals(client.getProfileId(), desClient.getProfileId());
    Assert.assertEquals(client.getDeviceId(), desClient.getDeviceId());
    Assert.assertEquals(client.getState(), desClient.getState());
    Assert.assertEquals(client.getSession(), desClient.getSession());
    Assert.assertEquals(client.getPowerMode(), desClient.getPowerMode());
    Assert.assertEquals(client.getPsmActivityTimer(), desClient.getPsmActivityTimer());
    Assert.assertEquals(client.getPagingTransmissionWindow(), desClient.getPagingTransmissionWindow());
    Assert.assertEquals(client.getEdrxCycle(), desClient.getEdrxCycle());
    Assert.assertEquals(client.getRegistration(), desClient.getRegistration());
    Assert.assertEquals(client.isAsleep(), desClient.isAsleep());
    Assert.assertEquals(client.getLastUplinkTime(), desClient.getLastUplinkTime());
    Assert.assertEquals(client.getSleepTask(), desClient.getSleepTask());
    Assert.assertEquals(client.getClientSupportContentFormats(), desClient.getClientSupportContentFormats());
    Assert.assertEquals(client.getDefaultContentFormat(), desClient.getDefaultContentFormat());
    Assert.assertEquals(client.getRetryAttempts().get(), desClient.getRetryAttempts().get());
    Assert.assertEquals(client.getLastSentRpcId(), desClient.getLastSentRpcId());
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TransportDeviceInfo(org.thingsboard.server.common.transport.auth.TransportDeviceInfo) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Registration(org.eclipse.leshan.server.registration.Registration) InetSocketAddress(java.net.InetSocketAddress) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) Link(org.eclipse.leshan.core.link.Link) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient) Test(org.junit.Test)

Example 32 with DeviceProfileId

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

the class TbDeviceProfileNode method invalidateDeviceProfileCache.

protected void invalidateDeviceProfileCache(DeviceId deviceId, String deviceJson) {
    DeviceState deviceState = deviceStates.get(deviceId);
    if (deviceState != null) {
        DeviceProfileId currentProfileId = deviceState.getProfileId();
        Device device = JacksonUtil.fromString(deviceJson, Device.class);
        if (!currentProfileId.equals(device.getDeviceProfileId())) {
            removeDeviceState(deviceId);
        }
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device)

Example 33 with DeviceProfileId

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

the class TbDeviceProfileNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException {
    EntityType originatorType = msg.getOriginator().getEntityType();
    if (msg.getType().equals(PERIODIC_MSG_TYPE)) {
        scheduleAlarmHarvesting(ctx, msg);
        harvestAlarms(ctx, System.currentTimeMillis());
    } else if (msg.getType().equals(PROFILE_UPDATE_MSG_TYPE)) {
        updateProfile(ctx, new DeviceProfileId(UUID.fromString(msg.getData())));
    } else if (msg.getType().equals(DEVICE_UPDATE_MSG_TYPE)) {
        JsonNode data = JacksonUtil.toJsonNode(msg.getData());
        DeviceId deviceId = new DeviceId(UUID.fromString(data.get("deviceId").asText()));
        if (data.has("profileId")) {
            invalidateDeviceProfileCache(deviceId, new DeviceProfileId(UUID.fromString(data.get("deviceProfileId").asText())));
        } else {
            removeDeviceState(deviceId);
        }
    } else {
        if (EntityType.DEVICE.equals(originatorType)) {
            DeviceId deviceId = new DeviceId(msg.getOriginator().getId());
            if (msg.getType().equals(DataConstants.ENTITY_UPDATED)) {
                invalidateDeviceProfileCache(deviceId, msg.getData());
                ctx.tellSuccess(msg);
            } else if (msg.getType().equals(DataConstants.ENTITY_DELETED)) {
                removeDeviceState(deviceId);
                ctx.tellSuccess(msg);
            } else {
                DeviceState deviceState = getOrCreateDeviceState(ctx, deviceId, null);
                if (deviceState != null) {
                    deviceState.process(ctx, msg);
                } else {
                    log.info("Device was not found! Most probably device [" + deviceId + "] has been removed from the database. Acknowledging msg.");
                    ctx.ack(msg);
                }
            }
        } else {
            ctx.tellSuccess(msg);
        }
    }
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)33 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)12 UUID (java.util.UUID)9 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 ApiOperation (io.swagger.annotations.ApiOperation)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Device (org.thingsboard.server.common.data.Device)8 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 IOException (java.io.IOException)3 EntityType (org.thingsboard.server.common.data.EntityType)3 PageLink (org.thingsboard.server.common.data.page.PageLink)3 TransportDeviceInfo (org.thingsboard.server.common.transport.auth.TransportDeviceInfo)3 ByteString (com.google.protobuf.ByteString)2 ExecutionException (java.util.concurrent.ExecutionException)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2