private void RabbitToPandas(PumpSystem ppSys)
 {
     foreach (var graph in RuntimeRepo.RtData.Graphs)
     {
         var msgs = new List <string>();
         msgs.Add(ppSys.Guid.ToFormatedString());
         msgs.Add(GetSpeed(ppSys)?.ToString() ?? "-1");
         msgs.Add(graph.Time.ToString("yyyy-MM-dd HH:mm:ss"));
         msgs.Add(graph.Pos.ToString());
         msgs.Add(graph.Type.ToString());
         msgs.Add(GraphArchive.FromGraph(graph).DataStr);
         var msg = string.Join("|||", msgs);
         MessageProducer.Send(msg);
     }
 }
Ejemplo n.º 2
0
        public void BuildFaultItemReports(PumpSystem ppSys)
        {
            using (var context = new PumpSystemContext()) {
                var reportsToSave = new List <FaultItemReport>();
                foreach (var comp in ppSys)
                {
                    foreach (var fItem in comp.FaultItems.Where(fi => fi.IsHappening))
                    {
                        var passedCts = fItem.Criteria.Where(ct => ct.IsHappening && ct.AsReportResult).ToArray();
                        if (!passedCts.Any())
                        {
                            continue;
                        }

                        var newRpt = new FaultItemReport();

                        //设置报告时间
                        var happenTime = passedCts.Max(ct => ct.Time);
                        newRpt.FirstTime  = happenTime;
                        newRpt.LatestTime = happenTime;

                        //设置报告组件
                        newRpt.CompCode = comp.Code;

                        //设置报告故障内容和建议

                        newRpt.Advise = fItem.Advise;

                        //设置报告故障严重度
                        newRpt.Severity = GetSeverityFromCriteria(passedCts);

                        newRpt.CriterionBuiltIds = string.Join(Repo.Separator, passedCts.Select(ct => ct.LibId));
                        newRpt.RtDatas           = string.Join("|||", passedCts.Select(ct => ct.RtDataStr));

                        //设置显示文本
                        newRpt.DisplayText = fItem.Description;

                        #region 给显示文本添加驱动端/非驱动端位置信息

                        var dict             = ParseCriterionRtDataStr(newRpt.RtDatas);
                        var hasPos_drived    = dict.Find(d => d.Key.Contains("_In")) != null;
                        var hasPos_nonDrived = dict.Find(d => d.Key.Contains("_Out")) != null;
                        var drivePos         = string.Empty;
                        //有的fitem 驱动端/非驱动端/xyz都有, 加上这个位置信息反而看不懂
                        //所以只加 只有in的或只有out的
                        if (hasPos_drived && !hasPos_nonDrived)
                        {
                            drivePos = "(驱动端)";
                        }
                        else if (!hasPos_drived && hasPos_nonDrived)
                        {
                            drivePos = "(非驱动端)";
                        }
                        newRpt.DisplayText += drivePos;

                        #endregion

                        //设置默认发生次数
                        newRpt.HappenCount = 1;

                        var existRpts =
                            context.FaultItemReports.Where(rptRecord => rptRecord.CompCode == newRpt.CompCode &&
                                                           rptRecord.DisplayText == newRpt.DisplayText &&
                                                           rptRecord.CriterionBuiltIds == newRpt.CriterionBuiltIds &&
                                                           rptRecord.Severity >= newRpt.Severity).ToArray();



                        var newReportAction = new Action(() =>
                        {
                            //添加到图谱的映射
                            var gMap      = new Dictionary <int, int>();
                            var graphNums = passedCts.SelectMany(ct => ct.GraphNumbers).Distinct().ToArray();
                            foreach (var graphNum in graphNums)
                            {
                                var graph = RuntimeRepo.RtData.Graphs[graphNum - 1]; //graphNum从1开始, graphs数组索引从0开始
                                var ga    = GraphArchive.FromGraph(graph);
                                var gaId  = context.AddGraph(ga);
                                if (!gMap.ContainsKey(graphNum))
                                {
                                    gMap.Add(graphNum, gaId);
                                }
                            }
                            newRpt.GraphMap = string.Join(Repo.Separator, gMap.Select(m => $"{m.Key}:{m.Value}"));
                            _recordValidGrade(newRpt);

                            //保存
                            reportsToSave.Add(newRpt);
                        });

                        if (!existRpts.Any())
                        {
                            newReportAction();
                        }
                        else
                        {
                            //获取数据库中最新的记录
                            var latestTime = existRpts.Max(rr => rr.LatestTime);
                            var latestRpt  = existRpts.First(r => r.LatestTime == latestTime);

                            var newDict    = ParseCriterionRtDataStr(newRpt.RtDatas);
                            var latestDict = ParseCriterionRtDataStr(latestRpt.RtDatas);

                            //如果newRpt有阈值字段,那就找latestRpt里的阈值进行比较,如果newRpt没有阈值字段,那就不用比了,不存
                            double newMax;
                            if (newDict.TryGetMaxValue(fItem.ThresholdField, out newMax))
                            {
                                double latestMax;
                                if (latestDict.TryGetMaxValue(fItem.ThresholdField, out latestMax))
                                {
                                    //如果都有阈值,那就比较有没有超过10%,有:就作为新记录,没有:就更新记录时间
                                    if (newMax > (1 + _overLimitRatio) * latestMax)
                                    {
                                        newRpt.Remark1 = $"较此表Id为{latestRpt.Id}的故障超限至少{_overLimitRatio*100}%,原值:{latestMax} 新值:{newMax},因此记录为新故障。";
                                        newReportAction();
                                    }
                                    else
                                    {
                                        latestRpt.LatestTime = newRpt.FirstTime;
                                        latestRpt.HappenCount++;
                                        _recordValidGrade(latestRpt);
                                    }
                                }
                                //如果newRpt有阈值字段,但latestRpt没有,那就当做新的记录存下来
                                else
                                {
                                    newReportAction();
                                }
                            }
                        }
                    }
                }

                if (reportsToSave.Any())
                {
                    context.FaultItemReports.AddRange(reportsToSave);
                }

                context.SaveChanges();
            }
        }