mirror of
https://github.com/ckaczor/WeatherService.git
synced 2026-01-13 17:23:11 -05:00
- Stop storing history in memory
- Add start/end dates to history requests - Cleanup
This commit is contained in:
@@ -5,12 +5,30 @@ using System.Linq;
|
||||
using WeatherService.Data;
|
||||
using WeatherService.Values;
|
||||
using WeatherService.Devices;
|
||||
using WeatherService.Reporting;
|
||||
|
||||
namespace WeatherService.Remote
|
||||
{
|
||||
internal static class WeatherServiceCommon
|
||||
{
|
||||
private static List<ReadingBase> LoadHistory(WeatherValueType valueType, int deviceId, DateTimeOffset start, DateTimeOffset end)
|
||||
{
|
||||
var history = new List<ReadingBase>();
|
||||
|
||||
for (var year = start.Year; year <= end.Year; year++)
|
||||
{
|
||||
using (var archiveData = new WeatherArchiveData(year))
|
||||
{
|
||||
var readings = archiveData.Readings;
|
||||
|
||||
var yearlyHistory = readings.Where(r => r.DeviceId == deviceId && r.Type == (int) valueType && r.ReadTime >= start && r.ReadTime <= end).ToList();
|
||||
|
||||
history.AddRange(yearlyHistory.Select(r => ReadingBase.CreateReading(valueType, r.ReadTime.DateTime, r.Value)));
|
||||
}
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
public static List<DeviceBase> GetDevices()
|
||||
{
|
||||
var deviceList = Program.Session.Devices.ToList();
|
||||
@@ -18,48 +36,21 @@ namespace WeatherService.Remote
|
||||
return deviceList;
|
||||
}
|
||||
|
||||
public static ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType)
|
||||
{
|
||||
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
|
||||
|
||||
if (deviceBase == null)
|
||||
return null;
|
||||
|
||||
return deviceBase.GetValue(valueType).Current;
|
||||
}
|
||||
|
||||
public static DeviceHistory GetDeviceHistory(string deviceAddress)
|
||||
{
|
||||
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
|
||||
|
||||
if (deviceBase == null)
|
||||
return null;
|
||||
|
||||
var deviceHistory = new DeviceHistory();
|
||||
|
||||
foreach (var valueEntry in deviceBase.Values)
|
||||
{
|
||||
deviceHistory[valueEntry.Key] = valueEntry.Value.History;
|
||||
}
|
||||
|
||||
return deviceHistory;
|
||||
}
|
||||
|
||||
public static Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType)
|
||||
public static Dictionary<DeviceBase, List<ReadingBase>> GetGenericHistory(WeatherValueType valueType, DateTimeOffset start, DateTimeOffset end)
|
||||
{
|
||||
var devices = Program.Session.Devices.Where(d => d.SupportedValues.Contains(valueType));
|
||||
|
||||
var deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
|
||||
var deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
|
||||
|
||||
foreach (var device in devices)
|
||||
{
|
||||
deviceHistoryList[device] = device.GetValue(valueType).History;
|
||||
deviceHistoryList[device] = LoadHistory(valueType, device.Id, start, end);
|
||||
}
|
||||
|
||||
return deviceHistoryList;
|
||||
}
|
||||
|
||||
public static Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes)
|
||||
public static Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes, DateTimeOffset start, DateTimeOffset end)
|
||||
{
|
||||
var windSpeedHistory = new Dictionary<string, List<WindSpeedReading>>();
|
||||
|
||||
@@ -68,7 +59,7 @@ namespace WeatherService.Remote
|
||||
if (device == null)
|
||||
return null;
|
||||
|
||||
var values = device.GetValue(WeatherValueType.WindSpeed).History;
|
||||
var values = LoadHistory(WeatherValueType.WindSpeed, device.Id, start, end);
|
||||
|
||||
var interval = new TimeSpan(0, groupIntervalMinutes, 0);
|
||||
|
||||
@@ -78,21 +69,23 @@ namespace WeatherService.Remote
|
||||
Readings = d.Select(r => r.Value)
|
||||
}).ToList();
|
||||
|
||||
windSpeedHistory["Average"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Average() }).ToList();
|
||||
windSpeedHistory["Minimum"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Min() }).ToList();
|
||||
windSpeedHistory["Maximum"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Max() }).ToList();
|
||||
windSpeedHistory["Average"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Average() }).ToList();
|
||||
windSpeedHistory["Minimum"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Min() }).ToList();
|
||||
windSpeedHistory["Maximum"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Max() }).ToList();
|
||||
|
||||
return windSpeedHistory;
|
||||
}
|
||||
|
||||
public static Dictionary<string, int> GetWindDirectionHistory()
|
||||
public static Dictionary<string, int> GetWindDirectionHistory(DateTimeOffset start, DateTimeOffset end)
|
||||
{
|
||||
var device = Program.Session.Devices.FirstOrDefault(d => d.SupportedValues.Contains(WeatherValueType.WindDirection));
|
||||
|
||||
if (device == null)
|
||||
return null;
|
||||
|
||||
var history = device.GetValue(WeatherValueType.WindDirection).History
|
||||
var values = LoadHistory(WeatherValueType.WindDirection, device.Id, start, end);
|
||||
|
||||
var history = values
|
||||
.Cast<WindDirectionReading>()
|
||||
.Where(r => r.WindDirectionValue != WindDirection.Unknown)
|
||||
.OrderBy(r => r.Value);
|
||||
@@ -104,31 +97,33 @@ namespace WeatherService.Remote
|
||||
return grouped;
|
||||
}
|
||||
|
||||
public static List<DailySummary> GetDailySummary(int deviceId, int valueType, DateTime startDate, DateTime endDate)
|
||||
public static Dictionary<string, List<ReadingBase>> GetDailySummary(WeatherValueType valueType, int deviceId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var summaryList = new List<DailySummary>();
|
||||
var summaryList = new Dictionary<string, List<ReadingBase>>();
|
||||
|
||||
summaryList["Average"] = new List<ReadingBase>();
|
||||
summaryList["Minimum"] = new List<ReadingBase>();
|
||||
summaryList["Maximum"] = new List<ReadingBase>();
|
||||
|
||||
for (var year = startDate.Year; year <= endDate.Year; year++)
|
||||
{
|
||||
using (var archiveData = new WeatherArchiveData(year))
|
||||
{
|
||||
var groupedReadings = archiveData.Readings
|
||||
var groupList = archiveData.Readings
|
||||
.Where(r => r.ReadTime >= startDate &&
|
||||
r.ReadTime <= endDate &&
|
||||
r.DeviceId == deviceId &&
|
||||
r.Type == valueType)
|
||||
r.Type == (int) valueType)
|
||||
.GroupBy(r => DbFunctions.TruncateTime(r.ReadTime))
|
||||
.Select(r => new DailySummary
|
||||
.Select(g => new
|
||||
{
|
||||
Date = r.Key,
|
||||
Count = r.Count(),
|
||||
Minimum = r.Min(v => v.Value),
|
||||
Maximum = r.Max(v => v.Value),
|
||||
Average = r.Average(v => v.Value)
|
||||
})
|
||||
.OrderBy(d => d.Date);
|
||||
ReadTime = g.Key,
|
||||
Readings = g.Select(r => r.Value)
|
||||
}).ToList();
|
||||
|
||||
summaryList.AddRange(groupedReadings);
|
||||
summaryList["Average"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Average())));
|
||||
summaryList["Minimum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Min())));
|
||||
summaryList["Maximum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Max())));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user