GridAdvVm.cs 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
using FlyADBase;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace Flyad7_WPF
{
    public class GridAdvVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public bool HasGridAdv { get; set; }

        public RelayCommand SaveCmd { get; private set; }
        public RelayCommand LoadCmd { get; private set; }

        public RelayCommand CalAdLagCmd { get; private set; }
潘栩锋's avatar
潘栩锋 committed
25
        Chart chart3 = null;
26 27
        FlyAD7 flyad;
        int timeGridAdvIndex = 0;
潘栩锋's avatar
潘栩锋 committed
28
        List<TimeGridAdv2EventArgs> timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
29

潘栩锋's avatar
潘栩锋 committed
30
        public GridAdvVm(FlyAD7 flyad) 
31 32 33 34 35
        {
            SaveCmd = new RelayCommand(Save);
            LoadCmd = new RelayCommand(Load);
            CalAdLagCmd = new RelayCommand(CalAdLag);

潘栩锋's avatar
潘栩锋 committed
36
            this.flyad = flyad;
潘栩锋's avatar
潘栩锋 committed
37
            flyad.TimeGridAdv2Event += flyad_TimeGridAdv2Event;
38
        }
潘栩锋's avatar
潘栩锋 committed
39
        public void Init(Chart chart) 
40
        {
潘栩锋's avatar
潘栩锋 committed
41 42
            this.chart3 = chart;
        }
43 44 45 46



        #region timeGridAdv
潘栩锋's avatar
潘栩锋 committed
47
        private async void flyad_TimeGridAdv2Event(object sender, TimeGridAdv2EventArgs e)
48
        {
潘栩锋's avatar
潘栩锋 committed
49 50
            if (chart3 == null)
                return;
51 52 53
            if (!HasGridAdv)
                return;

潘栩锋's avatar
潘栩锋 committed
54 55 56
            if (timeGridAdv2Datas == null)
                timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
            timeGridAdv2Datas.Add(e);
57 58 59 60

            await Task.Factory.StartNew(() =>
            {
                //画图
潘栩锋's avatar
潘栩锋 committed
61
                DrawGridAdv(timeGridAdv2Datas.Last());
62

潘栩锋's avatar
潘栩锋 committed
63 64
                while (timeGridAdv2Datas.Count > chart3.Series.Count())
                    timeGridAdv2Datas.RemoveAt(0);
65

潘栩锋's avatar
潘栩锋 committed
66
                if (timeGridAdv2Datas.Count() >= 2)
67
                {
潘栩锋's avatar
潘栩锋 committed
68
                    CurrR = CalGridAdvR(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], 0);
69 70 71 72
                }
            });
        }

潘栩锋's avatar
潘栩锋 committed
73
        void DrawGridAdv(TimeGridAdv2EventArgs units, int adLag = 0)
74
        {
潘栩锋's avatar
潘栩锋 committed
75 76
            if (chart3 == null)
                return;
潘栩锋's avatar
潘栩锋 committed
77
            var datas = TimeGridAdvHelperExt.ToGrid(units.AdList, units.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
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
            //画图
            timeGridAdvIndex++;
            if (timeGridAdvIndex >= chart3.Series.Count())
                timeGridAdvIndex = 0;

            App.Current.Dispatcher.Invoke(() =>
            {
                System.Windows.Forms.DataVisualization.Charting.Series series = chart3.Series[timeGridAdvIndex];
                series.Points.Clear();

                int gridLen = flyad.PosLen / flyad.PosOfGrid;

                for (int i = 0; i < datas.Length; i++)
                {
                    int pos = i * flyad.PosOfGrid;
                    int ad = datas[i];
                    series.Points.AddXY(pos, ad);
                    if (ad == Misc.MyBase.NULL_VALUE)
                        series.Points[i].IsEmpty = true;
                }

                chart3.ChartAreas[0].AxisX.Minimum = 0;
                chart3.ChartAreas[0].AxisX.Maximum = flyad.PosLen;
            });
        }

        //计算相关性
        public int BestAdLag { get; private set; }
        public double BestR { get; private set; }
        public double CurrR { get; private set; }

        public double ProgressOfAdLag { get; private set; }
        public double ProgressOfR { get; private set; }

潘栩锋's avatar
潘栩锋 committed
112
        double CalGridAdvR(TimeGridAdv2EventArgs units1, TimeGridAdv2EventArgs units2, int adLag)
113
        {
潘栩锋's avatar
潘栩锋 committed
114 115 116
            var datas1 = TimeGridAdvHelperExt.ToGrid(units1.AdList, units1.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
            var datas2 = TimeGridAdvHelperExt.ToGrid(units2.AdList, units2.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
            
117 118 119
            return Misc.MyMath.Correl(datas1, datas2);
        }

潘栩锋's avatar
潘栩锋 committed
120
        void CalAdLag(TimeGridAdv2EventArgs units1, TimeGridAdv2EventArgs units2, int adLag, int range, double targetR, out int bestAdLag, out double bestR)
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
        {
            int step = range / 5;
            if (step < 0)
                step = 1;

            List<AdLagAndR> rList = new List<AdLagAndR>();
            for (int i = 0; i < range; i += step)
            {
                int _adLag = adLag - i;
                double _r = CalGridAdvR(units1, units2, _adLag);
                rList.Add(new AdLagAndR() { AdLag = _adLag, R = _r });
                ProgressOfAdLag = _adLag;
                ProgressOfR = _r;
            }
            for (int i = step; i < range; i += step)
            {
                int _adLag = adLag + i;
                double _r = CalGridAdvR(units1, units2, _adLag);
                rList.Add(new AdLagAndR() { AdLag = _adLag, R = _r });
                ProgressOfAdLag = _adLag;
                ProgressOfR = _r;
            }
            double maxR = rList.Max(lr => lr.R);
            adLag = rList.Find(_lr => _lr.R == maxR).AdLag;

            if (step == 1)
            {
                //已经是最小查找步进
                bestAdLag = adLag;
                bestR = maxR;
                return;
            }
            if (maxR >= targetR)
            {
                //找到了
                bestAdLag = adLag;
                bestR = maxR;
                return;
            }

            //缩小范围,继续找
            range = step * 2;
            CalAdLag(units1, units2, adLag, range, targetR, out bestAdLag, out bestR);

        }
        class AdLagAndR
        {
            public int AdLag;
            public double R;
        }

        private void Save()
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string path = System.IO.Path.Combine(strDesktopPath, $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.json");


潘栩锋's avatar
潘栩锋 committed
178
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(timeGridAdv2Datas);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
            File.WriteAllText(path, json);

            MessageBox.Show($"成功保存到 {path}");
        }

        private async void Load()
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = ".json";
            openFileDialog.Filter = "json文件|*.json";
            openFileDialog.InitialDirectory = strDesktopPath;
            if (openFileDialog.ShowDialog() != true)
                return;

            string path = openFileDialog.FileName;
            string json = File.ReadAllText(path);
            try
            {
潘栩锋's avatar
潘栩锋 committed
198
                timeGridAdv2Datas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TimeGridAdv2EventArgs>>(json);
199 200 201 202 203 204
            }
            catch (Exception ex)
            {
                MessageBox.Show($"读取失败 {ex}");
                return;
            }
潘栩锋's avatar
潘栩锋 committed
205
            if (timeGridAdv2Datas == null)
206
            {
潘栩锋's avatar
潘栩锋 committed
207
                timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
208 209 210 211 212 213
                MessageBox.Show($"读取失败 没有数据");
            }
            else
            {
                await Task.Factory.StartNew(() =>
                {
潘栩锋's avatar
潘栩锋 committed
214
                    foreach (var units in timeGridAdv2Datas)
215 216 217 218
                    {

                        DrawGridAdv(units);
                    }
潘栩锋's avatar
潘栩锋 committed
219 220
                    if (timeGridAdv2Datas.Count > chart3.Series.Count())
                        timeGridAdv2Datas.RemoveAt(0);
221

潘栩锋's avatar
潘栩锋 committed
222
                    if (timeGridAdv2Datas.Count() >= 2)
223
                    {
潘栩锋's avatar
潘栩锋 committed
224
                        CurrR = CalGridAdvR(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], 0);
225 226 227 228 229 230 231 232 233
                    }
                });

                MessageBox.Show($"读取成功");
            }

        }
        private async void CalAdLag()
        {
潘栩锋's avatar
潘栩锋 committed
234
            if (timeGridAdv2Datas.Count() < 2)
235 236 237 238 239 240 241
            {
                MessageBox.Show("数量小于2次");
                return;
            }
            int bestAdLag = 0;
            double bestR = -1;
            await Task.Factory.StartNew(() => {
潘栩锋's avatar
潘栩锋 committed
242
                CalAdLag(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2],
243 244 245 246 247
                    0, (int)(1000 / 1.28), 0.99, out bestAdLag, out bestR);
            });
            BestAdLag = bestAdLag;
            BestR = bestR;
            await Task.Factory.StartNew(() => {
潘栩锋's avatar
潘栩锋 committed
248 249
                DrawGridAdv(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], BestAdLag);
                DrawGridAdv(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], BestAdLag);
250 251 252 253 254 255
            });
            MessageBox.Show($"计算完成 最佳滞后={BestAdLag}");
        }
        #endregion
    }
}