/// <summary> /// tcp解析 /// </summary> /// <param name="cacheorbase">获取数据方式</param> public void ProcessingTCP() { Thread.Sleep(1 * 1000); while (1 == 1) { while (!TcpMessagePool.IsNull()) { _isWork = true; //从消息队里中取得消息 MessageVo messageVO = null; try { messageVO = TcpMessagePool.Dequeue(); }catch (Exception ee) { LogUtil.error("队列:" + ee.Message); } if (messageVO == null) { _isWork = false; break; } try { //解析消息 TCPMessage tcpmessage = null; try { tcpmessage = new TCPMessage(messageVO.key, messageVO.message); //add by hbqian int 20130418 for其实0。1分的发电量是昨天,按道理不应该发的,但是现在LOG有个换存,导致0:到15分的这个时间发的发电量可能还是上一天的 //modify by hbqian at 2013-08-06 for 时间不对超过24小时的自动抛弃, if ((tcpmessage.messageHeader.TimeNow - DateTime.Now).TotalHours > 24 || (tcpmessage.messageHeader.TimeNow.Hour == 0 && tcpmessage.messageHeader.TimeNow.Minute < 15)) { continue; } } catch (Exception ee) { LogUtil.error("解析消息" + messageVO.key + ",sn:" + TcpHeader.getSn(messageVO.message) + ",消息内容:" + messageVO.message + "异常:" + ee.Message); //处理错误的消息入队 //处理完从memched中删除此消息 if (messageVO.key != null) { IList <string> analyzedKeys = (List <string>)MemcachedClientSatat.getInstance().Get(MemcachedClientSatat.analyzedkey); MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key, analyzedKeys); MemcachedClientSatat.getInstance().remember(messageVO.key); } continue; } //持久化将数据保存到缓存 DateTime curdt = DateTime.Now; CacheHandler.LocalCacheData(tcpmessage); LogUtil.writeline(tcpmessage.messageHeader.CollectorCode + " CacheHandler.LocalCacheData解析用时:" + (DateTime.Now.Subtract(curdt).TotalSeconds)); try { //设备发电量先放到map,然后集中处理 double tmpvalue = 0; foreach (string key in tcpmessage.deviceEnergyMap.Keys) { tmpvalue = tcpmessage.deviceEnergyMap[key]; if (key != null && TcpDataProcesser.deviceEnergyMap.ContainsKey(key)) { double ovalue = TcpDataProcesser.deviceEnergyMap[key]; if (ovalue < tmpvalue) { TcpDataProcesser.deviceEnergyMap[key] = tmpvalue; } } else { TcpDataProcesser.deviceEnergyMap[key] = tmpvalue; } } }catch (Exception e22) { LogUtil.error("device energy map error:" + e22.Message); } try { //采集器天发电量放到map string ekey = tcpmessage.GetCollectorId() + ":" + tcpmessage.messageHeader.year + ":" + tcpmessage.messageHeader.month + ":" + tcpmessage.messageHeader.day; //if (!TcpDataProcesser.collectorEnergyMap.ContainsKey(ekey) || (TcpDataProcesser.collectorEnergyMap.ContainsKey(ekey) && TcpDataProcesser.collectorEnergyMap[ekey] < tcpmessage.messageHeader.DayEnergy)) //{ //必须添加hasData作为条件,否则会出现新协议的发电量被设备数据产生的空messageHeader冲成0了。 if (tcpmessage.messageHeader.hasData && tcpmessage.messageHeader.DayEnergy != null) { TcpDataProcesser.collectorEnergyMap[ekey] = tcpmessage.messageHeader.DayEnergy.Value; } //} }catch (Exception e223) { LogUtil.error("collectorEnergyMap energy map error:" + e223.Message); } //对应采集器发生先放入list,然后集中批处理到缓存 try { if (tcpmessage.collectorDataCount != null) { TcpDataProcesser.collectorDataCounts.Add(tcpmessage.collectorDataCount); LogUtil.error("add collector Count Data 完成:collectorId:" + tcpmessage.collectorDataCount.deviceId + ":" + tcpmessage.collectorDataCount.maxValue + "-" + tcpmessage.collectorDataCount.maxTime); } } catch (Exception ddcEe) { LogUtil.error("add collector Count Data exception:" + ddcEe.Message); //处理完从memched中删除此消息 if (messageVO.key != null) { IList <string> analyzedKeys = (List <string>)MemcachedClientSatat.getInstance().Get(MemcachedClientSatat.analyzedkey); MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key, analyzedKeys); MemcachedClientSatat.getInstance().remember(messageVO.key); } continue; } AnalyzeCount.successNum++; AnalyzeCount.curSuccessNum++; AnalyzeCount.total++; AnalyzeCount.curtotal++; // (AnalyzeCount.lasttime.Year==1|| tcpmessage.messageHeader.TimeNow.Subtract(AnalyzeCount.lasttime).TotalSeconds>0) // AnalyzeCount.lasttime = tcpmessage.messageHeader.TimeNow; // LogUtil.writeline("成功处理:" + "sn:" + TcpHeader.getSn(messageVO.message) + ",key:" + messageVO.key); //FileLogUtil.info("成功处理:" + "sn:" + TcpHeader.getSn(messageVO.message) + ",key:" + messageVO.key); //设置最后成功处理时间到memcached,以便检测监控程序能判断是否正常运行 MemcachedClientSatat.getInstance().Set("monitor_analyze_run_lasttime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } catch (Exception ee) { LogUtil.error("异常:" + ee.Message); //处理错误的消息入队 //TcpMessagePool.Enqueue(messageVO); //continue; AnalyzeCount.failNum++; AnalyzeCount.curFailNum++; } //处理完从memched中删除此消息 if (messageVO.key != null) { IList <string> analyzedKeys = (List <string>)MemcachedClientSatat.getInstance().Get(MemcachedClientSatat.analyzedkey); MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key, analyzedKeys); MemcachedClientSatat.getInstance().remember(messageVO.key); } } _isWork = false; Thread.Sleep(1 * 100); } }
/// <summary> /// tcp解析 /// </summary> /// <param name="cacheorbase">获取数据方式</param> public void ProcessingTCP() { //电站最大发生值 Thread.Sleep(1 * 1000); while (1 == 1) { while (!TcpMessagePool.IsNull()) { _isWork = true; //从消息队里中取得消息 MessageVo messageVO = null; try { messageVO = TcpMessagePool.Dequeue(); }catch (Exception ee) { LogUtil.error("队列:" + ee.Message); } if (messageVO == null) { _isWork = false; return; } try { //解析消息 TCPMessage tcpmessage = null; try { tcpmessage = new TCPMessage(messageVO.key, messageVO.message); } catch (Exception ee) { LogUtil.error("解析消息" + messageVO.key + ",消息内容:" + messageVO.message + "异常:" + ee.Message); //处理错误的消息入队 //处理完从memched中删除此消息 if (messageVO.key != null) { MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key); MemcachedClientSatat.getInstance().remember(messageVO.key); } continue; } //持久化将数据保存到缓存 DateTime curdt = DateTime.Now; CacheHandler.LocalCacheData(tcpmessage); LogUtil.writeline(tcpmessage.messageHeader.CollectorCode + " CacheHandler.LocalCacheData解析用时:" + (DateTime.Now.Subtract(curdt).TotalSeconds)); try { //设备发电量先放到map,然后集中处理 double tmpvalue = 0; foreach (string key in tcpmessage.deviceEnergyMap.Keys) { tmpvalue = tcpmessage.deviceEnergyMap[key]; if (key != null && TcpDataProcesser.deviceEnergyMap.ContainsKey(key)) { double ovalue = TcpDataProcesser.deviceEnergyMap[key]; if (ovalue < tmpvalue) { TcpDataProcesser.deviceEnergyMap[key] = tmpvalue; } } else { TcpDataProcesser.deviceEnergyMap[key] = tmpvalue; } } }catch (Exception e22) { LogUtil.error("device energy map error:" + e22.Message); } try { //采集器天发电量放到map string ekey = tcpmessage.GetCollectorId() + ":" + tcpmessage.messageHeader.year + ":" + tcpmessage.messageHeader.month + ":" + tcpmessage.messageHeader.day; //if (!TcpDataProcesser.collectorEnergyMap.ContainsKey(ekey) || (TcpDataProcesser.collectorEnergyMap.ContainsKey(ekey) && TcpDataProcesser.collectorEnergyMap[ekey] < tcpmessage.messageHeader.DayEnergy)) //{ //现在 tcpmessage.messageHeader.DayEnergy已经废弃不用了,放进去是无意义,暂时保留 TcpDataProcesser.collectorEnergyMap[ekey] = tcpmessage.messageHeader.DayEnergy; //} }catch (Exception e223) { LogUtil.error("collectorEnergyMap energy map error:" + e223.Message); } //对应采集器发生先放入list,然后集中批处理到缓存 try { if (tcpmessage.collectorDataCount != null) { TcpDataProcesser.collectorDataCounts.Add(tcpmessage.collectorDataCount); } } catch (Exception ddcEe) { LogUtil.error("add collector Count Data exception:" + ddcEe.Message); //处理完从memched中删除此消息 if (messageVO.key != null) { MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key); MemcachedClientSatat.getInstance().remember(messageVO.key); } continue; } AnalyzeCount.successNum++; AnalyzeCount.curSuccessNum++; AnalyzeCount.total++; AnalyzeCount.curtotal++; // (AnalyzeCount.lasttime.Year==1|| tcpmessage.messageHeader.TimeNow.Subtract(AnalyzeCount.lasttime).TotalSeconds>0) // AnalyzeCount.lasttime = tcpmessage.messageHeader.TimeNow; // LogUtil.writeline("成功处理:" + messageVO.key); } catch (Exception ee) { LogUtil.error("异常:" + ee.Message); //处理错误的消息入队 //TcpMessagePool.Enqueue(messageVO); //continue; AnalyzeCount.failNum++; AnalyzeCount.curFailNum++; } //处理完从memched中删除此消息 if (messageVO.key != null) { MemcachedClientSatat.getInstance(TcpDataProcesser.msgmemchached).deleteAnalyzed(messageVO.key); MemcachedClientSatat.getInstance().remember(messageVO.key); } } _isWork = false; Thread.Sleep(1 * 100); } }