Exemple #1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "climate/{hubDeviceId}/{command}")]
            HttpRequestMessage req,
            string hubDeviceId,
            string command,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            if (!await req.CheckAuthorization("api"))
            {
                return(req.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized"));
            }

            if (String.IsNullOrWhiteSpace(hubDeviceId))
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown device"));
            }

            if (String.IsNullOrWhiteSpace(command) || !_commands.Contains(command))
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown command"));
            }

            var request = new ControlClimateDataRequest()
            {
                Device = hubDeviceId, Command = command
            };
            var response = await IotHub.InvokeDeviceMethodAsync(hubDeviceId, MethodNames.ControlClimateData, request);

            return(req.CreateResponse((HttpStatusCode)response.Status, "Command sent: " + command, "application/json"));
        }
Exemple #2
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "devicestate/{hubDeviceId}/{targetDevice}")]
            HttpRequestMessage req,
            string hubDeviceId,
            string targetDevice,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            if (!await req.CheckAuthorization("api"))
            {
                return(req.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized"));
            }

            if (String.IsNullOrWhiteSpace(hubDeviceId))
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown device"));
            }
            var request = new GetDeviceStateRequest()
            {
                Device = targetDevice
            };

            var(result, response) = await IotHub.InvokeDeviceMethodAsync <GetDeviceStateRequest, GetDeviceStateResponse>(
                hubDeviceId,
                MethodNames.GetDeviceState,
                request);

            return(req.CreateResponse((HttpStatusCode)response.Status, result, ResultFormatter.GetFormatter()));
        }
Exemple #3
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "blinds/{hubDeviceId}/{targetDevice}/{command}")]
            HttpRequestMessage req,
            string hubDeviceId,
            string targetDevice, //REVIEW: Should we validate target devices?
            string command,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            if (!await req.CheckAuthorization("api"))
            {
                return(req.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized"));
            }

            if (String.IsNullOrWhiteSpace(hubDeviceId))
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown device"));
            }

            if (String.IsNullOrWhiteSpace(command) || !_commands.Contains(command))
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown command"));
            }

            var request = new ControlBlindsRequest()
            {
                Device = targetDevice, Command = command
            };

            var(result, response) = await IotHub.InvokeDeviceMethodAsync <ControlBlindsRequest, ControlBlindsResponse>(
                hubDeviceId,
                MethodNames.ControlBlinds,
                request);

            return(req.CreateResponse((HttpStatusCode)response.Status, result, ResultFormatter.GetFormatter()));
        }