Search in sources :

Example 1 with CustomIntegrationMsg

use of org.thingsboard.integration.custom.message.CustomIntegrationMsg in project remote-integration-example by thingsboard.

the class CustomIntegration method init.

@Override
public void init(TbIntegrationInitParams params) throws Exception {
    super.init(params);
    JsonNode configuration = mapper.readTree(params.getConfiguration().getConfiguration().get("configuration").asText());
    try {
        bossGroup = new NioEventLoopGroup();
        workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel socketChannel) {
                socketChannel.pipeline().addLast(new StringEncoder(), new StringDecoder(), new LineBasedFrameDecoder(1024));
                socketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                        log.debug("Server received the message: {}", msg);
                        if (msg.startsWith("Hello to ThingsBoard!")) {
                            deviceName = msg.substring(msg.indexOf("[") + 1, msg.indexOf("]"));
                            ctx.writeAndFlush("Hello from ThingsBoard!");
                            initialized = true;
                        } else {
                            if (initialized) {
                                CustomResponse response = new CustomResponse();
                                process(new CustomIntegrationMsg(msg, response));
                                ctx.writeAndFlush(response.getResult());
                            } else {
                                log.warn("The flaw was not started correctly!");
                            }
                        }
                    }
                });
            }
        });
        int port = getBindPort(configuration);
        serverChannel = bootstrap.bind(port).sync().channel();
        client = new CustomClient(port, getMsgGeneratorIntervalMs(configuration));
    } catch (Exception e) {
        log.error("Failed to init TCP server!", e);
        throw new RuntimeException();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) JsonNode(com.fasterxml.jackson.databind.JsonNode) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CustomIntegrationMsg(org.thingsboard.integration.custom.message.CustomIntegrationMsg) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) StringEncoder(io.netty.handler.codec.string.StringEncoder) CustomClient(org.thingsboard.integration.custom.client.CustomClient) CustomResponse(org.thingsboard.integration.custom.message.CustomResponse) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)1 StringDecoder (io.netty.handler.codec.string.StringDecoder)1 StringEncoder (io.netty.handler.codec.string.StringEncoder)1 CustomClient (org.thingsboard.integration.custom.client.CustomClient)1 CustomIntegrationMsg (org.thingsboard.integration.custom.message.CustomIntegrationMsg)1 CustomResponse (org.thingsboard.integration.custom.message.CustomResponse)1