private boolean handleMessage(IMessage msg) { /** * 业务处理器收到通信前置机上行报文,需要预先处理, 先检查是否存在此终端档案,存在才进行业务处理,不然只保存原始记录 任务,异常,主站请求返回处理:包括解析及回写数据库 原始报文保存 */ MessageZj zjmsg = null; MessageGw gwmsg = null; int rtua = 0; boolean result = true; if (msg.getMessageType() == MessageType.MSG_ZJ) { zjmsg = (MessageZj) msg; rtua = zjmsg.head.rtua; } else if (msg.getMessageType() == MessageType.MSG_GW_10) { gwmsg = (MessageGw) msg; rtua = gwmsg.head.rtua; } BizRtu rtu = (RtuManage.getInstance().getBizRtuInCache(rtua)); if (rtu == null) { // 找不到终端则刷新数据库档案 boolean refreshTag = manageRtu.refreshBizRtu(rtua); if (!refreshTag) log.warn("not find rtu in db:" + HexDump.toHex(rtua)); else rtu = (RtuManage.getInstance().getBizRtuInCache(rtua)); } if (rtu != null) { if (zjmsg != null) { // 任务 异常 if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_TASK || zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK2 || zjmsg.head.c_func == MessageConst.GG_FUNC_Event) { msgQueue.setLastHandleDataTime(System.currentTimeMillis()); counter.add(); // 如果超过处理队列最大值则放回缓存队列,以免丢失数据 if (!asycService.addMessage(zjmsg)) { msgQueue.offer(zjmsg); result = false; log.warn("asycService.addMessage failed"); } if (zjmsg.head.msta != 0x00 && zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM) { // zj事件主动召测进入这里,与主站交互. hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg); } } else if (zjmsg.head.c_func == MessageConst.GG_UPGRADE) { GgUpgradeProcessor.getInstance().onUpgradeReturn(zjmsg); return true; } else if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_RELAY || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_CUR || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_ROBJ || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_OBJ || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_PROG || zjmsg.head.c_func == MessageConst.GG_FUNC_Action || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK1 || zjmsg.head.c_func == MessageConst.GG_FUNC_AutoRegistered || zjmsg.head.c_func == MessageConst.GG_Pay_token) { // 主站请求返回 hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg); } else if (zjmsg.isLogin()) { saveConnectSituation(zjmsg); } } else if (gwmsg != null) { if (gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA1 || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA2) { if (gwmsg.head.c_prm == 1) { // 主动上送的一、二数据报文都认为是任务 gwmsg.setTask(true); // 设置任务状态 } else { // 一、二类数据回复数据需要判断是否为任务漏点补招返回 int fseq = gwmsg.getFseq(); List<RtuCmdItem> rcis = masterDbService.getRtuComdItem(HexDump.toHex(rtua), fseq); for (RtuCmdItem rci : rcis) { if (rci.getZdzjbz() == Operator.GWLDBZ || rci.getZdzjbz() == Operator.ZZRW_SJCJ) { // 国网任务漏点补招请求返回或者主站任务轮招数据采集返回 gwmsg.setTask(true); // 设置任务状态 break; } } } } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_AUTH) { GwUpdateKeyHandler.getInstance() .processUpdateKeyMsg(asycService, manageRtu, masterDbService, gwmsg); return true; } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_RELAY_READ) { if (msgQueue.isDlmsRelay(gwmsg.getLogicalAddress())) { // 国网规约接dlms规约表中继 if (DlmsEventProcessor.getInstance().postUpRelayMessage(gwmsg)) { return result; } } } // 如果是任务或异常事件则放入相应队列 if (gwmsg.isTask() || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA3) { msgQueue.setLastHandleDataTime(System.currentTimeMillis()); counter.add(); // 如果超过处理队列最大值则放回缓存队列,以免丢失数据 if (!asycService.addMessage(gwmsg)) { msgQueue.offer(gwmsg); result = false; log.warn("asycService.addMessage failed"); } if (gwmsg.head.c_prm == MessageConst.DIR_DOWN) // 主站召测 hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg); } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_HEART) { // 当是登陆帧的时候,存储。 if (gwmsg.isLogin()) { saveConnectSituation(gwmsg); } } else hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg); } } return result; }
public Object decode(IMessage message) { HostCommand hc = new HostCommand(); try { String data = ParseTool.getMsgData(message); MessageGw msg = (MessageGw) message; if (msg.head.seq_tpv == 1) // TpV位置1表示附加域有时间标签 data = data.substring(0, data.length() - 12); // 消去6个字节的时间标签 if (msg.head.c_acd == 1) // ACD位置1表示附加域有事件计数器 data = data.substring(0, data.length() - 4); // 消去2个字节的事件计数器 String sAFN = Integer.toString(msg.getAFN() & 0xff, 16).toUpperCase(); sAFN = DataSwitch.StrStuff("0", 2, sAFN, "left"); while (data.length() >= 8) { int[] tn = DataItemParser.measuredPointParser(data.substring(0, 4)); String[] codes = DataItemParser.dataCodeParser(data.substring(4, 8), sAFN); data = data.substring(8); // 消去数据单元标识 if (codes.length > 0 && codes[0].equals("0AF010")) { if (data.length() >= 8) { int count = Integer.parseInt( DataItemParser.parseValue(data.substring(0, 4), "HTB2").getValue()); data = data.substring(4); String value = ""; for (int i = 0; i < count; i++) { value = value + DataItemParser.parseValue(data.substring(0, 4), "HTB2").getValue() + ","; data = data.substring(4); } if (!value.equals("")) value = value.substring(0, value.length() - 1); for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(value); hc.addResult(hcr); } } } } else if (codes.length > 0 && (codes[0].equals("0AF011") || codes[0].equals("0AF013") || codes[0].equals("0AF014") || codes[0].equals("0AF015") || codes[0].equals("0AF033") || codes[0].equals("0AF034"))) { if (data.length() >= 4) { int count = Integer.parseInt( DataItemParser.parseValue(data.substring(0, 2), "HTB1").getValue()); data = data.substring(2); String value = ""; for (int i = 0; i < count; i++) { value = value + DataItemParser.parseValue(data.substring(0, 2), "HTB1").getValue() + ","; data = data.substring(2); } if (!value.equals("")) value = value.substring(0, value.length() - 1); for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(value); hc.addResult(hcr); } } } } else if (codes.length > 0 && (codes[0].equals("0AF038") || codes[0].equals("0AF039"))) { if (data.length() >= 6) { String value = data.substring(0, 2); data = data.substring(2); int count = Integer.parseInt( DataItemParser.parseValue(data.substring(0, 2), "HTB1").getValue()); data = data.substring(2); for (int i = 0; i < count; i++) { value = value + DataItemParser.parseValue(data.substring(0, 2), "HTB1").getValue() + ","; data = data.substring(2); } if (!value.equals("")) value = value.substring(0, value.length() - 1); for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(value); hc.addResult(hcr); } } } } else if (sAFN.equals("0D")) { DataTimeTag dataTimeTag = new DataTimeTag(); int dataType = 0; String date = ""; for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { dataType = getGw0DDataType(Integer.parseInt(codes[j].substring(3, 6))); if (dataType == 0) { // 小时冻结时标解析得到:开始时间(YYYYMMDDHHNN)+时间间隔+数据点数 dataTimeTag = DataItemParser.getTaskDateTimeInfo(data.substring(0, 14), 2, ""); date = DataSwitch.ReverseStringByByte(data.substring(0, 14)); data = data.substring(14); } else { // 冻结数据 if (dataType == 1) { // 日冻结时标3个字YYYYMMDD dataTimeTag = DataItemParser.getTaskDateTimeInfo(data.substring(0, 6), 3, ""); date = DataSwitch.ReverseStringByByte(data.substring(0, 6)); data = data.substring(6); } else { // 月冻结时标2个字YYYYMM dataTimeTag = DataItemParser.getTaskDateTimeInfo(data.substring(0, 4), 4, ""); date = DataSwitch.ReverseStringByByte(data.substring(0, 4)); data = data.substring(4); } } HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(date); hc.addResult(hcr); } } } else if (sAFN.equals("0E") || sAFN.equals("10") || sAFN.equals("0F")) { for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(data); hc.addResult(hcr); } } data = ""; } else { for (int i = 0; i < tn.length; i++) { for (int j = 0; j < codes.length; j++) { HostCommandResult hcr = new HostCommandResult(); hcr.setCode(codes[j]); hcr.setTn("" + tn[i]); hcr.setValue(""); hc.addResult(hcr); } } } } hc.setStatus(HostCommand.STATUS_SUCCESS); } catch (Exception e) { throw new MessageDecodeException(e); } return hc; } // 判断二类数据类型:0曲线数据;1日冻结;2月冻结