ningshuxia
2024-06-24 5d674c809bf51b77ddd31a365bcfcfe12b35c556
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
namespace IStation.Application
{
    /// <summary>
    /// 陈行输水调度
    /// </summary>
    [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();
 
        /// <summary>
        /// 计算
        /// </summary>
        [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<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);
                        }
 
                    }
                    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<double>();
                    var water_level_valid_value_list2 = new List<double>();
 
                    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<Model.SchedulePump> schedule_pump_list1 = null;
            List<Model.SchedulePump> 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<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.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<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.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;
        } 
 
    }
}