Shuxia Ning
2024-07-17 fd681339c81201ed6fb3303647ecab89e3e6c0c1
IStation.Application/01-ch/02-schedule/Schedule_Controller.cs
@@ -15,8 +15,8 @@
        private readonly Service.SchedulePump _service_schedule_pump = new();
        private readonly Service.Station _service_station = new();
        private readonly Service.Scada _service_scada = new();
        private readonly Service.ScheduleConfig _service_schedule_config = new();
        /// <summary>
        /// 计算
@@ -33,154 +33,81 @@
            log_title = "初始化基础信息文件";
            var station_info = _service_station.Get();
            var scada_info = _service_scada.Get();
            var schedule_config1 = _service_schedule_config.GetStation1();
            var schedule_config2 = _service_schedule_config.GetStation2();
            if (station_info == null || scada_info == null)
            if (station_info == null)
            {
                Log.Info(request_id, log_title, "文件缺失!");
                return new StationScheduleOutput() { flag = 0, message = "文件缺失" };
            }
            var output = new StationScheduleOutput();
            output.InitObjects(station_info.S1Flags, station_info.S2Flags);
            log_title = "需水请求";
            Log.Debug(request_id, log_title, JsonHelper.Object2Json(input));
            var target_flow1 = input.objects["TotalFlow1"];
            var target_pressure1 = input.objects["TotalPressure1"];
            var target_flow2 = input.objects["TotalFlow2"];
            var target_pressure2 = input.objects["TotalPressure2"];
            var water_level1 = 0.0;
            var water_level2 = 0.0;
            var current_open_pump_flags1 = new List<int>();
            var current_open_pump_flags2 = new List<int>();
            var scada_list = new List<Model.ScheduleScada>();
            log_title = "需水请求";
            Log.Info(request_id, log_title, $"target_flow1:{target_flow1},target_pressure1:{target_pressure1},target_flow2:{target_flow2},target_pressure2:{target_pressure2}");
            Log.Debug(request_id, log_title, JsonHelper.Object2Json(input));
            if (Settings.ParasHelper.ZyDocking.Enable)
            {
                var url = Settings.ParasHelper.ZyDocking.ScadaHttpUrl;
                try
                {
                    log_title = "Scada请求";
                    Log.Info(request_id, log_title, $"{url}");
                    var response_text = Yw.Untity.HttpRequestHelper.Get(url);
                    Log.Debug(request_id, log_title, $"url:{url},responseText:{response_text}");
                    var scada_output = JsonHelper.Json2Object<ScadaScheduleInput>(response_text);
                    if (scada_output.data != null && scada_output.data.Any())
                    {
                        foreach (var scada_dict in scada_output.data)
                        {
                            var vals = scada_dict.Value.ElementAt(0).Value;
                            var time = scada_dict.Value.ElementAt(1).Value;
                            var key = scada_dict.Value.ElementAt(2).Value;
                            var scada = new Model.ScheduleScada();
                            scada.RequestID = request_id;
                            scada.Tag = key;
                            if (DateTime.TryParse(time, out DateTime t))
                                scada.Time = t;
                            if (double.TryParse(vals, out double v))
                                scada.Value = v;
                            scada_list.Add(scada);
                        }
            var target_head1 = Curve.PumpCalculateHelper.Mpa2M(target_pressure1);
            var target_head2 = Curve.PumpCalculateHelper.Mpa2M(target_pressure2);
                    }
                    else
                    {
                        Log.Info(request_id, log_title, "scada数据为空");
                    }
                }
                catch (System.Exception ex)
                {
                    Log.Error(request_id, log_title, "http请求异常", ex);
                }
            var is_zy_scada_debug = false;
#if DEBUG
            is_zy_scada_debug = true;
#endif
                if (scada_list.Any())
            log_title = "实时ZyScada请求";
            var get_msg = GlobalHelper.GetRealTimeZyScadaList(out List<Model.ZyScada> zy_scada_list, is_zy_scada_debug);
            if (!string.IsNullOrEmpty(get_msg))
                {
                    var water_level_tag_list_1 = scada_info.GetS1WaterLevelTagList();
                    var water_level_tag_list_2 = scada_info.GetS2WaterLevelTagList();
                    var water_level_valid_value_list1 = new List<double>();
                    var water_level_valid_value_list2 = new List<double>();
                Log.Info(request_id, log_title, get_msg);
            }
            Log.Debug(request_id, log_title, JsonHelper.Object2Json(zy_scada_list));
                    var run_status_tag_dict_1 = scada_info.GetS1RunStatusTagDict();
                    var run_status_tag_dict_2 = scada_info.GetS2RunStatusTagDict();
            var station1 = station_info.Station1;
            var station2 = station_info.Station2;
            var station1_same_type_flag_group_first = GlobalHelper.Station1SameTypeFlagGroupFirst;
            var station2_same_type_flag_group_first = GlobalHelper.Station2SameTypeFlagGroupFirst;
            var station1_schedule_config = _service_schedule_config.GetStation1();
            var station2_schedule_config = _service_schedule_config.GetStation2();
            var station1_flag_list = GlobalHelper.Station1FlagList;
            var station2_flag_list = GlobalHelper.Station2FlagList;
                    foreach (var scada in scada_list)
                    {
                        if (!scada.Value.HasValue)
                            continue;
                        var tag = scada.Tag;
                        var value = scada.Value.Value;
                        if (water_level_tag_list_1.Contains(tag))
                        {
                            water_level_valid_value_list1.Add(value);
                            continue;
                        }
                        else if (water_level_tag_list_2.Contains(tag))
                        {
                            water_level_valid_value_list2.Add(value);
                            continue;
                        }
                        if (value != 1)
                            continue;
                        if (run_status_tag_dict_1.ContainsKey(tag))
                        {
                            current_open_pump_flags1.Add(run_status_tag_dict_1[tag]);
                            continue;
                        }
                        else if (run_status_tag_dict_2.ContainsKey(tag))
                        {
                            current_open_pump_flags2.Add(run_status_tag_dict_2[tag]);
                            continue;
                        }
                    }
                    if (water_level_valid_value_list1.Any())
                        water_level1 = water_level_valid_value_list1.Average(x => x);
                    if (water_level_valid_value_list2.Any())
                        water_level2 = water_level_valid_value_list2.Average(x => x);
                }
            }
            log_title = "当前水位";
            var target_head1 = Curve.PumpCalculateHelper.Mpa2M(target_pressure1) - water_level1;
            var target_head2 = Curve.PumpCalculateHelper.Mpa2M(target_pressure2) - water_level2;
            Log.Info(request_id, log_title, $"water_level1:{water_level1},target_head1:{target_head1},water_level2:{water_level2},target_head2:{target_head2}");
            GlobalHelper.GetStationOpenFlagList(zy_scada_list, out List<int> station1_open_flag_list, out List<int> station2_open_flag_list);
            GlobalHelper.GetFlagInletWaterLevelDict(zy_scada_list, out Dictionary<int, double> station1_flag_inlet_water_level_dict, out Dictionary<int, double> station2_flag_inlet_water_level_dict);
            var helper = new Algorithm.ScheduleHelper();
            helper.Initial(current_open_pump_flags1, schedule_config1);
            var optimal_combine1 = helper.Calc(station_info.S1, station_info.S1FlagsPart1, station_info.S1FlagsPart2, target_flow1, target_head1);
            helper.Initial(station1_open_flag_list, station1_schedule_config);
            var optimal_combine1 = helper.GetOptAnaCombine(station1, station1_same_type_flag_group_first, station1_flag_inlet_water_level_dict, target_flow1, target_head1);
            helper.Initial(current_open_pump_flags2, schedule_config2);
            var optimal_combine2 = helper.Calc(station_info.S2, station_info.S2FlagsPart1, station_info.S2FlagsPart2, target_flow2, target_head2);
            Model.ScheduleRequest schedule_request = new Model.ScheduleRequest();
            schedule_request = new Model.ScheduleRequest();
            schedule_request.ID = request_id;
            schedule_request.TargetFlow1 = target_flow1;
            schedule_request.TargetPressure1 = target_pressure1;
            schedule_request.TargetFlow2 = target_flow2;
            schedule_request.TargetPressure2 = target_pressure2;
            schedule_request.WaterLevel1 = water_level1;
            schedule_request.WaterLevel2 = water_level2;
            schedule_request.ScheduleStatus1 = optimal_combine1 != null;
            schedule_request.ScheduleStatus2 = optimal_combine2 != null;
            schedule_request.TotalTimeSpent = (DateTime.Now - receipt_time).TotalSeconds;
            schedule_request.ReceptionTime = receipt_time;
            helper.Initial(station2_open_flag_list, station2_schedule_config);
            var optimal_combine2 = helper.GetOptAnaCombine(station2, station2_same_type_flag_group_first, station2_flag_inlet_water_level_dict, target_flow2, target_head2);
            var schedule_request = new Model.ScheduleRequest
            {
                ID = request_id,
                TargetFlow1 = target_flow1,
                TargetPressure1 = target_pressure1,
                TargetFlow2 = target_flow2,
                TargetPressure2 = target_pressure2,
                ScheduleStatus1 = optimal_combine1 != null,
                ScheduleStatus2 = optimal_combine2 != null,
                TotalTimeSpent = (DateTime.Now - receipt_time).TotalSeconds,
                ReceptionTime = receipt_time
            };
            Model.ScheduleConclusion schedule_conclusion1 = null;
            Model.ScheduleConclusion schedule_conclusion2 = null;
            List<Model.SchedulePump> schedule_pump_list1 = null;
            List<Model.SchedulePump> schedule_pump_list2 = null;
            var msg = "计算成功";
            log_title = "调度返回";
            var msg = "计算结束!";
            var output = new StationScheduleOutput();
            output.InitObjects(station1_flag_list, station2_flag_list);
            if (optimal_combine1 == null && optimal_combine2 == null)
            {
                output.flag = 0;
@@ -191,29 +118,30 @@
            {
                if (optimal_combine1 != null)
                {
                    schedule_conclusion1 = new Model.ScheduleConclusion();
                    schedule_conclusion1.ID = Yw.YitIdHelper.NextId();
                    schedule_conclusion1.RequestID = request_id;
                    schedule_conclusion1.Station = IStation.eDockingStation.Ch1s;
                    schedule_conclusion1.TotalFlow = optimal_combine1.Flow;
                    schedule_conclusion1.TotalHead = optimal_combine1.Head;
                    schedule_conclusion1.TotalPower = optimal_combine1.Power;
                    schedule_conclusion1.TotalEfficiency = optimal_combine1.Efficiency;
                    schedule_conclusion1.WP = optimal_combine1.WP;
                    schedule_conclusion1.UWP = optimal_combine1.UWP;
                    schedule_conclusion1.Flags = IntListHelper.ToString(optimal_combine1.Flags);
                    schedule_conclusion1.MeritRatio = optimal_combine1.MeritRatio;
                    optimal_combine1.Round();
                    schedule_conclusion1 = new Model.ScheduleConclusion
                    {
                        ID = Yw.YitIdHelper.NextId(),
                        RequestID = request_id,
                        Station = IStation.eDockingStation.Ch1s,
                        TotalFlow = optimal_combine1.TotalFlow,
                        TotalHead = optimal_combine1.TotalHead,
                        TotalPower = optimal_combine1.TotalPower,
                        TotalEfficiency = optimal_combine1.TotalEfficiency,
                        WP = optimal_combine1.WP,
                        UWP = optimal_combine1.UWP,
                        Flags = IntListHelper.ToString(optimal_combine1.Flags),
                        MeritRatio = optimal_combine1.MeritRatio
                    };
                    schedule_pump_list1 = new List<Model.SchedulePump>();
                    output.objects["1输水总流量"] = optimal_combine1.Flow;
                    output.objects["1输水总压力"] = Math.Round(Curve.PumpCalculateHelper.M2Mpa(optimal_combine1.Head), 4);
                    output.objects["1输水总功率"] = optimal_combine1.Power;
                    output.objects["1输水总效率"] = optimal_combine1.Efficiency;
                    output.objects["1输水总流量"] = optimal_combine1.TotalFlow;
                    output.objects["1输水总压力"] = Math.Round(Curve.PumpCalculateHelper.M2Mpa(optimal_combine1.TotalHead), 4);
                    output.objects["1输水总功率"] = optimal_combine1.TotalPower;
                    output.objects["1输水总效率"] = optimal_combine1.TotalEfficiency;
                    output.objects["1输水总千吨能耗"] = optimal_combine1.WP;
                    output.objects["1输水总单位能耗"] = optimal_combine1.UWP;
                    foreach (var combine in optimal_combine1.Combines)
                    {
                        foreach (var fre_pump in combine.FrePumps)
                    foreach (var fre_pump in optimal_combine1.AnaFrePumps)
                        {
                            var flag = fre_pump.Flag;
                            output.objects[$"1输水{flag}#流量"] = fre_pump.Flow;
@@ -269,52 +197,56 @@
                                    break;
                            }
                            var schedule_pump = new Model.SchedulePump();
                            schedule_pump.RequestID = request_id;
                            schedule_pump.Station = IStation.eDockingStation.Ch1s;
                            schedule_pump.Flag = flag;
                            schedule_pump.Flow = fre_pump.Flow;
                            schedule_pump.Head = fre_pump.Head;
                            schedule_pump.Power = fre_pump.Power;
                            schedule_pump.Efficiency = fre_pump.Efficiency;
                            schedule_pump.Frequency = fre_pump.Frequency;
                            schedule_pump.Speed = fre_pump.Speed;
                        var schedule_pump = new Model.SchedulePump
                        {
                            RequestID = request_id,
                            Station = IStation.eDockingStation.Ch1s,
                            Flag = flag,
                            Flow = fre_pump.Flow,
                            Head = fre_pump.Head,
                            Power = fre_pump.Power,
                            Efficiency = fre_pump.Efficiency,
                            Frequency = fre_pump.Frequency,
                            Speed = fre_pump.Speed
                        };
                            schedule_pump_list1.Add(schedule_pump);
                        }
                    }
                }
                else
                {
                    msg += $"\r\n1输水调度计算失败,无法满足目标流量:{target_flow1},目标压力:{target_pressure1}!";
                    msg += $"  1输水调度计算失败,无法满足目标流量:{target_flow1},目标压力:{target_pressure1}!";
                }
                if (optimal_combine2 != null)
                {
                    schedule_conclusion2 = new Model.ScheduleConclusion();
                    schedule_conclusion2.ID = Yw.YitIdHelper.NextId();
                    schedule_conclusion2.RequestID = request_id;
                    schedule_conclusion2.Station = IStation.eDockingStation.Ch2s;
                    schedule_conclusion2.TotalFlow = optimal_combine2.Flow;
                    schedule_conclusion2.TotalHead = optimal_combine2.Head;
                    schedule_conclusion2.TotalPower = optimal_combine2.Power;
                    schedule_conclusion2.TotalEfficiency = optimal_combine2.Efficiency;
                    schedule_conclusion2.WP = optimal_combine2.WP;
                    schedule_conclusion2.UWP = optimal_combine2.UWP;
                    schedule_conclusion2.Flags = IntListHelper.ToString(optimal_combine2.Flags);
                    schedule_conclusion2.MeritRatio = optimal_combine2.MeritRatio;
                    optimal_combine2.Round();
                    schedule_conclusion2 = new Model.ScheduleConclusion
                    {
                        ID = Yw.YitIdHelper.NextId(),
                        RequestID = request_id,
                        Station = IStation.eDockingStation.Ch2s,
                        TotalFlow = optimal_combine2.TotalFlow,
                        TotalHead = optimal_combine2.TotalHead,
                        TotalPower = optimal_combine2.TotalPower,
                        TotalEfficiency = optimal_combine2.TotalEfficiency,
                        WP = optimal_combine2.WP,
                        UWP = optimal_combine2.UWP,
                        Flags = IntListHelper.ToString(optimal_combine2.Flags),
                        MeritRatio = optimal_combine2.MeritRatio
                    };
                    schedule_pump_list2 = new List<Model.SchedulePump>();
                    output.objects["2输水总流量"] = optimal_combine2.Flow;
                    output.objects["2输水总压力"] = Math.Round(Curve.PumpCalculateHelper.M2Mpa(optimal_combine2.Head), 4);
                    output.objects["2输水总功率"] = optimal_combine2.Power;
                    output.objects["2输水总效率"] = optimal_combine2.Efficiency;
                    output.objects["2输水总流量"] = optimal_combine2.TotalFlow;
                    output.objects["2输水总压力"] = Math.Round(Curve.PumpCalculateHelper.M2Mpa(optimal_combine2.TotalHead), 4);
                    output.objects["2输水总功率"] = optimal_combine2.TotalPower;
                    output.objects["2输水总效率"] = optimal_combine2.TotalEfficiency;
                    output.objects["2输水总千吨能耗"] = optimal_combine2.WP;
                    output.objects["2输水总单位能耗"] = optimal_combine2.UWP;
                    foreach (var combine in optimal_combine2.Combines)
                    foreach (var fre_pump in optimal_combine2.AnaFrePumps)
                    {
                        foreach (var fre_pump in combine.FrePumps)
                        {
                            var flag = fre_pump.Flag;
                            output.objects[$"2输水{flag}#流量"] = fre_pump.Flow;
                            output.objects[$"2输水{flag}#扬程"] = fre_pump.Head;
@@ -364,27 +296,29 @@
                                    break;
                            }
                            var schedule_pump = new Model.SchedulePump();
                            schedule_pump.RequestID = request_id;
                            schedule_pump.Station = IStation.eDockingStation.Ch2s;
                            schedule_pump.Flag = flag;
                            schedule_pump.Flow = fre_pump.Flow;
                            schedule_pump.Head = fre_pump.Head;
                            schedule_pump.Power = fre_pump.Power;
                            schedule_pump.Efficiency = fre_pump.Efficiency;
                            schedule_pump.Frequency = fre_pump.Frequency;
                            schedule_pump.Speed = fre_pump.Speed;
                        var schedule_pump = new Model.SchedulePump
                        {
                            RequestID = request_id,
                            Station = IStation.eDockingStation.Ch2s,
                            Flag = flag,
                            Flow = fre_pump.Flow,
                            Head = fre_pump.Head,
                            Power = fre_pump.Power,
                            Efficiency = fre_pump.Efficiency,
                            Frequency = fre_pump.Frequency,
                            Speed = fre_pump.Speed
                        };
                            schedule_pump_list2.Add(schedule_pump);
                        }
                    }
                }
                else
                {
                    msg += $"\r\n2输水调度计算失败,无法满足目标流量:{target_flow2},目标压力:{target_pressure2}!";
                    msg += $"  2输水调度计算失败,无法满足目标流量:{target_flow2},目标压力:{target_pressure2}!";
                }
                output.flag = 1;
            }
            output.flag = optimal_combine1 != null && optimal_combine2 != null ? 1 : 0;
            output.ReceiptTime = receipt_time;
            output.ReturnTime = DateTime.Now;
            output.message = msg;
@@ -401,12 +335,14 @@
                    Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_request));
                }
                if (schedule_config1 != null)
                if (station1_schedule_config != null)
                {
                    var schedule_rule1 = new Model.ScheduleRule();
                    schedule_rule1.RequestID = request_id;
                    schedule_rule1.Station = eDockingStation.Ch1s;
                    schedule_rule1.Config = JsonHelper.Object2Json(schedule_config1);
                    var schedule_rule1 = new Model.ScheduleRule
                    {
                        RequestID = request_id,
                        Station = eDockingStation.Ch1s,
                        Config = JsonHelper.Object2Json(station1_schedule_config)
                    };
                    bol = _service_schedule_rule.Insert(schedule_rule1) > 0;
                    if (!bol)
                    {
@@ -415,12 +351,14 @@
                    }
                }
                if (schedule_config2 != null)
                if (station2_schedule_config != null)
                {
                    var schedule_rule2 = new Model.ScheduleRule();
                    schedule_rule2.RequestID = request_id;
                    schedule_rule2.Station = eDockingStation.Ch2s;
                    schedule_rule2.Config = JsonHelper.Object2Json(schedule_config2);
                    var schedule_rule2 = new Model.ScheduleRule
                    {
                        RequestID = request_id,
                        Station = eDockingStation.Ch2s,
                        Config = JsonHelper.Object2Json(station2_schedule_config)
                    };
                    bol = _service_schedule_rule.Insert(schedule_rule2) > 0;
                    if (!bol)
                    {
@@ -429,8 +367,19 @@
                    }
                }
                if (scada_list.Any())
                if (zy_scada_list != null && zy_scada_list.Any())
                {
                    var scada_list = zy_scada_list
                        .OrderBy(x => x.Code)
                        .Select(x => new Model.ScheduleScada()
                        {
                            RequestID = request_id,
                            Time = x.Time,
                            Code = x.Code,
                            Tag = x.Tag,
                            Value = x.Value,
                        }).ToList();
                    bol = _service_schedule_scada.Inserts(scada_list);
                    if (!bol)
                    {
@@ -481,8 +430,11 @@
            {
                Log.Error(request_id, log_title, "数据库异常!", ex);
            }
            return output;
        }
    }
}