Search in sources :

Example 1 with UplinkResponseMsg

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

the class EdgeGrpcClient method initOutputStream.

private StreamObserver<ResponseMsg> initOutputStream(String edgeKey, Consumer<UplinkResponseMsg> onUplinkResponse, Consumer<EdgeConfiguration> onEdgeUpdate, Consumer<DownlinkMsg> onDownlink, Consumer<Exception> onError) {
    return new StreamObserver<>() {

        @Override
        public void onNext(ResponseMsg responseMsg) {
            if (responseMsg.hasConnectResponseMsg()) {
                ConnectResponseMsg connectResponseMsg = responseMsg.getConnectResponseMsg();
                if (connectResponseMsg.getResponseCode().equals(ConnectResponseCode.ACCEPTED)) {
                    log.info("[{}] Configuration received: {}", edgeKey, connectResponseMsg.getConfiguration());
                    onEdgeUpdate.accept(connectResponseMsg.getConfiguration());
                } else {
                    log.error("[{}] Failed to establish the connection! Code: {}. Error message: {}.", edgeKey, connectResponseMsg.getResponseCode(), connectResponseMsg.getErrorMsg());
                    try {
                        EdgeGrpcClient.this.disconnect(true);
                    } catch (InterruptedException e) {
                        log.error("[{}] Got interruption during disconnect!", edgeKey, e);
                    }
                    onError.accept(new EdgeConnectionException("Failed to establish the connection! Response code: " + connectResponseMsg.getResponseCode().name()));
                }
            } else if (responseMsg.hasEdgeUpdateMsg()) {
                log.debug("[{}] Edge update message received {}", edgeKey, responseMsg.getEdgeUpdateMsg());
                onEdgeUpdate.accept(responseMsg.getEdgeUpdateMsg().getConfiguration());
            } else if (responseMsg.hasUplinkResponseMsg()) {
                log.debug("[{}] Uplink response message received {}", edgeKey, responseMsg.getUplinkResponseMsg());
                onUplinkResponse.accept(responseMsg.getUplinkResponseMsg());
            } else if (responseMsg.hasDownlinkMsg()) {
                log.debug("[{}] Downlink message received {}", edgeKey, responseMsg.getDownlinkMsg());
                onDownlink.accept(responseMsg.getDownlinkMsg());
            }
        }

        @Override
        public void onError(Throwable t) {
            log.debug("[{}] The rpc session received an error!", edgeKey, t);
            try {
                EdgeGrpcClient.this.disconnect(true);
            } catch (InterruptedException e) {
                log.error("[{}] Got interruption during disconnect!", edgeKey, e);
            }
            onError.accept(new RuntimeException(t));
        }

        @Override
        public void onCompleted() {
            log.debug("[{}] The rpc session was closed!", edgeKey);
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) UplinkResponseMsg(org.thingsboard.server.gen.edge.v1.UplinkResponseMsg) ConnectResponseMsg(org.thingsboard.server.gen.edge.v1.ConnectResponseMsg) ResponseMsg(org.thingsboard.server.gen.edge.v1.ResponseMsg) DownlinkResponseMsg(org.thingsboard.server.gen.edge.v1.DownlinkResponseMsg) ConnectResponseMsg(org.thingsboard.server.gen.edge.v1.ConnectResponseMsg) EdgeConnectionException(org.thingsboard.edge.exception.EdgeConnectionException)

Example 2 with UplinkResponseMsg

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

the class EdgeGrpcClient method connect.

@Override
public void connect(String edgeKey, String edgeSecret, Consumer<UplinkResponseMsg> onUplinkResponse, Consumer<EdgeConfiguration> onEdgeUpdate, Consumer<DownlinkMsg> onDownlink, Consumer<Exception> onError) {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(rpcHost, rpcPort).keepAliveTime(keepAliveTimeSec, TimeUnit.SECONDS);
    if (sslEnabled) {
        try {
            SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
            if (StringUtils.isNotEmpty(certResource)) {
                sslContextBuilder.trustManager(ResourceUtils.getInputStream(this, certResource));
            }
            builder.sslContext(sslContextBuilder.build());
        } catch (SSLException e) {
            log.error("Failed to initialize channel!", e);
            throw new RuntimeException(e);
        }
    } else {
        builder.usePlaintext();
    }
    channel = builder.build();
    EdgeRpcServiceGrpc.EdgeRpcServiceStub stub = EdgeRpcServiceGrpc.newStub(channel);
    log.info("[{}] Sending a connect request to the TB!", edgeKey);
    this.inputStream = stub.withCompression("gzip").handleMsgs(initOutputStream(edgeKey, onUplinkResponse, onEdgeUpdate, onDownlink, onError));
    this.inputStream.onNext(RequestMsg.newBuilder().setMsgType(RequestMsgType.CONNECT_RPC_MESSAGE).setConnectRequestMsg(ConnectRequestMsg.newBuilder().setEdgeRoutingKey(edgeKey).setEdgeSecret(edgeSecret).setEdgeVersion(EdgeVersion.V_3_3_3).build()).build());
}
Also used : SslContextBuilder(io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) EdgeRpcServiceGrpc(org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc) SSLException(javax.net.ssl.SSLException)

Example 3 with UplinkResponseMsg

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

the class BaseEdgeTest method testDeviceReachedMaximumAllowedOnCloud.

@Test
public void testDeviceReachedMaximumAllowedOnCloud() throws Exception {
    // update tenant profile configuration
    loginSysAdmin();
    TenantProfile tenantProfile = doGet("/api/tenantProfile/" + savedTenant.getTenantProfileId().getId(), TenantProfile.class);
    DefaultTenantProfileConfiguration profileConfiguration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration();
    profileConfiguration.setMaxDevices(1);
    tenantProfile.getProfileData().setConfiguration(profileConfiguration);
    doPost("/api/tenantProfile/", tenantProfile, TenantProfile.class);
    loginTenantAdmin();
    UUID uuid = Uuids.timeBased();
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    DeviceUpdateMsg.Builder deviceUpdateMsgBuilder = DeviceUpdateMsg.newBuilder();
    deviceUpdateMsgBuilder.setIdMSB(uuid.getMostSignificantBits());
    deviceUpdateMsgBuilder.setIdLSB(uuid.getLeastSignificantBits());
    deviceUpdateMsgBuilder.setName("Edge Device");
    deviceUpdateMsgBuilder.setType("default");
    deviceUpdateMsgBuilder.setMsgType(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE);
    uplinkMsgBuilder.addDeviceUpdateMsg(deviceUpdateMsgBuilder.build());
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    UplinkResponseMsg latestResponseMsg = edgeImitator.getLatestResponseMsg();
    Assert.assertTrue(latestResponseMsg.getSuccess());
}
Also used : UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) TenantProfile(org.thingsboard.server.common.data.TenantProfile) UUID(java.util.UUID) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) UplinkResponseMsg(org.thingsboard.server.gen.edge.v1.UplinkResponseMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Aggregations

UplinkResponseMsg (org.thingsboard.server.gen.edge.v1.UplinkResponseMsg)2 NettyChannelBuilder (io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder)1 SslContextBuilder (io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder)1 StreamObserver (io.grpc.stub.StreamObserver)1 UUID (java.util.UUID)1 SSLException (javax.net.ssl.SSLException)1 Test (org.junit.Test)1 EdgeConnectionException (org.thingsboard.edge.exception.EdgeConnectionException)1 TenantProfile (org.thingsboard.server.common.data.TenantProfile)1 DefaultTenantProfileConfiguration (org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration)1 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)1 ConnectResponseMsg (org.thingsboard.server.gen.edge.v1.ConnectResponseMsg)1 DeviceUpdateMsg (org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg)1 DownlinkResponseMsg (org.thingsboard.server.gen.edge.v1.DownlinkResponseMsg)1 EdgeRpcServiceGrpc (org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc)1 ResponseMsg (org.thingsboard.server.gen.edge.v1.ResponseMsg)1 UplinkMsg (org.thingsboard.server.gen.edge.v1.UplinkMsg)1