-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherReading.cs
44 lines (38 loc) · 1.29 KB
/
WeatherReading.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
using System;
using System.Collections.Concurrent;
namespace Wicher_mono
{
public class WeatherReading
{
/**
* The text that is used on the webpage when the value for a given measurement is not available.
*/
public const string ReadingUnavailable = "-";
private ConcurrentDictionary<string, ReadingItem> WeatherReadings = new ConcurrentDictionary<string, ReadingItem>();
public string this[string key]
{
get => WeatherReadings[key].Value;
}
public void AddOrUpdate(string key, string value)
{
if (value == ReadingUnavailable &&
WeatherReadings.ContainsKey(key) &&
WeatherReadings[key].Timestamp.Subtract(TimeSpan.FromMinutes(15)) <= DateTime.UtcNow)
{
return;
}
var readingItem = new ReadingItem(value, DateTime.UtcNow);
WeatherReadings.AddOrUpdate(key, readingItem, (_, __) => readingItem);
}
private class ReadingItem
{
public string Value;
public DateTime Timestamp;
public ReadingItem(string value, DateTime timestamp)
{
Value = value;
Timestamp = timestamp;
}
}
}
}