FlyAdService.cs 8.15 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
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
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FlyADBase;
using FObjBase;
using FObjBase.Reflect;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AREA_ERR = FLY.Thick.Base.Common.AREA_ERR;
using AREA_STATUS = FLY.Thick.Base.Common.AREA_STATUS;
using MOTORTYPE = FLY.Thick.Base.Common.MOTORTYPE;

namespace FLY.Thick.Base.Server
{
    public class FlyAdService : IFlyADService
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_ACCESS = 2;
        #endregion

        public bool HasCRC
        {
            get
            {
                return flyad.HasCRC;
            }
            set
            {
                flyad.HasCRC = value;
            }
        }
        public string Addr
        {
            get
            {
                return flyad.Addr;
            }
            set
            {
                if (flyad.Addr != value)
                {
                    flyad.Connect(value);
                }
            }
        }
        public UInt16 PosOfGrid
        {
            get
            {
                return flyad.PosOfGrid;
            }
            set
            {
                flyad.PosOfGrid = value;
            }
        }
        public int PosLen
        {
            get
            {
                return flyad.PosLen;
            }
            set
            {
                flyad.PosLen = value;
            }
        }
        public MOTORTYPE MotorType
        {
            get
            {
                return (MOTORTYPE)flyad.MotorType;
            }
            set
            {
                flyad.MotorType = (FlyADBase.MOTORTYPE)value;
            }
        }
        public UInt16 Ratio01
        {
            get
            {
                return flyad.Ratio01;
            }
            set
            {
                flyad.Ratio01 = value;
            }
        }
        public UInt16 Ratio02
        {
            get
            {
                return flyad.Ratio02;
            }
            set
            {
                flyad.Ratio02 = value;
            }
        }
        public Int16 PosOffset
        {
            get
            {
                return flyad.PosOffset;
            }
            set
            {
                flyad.PosOffset = value;
            }
        }
        public UInt32 JogVelocity
        {
            get
            {
                return flyad.JogVelocity;
            }
            set
            {
                flyad.JogVelocity = value;
            }
        }
        public int GridSmooth
        {
            get
            {
                return flyad.GridSmooth;
            }
            set
            {
                flyad.GridSmooth = value;
            }
        }
        public bool IsCalSpeed
        {
            get
            {
                return flyad.IsCalSpeed;
            }
            set
            {
                flyad.IsCalSpeed = value;
            }
        }
        public int ADLag
        {
            get { return flyad.ADLag; }
            set
            {
                flyad.ADLag = value;
            }
        }
        FlyAD7 flyad;
        AD2ThkHandler ad2thk;
        public FlyAdService()
        {

        }
        public void Init(FlyAD7 flyad, AD2ThkHandler func_Ad2Thk)
        {

            ad2thk = func_Ad2Thk;
            this.flyad = flyad;

            this.flyad.PropertyChanged += Flyad_PropertyChanged;
        }
        private void Flyad_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var type = typeof(FlyAdService);
            if (type.GetProperty(e.PropertyName) != null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(e.PropertyName));
            }

            //if(e.PropertyName )
        }

        /// <summary>
        /// 获取序列码
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(AccessInfo))]
        public void GetAccessInfo(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            AccessInfo ainfo = new AccessInfo()
            {
                status = (AREA_STATUS)flyad.AreaStatus,
                ret = (AREA_ERR)flyad.AreaRet,
                surplus = flyad.Surplus,
                code = flyad.Code,
                access = flyad.Access
            };
            asyncDelegate(asyncContext, ainfo);
        }

        class AsyncDelegateObj
        {
            public AsyncCBHandler asyncDelegate;
            public object asyncContext;
        }
        List<AsyncDelegateObj> setAccessRetList = new List<AsyncDelegateObj>();

        PropertyChangedEventHandler mFlyAD_PropertyChangedHandler = null;

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 设置授权码
        /// </summary>
        /// <param name="access"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(AccessInfo))]
        public void SetAccess(byte[] access, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            flyad.SetAccess(access);
            setAccessRetList.Add(new AsyncDelegateObj() { asyncDelegate = asyncDelegate, asyncContext = asyncContext });

            if (mFlyAD_PropertyChangedHandler == null)
            {
                mFlyAD_PropertyChangedHandler = new PropertyChangedEventHandler(delegate (object sender, PropertyChangedEventArgs e)
                {
                    if ((e.PropertyName == nameof(flyad.AreaStatus)) ||
                        (e.PropertyName == nameof(flyad.Code)) ||
                        (e.PropertyName == nameof(flyad.Surplus)) ||
                        (e.PropertyName == nameof(flyad.Access)) ||
                        (e.PropertyName == nameof(flyad.AreaRet)))
                    {
                        FObjBase.PollModule.Current.Poll_JustOnce(
                                new PollModule.PollHandler(delegate ()
                                {
                                    flyad.PropertyChanged -= mFlyAD_PropertyChangedHandler;
                                    AccessInfo ainfo = new AccessInfo()
                                    {
                                        status = (AREA_STATUS)flyad.AreaStatus,
                                        ret = (AREA_ERR)flyad.AreaRet,
                                        surplus = flyad.Surplus,
                                        code = flyad.Code,
                                        access = flyad.Access
                                    };
                                    for (int i = 0; i < setAccessRetList.Count(); i++)
                                    {
                                        setAccessRetList[i].asyncDelegate(setAccessRetList[i].asyncContext, ainfo);
                                    }
                                    setAccessRetList.Clear();
                                }), this, MARKNO_PUSH_ACCESS);
                    };
                });
            }
            flyad.PropertyChanged += mFlyAD_PropertyChangedHandler;
        }

        /// <summary>
        /// 获取grid
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(GridInfo))]
        public void GetGrid(Misc.DIRECTION direction, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            flyad.GetGrid(direction, out int[] dat);
            double[] thks = null;
            if (dat != null)
            {
                thks = dat.Select(ad => ad2thk(ad)).ToArray();
            }

            GridInfo p = new GridInfo()
            {
                direction = direction,
                data = dat,
                thick = thks
            };
            asyncDelegate(asyncContext, p);
        }

        /// <summary>
        /// 向AD盒获取数据
        /// </summary>
        public void UpdateParam()
        {
            flyad.UpdateParam();
        }
    }
}