Search in sources :

Example 1 with DownlinkMsg

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

the class EdgeGrpcSession method doSync.

private void doSync(EdgeSyncCursor cursor) {
    if (cursor.hasNext()) {
        log.info("[{}][{}] starting sync process, cursor current idx = {}", edge.getTenantId(), edge.getId(), cursor.getCurrentIdx());
        ListenableFuture<UUID> uuidListenableFuture = startProcessingEdgeEvents(cursor.getNext());
        Futures.addCallback(uuidListenableFuture, new FutureCallback<>() {

            @Override
            public void onSuccess(@Nullable UUID result) {
                doSync(cursor);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Exception during sync process", edge.getTenantId(), edge.getId(), t);
            }
        }, ctx.getGrpcCallbackExecutorService());
    } else {
        DownlinkMsg syncCompleteDownlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).setSyncCompletedMsg(SyncCompletedMsg.newBuilder().build()).build();
        Futures.addCallback(sendDownlinkMsgsPack(Collections.singletonList(syncCompleteDownlinkMsg)), new FutureCallback<Void>() {

            @Override
            public void onSuccess(Void result) {
                syncCompleted = true;
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Exception during sending sync complete", edge.getTenantId(), edge.getId(), t);
            }
        }, ctx.getGrpcCallbackExecutorService());
    }
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) UUID(java.util.UUID)

Example 2 with DownlinkMsg

use of org.thingsboard.server.gen.edge.v1.DownlinkMsg 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 3 with DownlinkMsg

use of org.thingsboard.server.gen.edge.v1.DownlinkMsg 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 4 with DownlinkMsg

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

the class RelationEdgeProcessor method processRelationToEdge.

public DownlinkMsg processRelationToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType) {
    EntityRelation entityRelation = mapper.convertValue(edgeEvent.getBody(), EntityRelation.class);
    RelationUpdateMsg relationUpdateMsg = relationMsgConstructor.constructRelationUpdatedMsg(msgType, entityRelation);
    return DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRelationUpdateMsg(relationUpdateMsg).build();
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RelationUpdateMsg(org.thingsboard.server.gen.edge.v1.RelationUpdateMsg)

Example 5 with DownlinkMsg

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

the class WidgetBundleEdgeProcessor method processWidgetsBundleToEdge.

public DownlinkMsg processWidgetsBundleToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType edgeEdgeEventActionType) {
    WidgetsBundleId widgetsBundleId = new WidgetsBundleId(edgeEvent.getEntityId());
    DownlinkMsg downlinkMsg = null;
    switch(edgeEdgeEventActionType) {
        case ADDED:
        case UPDATED:
            WidgetsBundle widgetsBundle = widgetsBundleService.findWidgetsBundleById(edgeEvent.getTenantId(), widgetsBundleId);
            if (widgetsBundle != null) {
                WidgetsBundleUpdateMsg widgetsBundleUpdateMsg = widgetsBundleMsgConstructor.constructWidgetsBundleUpdateMsg(msgType, widgetsBundle);
                downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addWidgetsBundleUpdateMsg(widgetsBundleUpdateMsg).build();
            }
            break;
        case DELETED:
            WidgetsBundleUpdateMsg widgetsBundleUpdateMsg = widgetsBundleMsgConstructor.constructWidgetsBundleDeleteMsg(widgetsBundleId);
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addWidgetsBundleUpdateMsg(widgetsBundleUpdateMsg).build();
            break;
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) WidgetsBundleId(org.thingsboard.server.common.data.id.WidgetsBundleId) WidgetsBundleUpdateMsg(org.thingsboard.server.gen.edge.v1.WidgetsBundleUpdateMsg) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle)

Aggregations

DownlinkMsg (org.thingsboard.server.gen.edge.v1.DownlinkMsg)16 CustomerId (org.thingsboard.server.common.data.id.CustomerId)7 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 Device (org.thingsboard.server.common.data.Device)2 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)2 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)2 DeviceUpdateMsg (org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 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 EdgeConnectionException (org.thingsboard.edge.exception.EdgeConnectionException)1 AdminSettings (org.thingsboard.server.common.data.AdminSettings)1 Customer (org.thingsboard.server.common.data.Customer)1 Dashboard (org.thingsboard.server.common.data.Dashboard)1 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 EntityView (org.thingsboard.server.common.data.EntityView)1 User (org.thingsboard.server.common.data.User)1