Created
February 5, 2020 18:43
-
-
Save andreas-nesheim/1acfc00673d292b57418504e457327d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Extensions.Logging; | |
using Millheat.Api; | |
using System.Threading.Tasks; | |
namespace Millheat.Functions | |
{ | |
public static class SetTemperatureFunction | |
{ | |
[FunctionName("SetTemperatureFunction")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, | |
ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
string temperatureStr = req.Query["temperature"]; | |
if (int.TryParse(temperatureStr, out int temp)) | |
{ | |
var millheatClient = new MillheatClient(); | |
var authorizationCode = await millheatClient.GetAuthorizationCode(); | |
var accessToken = await millheatClient.GetAccessToken(authorizationCode); | |
var home = await millheatClient.GetHome(accessToken); | |
var room = await millheatClient.GetRoom(home); | |
var device = await millheatClient.GetDevice(room); | |
await millheatClient.SetDevice(device, temp, 1, 1); | |
return new OkObjectResult($"Heat has been set to {temp} degrees Celcius."); | |
} | |
return new BadRequestObjectResult("Please pass a temperature on the query string or in the request body"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment