|  |  | 
 |  |  |  | 
 |  |  | import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate; | 
 |  |  | import com.dy.common.util.ByteUtil; | 
 |  |  | import com.dy.common.util.DateTime; | 
 |  |  | import com.dy.simRtu.ServerProperties; | 
 |  |  | import org.apache.logging.log4j.LogManager; | 
 |  |  | import org.apache.logging.log4j.Logger; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     private static final Logger log = LogManager.getLogger(UpHeartBeat.class) ; | 
 |  |  |  | 
 |  |  |     private static boolean isFirst = true ; | 
 |  |  |  | 
 |  |  |     public static boolean upHeartBeat = true ;//是否可以发送心跳 | 
 |  |  |  | 
 |  |  |     public static void upCd02Data(String rtuAddr){ | 
 |  |  |         if(upHeartBeat){ | 
 |  |  |             try{ | 
 |  |  |                 if(UpData.session != null && UpData.session.isConnected()){ | 
 |  |  |                     byte[] bs = createData(rtuAddr) ; | 
 |  |  |                     byte[] bs ; | 
 |  |  |                     if(ServerProperties.argHeardBeatBy02True81False){ | 
 |  |  |                         bs = createCd02Data(rtuAddr) ; | 
 |  |  |                     }else{ | 
 |  |  |                         bs = createCd81Data(rtuAddr) ; | 
 |  |  |                     } | 
 |  |  |                     UpData.upSend(bs) ; | 
 |  |  |                 }else{ | 
 |  |  |                     log.error("未连接通信中间件,不能发送数据"); | 
 |  |  | 
 |  |  |      * @return 字节数组 | 
 |  |  |      * @throws Exception 异常 | 
 |  |  |      */ | 
 |  |  |     private static byte[] createData(String rtuAddr) throws Exception { | 
 |  |  |     private static byte[] createCd02Data(String rtuAddr) throws Exception { | 
 |  |  |         byte[] bytes = creatHead(rtuAddr, "02", (byte)0xB0); | 
 |  |  |  | 
 |  |  |         byte[] bs = new byte[1] ; | 
 |  |  |         bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。 | 
 |  |  |         if(isFirst){ | 
 |  |  |             isFirst = false ; | 
 |  |  |             bs[0] = (byte)0xF0 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。 | 
 |  |  |         }else{ | 
 |  |  |             bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         bytes = ByteUtil.bytesMerge(bytes, bs) ; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |         return bytes ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 构造心跳数据 | 
 |  |  |      * @return 字节数组 | 
 |  |  |      * @throws Exception 异常 | 
 |  |  |      */ | 
 |  |  |     private static byte[] createCd81Data(String rtuAddr) throws Exception { | 
 |  |  |         byte[] bytes = creatHead(rtuAddr, "81", (byte)0xB0); | 
 |  |  |  | 
 |  |  |         int index = 0 ; | 
 |  |  |         byte[] bs1 = new byte[4] ; | 
 |  |  |         bs1[index++] = (byte)0x22 ; | 
 |  |  |         bs1[index++] = (byte)0x6 ; | 
 |  |  |         bs1[index++] = (byte)0x44 ; | 
 |  |  |         bs1[index++] = (byte)0x00 ; | 
 |  |  |         bytes = ByteUtil.bytesMerge(bytes, bs1) ; | 
 |  |  |  | 
 |  |  |         int[] ymdhms = DateTime.yyyy_MM_dd_HH_MM_SS_2_ymdhmsGroup(DateTime.yyyy_MM_dd_HH_mm_ss()) ; | 
 |  |  |         byte[] bs2 = new byte[6] ; | 
 |  |  |         index = 0 ; | 
 |  |  |         int index1 = 5 ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ; | 
 |  |  |         bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--] - 2000)[0] ; | 
 |  |  |         bytes = ByteUtil.bytesMerge(bytes, bs2) ; | 
 |  |  |  | 
 |  |  |         GlCreate.createLen(bytes);//长度放字节数组中 | 
 |  |  |  | 
 |  |  |         byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中 | 
 |  |  |  | 
 |  |  |         bytes = ByteUtil.bytesMerge(bytes, bsTail) ; | 
 |  |  |  | 
 |  |  |         return bytes ; | 
 |  |  |     } | 
 |  |  | } |