Skip to content

Instantly share code, notes, and snippets.

@andreas-nesheim
Created February 5, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreas-nesheim/1acfc00673d292b57418504e457327d2 to your computer and use it in GitHub Desktop.
Save andreas-nesheim/1acfc00673d292b57418504e457327d2 to your computer and use it in GitHub Desktop.
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