forked from asyncapi/saunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.cs
72 lines (63 loc) · 2.14 KB
/
API.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Saunter.Attributes;
namespace StreetlightsAPI
{
public class Streetlight
{
/// <summary>
/// Id of the streetlight.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Lat-Long coordinates of the streetlight.
/// </summary>
public double[] Position { get; set; }
}
[ApiController]
[Route("api/streetlights")]
public class StreetlightsController
{
// Simulate a database of streetlights
private static int StreetlightSeq = 2;
private static readonly List<Streetlight> StreetlightDatabase = new List<Streetlight>
{
new Streetlight { Id = 1, Position = new [] { -36.320320, 175.485986 } },
};
private readonly IStreetlightMessageBus _streetlightMessageBus;
public StreetlightsController(IStreetlightMessageBus streetlightMessageBus)
{
_streetlightMessageBus = streetlightMessageBus;
}
/// <summary>
/// Get all streetlights
/// </summary>
[HttpGet]
public IEnumerable<Streetlight> Get() => StreetlightDatabase;
/// <summary>
/// Add a new streetlight
/// </summary>
[HttpPost]
public Streetlight Add([FromBody] Streetlight streetlight)
{
streetlight.Id = StreetlightSeq++;
StreetlightDatabase.Add(streetlight);
return streetlight;
}
/// <summary>
/// Measure environmental lighting conditions for a particular streetlight.
/// </summary>
[HttpPost]
[Route("{id}/measure-light")]
public void MeasureLight([FromRoute] int id)
{
var streetlight = StreetlightDatabase.Single(s => s.Id == id);
var lumens = new Random().Next(0, 3000); // Simulate "measuring" the light intensity
_streetlightMessageBus.PublishLightMeasuredEvent(streetlight, lumens);
}
}
}