public async Task<IEnumerable<Device>> GetEnergyDevices(string username, string password)
        {
            var request = new ApiClientRequest
            {
                Username = username,
                Password = password,
                BaseAddress = ApiBaseAddress,
                RequestUri = "EnergyService.svc",
                Content = CreateRequestContent(GetEnergyDevicesRequest, username, password)
            };

            var response = await _soapApiClient.Request(request);
            var responseAsXml = XDocument.Parse(response);

            return responseAsXml
                .Descendants(EnergyNamespace + "EnergyDeviceDataContract")
                .Select(d => new Device
                {
                    Identifier = new Guid(d.Descendants(EnergyNamespace + "EnergyDeviceId").Single().Value),
                    Name = d.Descendants(EnergyNamespace + "Name").Single().Value,
                    Capability = bool.Parse(d.Descendants(EnergyNamespace + "IsSwitch").Single().Value)
                                ? DeviceCapability.Switchable
                                : DeviceCapability.None,
                    Type = DeviceType.Fifthplay
                });
        }
Esempio n. 2
0
        public Task<string> Request(ApiClientRequest 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));
            if (string.IsNullOrWhiteSpace(request.BaseAddress)) throw new ArgumentException("BaseAddress is required", nameof(request));
            if (string.IsNullOrWhiteSpace(request.RequestUri)) throw new ArgumentException("RequestUri is required", nameof(request));
            if (string.IsNullOrWhiteSpace(request.Content)) throw new ArgumentException("Content is required", nameof(request));

            using (var httpClient = CreateHttpClient(request.Username, request.Password, request.BaseAddress))
            using (var content = new StringContent(request.Content, Encoding.UTF8, "application/soap+xml"))
            {
                var response = httpClient.PostAsync(request.RequestUri, content).Result;

                if (!response.IsSuccessStatusCode)
                {
                    var error = response.Content.ReadAsStringAsync().Result;

                    throw new InvalidOperationException($"{error} ({response.StatusCode} {response.ReasonPhrase})");
                }

                return response
                   .Content
                   .ReadAsStringAsync();
            }
        }
        public async Task SwitchSmartPlug(string username, string password, Guid identifier, bool isOn)
        {
            var request = new ApiClientRequest
            {
                Username = username,
                Password = password,
                BaseAddress = ApiBaseAddress,
                RequestUri = "EnergyService.svc",
                Content = CreateRequestContent(SwitchSmartPlugRequest, username, password)
                    .Replace("{smartplugid}", identifier.ToString())
                    .Replace("{value}", isOn.ToString().ToLowerInvariant())
            };

            var response = await _soapApiClient.Request(request);
            if (response.Contains("Fault"))
                throw new Exception("Failed to switch smart plug");
        }