End Broker
This commit is contained in:
74
PlantBox.Broker/HistoricManager.cs
Normal file
74
PlantBox.Broker/HistoricManager.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace PlantBox.Broker
|
||||
{
|
||||
class HistoricManager
|
||||
{
|
||||
private List<CaptorsValue> _minutesHistoric;
|
||||
public IReadOnlyList<CaptorsValue> MinutesHistoric => _minutesHistoric.AsReadOnly();
|
||||
|
||||
private List<CaptorsValue> _hoursHistoric;
|
||||
public IReadOnlyList<CaptorsValue> HoursHistoric => _hoursHistoric.AsReadOnly();
|
||||
|
||||
private List<CaptorsValue> _daysHistoric;
|
||||
public IReadOnlyList<CaptorsValue> DaysHistoric => _daysHistoric.AsReadOnly();
|
||||
|
||||
private List<CaptorsValue> _weeksHistoric;
|
||||
public IReadOnlyList<CaptorsValue> WeeksHistoric => _weeksHistoric.AsReadOnly();
|
||||
|
||||
private List<CaptorsValue> _monthsHistoric;
|
||||
public IReadOnlyList<CaptorsValue> MonthsHistoric => _monthsHistoric.AsReadOnly();
|
||||
|
||||
public HistoricManager()
|
||||
{
|
||||
_minutesHistoric = new List<CaptorsValue>();
|
||||
_hoursHistoric = new List<CaptorsValue>();
|
||||
_daysHistoric = new List<CaptorsValue>();
|
||||
_weeksHistoric = new List<CaptorsValue>();
|
||||
_monthsHistoric = new List<CaptorsValue>();
|
||||
}
|
||||
public HistoricManager(List<CaptorsValue> minutesHistoric, List<CaptorsValue> hoursHistoric, List<CaptorsValue> daysHistoric, List<CaptorsValue> weeksHistoric, List<CaptorsValue> monthsHistoric)
|
||||
{
|
||||
_minutesHistoric = minutesHistoric;
|
||||
_hoursHistoric = hoursHistoric;
|
||||
_daysHistoric = daysHistoric;
|
||||
_weeksHistoric = weeksHistoric;
|
||||
_monthsHistoric = monthsHistoric;
|
||||
}
|
||||
|
||||
public void Add(CaptorsValue captorsValue)
|
||||
{
|
||||
_minutesHistoric.Add(captorsValue);
|
||||
|
||||
if (_minutesHistoric.Count % 12 == 0)
|
||||
{
|
||||
_hoursHistoric.Add(new CaptorsValue(GetAverage(_minutesHistoric.TakeFromEnd(12))));
|
||||
}
|
||||
if (_hoursHistoric.Count % 24 == 0)
|
||||
{
|
||||
_daysHistoric.Add(new CaptorsValue(GetAverage(_hoursHistoric.TakeFromEnd(24))));
|
||||
}
|
||||
if (_daysHistoric.Count % 7 == 0)
|
||||
{
|
||||
_weeksHistoric.Add(new CaptorsValue(GetAverage(_daysHistoric.TakeFromEnd(7))));
|
||||
}
|
||||
if (_daysHistoric.Count % 31 == 0)
|
||||
{
|
||||
_monthsHistoric.Add(new CaptorsValue(GetAverage(_daysHistoric.TakeFromEnd(31))));
|
||||
}
|
||||
}
|
||||
|
||||
private (double humidity, double luminosity, double temperature) GetAverage(IEnumerable<CaptorsValue> captorsValues)
|
||||
{
|
||||
return
|
||||
(
|
||||
captorsValues.Average(x => x.Humidity),
|
||||
captorsValues.Average(x => x.Luminosity),
|
||||
captorsValues.Average(x => x.Temperature)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user