namespace IStation.Application
{
///
/// 陈行输水调度
///
[AllowAnonymous]
[Route("OpenApi/Dispatch/Solution")]
[ApiDescriptionSettings("Schedule", Name = "输水调度", Order = 1)]
public class Schedule_Controller : IDynamicApiController, ITransient
{
private readonly Service.ScheduleRequest _service_schedule_request = new();
private readonly Service.ScheduleScada _service_schedule_scada = new();
private readonly Service.ScheduleRule _service_schedule_rule = new();
private readonly Service.ScheduleConclusion _service_schedule_conclusion = new();
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();
///
/// 计算
///
[AllowAnonymous]
[Route("Calculate@V1.0")]
[HttpPost]
[NonUnify]
public StationScheduleOutput Calculate([Required] StationScheduleInput input)
{
var receipt_time = DateTime.Now;
var request_id = Yw.YitIdHelper.NextId();
var log_title = string.Empty;
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)
{
Log.Info(request_id, log_title, "文件缺失!");
return new StationScheduleOutput() { flag = 0, message = "文件缺失" };
}
var output = new StationScheduleOutput();
output.InitObjects(station_info.S1Flags, station_info.S2Flags);
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();
var current_open_pump_flags2 = new List();
var scada_list = new List();
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(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);
}
}
else
{
Log.Info(request_id, log_title, "scada数据为空");
}
}
catch (System.Exception ex)
{
Log.Error(request_id, log_title, "http请求异常", ex);
}
if (scada_list.Any())
{
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();
var water_level_valid_value_list2 = new List();
var run_status_tag_dict_1 = scada_info.GetS1RunStatusTagDict();
var run_status_tag_dict_2 = scada_info.GetS2RunStatusTagDict();
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}");
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(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;
Model.ScheduleConclusion schedule_conclusion1 = null;
Model.ScheduleConclusion schedule_conclusion2 = null;
List schedule_pump_list1 = null;
List schedule_pump_list2 = null;
var msg = "计算成功";
log_title = "调度返回";
if (optimal_combine1 == null && optimal_combine2 == null)
{
output.flag = 0;
output.message = "厂内调度方案无法计算!";
Log.Info(request_id, log_title, "厂内调度方案无法计算!");
}
else
{
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;
schedule_pump_list1 = new List();
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.WP;
output.objects["1输水总单位能耗"] = optimal_combine1.UWP;
foreach (var combine in optimal_combine1.Combines)
{
foreach (var fre_pump in combine.FrePumps)
{
var flag = fre_pump.Flag;
output.objects[$"1输水{flag}#流量"] = fre_pump.Flow;
output.objects[$"1输水{flag}#扬程"] = fre_pump.Head;
output.objects[$"1输水{flag}#功率"] = fre_pump.Power;
output.objects[$"1输水{flag}#效率"] = fre_pump.Efficiency;
output.objects[$"1输水{flag}#频率"] = fre_pump.Frequency;
output.objects[$"1输水{flag}#转速"] = fre_pump.Speed;
switch (flag)
{
case 11:
{
output.objects["一输水泵11号变频单泵.变频器运行"] = 1;
}
break;
case 12:
{
output.objects["一输水泵12号变频单泵.变频器运行"] = 1;
}
break;
case 13:
{
output.objects["一输水泵13号变频单泵.变频器运行"] = 1;
}
break;
case 14:
{
output.objects["一输水泵14号变频单泵.变频器运行"] = 1;
}
break;
case 15:
{
output.objects["HF长江原水厂ABPLC.6KV配电一输水.0402010201011501003一输水15号泵运行"] = 1;
}
break;
case 16:
{
output.objects["HF一输水16号工频控制图.16#水泵运行"] = 1;
}
break;
case 17:
{
output.objects["一输水泵17号变频单泵.变频器运行"] = 1;
}
break;
case 18:
{
output.objects["一输水泵18号变频单泵.变频器运行"] = 1;
}
break;
default:
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;
schedule_pump_list1.Add(schedule_pump);
}
}
}
else
{
msg += $"\r\n1输水调度计算失败,无法满足目标流量:{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;
schedule_pump_list2 = new List();
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.WP;
output.objects["2输水总单位能耗"] = optimal_combine2.UWP;
foreach (var combine in optimal_combine2.Combines)
{
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;
output.objects[$"2输水{flag}#功率"] = fre_pump.Power;
output.objects[$"2输水{flag}#效率"] = fre_pump.Efficiency;
output.objects[$"2输水{flag}#频率"] = fre_pump.Frequency;
output.objects[$"2输水{flag}#转速"] = fre_pump.Speed;
switch (flag)
{
case 21:
{
output.objects["二输水21号水泵视图.泵运行"] = 1;
}
break;
case 22:
{
output.objects["二输水22号水泵视图.泵运行"] = 1;
}
break;
case 23:
{
output.objects["二输水23号水泵软启动.运行"] = 1;
}
break;
case 24:
{
output.objects["二输水24号水泵软启动.运行"] = 1;
}
break;
case 25:
{
output.objects["二输水25号水泵软启动.运行"] = 1;
}
break;
case 26:
{
output.objects["二输水26号水泵软启动.运行"] = 1;
}
break;
case 27:
{
output.objects["二输水27号水泵变频器.运行状态"] = 1;
}
break;
default:
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;
schedule_pump_list2.Add(schedule_pump);
}
}
}
else
{
msg += $"\r\n2输水调度计算失败,无法满足目标流量:{target_flow2},目标压力:{target_pressure2}!";
}
output.flag = 1;
}
output.ReceiptTime = receipt_time;
output.ReturnTime = DateTime.Now;
output.message = msg;
Log.Info(request_id, log_title, msg);
try
{
log_title = "存储方案";
var bol = false;
bol = _service_schedule_request.Insert(schedule_request) > 0;
if (!bol)
{
Log.Info(request_id, log_title, "schedule_request 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_request));
}
if (schedule_config1 != null)
{
var schedule_rule1 = new Model.ScheduleRule();
schedule_rule1.RequestID = request_id;
schedule_rule1.Station = eDockingStation.Ch1s;
schedule_rule1.Config = JsonHelper.Object2Json(schedule_config1);
bol = _service_schedule_rule.Insert(schedule_rule1) > 0;
if (!bol)
{
Log.Info(request_id, log_title, "schedule_config1 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_rule1));
}
}
if (schedule_config2 != null)
{
var schedule_rule2 = new Model.ScheduleRule();
schedule_rule2.RequestID = request_id;
schedule_rule2.Station = eDockingStation.Ch2s;
schedule_rule2.Config = JsonHelper.Object2Json(schedule_config2);
bol = _service_schedule_rule.Insert(schedule_rule2) > 0;
if (!bol)
{
Log.Info(request_id, log_title, "schedule_config2 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_rule2));
}
}
if (scada_list.Any())
{
bol = _service_schedule_scada.Inserts(scada_list);
if (!bol)
{
Log.Info(request_id, log_title, "scada_list 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(scada_list));
}
}
if (schedule_conclusion1 != null)
{
bol = _service_schedule_conclusion.Insert(schedule_conclusion1) > 0;
if (!bol)
{
Log.Info(request_id, log_title, "schedule_conclusion1 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_conclusion1));
}
}
if (schedule_conclusion2 != null)
{
bol = _service_schedule_conclusion.Insert(schedule_conclusion2) > 0;
if (!bol)
{
Log.Info(request_id, log_title, "schedule_conclusion2 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_conclusion2));
}
}
if (schedule_pump_list1 != null)
{
bol = _service_schedule_pump.Inserts(schedule_pump_list1);
if (!bol)
{
Log.Info(request_id, log_title, "schedule_pump_list1 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_pump_list1));
}
}
if (schedule_pump_list2 != null)
{
bol = _service_schedule_pump.Inserts(schedule_pump_list2);
if (!bol)
{
Log.Info(request_id, log_title, "schedule_pump_list2 插入异常");
Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_pump_list2));
}
}
}
catch (Exception ex)
{
Log.Error(request_id, log_title, "数据库异常!", ex);
}
return output;
}
}
}