public static bool BuildPropsPreview(PumpSystem ppSys)
        {
            IMongoCollection <BsonDocument> collection;

            if (!TryGetDb(DBCollections.ppSysReport + "_" + ppSys.Guid, out collection))
            {
                return(false);
            }
            collection.DeleteMany(Builders <BsonDocument> .Filter.Empty);
            var insertItems =
                (from reportItem in ppSys.GetReport()
                 select new BsonDocument {
                { nameof(reportItem.PumpCode), reportItem.PumpCode },
                { nameof(reportItem.CompCode), reportItem.CompCode },
                { nameof(reportItem.CompName), reportItem.CompName },
                { nameof(reportItem.CompType), reportItem.CompType.ToString() },
                { nameof(reportItem.TdPos), reportItem.TdPos?.ToString() ?? "" },
                { nameof(reportItem.PropName), reportItem.PropName },
                { nameof(reportItem.Variable), reportItem.Variable },
                { nameof(reportItem.Value), reportItem.Value ?? "" }
            }).ToList();

            collection.InsertMany(insertItems);
            return(true);
        }
        private static double?GetSpeed(PumpSystem ppSys)
        {
            //获取一下转速
            var speedSignal = ppSys.GetReport().First(rpt => rpt.CompType == CompType.Td_S && rpt.Variable == "@Speed").Value;
            var speed       = RuntimeRepo.RtData.FindSignalValue(speedSignal);

            return(speed);
        }
Example #3
0
 private static void BuildPumpSystems()
 {
     RuntimeRepo.PumpSysList.Clear();
     foreach (var ppGuid in Repo.PumpGuids)
     {
         var ppSys = new PumpSystem(ppGuid);
         RuntimeRepo.PumpSysList.Add(ppSys);
     }
 }
 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);
     }
 }
        public void FindMainVibraSpec(PumpSystem ppSys)
        {
            var specGraphs = RuntimeRepo.RtData.Graphs.Where(g => g.Signal.Contains("Spec")).ToArray();

            //所有频谱中的最大值
            var max = specGraphs.Max(g => g.Data.Max());

            //找到最大值对应的频率是那个频谱
            var graph = specGraphs.First(g => g.Data.Max() == max);

            //找到是频谱的第几线
            var line = (double)graph.Data.IndexOf(max);

            var specWidth = double.Parse(ConfigurationManager.AppSettings["SpecWidth"]);
            var lineCount = (double)graph.Data.Count - 1;//去掉第一个0
            var f         = line * (specWidth / lineCount);

            double?speed = GetSpeed(ppSys);

            var featureF = f / (speed / 60);

            var range = 0.12D;
            var featureFRangeStart = featureF * (1 - range);
            var featureFRangeEnd   = featureF * (1 + range);
            var feature            = string.Empty;
            var featureList        = new[] { 0.5, 1, 2, 3, 4 };

            foreach (var fvalue in featureList)
            {
                if (fvalue > featureFRangeStart && fvalue < featureFRangeEnd)
                {
                    feature = fvalue + "X";
                }
            }

            var mainSpec = new MainSpec {
                PPGuid      = ppSys.Guid.ToFormatedString(),
                FirstTime   = graph.Time,
                LatestTime  = graph.Time,
                Feature     = feature,
                Position    = graph.Pos.ToString(),
                LatestValue = max,
                MaxValue    = max
            };

            MainSpecUpdated?.Invoke(mainSpec);
        }
        public void DiagnoseRunningPump_Round2(PumpSystem ppSys)
        {
            var ppSysAllCts = new List <Criterion>();

            foreach (var comp in ppSys)
            {
                ppSysAllCts.AddRange(comp.GetAllCriteria());
            }

            var dict = new Dictionary <int, int>();

            foreach (var ct in ppSysAllCts)
            {
                var key   = ct.LibId;
                var value = ct.IsHappening ? 1 : 0;
                if (!dict.ContainsKey(key))
                {
                    dict.Add(key, value);
                }
            }

            foreach (var comp in ppSys)
            {
                foreach (var icItem in comp.InferComboItems)
                {
                    _icParser.ParseInferComboItem(icItem, dict);
                    var ctLibIds = _icParser.GetCurrentItemCtLibIds();

                    icItem.ExpressionCts.Clear();
                    var ctCopies = ppSysAllCts.Where(ct => ctLibIds.Contains(ct.LibId)).ToList().DeepClone();
                    icItem.ExpressionCts.AddRange(ctCopies);
                }
            }

//            if (RuntimeRepo.InferResultItems.Any()) {
            InferComboHappened?.Invoke(ppSys);
//            }
        }
Example #7
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();
            }
        }
Example #8
0
        public void BuildInferComboReports(PumpSystem ppSys)
        {
            using (var context = new PumpSystemContext()) {
                var reportsToSave = new List <InferComboReport>();
                foreach (var comp in ppSys)
                {
                    foreach (var icItem in comp.InferComboItems.Where(ic => ic.IsHappening))
                    {
                        GradedCriterion.ForEachValidGradeRange(gradeRange =>
                        {
                            var passedGcts =
                                icItem.ExpressionCts.Where(ct => ct is GradedCriterion)
                                .Where(gct => gct.IsHappening)
                                .Cast <GradedCriterion>().ToList();

                            //假如range是3的话,那么通过的里面至少有个3,且只能有3,不能有1,2
                            //假如range是2,3的话, 那么通过的里面至少有个2,且只能有2,3
                            //假如range是1,2,3的话, 那么通过的里面至少有个1,且只能有1,2,3

                            var checkPass = passedGcts.Any();
                            checkPass    &= passedGcts.Exists(g => (int)g.HappeningGrade == gradeRange[0]);
                            checkPass    &= passedGcts.All(g => gradeRange.Contains((int)g.HappeningGrade));

                            if (!checkPass)
                            {
                                return;
                            }

                            var newRpt = new InferComboReport();

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

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

                            //设置报告故障内容和建议
                            //                        var faultItem = Repo.FaultItems.First(fi => fi.IsSameFaultItem(icItem));
                            newRpt.DisplayText = icItem.FaultResult;
                            newRpt.EventMode   = icItem.EventMode;
                            newRpt.Expression  = icItem.Expression;
                            newRpt.RtDatas     = string.Join(Repo.Separator,
                                                             icItem.ExpressionCts.Select(ct => $"{ct.LibId}:{(ct.IsHappening ? 1 : 0)}"));
                            //                        newRpt.Advise = faultItem.Advise;

                            newRpt.HappenCount = 1;

                            //remark2 作为所用分档
                            newRpt.Remark2 = "Grade:" + string.Join(",", gradeRange);

                            //把通过的(为1的)分别是几档写到remark3里
                            newRpt.Remark3 = "GradeRefer: " + string.Join(Repo.Separator,
                                                                          passedGcts.Select(g => $"{g.LibId}:{(int) g.HappeningGrade}"));


                            var icReports = context.InferComboReports.ToList();

                            //判断是否存在报告,不存在则添加,存在则更新
                            var existRpts = icReports.Where(rptRecord => rptRecord.LibId == newRpt.LibId &&
                                                            rptRecord.EventMode == newRpt.EventMode &&
                                                            rptRecord.Expression == newRpt.Expression &&
                                                            rptRecord.Remark2 == newRpt.Remark2).ToArray();
                            if (!existRpts.Any())
                            {
                                var intersects = icReports.Select(r => r.LibId).Intersect(icItem.PrevIds).ToList();
                                if (intersects.Any())
                                {
                                    newRpt.DisplayText += "(小概率)";
                                }
                                reportsToSave.Add(newRpt);
                            }
                            else
                            {
                                var latestTime       = existRpts.Max(rr => rr.LatestTime);
                                var latestRpt        = existRpts.First(r => r.LatestTime == latestTime);
                                latestRpt.LatestTime = newRpt.FirstTime;
                                latestRpt.HappenCount++;
                            }
                        });
                    }
                }

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

                context.SaveChanges();
            }
        }
        public void DiagnoseRunningPump_Round1(PumpSystem ppSys)
        {
#if DEBUG
            //显示信号量与属性关联的结果, 结果通过robo3T可视化工具查看表格
            MyMongo.BuildPropsPreview(ppSys);
#endif

            var ppSysReport = ppSys.GetReport();

            //对系统中的每一个部件
            foreach (var comp in ppSys)
            {
                //寻找其对应的判据
                foreach (var ct in comp.GetAllCriteria())
                {
                    #region 获取实时数据的字典

                    var filtedRpt = ppSysReport.Where(rptItem =>
                    {
                        var td = comp as BaseTransducer;
                        if (td == null)   //如果不是传感器,直接过滤组件类型
                        {
                            return(rptItem.CompType == ct.CompType);
                        }
                        //如果是传感器,还要过滤传感器的位置
                        return(rptItem.CompType == ct.CompType && rptItem.TdPos == td.Position);
                    }).ToList();


                    //检查不重复后添加到字典
                    var dict      = new Dictionary <string, string>();
                    var conflicts = new List <string>();
                    foreach (var reportItem in filtedRpt)
                    {
                        var key   = reportItem.Variable;
                        var value = reportItem.Value;
                        if (dict.ContainsKey(key))
                        {
                            conflicts.Add(key);
                        }
                        else
                        {
                            dict.Add(key, value);
                        }
                    }

                    //提示修改重复项
                    if (conflicts.Any())
                    {
                        Log.Warn(
                            $"!!!组件{Repo.Map.TypeToEnum.First(t => t.Value == comp.Type).Key} 属性重复(请从access中手动去除):!!!");
                        foreach (var cfl in conflicts)
                        {
                            Console.Write($"{cfl}, ");
                        }
                        Log.Inform();
                    }

                    #endregion

                    _ctParser.ParseCriterion(ct, comp.Code, dict);
                }
            }

            FaultItemHappened?.Invoke(ppSys);
        }