PlcClientHandler.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.gzlh.device.plc.client;
  2. import com.gzlh.bus.EventConfig;
  3. import com.gzlh.config.dto.SerialSetting;
  4. import com.gzlh.device.plc.service.PLCService;
  5. import com.gzlh.utils.DeviceCache;
  6. import io.netty.channel.ChannelHandler;
  7. import io.netty.channel.ChannelHandlerContext;
  8. import io.netty.channel.SimpleChannelInboundHandler;
  9. import lombok.extern.slf4j.Slf4j;
  10. import java.lang.reflect.Field;
  11. import java.util.*;
  12. @Slf4j
  13. @ChannelHandler.Sharable
  14. public class PlcClientHandler extends SimpleChannelInboundHandler<String> {
  15. private PlcNettyConfig ledNettyConfig;
  16. private PLCService plcService = new PLCService();
  17. public PlcClientHandler(PlcNettyConfig ledNettyConfig) {
  18. this.ledNettyConfig = ledNettyConfig;
  19. }
  20. @Override
  21. protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  22. log.info("PLC 客户端收到消息:" + msg);
  23. DeviceCache.put("plc",1);
  24. String result = Integer.toBinaryString(Integer.parseInt(msg.substring(6, 8),16));
  25. String oldPlcInfo = DeviceCache.getPlcStatus();
  26. String newPlcInfo = String.format("%8s", result).replace(" ", "0");
  27. // 将plc的设备信息补到8存入缓存
  28. DeviceCache.setPlcStatus(newPlcInfo);
  29. // 设备信息发生变化时进入
  30. if (oldPlcInfo!=null&&!newPlcInfo.equals(oldPlcInfo)){
  31. SerialSetting.PlcDTO.StatusDTO status = EventConfig.serialSetting.getPlc().getStatus();
  32. String newStr = new StringBuffer(newPlcInfo).reverse().toString();
  33. String oldStr = new StringBuffer(oldPlcInfo).reverse().toString();
  34. for (int i=0;i<newStr.length();i++){
  35. // 第i位设备的值
  36. String value = String.valueOf(newStr.charAt(i));
  37. // 设备被触发
  38. if (value.equals("1")&&!value.equals(String.valueOf(oldStr.charAt(i)))){
  39. // 记录时间 配置的设备地址为map的key
  40. for (Field field:status.getClass().getDeclaredFields()){
  41. field.setAccessible(true);
  42. if (Integer.parseInt(String.valueOf(field.get(status)))==i){
  43. // String name = fields[i].getName();
  44. DeviceCache.putTime((String) field.get(status),new Date());
  45. break;
  46. }
  47. }
  48. // 前红外触发时调用方法
  49. if (String.valueOf(i).equals(status.getFRedPoint())){
  50. new Thread(()-> {
  51. try {
  52. plcService.fRedTrigger(newPlcInfo);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. }).start();
  57. }
  58. if (String.valueOf(i).equals(status.getRadarPoint())){
  59. new Thread(()-> {
  60. try {
  61. plcService.radarTrigger();
  62. } catch (InterruptedException e) {
  63. e.printStackTrace();
  64. }
  65. }).start();
  66. }
  67. }
  68. }
  69. }
  70. // for (int i=0;i<result.length();i++){
  71. // String status = String.valueOf(result.charAt((result.length() - 1) - i));
  72. // if (status.equals("1")){
  73. // list.add(i);
  74. // }
  75. // }
  76. // System.out.println(list);
  77. // log.info("PLC 客户端收到消息:" + msg);
  78. }
  79. @Override
  80. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  81. log.error("PLC 客户端 连接断开,进行重连");
  82. ledNettyConfig.connect();
  83. }
  84. @Override
  85. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  86. cause.printStackTrace();
  87. ctx.close();
  88. }
  89. }