GM_Base.cs 12.5 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FlyADBase;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
namespace FLY.Thick.Base.Server
{
    public abstract class GM_Base : IGageMode, INotifyErrorArisen
    {
        /// <summary>
        /// 启动时出错,已经在运行了
        /// </summary>
        public const byte ERRNO_StartUp_IsRunning = 1;
        /// <summary>
        /// 启动时出错,AD卡连接断开
        /// </summary>
        public const byte ERRNO_StartUp_FlyADNoConnected = 2;
        /// <summary>
        /// 参数异常
        /// </summary>
        public const byte ERRNO_StartUp_Param = 3;
        /// <summary>
        /// 参数异常
        /// </summary>
        public const byte ERRNO_Running_Manual = 4;

        protected FlyAD7 mFlyAD;
        protected FObjBase.PollModule.PollHandler onpoll_func;
        #region IGageMode 接口
34 35 36 37 38 39 40 41

        public CTRL_STATE GMState { get; protected set; }

        /// <summary>
        /// 运行中
        /// </summary>
        public bool IsRunning { get; protected set; }

潘栩锋's avatar
潘栩锋 committed
42 43 44
        public event ErrorArisenEventHander ErrorArisenEvent;
        protected void NotifyError(byte errno) 
        {
45
            ErrorArisenEvent?.Invoke(this, new ErrorArisenEventArgs() { Errno = errno });
潘栩锋's avatar
潘栩锋 committed
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
        }
        #endregion

        public GM_Base(FlyAD7 flyad)
        {
            mFlyAD = flyad;
            onpoll_func = new FObjBase.PollModule.PollHandler(OnPoll);
        }
        protected virtual void OnPoll()
        {
            if (mFlyAD.IsFinish)
            {
                Stop();
            }
        }
        #region IGageMode 接口
        public virtual bool Start()
        {
            if (IsRunning)
            {
                NotifyError(ERRNO_StartUp_IsRunning);
                return false;
            }
            if (!mFlyAD.IsConnected)
            {
                NotifyError(ERRNO_StartUp_FlyADNoConnected);
                return false;
            }
            IsRunning = true;
            FObjBase.PollModule.Current.Poll_Config(onpoll_func);
            return true;
        }
        public virtual void Stop()
        {
            FObjBase.PollModule.Current.Poll_Config(
                FObjBase.PollModule.POLL_CONFIG.REMOVE,
                onpoll_func);

            IsRunning = false;
        }

        #endregion
88
        
潘栩锋's avatar
潘栩锋 committed
89 90 91
        #region IGageMode 接口
        protected void NotifyPropertyChanged(string propertyName)
        {
92
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
潘栩锋's avatar
潘栩锋 committed
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 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 331 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 363 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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        }
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        #endregion

        /// <summary>
        /// 等待停止; 
        /// 正常停止返回true,没有停止及异常停止返回false
        /// </summary>
        /// <returns></returns>
        protected bool WaitFinish()
        {
            switch (mFlyAD.DriveStatus)
            {
                case DRIVE_MAN_STATUS.STOP://完成任务
                case DRIVE_MAN_STATUS.LIMIT:
                    {
                        return true;
                    }
                case DRIVE_MAN_STATUS.STOP_MANUAL://异常
                    {
                        NotifyError(ERRNO_Running_Manual);
                        Stop(); return false;
                    }
            }
            return false;
        }
    }

    public class GM_Goto : GM_Base
    {
        /// <summary>
        /// 扫描速度
        /// </summary>
        public UInt32 Velocity
        {
            get;
            set;
        }

        public GM_Goto(FlyAD7 flyad):base(flyad)
        {
            GMState = CTRL_STATE.RUNNING;
            Velocity = 5000;
        }
        public override bool Start()
        {
            return Start(1000);
        }
        public bool Start(int pos) 
        {
            bool b = base.Start();
            if (!b)
                return false;
            mFlyAD.SetPosParam(Velocity, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
            mFlyAD.Runto(pos);
            return true;
        }
    }

    public class GM_Forward : GM_Base 
    {
        /// <summary>
        /// 扫描速度
        /// </summary>
        public UInt32 Velocity
        {
            get;
            set;
        }
        public GM_Forward(FlyAD7 flyad)
            :base(flyad)
        {
            GMState = CTRL_STATE.FORW;
            Velocity = 5000;
        }
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;

            mFlyAD.SetPosParam( Velocity, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
            mFlyAD.RuntoMax();
            return true;
        }
    }

    public class GM_Backward : GM_Base 
    {
        /// <summary>
        /// 扫描速度
        /// </summary>
        public UInt32 Velocity
        {
            get;
            set;
        }
        public GM_Backward(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.BACKW;
            Velocity = 5000;
        }
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;
            mFlyAD.SetPosParam(Velocity, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
            mFlyAD.RuntoMin();
            return true;
        }
    }

    public class GM_Origin : GM_Base
    {
        public GM_Origin(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.ORG;
        }
        public override bool Start()
        {
            bool b =base.Start();
            if (!b)
                return false;
            mFlyAD.Origin();
            return true;
        }
    }

    public class GM_Stop : GM_Base
    {
        public GM_Stop(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.STOP;
        }
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;
            mFlyAD.Stop();
            return true;
        }
    }

    public class GM_Disconnected: GM_Base
    {
        GM_AutoScan mGMAutoScan;
        DynArea mDynArea;
        DateTime dt_lastscan = DateTime.MinValue;
        public GM_Disconnected(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.DISCONNECTED;
            
        }
        public void Init(DynArea dynarea, GM_AutoScan gmAutoScan) 
        {
            mDynArea = dynarea;
            mGMAutoScan = gmAutoScan;
            mDynArea.PropertyChanged += new PropertyChangedEventHandler(mDynArea_PropertyChanged);
        }

        void mDynArea_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FLYADIsConnect")
            {
                if (!mDynArea.FLYADIsConnect)
                {
                    //AD卡已经断开
                    //如果刚才在扫描,在1分钟内,AD卡连接后,重新执行扫描动作
                    if (mDynArea.ControllerState == CTRL_STATE.SCAN)
                    {
                        dt_lastscan = DateTime.Now;
                    }

                    Start();
                }
                else
                {
                    if (IsRunning)
                    {
                        if ((DateTime.Now - dt_lastscan) < TimeSpan.FromMinutes(1))//1min 内重新扫描
                        {                            
                            //断开连接,到现在重新连上,只过了30秒,重新执行扫描
                            mGMAutoScan.Start(5);
                        }
                        else
                        {
                            Stop();
                        }
                    }
                    dt_lastscan = DateTime.MinValue;
                }
            }
        }
        public override bool Start()
        {
            IsRunning = true;
            return true;
        }
        public override void Stop()
        {
            IsRunning = false;
        }
    }

    public class GM_AutoScan : GM_Base
    {
        public int Delay
        {
            get;
            set;
        }
        private int counter=5;
        public int Counter
        {
            get { return counter;

            }
            set {
                if (counter != value) 
                {
                    counter = value;
                    NotifyPropertyChanged("Counter");
                }
            }
        }
        Action ReScan;
        DateTime dtLast = DateTime.MinValue;
        public GM_AutoScan(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.AUTOSCAN;
            Delay = 5;
            Counter = Delay;
        }
        public void Init(Action reScan) 
        {
            ReScan = reScan;
        }
        protected override void OnPoll()
        {
            if ((DateTime.Now - dtLast).TotalSeconds >= 1)
            {
                dtLast = DateTime.Now;
                if (Counter > 0)
                    Counter--;

                if (Counter <= 0)
                {
                    Stop();
                    ReScan();
                }
            }
        }
        public bool Start(int delay) 
        {
            Delay = delay;
            return Start();
        }
        public override bool Start()
        {
            IsRunning = true;
            Counter = Delay;
            dtLast = DateTime.Now;
            FObjBase.PollModule.Current.Poll_Config(onpoll_func);
            return true;
        }
    }
    
    public class GM_Pause : GM_Base
    {
        public int Delay
        {
            get;
            set;
        }
        public bool Enable
        {
            get;
            set;
        }
        DynArea mDynArea;
        GM_AutoScan mGMAutoScan;
        enum STATE 
        {
            WAIT_ORG,
            WAIT_RESCAN
        }
        STATE mState;


        public GM_Pause(FlyAD7 flyad)
            : base(flyad)
        {
            GMState = CTRL_STATE.PAUSE;
            Enable = false;
            this.PropertyChanged += new PropertyChangedEventHandler(GM_Pause_PropertyChanged);
        }

        void GM_Pause_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Enable") 
            {
                if (Enable)
                {
                    update_datavalid();
                }
                else 
                {
                    if(IsRunning)
                        Stop();
                }
            }
        }
        public void Init(DynArea dynarea, GM_AutoScan gmAutoScan)
        {
            mDynArea = dynarea;
            mGMAutoScan = gmAutoScan;
            mDynArea.PropertyChanged+=new PropertyChangedEventHandler(mDynArea_PropertyChanged);
        }
        void update_datavalid() 
        {
            if (mDynArea.DataValid)
            {
                switch(mDynArea.ControllerState)
                {
                    case CTRL_STATE.FIX:
                    case CTRL_STATE.PAUSE:
                        mGMAutoScan.Start(Delay);
                        break;
                }
            }
            else
            {
                switch (mDynArea.ControllerState)
                {
                    case CTRL_STATE.SCAN:
                    case CTRL_STATE.AUTOSCAN:
                        Start();
                        break;
                    default:
                        mGMAutoScan.Stop();
                        break;
                }
            }
        }
        void mDynArea_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "DataValid") 
            {
                if(Enable)
                    update_datavalid();
            }
        }
        protected override void OnPoll()
        {
            switch (mState) 
            {
                case STATE.WAIT_ORG:
                    if (mFlyAD.IsFinish)
                    {
                        mState = STATE.WAIT_RESCAN;
                    }
                    break;
                case STATE.WAIT_RESCAN:
                    break;
            }
        }
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;
            mState = STATE.WAIT_ORG;
            mFlyAD.Origin();
            return true;
        }

    }
}