Beispiel #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"{DateTime.Now} :: HTTP trigger function processing a \"Turn On Device\" request.");

            // Get parameters from GET request
            string deviceName = req.Query["device"];
            string senderName = req.Query["sender"];
            string condition  = req.Query["condition"];
            string skip       = req.Query["skip"];

            // duration will have the value of -1 if it is not specified.
            Int32.TryParse(req.Query["duration"], out int duration);

            // Parse request body for POST reqeust
            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            // Get parameters from POST request, if parameter is not valid in GET request.
            deviceName ??= data?.device;
            senderName ??= data?.sender;
            condition ??= data?.condition;
            skip ??= data?.skip;
            // duration will have the value of -1 if it is not specified.
            if (duration == -1)
            {
                Int32.TryParse(data?.duration, out duration);
            }

            log.LogInformation($"Device: {deviceName}, Sender: {senderName}, Duration: {duration}");
            string responseMessage = "";

            if (deviceName != null)
            {
                switch (condition)
                {
                // Turn on the device during daytime only.
                case "day":
                    if (Leo.AtNight(log))
                    {
                        responseMessage = $"It is nighttime now. {deviceName} not turned on.";
                        log.LogInformation(responseMessage);
                        return(new OkObjectResult($"It is nighttime now. {deviceName} not turned on."));
                    }
                    break;

                // Turn on the device during nighttime only.
                case "night":
                    if (!Leo.AtNight(log))
                    {
                        responseMessage = $"It is daytime now. {deviceName} not turned on.";
                        log.LogInformation(responseMessage);
                        return(new OkObjectResult(responseMessage));
                    }
                    break;
                }

                // Turn on the device only if skip is null (not found in the parameters).
                if (skip == null)
                {
                    // Turn on the device
                    TurnOnDeviceByHttpRequest(log, deviceName);
                    responseMessage += $"Turned on {deviceName}. ";
                }

                // Schedule the device to be turned off
                if (duration > 0)
                {
                    await ScheduleOff(log, senderName, deviceName, duration);

                    responseMessage += $"Scheduled {deviceName} to be turned off in {duration} seconds.";
                }
                // Return Response
                log.LogInformation(responseMessage);
                return(new OkObjectResult(responseMessage));
            }

            // Return bad request if the device parameter is null
            log.LogError("Parameter \"device\" not found on the query string or in the request body.");
            return(new BadRequestObjectResult("Please pass a \"device\" parameter on the query string or in the request body."));
        }