Exemple #1
0
        public async Task ShutdownRemoteDeviceAsync(HideezErrorCode code)
        {
            try
            {
                if (_remoteDevice != null)
                {
                    CancelRemoteDeviceCreation();
                    CancelDeviceAuthorization();

                    var tempRemoteDevice = _remoteDevice;
                    _remoteDevice   = null;
                    PasswordManager = null;

                    if (tempRemoteDevice != null)
                    {
                        tempRemoteDevice.ButtonPressed   -= RemoteDevice_ButtonPressed;
                        tempRemoteDevice.StorageModified -= RemoteDevice_StorageModified;
                        tempRemoteDevice.PropertyChanged -= RemoteDevice_PropertyChanged;
                        await tempRemoteDevice.Shutdown(code);

                        await _metaMessenger.PublishOnServer(new RemoveDeviceMessage(tempRemoteDevice.Id));

                        await tempRemoteDevice.DeleteContext();

                        tempRemoteDevice.Dispose();
                    }
                    try
                    {
                        await _remoteDeviceMessenger.DisconnectFromServer();
                    }
                    catch (InvalidOperationException)
                    {
                        // IMetaPubSub may throw InvalidOperationException if we try to disconnect without first connecting
                    }
                }
            }
            catch (Exception ex)
            {
                _log.WriteLine(ex);
            }

            IsCreatingRemoteDevice    = false;
            IsAuthorizingRemoteDevice = false;
            IsLoadingStorage          = false;
            IsStorageLoaded           = false;

            NotifyPropertyChanged(nameof(IsAuthorized));
        }
        public async Task EnumerateDevices_FluctuatingServiceConnection_DevicesEnumerated()
        {
            var         devices   = new List <DeviceDTO>();
            IMetaPubSub messenger = GetMessenger();
            IMetaPubSub hub       = new MetaPubSub();

            hub.StartServer("Test1");

            int devicesCount = 100;

            for (int i = 0; i < devicesCount; i++)
            {
                devices.Add(GetRandomDeviceDTO());
            }

            int serviceReconnectsCount = 10;

            for (int i = 0; i < serviceReconnectsCount; i++)
            {
                await messenger.TryConnectToServer("Test1");

                IDeviceManager deviceManager = GetDeviceManager(messenger, devices);
                await Task.Delay(200);

                Assert.AreEqual(devicesCount, deviceManager.Devices.Count());

                await messenger.DisconnectFromServer();

                await Task.Delay(200);

                Assert.AreEqual(0, deviceManager.Devices.Count());
            }
        }
        public async Task EnumerateDevices_ClearDevices_DevicesCollectionCleared()
        {
            var         devices   = new List <DeviceDTO>();
            IMetaPubSub messenger = GetMessenger();
            IMetaPubSub hub       = new MetaPubSub();

            hub.StartServer("Test2");

            await messenger.TryConnectToServer("Test2");

            IDeviceManager deviceManager = GetDeviceManager(messenger);

            int devicesCount = 1000;

            for (int i = 0; i < devicesCount; i++)
            {
                devices.Add(GetRandomDeviceDTO());
            }

            await messenger.PublishOnServer(new DevicesCollectionChangedMessage(devices.ToArray()));

            await messenger.DisconnectFromServer();

            await Task.Delay(200);

            Assert.AreEqual(0, deviceManager.Devices.Count());
        }