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();
}
}
Aggregations