IBCCtrlGraph.xaml.cs 14 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FLY.IBC.Common;
潘栩锋's avatar
潘栩锋 committed
2
using FLY.IBC.IService;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.IBC.Server.Model;
潘栩锋's avatar
潘栩锋 committed
4
using FLY.OBJComponents.Client;
潘栩锋's avatar
潘栩锋 committed
5
using FLY.OBJComponents.IService;
潘栩锋's avatar
潘栩锋 committed
6 7
using LiveCharts;
using LiveCharts.Configurations;
潘栩锋's avatar
潘栩锋 committed
8
using MultiLayout;
潘栩锋's avatar
潘栩锋 committed
9 10 11 12
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
潘栩锋's avatar
潘栩锋 committed
13
using System.IO;
潘栩锋's avatar
潘栩锋 committed
14 15 16 17 18 19 20 21 22 23 24 25
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
26
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
27
using Unity;
潘栩锋's avatar
潘栩锋 committed
28

潘栩锋's avatar
潘栩锋 committed
29
namespace FLY.IBC.UI.Client.UiModule
潘栩锋's avatar
潘栩锋 committed
30 31 32 33
{
    /// <summary>
    /// IBCCtrlGraph.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
34
    public partial class IbcCtrlGraph : UserControl
潘栩锋's avatar
潘栩锋 committed
35
    {
潘栩锋's avatar
潘栩锋 committed
36 37 38
        IbcCtrlGraphVm viewModel;
        IUnityContainer container;
        public IbcCtrlGraph()
潘栩锋's avatar
潘栩锋 committed
39 40 41
        {
            InitializeComponent();
        }
潘栩锋's avatar
潘栩锋 committed
42 43 44 45 46
        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IIbcSystemService ibcSystemService,
            IBuffer<Db_Width> ctrlList)
潘栩锋's avatar
潘栩锋 committed
47
        {
潘栩锋's avatar
潘栩锋 committed
48 49
            viewModel = new IbcCtrlGraphVm();
            viewModel.Init(container, ibcSystemService, ctrlList);
潘栩锋's avatar
潘栩锋 committed
50

潘栩锋's avatar
潘栩锋 committed
51 52
            grid_modelview.DataContext = viewModel;
            grid_bufferwindow.DataContext = viewModel.bufferWindow;
潘栩锋's avatar
潘栩锋 committed
53 54 55
        }
        private void button_info_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
56 57 58
            WdGraphSet w = new WdGraphSet();
            w.Init();
            //container.BuildUp(w);
潘栩锋's avatar
潘栩锋 committed
59 60 61 62 63 64
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void button_newest_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
65
            viewModel.Newest();
潘栩锋's avatar
潘栩锋 committed
66 67 68 69
        }

        private void button_pre_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
70
            viewModel.Pre();
潘栩锋's avatar
潘栩锋 committed
71 72 73 74
        }

        private void button_next_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
75
            viewModel.Next();
潘栩锋's avatar
潘栩锋 committed
76 77
        }
    }
潘栩锋's avatar
潘栩锋 committed
78
    public class IbcCtrlGraphVm : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
79
    {
80
        const int MARKNO_TOVALUES = 1;
81 82 83 84
        /// <summary>
        /// 当前现在不是最新,而且没有移动动作,20秒自动按 Newest()
        /// </summary>
        DispatcherTimer timer;
潘栩锋's avatar
潘栩锋 committed
85 86
        public event PropertyChangedEventHandler PropertyChanged;

潘栩锋's avatar
潘栩锋 committed
87 88
        IIbcSystemService ibcSystemService;
        IBuffer<Db_Width> ctrlList;
潘栩锋's avatar
潘栩锋 committed
89
        public BufferWindow<Db_Width> bufferWindow;
潘栩锋's avatar
潘栩锋 committed
90
        public IbcCtrlGraphParams graphParams;
潘栩锋's avatar
潘栩锋 committed
91

92 93 94
        /// <summary>
        /// 画面只显示200个数据, 就算设置画面设置显示1000个数,也会整合为200个数据
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
95
        const int ValuesCap = 200;
96 97 98 99
        /// <summary>
        /// 自动按保持最新值,总时间
        /// </summary>
        const int AutoKeepNewestTimeCounter = 10;
潘栩锋's avatar
潘栩锋 committed
100
        #region property
101 102 103 104 105 106

        /// <summary>
        ///自动按保持最新值,剩余时间
        /// </summary>
        public int AutoKeepNewestTimeRemaining { get; set; }

潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111
        /// <summary>
        /// 曲线, 是有 bufferWindow.Record 生成的
        /// Values 最大数量只有 ValuesCap
        /// 平均从 bufferWindow.Record提取
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
112
        public ChartValues<Db_Width> Values { get; set; }
潘栩锋's avatar
潘栩锋 committed
113 114 115 116

        /// <summary>
        /// 折径曲线 XY 定义
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
117
        public CartesianMapper<Db_Width> Mapper_FilmWidth { get; set; }
潘栩锋's avatar
潘栩锋 committed
118 119 120 121

        /// <summary>
        /// 进风-出风曲线 XY 定义
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
122
        public CartesianMapper<Db_Width> Mapper_D { get; set; }
潘栩锋's avatar
潘栩锋 committed
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

        /// <summary>
        /// X轴格式
        /// </summary>
        public Func<double, string> DateTimeFormatter { get; set; }

        /// <summary>
        /// y轴格式
        /// </summary>
        public Func<double, string> YFormatter { get; set; }

        /// <summary>
        /// 折径目标值
        /// </summary>
        public double FilmWidthAxisYTarget { get; set; }

        /// <summary>
        /// 折径 最大值
        /// </summary>
        public double FilmWidth_MaxValue { get; set; }
        /// <summary>
        /// 折径 最小值
        /// </summary>
        public double FilmWidth_MinValue { get; set; }

        /// <summary>
        /// 进风-出风 最大值
        /// </summary>
        public double D_MaxValue { get; set; }
        /// <summary>
        /// 进风-出风 最小值
        /// </summary>
        public double D_MinValue { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime BeginTime { get; set; }

        /// <summary>
        /// 结束数据
        /// </summary>
        public DateTime EndTime { get; set; }
        #endregion

潘栩锋's avatar
潘栩锋 committed
168
        public IbcCtrlGraphVm()
潘栩锋's avatar
潘栩锋 committed
169
        {
潘栩锋's avatar
潘栩锋 committed
170
            InitializeComponent();
潘栩锋's avatar
潘栩锋 committed
171 172 173 174 175 176 177 178 179 180 181
        }
        void ToValues()
        {
            if (bufferWindow.Record.Count <= ValuesCap)
            {
                Values.Clear();
                Values.AddRange(bufferWindow.Record);
            }
            else
            {
                Values.Clear();
潘栩锋's avatar
潘栩锋 committed
182
                List<Db_Width> record = new List<Db_Width>();
潘栩锋's avatar
潘栩锋 committed
183 184 185
                for (int i = 0; i < ValuesCap; i++)
                {
                    int idx = i * bufferWindow.Record.Count / ValuesCap;
186
                    record.Add(bufferWindow.Record[idx]);
潘栩锋's avatar
潘栩锋 committed
187
                }
188
                Values.AddRange(record);
潘栩锋's avatar
潘栩锋 committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202
            }
            if (Values.Count() > 0)
            {
                BeginTime = Values.First().Time;
                EndTime = Values.Last().Time;
            }
            else
            {
                BeginTime = DateTime.MinValue;
                EndTime = DateTime.MinValue;
            }
        }

        
潘栩锋's avatar
潘栩锋 committed
203
        void InitializeComponent()
潘栩锋's avatar
潘栩锋 committed
204
        {
潘栩锋's avatar
潘栩锋 committed
205
            Mapper_FilmWidth = Mappers.Xy<Db_Width>()
潘栩锋's avatar
潘栩锋 committed
206 207 208
                .X(value => value.Time.Ticks)
                .Y(value => value.FilmWidth);

潘栩锋's avatar
潘栩锋 committed
209
            Mapper_D = Mappers.Xy<Db_Width>()
潘栩锋's avatar
潘栩锋 committed
210
                .X(value => value.Time.Ticks)
潘栩锋's avatar
潘栩锋 committed
211
                .Y(value => value.InletAirFreq - value.OutletAirFreq);
潘栩锋's avatar
潘栩锋 committed
212 213 214 215

            DateTimeFormatter = value => new DateTime((long)value).ToString("mm:ss");
            YFormatter = value => value.ToString("F0");

潘栩锋's avatar
潘栩锋 committed
216
            Values = new ChartValues<Db_Width>();
潘栩锋's avatar
潘栩锋 committed
217 218
        }

潘栩锋's avatar
潘栩锋 committed
219 220 221 222 223
        [InjectionMethod]
        public void Init(
            IUnityContainer container, 
            IIbcSystemService ibcSystemService,
            IBuffer<Db_Width> ctrlList)
潘栩锋's avatar
潘栩锋 committed
224
        {
潘栩锋's avatar
潘栩锋 committed
225 226 227
            graphParams = IbcCtrlGraphParams.Current;
            this.ibcSystemService = ibcSystemService;
            this.ctrlList = ctrlList;
潘栩锋's avatar
潘栩锋 committed
228

潘栩锋's avatar
潘栩锋 committed
229
            bufferWindow = new BufferWindow<Db_Width>(this.ctrlList, graphParams.WindowSize);
潘栩锋's avatar
潘栩锋 committed
230 231 232 233 234

            Misc.BindingOperations.SetBinding(graphParams, "WindowSize", bufferWindow, "Size");

            bufferWindow.Record.CollectionChanged += (object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) =>
            {
235 236 237 238 239
                FObjBase.PollModule.Current.Poll_JustOnce(
                    () =>
                    {
                        ToValues();
                    }, this, MARKNO_TOVALUES);
潘栩锋's avatar
潘栩锋 committed
240 241
            };

潘栩锋's avatar
潘栩锋 committed
242
            Misc.BindingOperations.SetBinding(ibcSystemService.Item, "FilmWidthSet", this, "FilmWidthAxisYTarget");
潘栩锋's avatar
潘栩锋 committed
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

            Update_filmwidth_y();
            Update_d_y();

            this.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "FilmWidthAxisYTarget")
                    Update_filmwidth_y();
            };

            graphParams.PropertyChanged += (s, e) => {
                if (e.PropertyName == "IsAxisYRangeAuto")
                {
                    Update_filmwidth_y();
                    Update_d_y();
                }
                else if (e.PropertyName == "FilmWidthAxisYRange")
                {
                    Update_filmwidth_y();
                }
                else if (
                    (e.PropertyName == "DAxisYTarget") ||
                    (e.PropertyName == "DAxisYRange"))
                {
                    Update_d_y();
                }
            };

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
            //当不是最新数据时,10秒内无操作,自动按 [最新]
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
            bufferWindow.PropertyChanged += BufferWindow_PropertyChanged;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            AutoKeepNewestTimeRemaining--;
            if (AutoKeepNewestTimeRemaining <= 0)
            {
                AutoKeepNewestTimeRemaining = AutoKeepNewestTimeCounter;
                bufferWindow.MoveNewest();
            }
        }
        private void BufferWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsKeepNewest")
            {
                if (!bufferWindow.IsKeepNewest)
                {
                    //当前不是最新,开始倒计时
                    AutoKeepNewestTimeRemaining = AutoKeepNewestTimeCounter;
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
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

        void Update_filmwidth_y()
        {
            if (graphParams.IsAxisYRangeAuto)
            {
                FilmWidth_MaxValue = double.NaN;
                FilmWidth_MinValue = double.NaN;
            }
            else
            {
                FilmWidth_MaxValue = FilmWidthAxisYTarget + graphParams.FilmWidthAxisYRange;
                FilmWidth_MinValue = FilmWidthAxisYTarget - graphParams.FilmWidthAxisYRange;
            }
        }
        void Update_d_y()
        {
            if (graphParams.IsAxisYRangeAuto)
            {
                D_MaxValue = double.NaN;
                D_MinValue = double.NaN;
            }
            else
            { 
                D_MaxValue = graphParams.DAxisYTarget + graphParams.DAxisYRange;
                D_MinValue = graphParams.DAxisYTarget - graphParams.DAxisYRange;
            }
        }
        void Debug()
        {
潘栩锋's avatar
潘栩锋 committed
331
            Values = new ChartValues<Db_Width>();
潘栩锋's avatar
潘栩锋 committed
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

            double target_filmwidth = 1000;
            double filmwidth = 800;
            double inletfreq = 30;
            double outletfreq = 30;
            double pid_p = 0.01;
            double p_f = 8;

            DateTime date = DateTime.Now;

            Random r = new Random();
            double noise = 2;


            double last_filmwidth = filmwidth;
            for (int i = 0; i < 1000; i++)
            {
                double e = target_filmwidth - filmwidth;
                last_filmwidth = filmwidth;

                double e_freq = e * pid_p;

                inletfreq = e_freq + outletfreq;


                e_freq = inletfreq - outletfreq;

                filmwidth += e_freq * p_f;

                filmwidth += (r.NextDouble() * noise - noise / 2);

潘栩锋's avatar
潘栩锋 committed
363
                Values.Add(new Db_Width()
潘栩锋's avatar
潘栩锋 committed
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
                {
                    Time = date.AddSeconds(1 * i),
                    FilmWidth = (float)filmwidth,
                    InletAirFreq = (float)inletfreq,
                    OutletAirFreq = (float)outletfreq
                });
            }
            FilmWidthAxisYTarget = target_filmwidth;


        }

        public void Newest()
        {
            bufferWindow.MoveNewest();
        }
        public void Next()
        {
            bufferWindow.MoveNextPage();
        }
        public void Pre()
        {
            bufferWindow.MovePrePage();
        }
    }
潘栩锋's avatar
潘栩锋 committed
389
    public class IbcCtrlGraphParams : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
390
    {
潘栩锋's avatar
潘栩锋 committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
        static IbcCtrlGraphParams current = null;
        public static IbcCtrlGraphParams Current
        {
            get
            {
                if (current == null)
                {
                    current = new IbcCtrlGraphParams();
                    current.Load();
                }
                return current;
            }
        }

潘栩锋's avatar
潘栩锋 committed
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
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 窗口显示数据总量
        /// </summary>
        public int WindowSize { get; set; } = 1000;

        /// <summary>
        /// 折径,进风-出风, Y轴 自动设置 MaxValue, MinValue, 
        /// </summary>
        public bool IsAxisYRangeAuto { get; set; }
        /// <summary>
        /// 折径,Y轴显示范围, 中间值一定是 FilmWidthSet, 单位mm
        /// MaxValue = FilmWidthSet+FilmWidthAxisYRange
        /// MinValue = FilmWidthSet-FilmWidthAxisYRange
        /// </summary>
        public int FilmWidthAxisYRange { get; set; } = 20;


        /// <summary>
        /// 进风-出风 Y轴显示范围 单位Hz
        /// </summary>
        public int DAxisYRange { get; set; } = 10;

        /// <summary>
        /// 进风-出风 Y轴 中间显示值 单位Hz
        /// </summary>
        public int DAxisYTarget { get; set; } = 0;

潘栩锋's avatar
潘栩锋 committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
        string FilePath;
        public IbcCtrlGraphParams()
        {
            string path = System.IO.Path.Combine(FlyLayout.BasePath, "ibcCtrlGraph.json");
            FilePath = path;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(this);
潘栩锋's avatar
潘栩锋 committed
449

潘栩锋's avatar
潘栩锋 committed
450 451 452 453 454 455 456 457
                File.WriteAllText(FilePath, json);
            }
            catch
            {
                return false;
            }
            return true;
        }
潘栩锋's avatar
潘栩锋 committed
458

潘栩锋's avatar
潘栩锋 committed
459 460 461 462 463
        /// <summary>
        /// 读取
        /// </summary>
        /// <returns></returns>
        bool Load()
潘栩锋's avatar
潘栩锋 committed
464
        {
潘栩锋's avatar
潘栩锋 committed
465 466 467 468 469 470 471 472 473 474
            try
            {
                string json = File.ReadAllText(FilePath);
                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
                return true;
            }
            catch
            {
                return false;
            }
潘栩锋's avatar
潘栩锋 committed
475 476 477
        }
    }
}