|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String rtuAddr = (String)session.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrIdKey) ; | 
|---|
|  |  |  | String protocolName = null ; | 
|---|
|  |  |  | Short protocolVersion = null ; | 
|---|
|  |  |  | boolean isOnLine = false ; | 
|---|
|  |  |  | if(rtuAddr == null){ | 
|---|
|  |  |  | //说明刚建立网络连接,此数据应该是上线数据 | 
|---|
|  |  |  | isOnLine = true ; | 
|---|
|  |  |  | String[] rtuAddrProtocolName = this.parseOnLine(session, upBuf) ; | 
|---|
|  |  |  | if(rtuAddrProtocolName.length == 2){ | 
|---|
|  |  |  | Object[] rtuAddrProtocolNameVersion = this.parseOnLine(session, upBuf) ; | 
|---|
|  |  |  | if(rtuAddrProtocolNameVersion.length == 2){ | 
|---|
|  |  |  | //解析上线数据成功,并解析出RTU地址及通信协议名称 | 
|---|
|  |  |  | rtuAddr = rtuAddrProtocolName[0] ; | 
|---|
|  |  |  | protocolName = rtuAddrProtocolName[1] ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | rtuAddr = (String)rtuAddrProtocolNameVersion[0] ; | 
|---|
|  |  |  | protocolName = (String)rtuAddrProtocolNameVersion[1] ; | 
|---|
|  |  |  | protocolVersion = (Short)rtuAddrProtocolNameVersion[2] ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(rtuAddr != null && protocolName != null){ | 
|---|
|  |  |  | //设置session的属性ID | 
|---|
|  |  |  | TcpUnit.getInstance().setIoSessionArrId(session, rtuAddr); | 
|---|
|  |  |  | //缓存session | 
|---|
|  |  |  | TcpSessionCache.putNewTcpSession(rtuAddr, protocolName, session); | 
|---|
|  |  |  | TcpSessionCache.putNewTcpSession(rtuAddr, protocolName, protocolVersion, session); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("RTU(地址:" + rtuAddr + ",协议:" + protocolName + ")上线了。") ; | 
|---|
|  |  |  | log.info("RTU(地址:" + rtuAddr + ",协议:" + protocolName + ",协议版本号:" + protocolVersion + ")上线了。") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(toDeal){ | 
|---|
|  |  |  | if(protocolName == null){ | 
|---|
|  |  |  | protocolName = TcpSessionCache.getTcpProtocolName(rtuAddr) ; | 
|---|
|  |  |  | Object[] objs = TcpSessionCache.getTcpProtocolNameVersion(rtuAddr) ; | 
|---|
|  |  |  | protocolName = (String)objs[0] ; | 
|---|
|  |  |  | protocolVersion = (Short)objs[1] ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //设置收到数据时刻 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(protocolName != null){ | 
|---|
|  |  |  | //对上行数据进行处理 | 
|---|
|  |  |  | this.dealUpData(session, rtuAddr, protocolName, isOnLine, upBuf, upHex) ; | 
|---|
|  |  |  | this.dealUpData(session, rtuAddr, protocolName, protocolVersion, isOnLine, upBuf, upHex) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param session IO会话 | 
|---|
|  |  |  | * @param upBuf 上行数据 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private String[] parseOnLine(IoSession session, byte[] upBuf){ | 
|---|
|  |  |  | private Object[] parseOnLine(IoSession session, byte[] upBuf){ | 
|---|
|  |  |  | String rtuAddr = null ; | 
|---|
|  |  |  | String protocolName = null ; | 
|---|
|  |  |  | Short protocolVersion = null ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | OnLine.OnLineResult rs = new OnLineHandle().parse(upBuf) ; | 
|---|
|  |  |  | if(rs == null | 
|---|
|  |  |  | 
|---|
|  |  |  | log.error("严重错误,解析上线结果中协议名称为空 !" ) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | protocolName = rs.protocolName ; | 
|---|
|  |  |  | protocolVersion = rs.protocolVersion ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if(rs.result == OnLine.OnLineAction_success_response){ | 
|---|
|  |  |  | if(rs.remoteData != null && rs.remoteData.length > 0){ | 
|---|
|  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("严重错误,分析上线数据时产生异常 !\n" + e.getMessage() , e) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new String[]{rtuAddr, protocolName} ; | 
|---|
|  |  |  | return new Object[]{rtuAddr, protocolName, protocolVersion} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param session IO会话 | 
|---|
|  |  |  | * @param rtuAddrAtHead 控制器地址头部 | 
|---|
|  |  |  | * @param protocolName 协议名称 | 
|---|
|  |  |  | * @param protocolVersion 协议版本号 | 
|---|
|  |  |  | * @param isOnLine 是否上线数据 | 
|---|
|  |  |  | * @param upBuf 上行数据 | 
|---|
|  |  |  | * @param upHex 上行数据 | 
|---|
|  |  |  | * @throws Exception 异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void dealUpData(IoSession session, String rtuAddrAtHead, String protocolName, boolean isOnLine, byte[] upBuf, String upHex) throws Exception{ | 
|---|
|  |  |  | private void dealUpData(IoSession session, | 
|---|
|  |  |  | String rtuAddrAtHead, | 
|---|
|  |  |  | String protocolName, | 
|---|
|  |  |  | Short protocolVersion, | 
|---|
|  |  |  | boolean isOnLine, | 
|---|
|  |  |  | byte[] upBuf, | 
|---|
|  |  |  | String upHex) throws Exception{ | 
|---|
|  |  |  | Driver dri = ProtocolCache.getDriver(protocolName) ; | 
|---|
|  |  |  | if(dri == null){ | 
|---|
|  |  |  | log.error("严重错误,未能得到协议" + protocolName + "驱动类实例!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | //更新终端状态 | 
|---|
|  |  |  | if(rtuAddrInData != null && !rtuAddrInData.equals(rtuAddrAtHead)){ | 
|---|
|  |  |  | //数据头中的RTU地址与数据中的RTU地址不一致,更换成数据中的RTU地址 | 
|---|
|  |  |  | TcpSessionCache.changeRtuAddr(rtuAddrAtHead, rtuAddrInData, protocolName, session); | 
|---|
|  |  |  | TcpSessionCache.changeRtuAddr(rtuAddrAtHead, rtuAddrInData, protocolName, protocolVersion, session); | 
|---|
|  |  |  | session.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrIdKey, rtuAddrInData) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | InetSocketAddress sa = (InetSocketAddress)session.getRemoteAddress() ; | 
|---|
|  |  |  | if(isOnLine){ | 
|---|
|  |  |  | //上线了 | 
|---|
|  |  |  | RtuStatusDealer.onLine(rtuAddr, sa.getAddress().getHostAddress(), sa.getPort()); | 
|---|
|  |  |  | RtuStatusDealer.onLine(rtuAddr, sa.getAddress().getHostAddress(), sa.getPort(), protocolName, protocolVersion); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(reportOrResponse_trueOrFalse != null && reportOrResponse_trueOrFalse){ | 
|---|
|  |  |  | RtuStatusDealer.upReport(rtuAddr, upBuf.length) ; | 
|---|
|  |  |  | RtuStatusDealer.upAutoReport(rtuAddr, code, codeName, upBuf.length) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | RtuStatusDealer.upData(rtuAddr, upBuf.length) ; | 
|---|
|  |  |  | RtuStatusDealer.upData(rtuAddr, code, codeName, upBuf.length) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //记录日志 | 
|---|