Example #1
0
        private async Task <bool> AuthenticateServiceAsync(ZWayConfiguration configuration)
        {
            var authParameters = new AuthenticationParameters()
            {
                login     = configuration.Username,
                password  = configuration.Password,
                remeberme = false
            };

            var request = new HttpRequestMessage()
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(_baseAdress + "login"),
                Content    = new JsonContent(authParameters)
            };

            var result = await _httpClient.SendAsync(request);

            if (result.IsSuccessStatusCode)
            {
                _authenticationCookie = result.Headers.GetValues("Set-Cookie")?.FirstOrDefault()?.Split(';')?.FirstOrDefault();
                return(!string.IsNullOrEmpty(_authenticationCookie));
            }

            return(false);
        }
Example #2
0
        public async Task <bool> InitAsync(ZWayConfiguration configuration)
        {
            try
            {
                _logger.LogInformation("Initiating ZWayService");

                _baseAdress = configuration.Adress.TrimEnd('/') + '/';

                await AuthenticateServiceAsync(configuration);
                await PopulateDevices(configuration.Devices);

                UpdateDevicesAsync();
                return(true);
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, "Failed to initiate ZWayService");
                throw e;
            }
        }