Exemplo n.º 1
0
        public void Connect(string application = "", string user = "", string site = "")
        {
            if (IsConnected)
            {
                return;
            }

            var streams = _bluetoothService.Connect(_device);

            Unpaired = false;

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

            IsConnected = true;

            phx42.Error                  += Phx42OnError;
            phx42.CommandError           += Phx42OnCommandError;
            phx42.WriteFlashProgress     += Phx42OnWriteFlashProgress;
            phx42.ReadFlashProgress      += Phx42OnReadFlashProgress;
            phx42.UpdateFirmwareProgress += Phx42OnUpdateFirmwareProgress;
            phx42.GetLogsProgress        += Phx42OnGetLogsProgress;
            phx42.DataPolled             += Phx42OnDataPolled;
            Status = "Connected";
            OnConnected(new DeviceEventArgs(this));
        }
Exemplo n.º 2
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;
            }
        }