tangxu
2023-04-21 0ff2225384c72ab981b116ce15685e07e4615603
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Http.Extensions;
using IStation.Untity;
using Furion.DynamicApiController;
using System.ComponentModel.DataAnnotations;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using SqlSugar;
 
namespace IStation.Application
{
    /// <summary>
    /// ShysDispatchAna
    /// </summary>
    [Route("OpenApi/DispatchAna/SHYS")]
    [ApiDescriptionSettings("OpenApi", Name = "上海原水调度分析", Order = 1000)]
    public class DispatchAna_ShysController : IDynamicApiController
    {
        /// <summary>
        /// 计算保持的方案
        /// </summary> 
        [AllowAnonymous]
        [NonUnify]
        [Route("CalcuKeepDispatch")]
        [HttpPost]
        public IStation.Application.DispatchAnaSchemeOutput CalcuKeepDispatch([Required] StationDispatchExInput input)
        {
            if (input == null)
                return default;
            //众毅连续两次调用解决方案
            if (!string.IsNullOrEmpty(_lastFlowId))
            {
                if (_lastFlowId == input.flowId)
                {
                    LogHelper.Debug($"众毅重复入参:{JsonHelper.Object2Json(input)}");
                    return default;
                }
            }
            _lastFlowId = input.flowId;
            LogHelper.Debug($"众毅入参:{JsonHelper.Object2Json(input)}");
            var url_sg = Settings.WebApi.OpenApi.SanGaoDispatchUrl;
            var responseText = HttpRequestHelper.Post(url_sg, JsonHelper.Object2Json(input));
            LogHelper.Debug($"三高出参:{responseText}");
            var result_sg = JsonHelper.Json2Object<DispatchFactoryDto>(responseText);
            if (result_sg == null)
                return default;
            string error_info = null;
            switch (input.type)
            {
                case 1://长兴泵站
                    {
                        var result = shys_cxd(input, result_sg, out error_info);
                        LogHelper.Debug($"义维出参:{JsonHelper.Object2Json(result)}");
                        return result;
                    }
                default: return default;
            }
        }
        private static string _lastFlowId = null;
 
        /// <summary>
        /// 计算保持的方案
        /// </summary> 
        [AllowAnonymous]
        [NonUnify]
        [Route("CalcuKeepDispatch@Debug")]
        [HttpPost]
        public DispatchAnaSchemeOutput CalcuKeepDispatchDebug([Required] StationDispatchExInput input)
        {
            if (input == null)
                return default;
 
            var responseText = System.IO.File.ReadAllText(@"D:\result.txt");//(测试用的)
            var result_sg = JsonHelper.Json2Object<DispatchFactoryDto>(responseText);
            if (result_sg == null)
                return default;
 
            string error_info = null;
            switch (input.type)
            {
                case 1://长兴泵站
                    {
                        return shys_cxd(input, result_sg, out error_info);
                    }
                default: return default;
            }
        }
 
        /// <summary>
        /// 计算长兴保持的方案
        /// </summary> 
        [AllowAnonymous]
        [NonUnify]
        [Route("CalcuCxKeepDispatch@Debug")]
        [HttpGet]
        public DispatchAnaSchemeOutput CalcuCxKeepDispatchDebug
            (
                double waterLevel,//水位
                double flow1,//管道1流量
                double press1,//管道1压力
                double valveStatus1,//管道1阀门状态
                double flow2,//管道2流量
                double press2,//管道2压力
                double valveStatus2,//管道2阀门状态
                int middleValveStatus,//中间阀门状态
                int runStatus1,//1#机泵运行状态
                int runStatus2,//2#机泵运行状态
                int runStatus3,//3#机泵运行状态
                int runStatus4//4#机泵运行状态
            )
        {
            waterLevel = 2.558;
            flow1 = 2573;
            flow2 = 0;
            press1 = 9.5527 / 102;
            press2 = 9.5527 / 102;
            valveStatus1 = 1;
            valveStatus2 = 0;
            middleValveStatus = 1;
            runStatus1 = 1;
            runStatus2 = 0;
            runStatus3 = 0;
            runStatus4 = 1;
 
            var input = new StationDispatchExInput();
            input.flowId = "测试";
            input.type = 1;
            input.factory = new List<FactoryDispatchInput>() { new FactoryDispatchInput() { id = 1, value = 200 } };
 
            var result_sg = new DispatchFactoryDto();
            result_sg.result = new List<DispatchBranchDto>();
            result_sg.result.Add(new DispatchBranchDto() { key = "stationCX_1#_flow", value = flow1 });
            result_sg.result.Add(new DispatchBranchDto() { key = "stationCX_1#_pressure", value = press1 });
            result_sg.result.Add(new DispatchBranchDto() { key = "stationCX_2#_flow", value = flow2 });
            result_sg.result.Add(new DispatchBranchDto() { key = "stationCX_2#_pressure", value = press2 });
            result_sg.scada = new List<DispatchScadaDto>();
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010404030103001", value = waterLevel });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403150103003", value = middleValveStatus });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403010101003", value = runStatus1 });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403010201003", value = runStatus2 });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403010301003", value = runStatus3 });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403010401003", value = runStatus4 });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403150101003", value = valveStatus1 });
            result_sg.scada.Add(new DispatchScadaDto() { tagname = "_0402010403150102003", value = valveStatus2 });
 
            string error_info = null;
            switch (input.type)
            {
                case 1://长兴泵站
                    {
                        return shys_cxd(input, result_sg, out error_info);
                    }
                default: return default;
            }
        }
 
        /// <summary>
        /// 长兴岛
        /// </summary>
        /// <param name="input"></param>
        /// <param name="result_sg"></param> 
        /// <param name="error_info"></param> 
        /// <returns></returns>
        private IStation.Application.DispatchAnaSchemeOutput shys_cxd(
            StationDispatchExInput input,
            DispatchFactoryDto result_sg,
            out string error_info)
        {
            error_info = null;
 
            var complex_request_paras = new IStation.Calculation.DispatchAna.Model.RequestParasComplex();
            complex_request_paras.CorpID = 4;
            complex_request_paras.StationID = 2;
 
            //水位
            var scada_water = result_sg.scada.Find(t => t.tagname == "_0402010404030103001");
            double water_level = 0;
            if (scada_water == null)
            {
                error_info = "水位无法获取";
                return null;
            }
            else
            {
                water_level = Math.Round( scada_water.value,2);
            }
 
            complex_request_paras.InletPipePara = new List<IStation.Calculation.DispatchAna.Model.InletPipePara>();
            complex_request_paras.InletPipePara.Add(new IStation.Calculation.DispatchAna.Model.InletPipePara()
            {
                Name = "吸水井液位",
                Value = water_level
            });
 
 
 
            //1号管与2号管连接阀状态 0402010403150103003
            int middleValveStatus = 1;
            var scada_middle_vavle = result_sg.scada.Find(t => t.tagname == "_0402010403150103003");
            if (scada_middle_vavle != null)
                middleValveStatus = (int)scada_middle_vavle.value;
            complex_request_paras.ValvePara = new List<IStation.Calculation.DispatchAna.Model.ValvePara>();
            complex_request_paras.ValvePara.Add(new IStation.Calculation.DispatchAna.Model.ValvePara() { Name = "中间阀门", OpenStatus = middleValveStatus });
 
 
            //机泵 开停机状态
            List<long> machine_id_array = new List<long> { 19, 22, 25, 28 };
            List<long> machine_run_status = new List<long>(4);
            //1号泵开机状态
            var scada_runstatus1 = result_sg.scada.Find(t => t.tagname == "_0402010403010101003");
            if (scada_runstatus1 != null && (int)scada_runstatus1.value == 1)
                machine_run_status.Add(machine_id_array[0]);
 
            //2号泵开机状态
            var scada_runstatus2 = result_sg.scada.Find(t => t.tagname == "_0402010403010201003");
            if (scada_runstatus2 != null && (int)scada_runstatus2.value == 1)
                machine_run_status.Add(machine_id_array[1]);
 
            //3号泵开机状态
            var scada_runstatus3 = result_sg.scada.Find(t => t.tagname == "_0402010403010301003");
            if (scada_runstatus3 != null && (int)scada_runstatus3.value == 1)
                machine_run_status.Add(machine_id_array[2]);
 
            //4号泵开机状态
            var scada_runstatus4 = result_sg.scada.Find(t => t.tagname == "_0402010403010401003");
            if (scada_runstatus4 != null && (int)scada_runstatus4.value == 1)
                machine_run_status.Add(machine_id_array[3]);
 
            if (machine_run_status.Count == 0)
            {
                error_info = "没有任何泵开启.无法计算";
                return null;
            }
 
            //1号管 
            IStation.Calculation.DispatchAna.Model.OutletPipePara pipe1 = new Calculation.DispatchAna.Model.OutletPipePara();
            pipe1.Name = "一号管";
            var flow1 = result_sg.result.Find(x => x.key == "stationCX_1#_flow");
            if (flow1 == null)
            {
                error_info = "一号管流量无法获取";
                return null;
            }
            else
            {
                pipe1.TargetFlow = Math.Round(flow1.value,0);
            }
            var press1 = result_sg.result.Find(x => x.key == "stationCX_1#_pressure");
            if (press1 == null)
            {
                error_info = "一号管压力无法获取";
                return null;
            }
            else
            {
                pipe1.TargetPress = Math.Round(press1.value,4);
            }
            var pipe1_val = result_sg.scada.Find(t => t.tagname == "_0402010403150101003");
            if (pipe1_val != null)
            {
                pipe1.ValveStatus = (int)pipe1_val.value;
            }
 
 
 
            //2号管
            IStation.Calculation.DispatchAna.Model.OutletPipePara pipe2 = new Calculation.DispatchAna.Model.OutletPipePara();
            pipe2.Name = "二号管";
            var flow2 = result_sg.result.Find(x => x.key == "stationCX_2#_flow");
            if (flow2 == null)
            {
                error_info = "二号管流量无法获取";
                return null;
            }
            else
            {
                pipe2.TargetFlow = Math.Round(flow2.value,0);
            }
 
            var press2 = result_sg.result.Find(x => x.key == "stationCX_2#_pressure");
            if (press2 == null)
            {
                error_info = "二号管压力无法获取";
                return null;
            }
            else
            {
                pipe2.TargetPress = Math.Round(press2.value,4);
            }
            var pipe2_val = result_sg.scada.Find(t => t.tagname == "_0402010403150102003");
            if (pipe2_val != null)
            {
                pipe2.ValveStatus = (int)pipe2_val.value;
            }
 
 
            complex_request_paras.OutletPipePara = new List<IStation.Calculation.DispatchAna.Model.OutletPipePara>(2);
            complex_request_paras.OutletPipePara.Add(pipe1);
            complex_request_paras.OutletPipePara.Add(pipe2);
 
            complex_request_paras.SchemeSortType = Calculation.DispatchAna.Model.eAnaSchemeSortType.功率;
            complex_request_paras.SchemeNumber = 1;
 
            //构造计算器
            var calulator = IStation.Calculation.DispatchAnalyCalculatorFactory.CreateKeepStatusCalculator(4,
                IStation.ObjectType.Station, 2, null);
            calulator.IntialRequest(complex_request_paras,
                    null,
                    machine_run_status);
 
            #region 实时量
            double real_total_flow_pipe1 = 0;
            var pipe1_val_flow_real = result_sg.scada.Find(t => t.tagname == "_0402010403030002001");
            if (pipe1_val_flow_real != null && pipe1_val_flow_real.value > 100)
            {//实时量,  stationCX_2#_flow" 三高给的目标量
                real_total_flow_pipe1 = Math.Round( pipe1_val_flow_real.value,0);
            }
 
            double real_oulet_press_pipe1 = 0;
            var pipe1_val_press_real = result_sg.scada.Find(t => t.tagname == "_0402010403030002005");
            if (pipe1_val_press_real != null)
            {//实时量,  "stationCX_2#_flow" 是 三高给的目标量
                real_oulet_press_pipe1 = Math.Round(pipe1_val_press_real.value,5);
            }
 
 
            double real_total_flow_pipe2 = 0;
            var pipe2_val_flow_real = result_sg.scada.Find(t => t.tagname == "_0402010403030002002");
            if (pipe2_val_flow_real != null && pipe2_val_flow_real.value > 100)
            {
                real_total_flow_pipe2 = Math.Round(pipe2_val_flow_real.value,0);
            }
 
            double real_oulet_press_pipe2 = 0;
            var pipe2_val_press_real = result_sg.scada.Find(t => t.tagname == "_0402010403030002006");
            if (pipe2_val_press_real != null)
            {//实时量,  stationCX_2#_flow" 三高给的目标量
                real_oulet_press_pipe2 = Math.Round(pipe2_val_press_real.value,5);
            }
 
 
            //出方案 
            var scheme = calulator.Calc(out error_info);
            if (scheme == null)
                return default;
            var result = new IStation.Application.DispatchAnaSchemeOutput();
            result.flowId = input.flowId;
            result.Q = scheme.TotalWrkQ;
            result.H = scheme.TotalWrkH;
            result.E = scheme.TotalWrkE;
            result.P = scheme.TotalWrkP;
            result.WP = scheme.WP;
            result.UWP = scheme.UWP;
            result.Scheme = new DispatchAnaCurrentOutput();
            result.Scheme.MiddleValveStatus = middleValveStatus;
            result.Scheme.Pipes = new List<DispatchAnaCurrentOutput.PipeParaOutput>();
            result.Scheme.Pipes.Add(
                new DispatchAnaCurrentOutput.PipeParaOutput()
                {
                    Name = "1#管",
                    TargetFlow = pipe1.TargetFlow,
                    TargetPressure = pipe1.TargetPress,
                    CurrentFlow = real_total_flow_pipe1,
                    CurrentPressure = real_oulet_press_pipe1
                }
                );
            result.Scheme.Pipes.Add(
                new DispatchAnaCurrentOutput.PipeParaOutput()
                {
                    Name = "2#管",
                    TargetFlow = pipe2.TargetFlow,
                    TargetPressure = pipe2.TargetPress,
                    CurrentFlow = real_total_flow_pipe2,
                    CurrentPressure = real_oulet_press_pipe2
                }
            );
 
            List<string> machine_frq_hz_target_name = new List<string> {
                        "_0402010403010112007",
                        "_0402010403010212019",
                        "_0402010403010312007",
                        "_0402010403010412019" };
 
            List<string> machine_speed_target_name = new List<string> {
                        "_0402010403010112008",
                        "_0402010403010212020",
                        "_0402010403010312008",
                        "_0402010403010412020" };
 
            List<string> machine_names = new List<string> {
                        "长兴泵房1号机泵",
                        "长兴泵房2号机泵",
                        "长兴泵房3号机泵",
                        "长兴泵房4号机泵" };
 
            result.Pumps = new List<IStation.Application.DispatchAnaSchemeItemOutput>();
            foreach (IStation.Calculation.DispatchAna.Model.AnaSchemeItem item in scheme.Items)
            {
                var pump_item = new IStation.Application.DispatchAnaSchemeItemOutput();
                pump_item.Id = item.MachineID.ToString();
                pump_item.Index = machine_id_array.IndexOf(item.MachineID);
                pump_item.Name = item.MachineName;
                pump_item.IsFrequency = item.IsFrequency;
                pump_item.IsCurveExtend = item.IsCurveExtend;
                pump_item.Q = item.WorkPointQ;
                pump_item.H = item.WorkPointH;
                pump_item.E = item.WorkPointE;
                pump_item.P = item.WorkPointP;
                pump_item.Frequency = item.Frequence;
                pump_item.Speed = item.Speed;
 
                if (pump_item.Index >= 0 && pump_item.Index < 4)
                {
                    //获取实时频率
                    var fr_hz_tag_name = machine_frq_hz_target_name[pump_item.Index];
                    var current_val_fr_hz = result_sg.scada.Find(t => t.tagname == fr_hz_tag_name);
                    if (current_val_fr_hz != null && current_val_fr_hz.value > 10)
                    {
                        pump_item.CurrentFrequency = current_val_fr_hz.value;
                    }
 
                    //获取实时转速
                    var speed_tag_name = machine_speed_target_name[pump_item.Index];
                    var current_val_speed = result_sg.scada.Find(t => t.tagname == speed_tag_name);
                    if (current_val_speed != null && current_val_speed.value > 10)
                    {
                        pump_item.CurrentSpeed = current_val_speed.value;
                    }
 
                    //机泵名称
                    pump_item.Name = machine_names[pump_item.Index];
                }
 
                result.Pumps.Add(pump_item);
            }
 
            #endregion
 
            #region 修正
            if (machine_run_status.Count == 1)
            {
                result.Pumps.ForEach(x =>
                {//原水反馈 , 用曲线计算, 频率偏低, 人为增加0.5HZ
                    if (x.Frequency > 10)
                    {
                        if (x.Frequency < 49.5)
                        {
                            //x.Speed = Math.Round
                            //    (x.Speed * (x.Frequency + 0.5) / (x.Frequency), 0);
                            x.Frequency = x.Frequency + 0.5;
                            x.Speed = Math.Round(740 * x.Frequency / 50, 1);
                        }
                    }
                });
 
                //if (pipe1.TargetFlow + pipe2.TargetFlow > 100)
                //{
                //    double current_total_flow = 0;
                //    if (pipe1.TargetFlow > 100)
                //        current_total_flow = pipe1.TargetFlow;
                //    else
                //        current_total_flow = pipe2.TargetFlow;
                //    var open_pump = result.Pumps.First();
                //    var fr_hz_tag_name = machine_frq_hz_target_name[open_pump.Index];
                //    var current_val_fr_hz = result_sg.scada.Find(t => t.tagname == fr_hz_tag_name);
                //    if (current_val_fr_hz != null && current_val_fr_hz.value > 10)
                //    {
                //        bool isCor = false;
                //        if (current_total_flow < scheme.TotalWrkQ && current_val_fr_hz.value > open_pump.Frequency)
                //        {//保证趋势正确
                //            isCor = true;
                //            open_pump.Note = string.Format("ORI_{0}", open_pump.Frequency);
                //            open_pump.Frequency = Math.Round(current_val_fr_hz.value * scheme.TotalWrkQ / current_total_flow, 1);
                //        }
                //        else if (current_total_flow > scheme.TotalWrkQ && current_val_fr_hz.value < open_pump.Frequency)
                //        {//保证趋势正确
                //            isCor = true;
                //            open_pump.Note = string.Format("ORI_{0}", open_pump.Frequency);
                //            open_pump.Frequency = Math.Round(current_val_fr_hz.value * scheme.TotalWrkQ / current_total_flow, 1);
                //        }
 
                //    }
                //}
            }
            else
            {
                result.Pumps.ForEach(x =>
                {//原水反馈 , 用曲线计算, 频率偏低, 人为增加0.3HZ
                    if (x.Frequency > 10)
                    {
                        if (x.Frequency < 49.7)
                        {
                            x.Frequency = x.Frequency + 0.3;
                            x.Speed = Math.Round(740 * x.Frequency / 50, 1);
                        }
                    }
                });
            }
 
            #endregion
 
            result.Pumps.ForEach(x =>
            {//再检查一下转速
                if (x.Frequency > 10)
                {
                    x.Frequency = Math.Round(x.Frequency, 1);
                    x.Speed = Math.Round(740 * x.Frequency / 50, 1);
                }
                if (x.Frequency >= 50)
                {
                    x.Frequency = 50;
                    x.Speed = 740;
                }
            });
            return result;
        }
 
 
    }
}