GM_Fix.cs 3.93 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
10
using Misc;
潘栩锋's avatar
潘栩锋 committed
11 12 13 14 15 16 17 18 19 20

namespace FLY.Thick.Base.Server
{
    public class GM_Fix : IGageMode, IFixService
    {
        #region IGageMode
        public CTRL_STATE GMState
        {
            get { return CTRL_STATE.FIX; }
        }
潘栩锋's avatar
潘栩锋 committed
21 22 23

        public bool IsRunning { get; protected set; }

潘栩锋's avatar
潘栩锋 committed
24
        #endregion
25 26 27 28
        /// <summary>
        /// 闲置时(IsRunning==false) 忽略数据
        /// </summary>
        public bool IsIgnoreDataWhenIdle { get; set; } = true;
潘栩锋's avatar
潘栩锋 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        protected FlyADBase.FlyAD7 mFlyAD;
        protected DynArea mDynArea;
        protected AD2ThickHandler AD2Thick;

        public GM_Fix(FlyADBase.FlyAD7 flyad) 
        {
            mFlyAD = flyad;
            flyad.TimeGridEvent += new FlyADBase.TimeGridEventHandler(flyad_TimeGridEvent);
        }
        public void Init(DynArea dynarea, AD2ThickHandler func_ad2thick)
        {

            mDynArea = dynarea;

            AD2Thick = func_ad2thick;
        }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
46
        /// 1s 的AD值,就只是用来显示到主界面左上面角而已 DynArea.AD
潘栩锋's avatar
潘栩锋 committed
47 48 49 50 51 52
        /// </summary>
        List<int> Data1s = new List<int>();


        void flyad_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
53 54
            //if (IsIgnoreDataWhenIdle && !IsRunning)
            //    return;
潘栩锋's avatar
潘栩锋 committed
55

潘栩锋's avatar
潘栩锋 committed
56 57
            DateTime dt = e.Time;
            TimeSpan ts = e.Ts;
潘栩锋's avatar
潘栩锋 committed
58
            List<int> data = new List<int>(e.Data);
潘栩锋's avatar
潘栩锋 committed
59

潘栩锋's avatar
潘栩锋 committed
60
            //1秒数据
潘栩锋's avatar
潘栩锋 committed
61
            Data1s.AddRange(data);
潘栩锋's avatar
潘栩锋 committed
62
            if (TimeSpan.FromTicks(Data1s.Count * ts.Ticks) >= TimeSpan.FromSeconds(1)) 
潘栩锋's avatar
潘栩锋 committed
63
            {
潘栩锋's avatar
潘栩锋 committed
64
                int ad = (int)Data1s.Average();
潘栩锋's avatar
潘栩锋 committed
65 66 67 68 69
                Data1s.Clear();

                mDynArea.AD = ad;
                mDynArea.Thick = AD2Thick(ad);
            }
潘栩锋's avatar
潘栩锋 committed
70 71


潘栩锋's avatar
潘栩锋 committed
72
            //转换为thick
潘栩锋's avatar
潘栩锋 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            List<double> thicks = new List<double>();
            foreach(int ad in data)
            {
                double thick;
                int t = AD2Thick(ad);
                if (Misc.MyBase.ISVALIDATA(t))
                    thick = t / 100.0;
                else
                    thick = double.NaN;
                thicks.Add(thick);
            }

            //需要限制大小,push 不能太大
            //分拆为多个包
            int size = 200;
            while (data.Count()>0)
            {
                int cnt = (data.Count() > size) ? size : data.Count();
潘栩锋's avatar
潘栩锋 committed
91 92
                int[] ADs2 = data.Take(cnt).ToArray();
                double[] thicks2 = thicks.Take(cnt).ToArray();
潘栩锋's avatar
潘栩锋 committed
93 94 95 96 97 98 99 100 101

                data.RemoveRange(0, cnt);
                thicks.RemoveRange(0, cnt);

                FixEvent?.Invoke(this,
                    new FixEventArgs()
                    {
                        time = dt,
                        ts = ts,
潘栩锋's avatar
潘栩锋 committed
102 103
                        ADs = ADs2,
                        thicks = thicks2
潘栩锋's avatar
潘栩锋 committed
104 105 106
                    });
                dt += TimeSpan.FromTicks(cnt * ts.Ticks);
            }
潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        }

        #region IGageMode
        public bool Start()
        {
            IsRunning = true;
            return true;
        }

        public void Stop()
        {
            IsRunning = false;
        }

        #endregion


        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;


潘栩锋's avatar
潘栩锋 committed
127 128 129 130 131 132
        event FixEventHandler FixEvent;
        /// <summary>
        /// 注册定点数据事件 FixEvent += handler;
        /// </summary>
        /// <param name="handler"></param>
        public void RegistTimeGridEvent(FixEventHandler handler)
潘栩锋's avatar
潘栩锋 committed
133
        {
潘栩锋's avatar
潘栩锋 committed
134
            FixEvent += handler;
潘栩锋's avatar
潘栩锋 committed
135 136
        }

潘栩锋's avatar
潘栩锋 committed
137 138 139 140 141
        /// <summary>
        /// 取消注册定点数据事件 FixEvent -= handler;
        /// </summary>
        /// <param name="handler"></param>
        public void UnRegistTimeGridEvent(FixEventHandler handler)
潘栩锋's avatar
潘栩锋 committed
142
        {
潘栩锋's avatar
潘栩锋 committed
143
            FixEvent -= handler;
潘栩锋's avatar
潘栩锋 committed
144 145 146
        }
    }
}