public void run() { synchronized (works) { works.add(this); } this.setPriority(threadPriority); int count = 0; // 每处理1000个事件检测线程池是否需要调整 log.info("threadpool.work running:" + this.getName()); while (!BProcessor.this.state.isStopping() && !BProcessor.this.state.isStopped()) { try { busy = false; currentMessage = msgQueue.take(); if (null == currentMessage) { // 如果数据库连接异常,返回NULL。 Thread.sleep(100); continue; } if (log.isDebugEnabled()) log.debug( "Process up Message:" + currentMessage .getRawPacketString()); // log.debug("处理上行报文:"+currentMessage.getRawPacketString()); if (currentMessage.getMessageType() == MessageType.MSG_KILLTHREAD) break; else if (currentMessage.getMessageType() == MessageType.MSG_GATE) { MessageGate mgate = (MessageGate) currentMessage; if (mgate.getHead().getCommand() == MessageGate.CMD_GATE_REPLY) { IMessage msg = mgate.getInnerMessage(); if (!handleMessage(msg)) { // 后续数据库保存速度不足 Thread.sleep(500); } } } else if (currentMessage.getMessageType() == MessageType.MSG_ZJ || currentMessage.getMessageType() == MessageType.MSG_GW_10) { if (!handleMessage(currentMessage)) { // 后续数据库保存速度不足 Thread.sleep(500); } } else if (currentMessage.getMessageType() == MessageType.MSG_DLMS) { DlmsMessage msg = (DlmsMessage) currentMessage; DlmsEvent evt = new DlmsEvent(msg); evt.setSource(msg.getSource()); DlmsEventProcessor.getInstance().handleEvent(evt); } // 检测队列中事件个数。如果太多,增加线程。如果为0,减少线程 count++; if (count > 500) { justThreadSize(); count = 0; } } catch (Exception exp) { log.error("BP WorkThread occur error", exp); // log.error("业务处理器报文处理线程处理浙规报文出错", exp); continue; } } synchronized (works) { works.remove(this); } log.info("Thread pool work thread exit" + this.getName()); // 线程池的工作线程退出: }
public void readAlarm( DlmsEventProcessor processor, DlmsObisItem[] params, DlmsRequest req, DlmsContext context, String dateTime) { boolean isIranTime = DlmsConstant.getInstance().isIranTime; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (isIranTime && !isOldProtocol(context.subProtocol)) { // 伊朗老表上报时间是公历,这里就不要将伊朗历转为公历了。 dateTime = DateConvert.iranToGregorian(dateTime); } Date occurTime = null; try { occurTime = sdf.parse(dateTime); req.addAppendParam("EventOccurTime", occurTime); req.addAppendParam("OBIS", params[0].obisString); } catch (ParseException e) { } req.setOperator("EVENT_READING_AUTO"); req.setOpType(DLMS_OP_TYPE.OP_GET); params[0].accessSelector = 1; params[0].classId = 7; params[0].attributeId = 2; req.setMeterId(context.meterId); SelectiveAccessDescriptor sad = new SelectiveAccessDescriptor(); String fromTime = sdf.format(occurTime.getTime() - 1 * 1000 * 60); String now = sdf.format(occurTime.getTime() + 1 * 1000 * 60); DlmsAlarmStatus alarmStatus = DlmsAlarmManager.getInstance().getAlarmStatus(context.meterId, params[0].obisString); if (alarmStatus != null) { // -------------------包含最大读事件间隔-------------------------------------------------- // Calendar rc = Calendar.getInstance(); // rc.setTime(alarmStatus.getLastReportTime()); // Calendar fc = Calendar.getInstance(); // fc.setTime(occurTime); // rc.add(Calendar.HOUR, DlmsConstant.getInstance().alarmTimeReadInterval); // if(!rc.before(fc)){//读事件的间隔不能太长。 // fromTime=sdf.format(alarmStatus.getLastReportTime()); // } // -------------------不包含最大读事件间隔------------------------------------------------ Calendar fc = Calendar.getInstance(); fc.setTime(occurTime); fromTime = sdf.format(alarmStatus.getLastReportTime()); } // 事件上报的帧里,含有时间,可以利用这个时间进行抄读 fromTime = isIranTime ? DateConvert.gregorianToIran(fromTime) : fromTime; now = isIranTime ? DateConvert.gregorianToIran(now) : now; sad.selectByPeriodOfTime(fromTime, now, isOldProtocol(context.subProtocol)); params[0].data.assignValue(sad.getParameter()); req.setSubprotocol(context.subProtocol); req.setParams(params); processor.postWebRequest(req, null); }
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 void handleUpgrade(DlmsEventProcessor processor, DlmsRequest req, DlmsContext context) { try { boolean isFinished = false; // 是否升级结束 boolean isSuccess = true; // 命令是否成功 DlmsRequest dr = new DlmsRequest(); dr.setRelayParam(req.getRelayParam()); dr.setMeterId(context.meterId); dr.addAllAppendParmas(req.getAllParam()); dr.setDestAddr(req.getDestAddr()); if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_00)) { isSuccess = updateStep_01_01(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_01_01)) { isSuccess = upgradeStep_01(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_01)) { isSuccess = upgradeStep_02(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_02)) { isSuccess = upgradeStep_03(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_03) || req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_RESSIUE)) { isSuccess = blockTransfering(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_READMAP)) { upgradeStep_04(req, dr); } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_04)) { if (checkBitMap(req, dr)) { // 如果没有漏点,进入第五步 upgradeStep_05(req, dr); } } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_05)) { // 检测验证是否成功 if (req.getParams()[0].resultCode == 2) { // 如果返回的是temporary-failure,休息一会再读 try { Thread.sleep(sleepTimeStay06Step * 1000); } catch (InterruptedException e) { } } readTransferStatus(dr); // 这里无论返回什么,都去读升级状态 } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_READ_STATUS)) { int verfiyFlg = req.getParams()[0].resultData.getEnum(); if (verfiyFlg == 3) { upgradeStep_06(req, dr); } else if (verfiyFlg == 2) { try { Thread.sleep(sleepTimeStay06Step * 1000); } catch (InterruptedException e) { } readTransferStatus(dr); } else { // 除了上面两种情况,都是失败 isSuccess = false; } } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_06)) { // 读到的数据与主站下发做比较 // 如果成功,执行第7步 { upgradeStep_07(req, dr); } } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_07)) { // 如果结束 设置finished=true if (req.getParams()[0].resultCode == 0) isFinished = true; else { isSuccess = false; } } // 查询待发送列表中,是否有要升级的请求,如果有,则当前请求判定为失败 if (context.webReqList.size() > 0) { DlmsEvent evt = (DlmsEvent) context.webReqList.get(0); DlmsRequest request = (DlmsRequest) evt.getRequest(); if (request.getOperator() != null && request.getOperator().contains("UPGRADE")) { isSuccess = false; log.error( "meterid=" + context.meterId + ".New upgrade request is comming,current upgrade set fail"); } } if (!isSuccess) { DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.FAIL); // 设置为失败 DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "00"); if (tracer.isEnabled()) tracer.trace( "Upgrade Failed. MeterId=" + req.getMeterId() + ",Operator=" + req.getOperator()); return; } if (!isFinished) { DlmsUpgradeAssisant.getInstance().onUpgradeSuccess(dr); processor.postWebRequest(dr, null); } else { // 收尾阶段 req.addAppendParam( DlmsUpgradeAssisant.CURRENT_BLOCK_NUM, (Integer) req.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT)); DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.SUCCESS); // 设置为成功 DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "02"); } } catch (Exception e) { log.error(StringUtil.getExceptionDetailInfo(e)); DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.FAIL); // 设置为失败 DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "00"); } }