LedClientHandler.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.gzlh.device.led.client;
  2. import com.gzlh.bus.EventConfig;
  3. import com.gzlh.config.ModuleEnum;
  4. import com.gzlh.config.dto.SerialSetting;
  5. import com.gzlh.device.led.brand.LedBrandType;
  6. import com.gzlh.entity.DeviceStatus;
  7. import com.gzlh.utils.DeviceCache;
  8. import io.netty.channel.ChannelHandler;
  9. import io.netty.channel.ChannelHandlerContext;
  10. import io.netty.channel.SimpleChannelInboundHandler;
  11. import lombok.extern.slf4j.Slf4j;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. @Slf4j
  15. @ChannelHandler.Sharable
  16. public class LedClientHandler extends SimpleChannelInboundHandler<String> {
  17. private LedNettyConfig ledNettyConfig;
  18. public LedClientHandler(LedNettyConfig ledNettyConfig) {
  19. this.ledNettyConfig = ledNettyConfig;
  20. }
  21. @Override
  22. protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  23. log.info("LED 客户端收到消息:" + msg);
  24. DeviceCache.changeDeviceStatus(ModuleEnum.LED_MODULE.getModuleEn(),1);
  25. }
  26. @Override
  27. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  28. log.error("led 客户端 连接断开,进行重连");
  29. DeviceCache.changeDeviceStatus(ModuleEnum.LED_MODULE.getModuleEn(),0);
  30. ledNettyConfig.connect();
  31. }
  32. @Override
  33. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  34. cause.printStackTrace();
  35. ctx.close();
  36. }
  37. }