Search in sources :

Example 1 with EdgeConnectionException

use of org.thingsboard.edge.exception.EdgeConnectionException 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)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)1 EdgeConnectionException (org.thingsboard.edge.exception.EdgeConnectionException)1 ConnectResponseMsg (org.thingsboard.server.gen.edge.v1.ConnectResponseMsg)1 DownlinkResponseMsg (org.thingsboard.server.gen.edge.v1.DownlinkResponseMsg)1 ResponseMsg (org.thingsboard.server.gen.edge.v1.ResponseMsg)1 UplinkResponseMsg (org.thingsboard.server.gen.edge.v1.UplinkResponseMsg)1