Пример #1
0
        public void Reconnect()
        {
            try
            {
                Reconnecting?.Invoke(new DeviceEventArgs(this));
                _reconnecting = true;
                if (phx42 != null)
                {
                    try
                    {
                        phx42.Error                  -= Phx42OnError;
                        phx42.CommandError           -= Phx42OnCommandError;
                        phx42.WriteFlashProgress     -= Phx42OnWriteFlashProgress;
                        phx42.ReadFlashProgress      -= Phx42OnReadFlashProgress;
                        phx42.UpdateFirmwareProgress -= Phx42OnUpdateFirmwareProgress;

                        phx42.ShutdownNow = true;
                    }
                    catch (Exception ex)
                    {
                        phx42?.WriteToPhxLog("Problem disconnecting");
                        phx42?.WriteExceptionToPhxLog(ex);
                    }

                    IsConnected = false;
                    Status      = "Disconnected";
                }

                try
                {
                    var streams = _bluetoothService.Reconnect(_device);

                    phx42 = new Phx42(streams.InputStream,
                                      streams.OutputStream,
                                      _fileManager,
                                      _device.Name, TimeMongerService);

                    phx42.Error                  += Phx42OnError;
                    phx42.CommandError           += Phx42OnCommandError;
                    phx42.WriteFlashProgress     += Phx42OnWriteFlashProgress;
                    phx42.ReadFlashProgress      += Phx42OnReadFlashProgress;
                    phx42.UpdateFirmwareProgress += Phx42OnUpdateFirmwareProgress;

                    if (IsPollingData)
                    {
                        phx42.StartPollingData();
                    }

                    IsConnected = true;
                    Status      = "Connected";
                    ReconnectSuccess?.Invoke(new DeviceEventArgs(this));
                }
                catch (Exception ex)
                {
                    phx42?.WriteToPhxLog("Problem reconnecting");
                    phx42?.WriteExceptionToPhxLog(ex);
                    OnDisconnected(new DeviceEventArgs(this));
                    return;
                }
            }
            finally
            {
                _reconnecting = false;
            }
        }