public async Task SetLightStateTest()
        {
            const int LIGHT_ID = 4;

            IHueRequest request  = HueRequestBuilder.Select.Light(LIGHT_ID).Build();
            var         response = await _client.GetResponseAsync(request);

            Assert.True(response is IHueStatusMessage);

            var builder = HueRequestBuilder.Modify.Light(LIGHT_ID);

            if (((IHueStatusMessage)response).Status.IsOn)
            {
                builder.During(TimeSpan.FromSeconds(10)).TurnOff();
            }
            else
            {
                builder
                .TurnOn()
                .Color(46920, 254, 254)
                .ColorLoop();
            }

            request = builder.Build();

            response = await _client.GetResponseAsync(request);

            Assert.True(response is IHueStatusMessage);
        }
Exemple #2
0
 public Command(IHueRequest request) : base(request.Address, request.Method)
 {
     if (!(request is IHueStatusMessage statusRequest))
     {
         throw new InvalidOperationException("Request base for a command must implement IHueStatusMessage!");
     }
     Body = statusRequest.Status;
 }
        public async Task GetAllLightsTest()
        {
            IHueRequest request = HueRequestBuilder.Select.Lights().Build();

            var response = await _client.GetResponseAsync(request);

            Assert.True(response is ICollection <Light>);

            OnLog(response);
        }
        public async Task CreateErrorObjectTest()
        {
            IHueRequest initialRequest  = HueRequestBuilder.Select.Lights().Build();
            var         initialResponse = await _client.GetResponseAsync(initialRequest);

            var lights = initialResponse as ICollection <Light>;

            Assert.NotNull(lights);

            var id = lights.First(p => !p.Status.IsOn).Id;

            var request = HueRequestBuilder.Modify.Light(id).Increase.Brightness.By(100).Build();

            await Assert.ThrowsAsync <HueResponseException>(() => _client.GetResponseAsync(request));
        }
Exemple #5
0
        public async Task <IHueResponse> GetResponseAsync(IHueRequest hueRequest)
        {
            if (hueRequest is IContainsCommand setsCommand)
            {
                setsCommand.Command.CompleteAddress = $"/api/{User}/{setsCommand.Command.Address}";
            }

            var uri = GetRequestUri(hueRequest.Address);

            LogTrace(@"Preparing {method}-request to ""{uri}"".", hueRequest.Method.Method, uri);
            var requestMessage = new HttpRequestMessage(hueRequest.Method, uri);


            if (hueRequest is IUploadable uploadableRequest)
            {
                var requestContent = uploadableRequest.GetRequestBody();
                LogTrace($"Preparing json to send: {Environment.NewLine}{requestContent}");
                requestMessage.Content = new StringContent(requestContent, Encoding.UTF8);
            }
            else
            {
                requestMessage.Content = new StringContent(string.Empty);
            }

            using (var client = GetClient())
                using (var response = await client.SendAsync(requestMessage))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var responseString = await response.Content.ReadAsStringAsync();

                        LogTrace(responseString);
                        return(hueRequest.GetResponse(responseString));
                    }

                    LogError($"Error getting response from hub, HttpStatus {response.StatusCode}");
                    LogDebug(await response.Content.ReadAsStringAsync());
                    throw new HttpRequestException();
                }
        }
 public ConditionExpressionVisitor(IHueRequest request)
 {
     _request = request;
 }