private IEnumerable<Device> GetFifthplayDevices(ConnectDeviceRequest request)
        {
            var energyDevices = _fifthplayRepository.GetEnergyDevices(request.Username, request.Password);
            var thermostats = _fifthplayRepository.GetThermostats(request.Username, request.Password);
            var devices = _fifthplayRepository.GetDevices(request.Username, request.Password);

            Task.WaitAll(energyDevices, thermostats, devices);

            return energyDevices.Result
                .Union(thermostats.Result)
                .Union(devices.Result);
        }
        private async Task ConnectDevices()
        {
            await DispatcherHelper.RunAsync(() => IsLoading = true);

            var request = new ConnectDeviceRequest { Username = Email, Password = Password };

            try
            {
                await _deviceConnector.Connect(request);
            }
            finally
            {
                await DispatcherHelper.RunAsync(() => IsLoading = false);
            }
        }
Exemplo n.º 3
0
        protected override void Arrange()
        {
            CredentialsRepository = new MockCredentialRepository();
            DeviceRepository = new MockDeviceRepository();
            FifthplayRepository = new MockFifthplayRepository();

            Service = new FifthplayDeviceConnector(
                FifthplayRepository, CredentialsRepository, DeviceRepository);

            Request = new ConnectDeviceRequest
            {
                Username = "******",
                Password = "******"
            };
        }
        public Task Connect(ConnectDeviceRequest request)
        {
            if (request == null) throw new ArgumentNullException(nameof(request));
            if (string.IsNullOrWhiteSpace(request.Username)) throw new ArgumentException("Username is required", nameof(request));
            if (string.IsNullOrWhiteSpace(request.Password)) throw new ArgumentException("Password is required", nameof(request));

            var fifthplayDevices = GetFifthplayDevices(request);

            _credentialsRepository.SaveFifthplay(request.Username, request.Password);

            var knownFifthplayDevices = _deviceRepository.GetAll().Where(d => d.Type == DeviceType.Fifthplay);
            var devicesToAdd = fifthplayDevices.Where(d => !knownFifthplayDevices.Any(k => k.Identifier == d.Identifier));
            var devicesToUpdate = fifthplayDevices.Where(d => knownFifthplayDevices.Any(k => k.Identifier == d.Identifier));
            var devicesToDelete = knownFifthplayDevices.Where(d => !fifthplayDevices.Any(f => f.Identifier == d.Identifier));

            _deviceRepository.Add(devicesToAdd);
            _deviceRepository.Update(devicesToUpdate);
            _deviceRepository.Delete(devicesToDelete);

            return Task.FromResult(true);
        }