duheng
2025-03-28 9b673ca69c30616b5e5807d665ed8efa2720e1c8
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
using PBS.Model;
using PBS.Vmo;
using System.Data;
using System.IO;
using Yw;
using Yw.WinFrmUI;
using Yw.Untity;
using Yw.Service;
 
namespace IBox.WinFrmUI
{
    public partial class IBoxFormControll : DocumentPage
    {
 
        private string startCode = "[&start&]";
        private string endCode = "[&end&]";
        private string paramCode = "[&param&]";
        private string datadownloadCode = "datadownload";
 
        private void EboxFormControll_Load(object sender, EventArgs e)
        {
            //BluetoothHelper.GetInstance().SMR -= null;
            //BluetoothHelper.GetInstance().SMR += new BluetoothHelper.ShowMessReturn(GetReceive);
            //BluetoothHelper.GetInstance().ListenerData();
        }
 
        public void GetReceive(string msg)
        {
            if (!this.IsHandleCreated || this.IsDisposed || string.IsNullOrEmpty(msg)) return;
            if (msg.StartsWith("error:") && msg.Contains("目标主机坏了"))
            {
                MessageBoxHelper.ShowWarning(" c蓝牙设备连接失败,请重试!"); return;
            }
            if (!string.IsNullOrEmpty(msg) && msg.Contains(startCode))
            {
                var order = msg.Replace(endCode, "").Replace(startCode, "").Trim();
                SetOrder(order);
            }
            else MessageBoxHelper.ShowWarning(" c蓝牙设备连接失败");
        }
        public event EventHandler<string> SendData;
        private void SendText(string content)
        {
            if (SendData != null)
            {
                SendData?.Invoke(null, content);
 
            }
            else
            {
                if (IBoxHelper.Ping())
                {
                    var smsg = content.Split(new string[] { IBoxHelper.paramCode }, StringSplitOptions.RemoveEmptyEntries);
                    if (smsg.Length < 2)
                    {
                        return;
                    }
                    switch (smsg[0].Trim())
                    {
                        case IBoxHelper.datadownloadCode:
                            IBoxHelper.HttpPost(content);
                            break;
                    }
                }
                else
                {
                    MessageBoxHelper.ShowWarning("网络连接失败,请检查网络");
                }
            }
        }
 
        private void SetOrder(string order)
        {
            var smsg = order.Split(new string[] { paramCode }, StringSplitOptions.RemoveEmptyEntries);
            if (smsg.Length < 2)
            {
                return;
            }
 
            switch (smsg[0].Trim())
            {
                case "datadownload":
                    //WaitFrmHelper.HideWaitForm();
                    break;
            }
        }
        private FacilityVmo _facilities = null;
        private IBoxControllViewModel boxControllViewModel = null;
        public IBoxFormControll(IBoxControllViewModel ibox)
        {
            this.PageTitle.Caption = "智能控制";
            _facilities = ibox.facilities;
            boxControllViewModel = ibox;
            InitializeComponent();
            //this.treeListCtrl1.FocusedChangedEvent += EquipmentTreeListCtrl1_FocusedChangedEvent;
            InitDataSource();
        }
        //private string filePath = "Data\\AnalyData_{0}.json";
        private void InitDataSource()
        {
            var f = _facilities;
            this._SelectedBuild = new Build()
            {
                factoryName = f.Name,
                系统最大流量 = f.MaxWaterDemand.Value,
                Type = 二供分区Type.加压供水,
                ID = f.ID.ToString(),
                用户压力需求 = int.Parse(f.TerminalPressure.ToString()),
                层数 = double.Parse(f.Floor.ToString()),
                层高 = double.Parse(f.FloorHeight.ToString()),
            };
            //ShowChart();
        }
 
        //private void ShowChart()
        //{
        //    var file = string.Format(filePath, _facilities.ID.ToString());
        //    var path = Path.Combine(Directory.GetCurrentDirectory(), file);
        //    if (!File.Exists(path))
        //    {
        //        MessageBoxHelper.ShowInfo("请先进行系统曲线操作!");
        //        return;
        //    }
        //    else
        //    {
        //        SetResult(path);
        //    }
        //}
 
        //private void SetResult(string path)
        //{
        //    var content =  FileHelper.ReadFile(path);
        //    var result = JsonHelper.Json2Object<List<Result>>(content);
        //    AddPoints(result);
        //    ShowPressPoints();
        //}
        //private List<Result> _res;
        private Build _SelectedBuild;
        //public void AddPoints(List<Result> res)
        //{
        //    _res = res;
        //    List<PointF> nums = res.Select(o => new PointF((float)o.Demand, (float)o.ObjFunctionValue)).ToList();
        //    var chartDatas = nums.Select(p => new PointF(p.X, p.Y + _SelectedBuild.用户压力需求 + (float)_facilities.MaxHeight)).ToList();
        //    if (chartDatas == null || chartDatas.Count == 0) return;
 
        //    // 计算多项式在点 x 处的取值
        //    // 计算拟合曲线上的点
        //    var curve = new CurveFitHelper(chartDatas, 2);
        //    //List<PointF> fitPoints = curve.GetFitCurve(100);
        //    List<List<PointF>> confidencePoints = curve.GetConfidenceCurve(100);
 
        //    //_SelectedPump.流量压降平均.Data = fitPoints;
        //    //_SelectedBuild.流量压降曲线.Data = chartDatas;
        //    _SelectedBuild.Datasets.Clear();
        //    _SelectedBuild.流量压降曲线.Data = chartDatas;
        //    _SelectedBuild.流量压降下限.Data = confidencePoints[0];
        //    _SelectedBuild.流量压降上限.Data = confidencePoints[1];
        //}
        //public void ShowPressPoints()
        //{
        //    this.chart1.ChartAreas.Clear();
        //    this.chart1.ChartAreas.Add(new ChartArea());
        //    chart1.Series.Clear();
        //    chart1.Series.Add("数据");
        //    chart1.Series.Add("平均");
        //    chart1.Series["平均"].ChartType = SeriesChartType.Line;
        //    chart1.Series["平均"].BorderWidth = 2;
        //    chart1.Series.Add("上限");
        //    chart1.Series["上限"].ChartType = SeriesChartType.Line;
        //    chart1.Series["上限"].BorderWidth = 2;
        //    chart1.Series.Add("下限");
        //    chart1.Series["下限"].ChartType = SeriesChartType.Line;
        //    chart1.Series["下限"].BorderWidth = 2;
        //    chart1.Series.Add("泵出口静压线");
        //    chart1.Series["泵出口静压线"].ChartType = SeriesChartType.Line;
        //    chart1.Series["泵出口静压线"].BorderWidth = 2;
        //    chart1.Series["泵出口静压线"].Color = Color.Green;
 
        //    chart1.Series.Add("最高楼层标高");
        //    chart1.Series["最高楼层标高"].ChartType = SeriesChartType.Line;
        //    chart1.Series["最高楼层标高"].BorderWidth = 2;
        //    chart1.Series["最高楼层标高"].Color = Color.Yellow;
        //    if (chart1.ChartAreas.Count > 0)
        //    {
        //        chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.True;
        //        //chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
        //        chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.True;
        //        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";
        //        chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0";
        //        chart1.ChartAreas[0].AxisX.Title = "系统流量(m³/h)";
        //        chart1.ChartAreas[0].AxisY.Title = "泵后压力(m)";
        //        chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
        //    }
        //    //chart1.ChartAreas[0].AxisX.Minimum = double.NaN;
 
        //    //chart1.ChartAreas[0].AxisY.Minimum = double.NaN;
        //    var nums = _SelectedBuild.流量压降曲线.Data;
        //    if (nums.Count == 0) return;
        //    _SelectedBuild.流量压降曲线.CurveFit();
        //    var fitPoints = _SelectedBuild.流量压降曲线.FittedCurve;
        //    _SelectedBuild.流量压降下限.CurveFit();
        //    _SelectedBuild.流量压降上限.CurveFit();
        //    List<List<PointF>> confidencePoints = new List<List<PointF>>() { _SelectedBuild.流量压降下限.FittedCurve, _SelectedBuild.流量压降上限.FittedCurve };
        //    var chartDatas = nums.ToList();
        //    double yMin = double.MaxValue;
        //    double yMax = double.MinValue;
        //    float xMin = float.MaxValue;
        //    float xMax = float.MinValue;
        //    if (nums == null || nums.Count == 0) return;
        //    //this.chart1.Series[0].ChartType = SeriesChartType.Point;
        //    //double x = double.Parse(textBox_current_TotalDemand.Text);
        //    float x_Avg = 0;
        //    float x_Sum = 0;
        //    nums.ForEach(y =>
        //    {
        //        this.chart1.Series["数据"].Points.AddXY(y.X, y.Y);
        //        x_Sum += (y.X);
        //        if (yMin > y.Y) yMin = y.Y;
        //        if (yMax < y.Y) yMax = y.Y;
        //        if (xMin > y.X) xMin = y.X;
        //        if (xMax < y.X) xMax = y.X;
        //    });
        //    x_Avg = x_Sum / nums.Count;
 
        //    var dataPoints = this.chart1.Series[0].Points;
 
        //    // 计算多项式在点 x 处的取值
        //    // 计算拟合曲线上的点
        //    var waterpress = _SelectedBuild.流量压降下限.FittedCurve.Select((d) => { return new PointF(d.X, (float)_facilities.MaxHeight + _SelectedBuild.用户压力需求); }).ToList();
 
        //    var maxheight = _SelectedBuild.流量压降下限.FittedCurve.Select((d) => { return new PointF(d.X, (float)_facilities.MaxHeight); }).ToList();
        //    AddSeries(chart1.Series["平均"], fitPoints);
        //    AddSeries(chart1.Series["下限"], confidencePoints[0]);
        //    AddSeries(chart1.Series["上限"], confidencePoints[1]);
        //    AddSeries(chart1.Series["泵出口静压线"], waterpress);
        //    AddSeries(chart1.Series["最高楼层标高"], maxheight);
        //    if (yMin > 0 && yMin < 10)
        //    {
        //        this.chart1.ChartAreas[0].AxisY.Minimum = 0;
        //        this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling((yMax * 1.2) / 10) * 10;
        //        this.chart1.ChartAreas[0].AxisY.Interval = (chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) / 4;
        //    }
        //    else
        //    {
        //        this.chart1.ChartAreas[0].AxisY.Minimum = Math.Floor((yMin * 0.8) / 10) * 10;
        //        this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling((yMax * 1.2) / 10) * 10;
        //        chart1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
        //        //this.chart1.ChartAreas[0].AxisX.Minimum = double.NaN;
        //        //this.chart1.ChartAreas[0].AxisX.Maximum = double.NaN;
        //        this.chart1.ChartAreas[0].AxisY.Interval = (chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) / 4;
        //    }
        //    if (xMin > 0 && xMin < 10)
        //    {
        //        this.chart1.ChartAreas[0].AxisX.Minimum = 0;
        //        this.chart1.ChartAreas[0].AxisX.Maximum = Math.Ceiling(xMax);
        //    }
        //    else
        //    {
        //        this.chart1.ChartAreas[0].AxisX.Minimum = Math.Floor((xMin * 0.8) / 10) * 10;
        //        this.chart1.ChartAreas[0].AxisX.Maximum = Math.Ceiling(xMax * 1.2);
        //    }
        //}
 
        //private void AddSeries(System.Windows.Forms.DataVisualization.Charting.Series serie, List<PointF> points)
        //{
        //    if (points == null) return;
        //    serie.Points.Clear();
        //    foreach (var point in points)
        //    {
        //        serie.Points.AddXY(point.X, point.Y);
        //    }
        //}
       
 
        private void SendDownloadData()
        {
            var model = new IBoxControllDownloadViewModel();
            model.facilities = _facilities;
            model.kit = boxControllViewModel.Package;
            model.流量压降下限 = _SelectedBuild.流量压降下限.FittedCurve;
            model.流量压降上限 = _SelectedBuild.流量压降上限.FittedCurve;
            model.流量压降曲线 = _SelectedBuild.流量压降曲线.FittedCurve;
 
            if (!_SelectedBuild.流量压降曲线.IsFitted)
                 _SelectedBuild.流量压降曲线.CurveFit();
            model.coefficients = _SelectedBuild.流量压降上限.coefficients;
            model.equations = boxControllViewModel.equations;
            
            Thread.Sleep(500);
            SendText(startCode + datadownloadCode + paramCode + JsonHelper.Object2Json(model) + endCode);
        }
 
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //WaitFrmHelper.ShowWaitForm();
            SendDownloadData();
            MessageBoxHelper.ShowInfo("数据下发成功!");
            //MessageBoxHelper.ShowInfo("数据下发成功!");
        }
 
        
    }
}