Search in sources :

Example 1 with DeviceCredentialsUpdateMsg

use of org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg in project thingsboard by thingsboard.

the class DeviceEdgeProcessor method processDeviceToEdge.

public DownlinkMsg processDeviceToEdge(Edge edge, EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType edgeEdgeEventActionType) {
    DeviceId deviceId = new DeviceId(edgeEvent.getEntityId());
    DownlinkMsg downlinkMsg = null;
    switch(edgeEdgeEventActionType) {
        case ADDED:
        case UPDATED:
        case ASSIGNED_TO_EDGE:
        case ASSIGNED_TO_CUSTOMER:
        case UNASSIGNED_FROM_CUSTOMER:
            Device device = deviceService.findDeviceById(edgeEvent.getTenantId(), deviceId);
            if (device != null) {
                CustomerId customerId = getCustomerIdIfEdgeAssignedToCustomer(device, edge);
                DeviceUpdateMsg deviceUpdateMsg = deviceMsgConstructor.constructDeviceUpdatedMsg(msgType, device, customerId, null);
                downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addDeviceUpdateMsg(deviceUpdateMsg).build();
            }
            break;
        case DELETED:
        case UNASSIGNED_FROM_EDGE:
            DeviceUpdateMsg deviceUpdateMsg = deviceMsgConstructor.constructDeviceDeleteMsg(deviceId);
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addDeviceUpdateMsg(deviceUpdateMsg).build();
            break;
        case CREDENTIALS_UPDATED:
            DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(edge.getTenantId(), deviceId);
            if (deviceCredentials != null) {
                DeviceCredentialsUpdateMsg deviceCredentialsUpdateMsg = deviceMsgConstructor.constructDeviceCredentialsUpdatedMsg(deviceCredentials);
                downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addDeviceCredentialsUpdateMsg(deviceCredentialsUpdateMsg).build();
            }
            break;
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DeviceCredentialsUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 2 with DeviceCredentialsUpdateMsg

use of org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg in project thingsboard by thingsboard.

the class BaseEdgeTest method testSendDeviceCredentialsRequestToCloud.

@Test
public void testSendDeviceCredentialsRequestToCloud() throws Exception {
    Device device = findDeviceByName("Edge Device 1");
    DeviceCredentials deviceCredentials = doGet("/api/device/" + device.getUuidId() + "/credentials", DeviceCredentials.class);
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    DeviceCredentialsRequestMsg.Builder deviceCredentialsRequestMsgBuilder = DeviceCredentialsRequestMsg.newBuilder();
    deviceCredentialsRequestMsgBuilder.setDeviceIdMSB(device.getUuidId().getMostSignificantBits());
    deviceCredentialsRequestMsgBuilder.setDeviceIdLSB(device.getUuidId().getLeastSignificantBits());
    testAutoGeneratedCodeByProtobuf(deviceCredentialsRequestMsgBuilder);
    uplinkMsgBuilder.addDeviceCredentialsRequestMsg(deviceCredentialsRequestMsgBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceCredentialsUpdateMsg);
    DeviceCredentialsUpdateMsg deviceCredentialsUpdateMsg = (DeviceCredentialsUpdateMsg) latestMessage;
    Assert.assertEquals(deviceCredentialsUpdateMsg.getDeviceIdMSB(), device.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceCredentialsUpdateMsg.getDeviceIdLSB(), device.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(deviceCredentialsUpdateMsg.getCredentialsType(), deviceCredentials.getCredentialsType().name());
    Assert.assertEquals(deviceCredentialsUpdateMsg.getCredentialsId(), deviceCredentials.getCredentialsId());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) DeviceCredentialsRequestMsg(org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg) Device(org.thingsboard.server.common.data.Device) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) DeviceCredentialsUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Aggregations

Device (org.thingsboard.server.common.data.Device)2 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)2 DeviceCredentialsUpdateMsg (org.thingsboard.server.gen.edge.v1.DeviceCredentialsUpdateMsg)2 AbstractMessage (com.google.protobuf.AbstractMessage)1 Test (org.junit.Test)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)1 DeviceCredentialsRequestMsg (org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg)1 DeviceUpdateMsg (org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg)1 DownlinkMsg (org.thingsboard.server.gen.edge.v1.DownlinkMsg)1 UplinkMsg (org.thingsboard.server.gen.edge.v1.UplinkMsg)1