-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathAPI.cs
More file actions
115 lines (97 loc) · 3.67 KB
/
Copy pathAPI.cs
File metadata and controls
115 lines (97 loc) · 3.67 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
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; }
/// <summary>
/// History of light intensity measurements
/// </summary>
public List<KeyValuePair<DateTime, int>> LightIntensity { get; set; }
}
public class LightMeasuredEvent
{
/// <summary>
/// Id of the streetlight.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Light intensity measured in lumens.
/// </summary>
public int Lumens { get; set; }
/// <summary>
/// Light intensity measured in lumens.
/// </summary>
public DateTime SentAt { get; set; }
}
[AsyncApi]
[ApiController]
[Route("")]
public class StreetlightsController
{
private const string PublishLightMeasuredTopic = "publish/light/measured";
// 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 }, LightIntensity = new() },
};
private readonly IStreetlightMessageBus _streetlightMessageBus;
private readonly ILogger _logger;
public StreetlightsController(IStreetlightMessageBus streetlightMessageBus, ILoggerFactory loggerFactory)
{
_streetlightMessageBus = streetlightMessageBus;
_logger = loggerFactory.CreateLogger<StreetlightsController>();
}
/// <summary>
/// Get all streetlights
/// </summary>
[HttpGet]
[Route("api/streetlights")]
public IEnumerable<Streetlight> Get() => StreetlightDatabase;
/// <summary>
/// Add a new streetlight
/// </summary>
[HttpPost]
[Route("api/streetlights")]
public Streetlight Add([FromBody] Streetlight streetlight)
{
streetlight.Id = StreetlightSeq++;
StreetlightDatabase.Add(streetlight);
return streetlight;
}
/// <summary>
/// Inform about environmental lighting conditions for a particular streetlight.
/// </summary>
[Channel(PublishLightMeasuredTopic, Servers = new []{"webapi"}, XParams = new[] { "key1=value1", "key2=value2" })]
[PublishOperation(typeof(LightMeasuredEvent), "Light")]
[HttpPost]
[Route(PublishLightMeasuredTopic)]
public void MeasureLight([FromBody] LightMeasuredEvent lightMeasuredEvent)
{
lightMeasuredEvent.SentAt = DateTime.Now;
var payload = JsonSerializer.Serialize(lightMeasuredEvent);
_logger.LogInformation("Received message on {Topic} with payload {Payload} ", PublishLightMeasuredTopic, payload);
var streetlight = StreetlightDatabase.SingleOrDefault(s => s.Id == lightMeasuredEvent.Id);
if (streetlight != null)
{
streetlight.LightIntensity.Add(new(lightMeasuredEvent.SentAt, lightMeasuredEvent.Lumens));
// Re-publish messages we receive
_streetlightMessageBus.PublishLightMeasurement(lightMeasuredEvent);
}
}
}
}