Esempio n. 1
0
        public async Task ConnectAsync()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException("SerialDeviceManager");
            }
            Exception connectException = null;
            await deviceEventCtrl.RaiseAsync(this, async() =>
            {
                if (state != OTPDeviceState.Disconnected)
                {
                    throw new Exception("Device manager is in wrong state: " + state.ToString());
                }
                try
                {
                    port.Open();
                    commHelper        = new StreamCommHelper(port.BaseStream, config);
                    var lcgInitValues = await CommHelper.Resync();                     //run resync;
                    reqLCG.ReInit(lcgInitValues.Key);
                    ansLCG.ReInit(lcgInitValues.Value);
                }
                catch (Exception ex)
                {
                    commHelper       = null;
                    connectException = ex;
                    state            = OTPDeviceState.Failed;
                    return(new OTPDeviceEventArgs()
                    {
                        State = OTPDeviceState.Failed, Error = ex
                    });
                }
                //run ping worker thread
                pingWorker = PingWorker.StartNewWorker(pingLock, CommHelper, reqLCG, ansLCG, config.PING_INTERVAL, PingFailedCallback);
                //state change
                state = OTPDeviceState.Connected;
                return(new OTPDeviceEventArgs()
                {
                    State = OTPDeviceState.Connected, Error = null
                });
            });

            if (connectException != null)
            {
                throw connectException;
            }
        }
Esempio n. 2
0
        public async Task DisconnectAsync()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException("SerialDeviceManager");
            }
            Exception disconnectException = null;
            await deviceEventCtrl.RaiseAsync(this, () =>
            {
                if (state != OTPDeviceState.Connected)
                {
                    throw new Exception("Device manager is in wrong state: " + state.ToString());
                }
                try
                {
                    pingWorker.Stop();                     //kill ping worker thread (should not throw exceptions)
                    port.Close();
                }
                catch (Exception ex)
                {
                    disconnectException = ex;
                    state = OTPDeviceState.Failed;
                    return(Task.FromResult(new OTPDeviceEventArgs()
                    {
                        State = OTPDeviceState.Failed, Error = ex
                    }));
                }
                finally
                {
                    pingWorker = null;
                    commHelper = null;
                }
                //state change
                state = OTPDeviceState.Disconnected;
                return(Task.FromResult(new OTPDeviceEventArgs()
                {
                    State = OTPDeviceState.Disconnected, Error = null
                }));
            });

            if (disconnectException != null)
            {
                throw disconnectException;
            }
        }
Esempio n. 3
0
 public void PingFailedCallback(Exception ex)
 {
     try
     {
         deviceEventCtrl.Raise(this, () =>
         {
             if (state == OTPDeviceState.Connected)
             {
                 state = OTPDeviceState.Failed;
                 return(new OTPDeviceEventArgs()
                 {
                     State = OTPDeviceState.Failed, Error = ex
                 });
             }
             throw new PingFailedCallbackException();
         });
     }
     catch (PingFailedCallbackException) { }
 }
 private static void Reset()
 {
     lastState     = OTPDeviceState.Disconnected;
     lastException = null;
 }
 private static void OnDeviceManagerEvent(object sender, OTPDeviceEventArgs args)
 {
     lastState     = args.State;
     lastException = args.Error;
 }