-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
54 lines (40 loc) · 1.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using flaas;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting.WindowsServices;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddWindowsService();
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
var app = builder.Build();
var sensor = FanLightController.CreateFanLightController(app.Configuration["SensorName"]);
app.MapGet("/", () => Results.Ok(sensor.Get()));
app.MapPost("/", ([FromBody] State state) =>
{
sensor.Set(state);
return Results.Accepted();
});
app.MapPost("/on", () =>
{
sensor.On();
return Results.Accepted();
});
app.MapPost("/off", () =>
{
sensor.Off();
return Results.Accepted();
});
app.MapPost("/brightness", ([FromBody] State level) =>
{
sensor.SetBrightness(level.Brightness);
return Results.Accepted();
});
await app.RunAsync("http://*:5112");
// Our custom JSON serializer context that generates code to serialize
// arrays of planets so that we can use it with our HTTP endpoint.
[JsonSerializable(typeof(State[]))]
public partial class AppJsonSerializerContext : JsonSerializerContext
{
}