24 changed files with 552 additions and 8 deletions
@ -0,0 +1,104 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc; |
|||
|
|||
import com.google.common.io.Resources; |
|||
import io.grpc.Server; |
|||
import io.grpc.ServerBuilder; |
|||
import io.grpc.stub.StreamObserver; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.edge.gen.EdgeProtos; |
|||
import org.thingsboard.server.common.edge.gen.EdgeRpcServiceGrpc; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.annotation.PreDestroy; |
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
@ConditionalOnProperty(prefix = "edges.rpc", value = "enabled", havingValue = "true") |
|||
public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase { |
|||
|
|||
@Value("${edges.rpc.port}") |
|||
private int rpcPort; |
|||
@Value("${edges.rpc.ssl.enabled}") |
|||
private boolean sslEnabled; |
|||
@Value("${edges.rpc.ssl.cert}") |
|||
private String certFileResource; |
|||
@Value("${edges.rpc.ssl.privateKey}") |
|||
private String privateKeyResource; |
|||
|
|||
private Server server; |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
log.info("Initializing Edge RPC service!"); |
|||
ServerBuilder builder = ServerBuilder.forPort(rpcPort).addService(this); |
|||
if (sslEnabled) { |
|||
try { |
|||
File certFile = new File(Resources.getResource(certFileResource).toURI()); |
|||
File privateKeyFile = new File(Resources.getResource(privateKeyResource).toURI()); |
|||
builder.useTransportSecurity(certFile, privateKeyFile); |
|||
} catch (Exception e) { |
|||
log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e); |
|||
throw new RuntimeException("Unable to set up SSL context!", e); |
|||
} |
|||
} |
|||
server = builder.build(); |
|||
log.info("Going to start Edge RPC server using port: {}", rpcPort); |
|||
try { |
|||
server.start(); |
|||
} catch (IOException e) { |
|||
log.error("Failed to start Edge RPC server!", e); |
|||
throw new RuntimeException("Failed to start Edge RPC server!"); |
|||
} |
|||
log.info("Edge RPC service initialized!"); |
|||
} |
|||
|
|||
|
|||
@PreDestroy |
|||
public void destroy() { |
|||
if (server != null) { |
|||
server.shutdownNow(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public StreamObserver<EdgeProtos.UplinkMsg> sendUplink(StreamObserver<EdgeProtos.DownlinkMsg> responseObserver) { |
|||
log.info("sendUplink [{}]", responseObserver); |
|||
return new StreamObserver<EdgeProtos.UplinkMsg>() { |
|||
|
|||
@Override |
|||
public void onNext(EdgeProtos.UplinkMsg uplinkMsg) { |
|||
log.info("onNext [{}]", uplinkMsg); |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Throwable throwable) { |
|||
log.info("onError", throwable); |
|||
} |
|||
|
|||
@Override |
|||
public void onCompleted() { |
|||
log.info("onCompleted"); |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
@ -0,0 +1,129 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2019 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
<parent> |
|||
<groupId>org.thingsboard</groupId> |
|||
<version>2.4.1-SNAPSHOT</version> |
|||
<artifactId>common</artifactId> |
|||
</parent> |
|||
<groupId>org.thingsboard.common</groupId> |
|||
<artifactId>edge-api</artifactId> |
|||
<packaging>jar</packaging> |
|||
|
|||
<name>Thingsboard Server Remote Edge wrapper</name> |
|||
<url>https://thingsboard.io</url> |
|||
|
|||
<properties> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
<main.dir>${basedir}/../..</main.dir> |
|||
</properties> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.thingsboard.common</groupId> |
|||
<artifactId>data</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.thingsboard.common</groupId> |
|||
<artifactId>message</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.google.code.gson</groupId> |
|||
<artifactId>gson</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.slf4j</groupId> |
|||
<artifactId>slf4j-api</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.slf4j</groupId> |
|||
<artifactId>log4j-over-slf4j</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>ch.qos.logback</groupId> |
|||
<artifactId>logback-core</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>ch.qos.logback</groupId> |
|||
<artifactId>logback-classic</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework</groupId> |
|||
<artifactId>spring-context</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.netty</groupId> |
|||
<artifactId>netty-all</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.google.guava</groupId> |
|||
<artifactId>guava</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.grpc</groupId> |
|||
<artifactId>grpc-netty</artifactId> |
|||
<exclusions> |
|||
<exclusion> |
|||
<artifactId>netty-transport</artifactId> |
|||
<groupId>io.netty</groupId> |
|||
</exclusion> |
|||
<exclusion> |
|||
<artifactId>netty-common</artifactId> |
|||
<groupId>io.netty</groupId> |
|||
</exclusion> |
|||
</exclusions> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.grpc</groupId> |
|||
<artifactId>grpc-protobuf</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.grpc</groupId> |
|||
<artifactId>grpc-stub</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.google.protobuf</groupId> |
|||
<artifactId>protobuf-java</artifactId> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<plugins> |
|||
<plugin> |
|||
<groupId>org.xolstice.maven.plugins</groupId> |
|||
<artifactId>protobuf-maven-plugin</artifactId> |
|||
</plugin> |
|||
</plugins> |
|||
</build> |
|||
|
|||
<distributionManagement> |
|||
<repository> |
|||
<id>thingsboard-repo-deploy</id> |
|||
<name>ThingsBoard Repo Deployment</name> |
|||
<url>https://repo.thingsboard.io/artifactory/libs-release-public</url> |
|||
</repository> |
|||
</distributionManagement> |
|||
|
|||
</project> |
|||
@ -0,0 +1,84 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.edge.rpc; |
|||
|
|||
import com.google.common.io.Resources; |
|||
import io.grpc.ManagedChannel; |
|||
import io.grpc.netty.GrpcSslContexts; |
|||
import io.grpc.netty.NettyChannelBuilder; |
|||
import io.grpc.stub.StreamObserver; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.edge.gen.EdgeProtos; |
|||
import org.thingsboard.server.common.edge.gen.EdgeRpcServiceGrpc; |
|||
|
|||
import javax.net.ssl.SSLException; |
|||
import java.io.File; |
|||
import java.net.URISyntaxException; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class EdgeGrpcClient implements EdgeRpcClient { |
|||
|
|||
@Value("${cloud.rpc.host}") |
|||
private String rpcHost; |
|||
@Value("${cloud.rpc.port}") |
|||
private int rpcPort; |
|||
@Value("${cloud.rpc.timeout}") |
|||
private int timeoutSecs; |
|||
@Value("${cloud.rpc.ssl.enabled}") |
|||
private boolean sslEnabled; |
|||
@Value("${cloud.rpc.ssl.cert}") |
|||
private String certResource; |
|||
|
|||
private ManagedChannel channel; |
|||
|
|||
private StreamObserver<EdgeProtos.UplinkMsg> inputStream; |
|||
|
|||
@Override |
|||
public void connect() { |
|||
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(rpcHost, rpcPort).usePlaintext(); |
|||
if (sslEnabled) { |
|||
try { |
|||
builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(Resources.getResource(certResource).toURI())).build()); |
|||
} catch (URISyntaxException | SSLException e) { |
|||
log.error("Failed to initialize channel!", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
channel = builder.build(); |
|||
EdgeRpcServiceGrpc.EdgeRpcServiceStub stub = EdgeRpcServiceGrpc.newStub(channel); |
|||
StreamObserver<EdgeProtos.DownlinkMsg> responseObserver = new StreamObserver<EdgeProtos.DownlinkMsg>() { |
|||
@Override |
|||
public void onNext(EdgeProtos.DownlinkMsg downlinkMsg) { |
|||
log.info("onNext [{}]", downlinkMsg); |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Throwable throwable) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onCompleted() { |
|||
|
|||
} |
|||
}; |
|||
inputStream = stub.sendUplink(responseObserver); |
|||
inputStream.onNext(EdgeProtos.UplinkMsg.newBuilder().setMsgType(EdgeProtos.UplinkMsgType.DELETE_DEVICE_MESSAGE).build()); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.edge.rpc; |
|||
|
|||
public interface EdgeRpcClient { |
|||
|
|||
void connect(); |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
syntax = "proto3"; |
|||
|
|||
option java_package = "org.thingsboard.server.common.edge.gen"; |
|||
option java_outer_classname = "EdgeProtos"; |
|||
|
|||
package edge; |
|||
|
|||
// Interface exported by the ThingsBoard PRC Edge. |
|||
service EdgeRpcService { |
|||
|
|||
rpc sendUplink(stream UplinkMsg) returns (stream DownlinkMsg) {} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* Data Structures; |
|||
*/ |
|||
message UplinkMsg { |
|||
UplinkMsgType msgType = 1; |
|||
} |
|||
|
|||
message DownlinkMsg { |
|||
DownlinkMsgType msgType = 1; |
|||
} |
|||
|
|||
enum UplinkMsgType { |
|||
SAVE_DEVICE_MESSAGE = 0; |
|||
DELETE_DEVICE_MESSAGE = 1; |
|||
} |
|||
|
|||
enum DownlinkMsgType { |
|||
SAVE_ENTITY_MESSAGE = 0; |
|||
DELETE_ENTITY_MESSAGE = 1; |
|||
} |
|||
Loading…
Reference in new issue