package com.dy.common.mw.protocol; import java.util.Collection; import java.util.HashMap; public class OnLineHandle { /** * 解析上线数据,得到RtuAddr * @param bs 字节数组 * @return OnLine.OnLineResult */ public OnLine.OnLineResult parse(byte[] bs)throws Exception{ OnLine.OnLineResult rs = null ; HashMap onLineMap = ProtocolCache.getOnLineMap() ; Collection set = onLineMap.values() ; if(set.size() == 0){ throw new Exception("处理上线时,得到的协议集合为空。") ; } int prority = ProtocolConstant.firstPriority ; while(true){ Object[] objs = this.getClassObjAndAnnotationVo(prority, set) ; OnLine onLine = (OnLine)objs[0] ; if(onLine == null && prority == ProtocolConstant.firstPriority){ throw new Exception("处理上线时,未得到优先级为" + prority + "上线处理类!") ; }else if(onLine == null){ //说明上线处理集合已经遍历完了。 break ; } rs = onLine.parse(bs) ; OnLinePool.freeInstance(((AnnotationOnLineVo)objs[1]).clazz, onLine); if(rs != null){ if(rs.result == OnLine.OnLineAction_success){ //成功, 停止循环,返回结果 break ; }else if(rs.result == OnLine.OnLineAction_success_noMe){ //不是本协议数据 , 循环继续 prority++ ; }else if(rs.result == OnLine.OnLineAction_success_response){ //解析上线数据,需要回写数据 //停止循环,返回结果 break ; }else if(rs.result == OnLine.OnLineAction_fail){ //解析上线数据错误,一般是上行数据为空 throw new Exception("处理上线时,解析ID出错(可能程序错误,上行数据为null)!") ; } } } if(rs == null){ throw new Exception("处理上线时出错,未能成功分析出IMEI号!") ; } return rs ; } /** * 得到处理类对象 * @param priority 优先级 * @param set 集合 * @return Object[] * @throws Exception 异常 */ private Object[] getClassObjAndAnnotationVo(int priority, Collection set) throws Exception{ OnLine obj = null ; AnnotationOnLineVo rVo = null ; for(AnnotationOnLineVo vo : set){ if(priority == vo.priority){ obj = OnLinePool.getInstance(vo.clazz) ; rVo = vo ; break ; } } return new Object[]{obj, rVo} ; } }