From ab85e78f4c932c5de5e424ccbcd7006cb9629170 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Wed, 24 Apr 2019 23:14:10 +0200 Subject: [PATCH] Update Broker --- PlantBox.Broker/Broker.cs | 4 +++- PlantBox.Broker/CaptorsValue.cs | 4 ++++ PlantBox.Broker/ClientManager.cs | 17 ++++++++++++++--- PlantBox.Broker/HistoricManager.cs | 13 ++++++++++++- PlantBox.Broker/PlantBox.cs | 27 ++++++++++++++++++++++++++- PlantBox.Broker/PlantBoxesManager.cs | 21 +++++++++++++++++++++ PlantBox.Broker/ServerManager.cs | 8 +++++++- 7 files changed, 87 insertions(+), 7 deletions(-) diff --git a/PlantBox.Broker/Broker.cs b/PlantBox.Broker/Broker.cs index f0187c5..e1db821 100644 --- a/PlantBox.Broker/Broker.cs +++ b/PlantBox.Broker/Broker.cs @@ -11,7 +11,7 @@ namespace PlantBox.Broker public ClientManager ClientManager { get; } public ServerManager ServerManager { get; } - public bool IsRunning { get; set; } + public bool IsRunning { get; set; } = true; public Broker(string[] args) { @@ -20,6 +20,7 @@ namespace PlantBox.Broker PlantBoxesManager = new PlantBoxesManager(); PlantBoxesManager.Load(); + PlantBoxesManager.UpdatePlantsState(); ClientManager = new ClientManager(this); ServerManager = new ServerManager(this); @@ -43,6 +44,7 @@ namespace PlantBox.Broker Console.WriteLine("Stopping Broker..."); + IsRunning = false; PlantBoxesManager.Save(); } } diff --git a/PlantBox.Broker/CaptorsValue.cs b/PlantBox.Broker/CaptorsValue.cs index 754e299..1225bbf 100644 --- a/PlantBox.Broker/CaptorsValue.cs +++ b/PlantBox.Broker/CaptorsValue.cs @@ -10,6 +10,10 @@ namespace PlantBox.Broker public double Luminosity { get; set; } public double Temperature { get; set; } + public CaptorsValue() + { + + } public CaptorsValue(double humidity, double luminosity, double temperature) { Humidity = humidity; diff --git a/PlantBox.Broker/ClientManager.cs b/PlantBox.Broker/ClientManager.cs index 58e292e..60d51ec 100644 --- a/PlantBox.Broker/ClientManager.cs +++ b/PlantBox.Broker/ClientManager.cs @@ -34,9 +34,20 @@ namespace PlantBox.Broker protected override void HistoricCommand(CommandStream commandStream, CommandPacket packet) { + double[] FillArray(double[] values, int length) + { + var array = new double[length]; + int startIndex = length - values.Length; + + values.CopyTo(array, startIndex); + + return array; + } + HistoricRequest historicRequest = new HistoricRequest().Deserialize(packet.Arguments); ulong id = packet.ID; PlantBox plantBox = Broker.PlantBoxesManager[id]; + int count = historicRequest.Number; if (plantBox == null) { @@ -70,9 +81,9 @@ namespace PlantBox.Broker var response = new HistoricResponse ( DateTime.Now - plantBox.LastMeasureDate, - captorsValues.Select(x => x.Humidity).ToArray(), - captorsValues.Select(x => x.Luminosity).ToArray(), - captorsValues.Select(x => x.Temperature).ToArray() + FillArray(captorsValues.Select(x => x.Humidity).ToArray(), count), + FillArray(captorsValues.Select(x => x.Luminosity).ToArray(), count), + FillArray(captorsValues.Select(x => x.Temperature).ToArray(), count) ); commandStream.Send(response.ToCommandPacket(id)); } diff --git a/PlantBox.Broker/HistoricManager.cs b/PlantBox.Broker/HistoricManager.cs index 36b287a..4c607b3 100644 --- a/PlantBox.Broker/HistoricManager.cs +++ b/PlantBox.Broker/HistoricManager.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,19 +8,29 @@ namespace PlantBox.Broker { class HistoricManager { + [JsonProperty(PropertyName = nameof(MinutesHistoric))] private List _minutesHistoric; + [JsonIgnore] public IReadOnlyList MinutesHistoric => _minutesHistoric.AsReadOnly(); + [JsonProperty(PropertyName = nameof(HoursHistoric))] private List _hoursHistoric; + [JsonIgnore] public IReadOnlyList HoursHistoric => _hoursHistoric.AsReadOnly(); + [JsonProperty(PropertyName = nameof(DaysHistoric))] private List _daysHistoric; + [JsonIgnore] public IReadOnlyList DaysHistoric => _daysHistoric.AsReadOnly(); + [JsonProperty(PropertyName = nameof(WeeksHistoric))] private List _weeksHistoric; + [JsonIgnore] public IReadOnlyList WeeksHistoric => _weeksHistoric.AsReadOnly(); + [JsonProperty(PropertyName = nameof(MonthsHistoric))] private List _monthsHistoric; + [JsonIgnore] public IReadOnlyList MonthsHistoric => _monthsHistoric.AsReadOnly(); public HistoricManager() diff --git a/PlantBox.Broker/PlantBox.cs b/PlantBox.Broker/PlantBox.cs index 9ea1474..a06a401 100644 --- a/PlantBox.Broker/PlantBox.cs +++ b/PlantBox.Broker/PlantBox.cs @@ -8,7 +8,7 @@ namespace PlantBox.Broker class PlantBox { // General Info - public ulong ID { get; } + public ulong ID { get; set; } public string Name { get; set; } public PlantType Type { get; set; } public PlantState State { get; set; } @@ -22,5 +22,30 @@ namespace PlantBox.Broker // Historic public HistoricManager HistoricManager { get; set; } + + // State conditions + public void UpdateState() + { + if (Humidity.Value < Humidity.Min || Humidity.Value > Humidity.Max) + { + State = PlantState.Warning; + } + if (Luminosity.Value < Luminosity.Min || Luminosity.Value > Luminosity.Max) + { + State = PlantState.Warning; + } + if (Temperature.Value < Temperature.Min || Temperature.Value > Temperature.Max) + { + State = PlantState.Warning; + } + if (TankLevel < 20) + { + State = PlantState.Warning; + } + if ((DateTime.Now - LastMeasureDate).TotalMinutes > 6) + { + State = PlantState.Bad; + } + } } } diff --git a/PlantBox.Broker/PlantBoxesManager.cs b/PlantBox.Broker/PlantBoxesManager.cs index 7156a74..70d9c55 100644 --- a/PlantBox.Broker/PlantBoxesManager.cs +++ b/PlantBox.Broker/PlantBoxesManager.cs @@ -25,6 +25,27 @@ namespace PlantBox.Broker public PlantBox GetPlantBox(ulong id) => this[id]; + public void Add(PlantBox plantBox) + { + ulong id = plantBox.ID; + if (_plantBoxes.ContainsKey(id)) + { + _plantBoxes[id] = plantBox; + } + else + { + _plantBoxes.Add(id, plantBox); + } + } + + public void UpdatePlantsState() + { + foreach (PlantBox plantBox in _plantBoxes.Values) + { + plantBox.UpdateState(); + } + } + public void Load() { Console.WriteLine("Loading storage..."); diff --git a/PlantBox.Broker/ServerManager.cs b/PlantBox.Broker/ServerManager.cs index 0abcd3c..9859397 100644 --- a/PlantBox.Broker/ServerManager.cs +++ b/PlantBox.Broker/ServerManager.cs @@ -32,13 +32,17 @@ namespace PlantBox.Broker HistoricManager = new HistoricManager() }; } - + plantBox.ID = id; plantBox.Name = infoResponse.Name; plantBox.Type = infoResponse.Type; plantBox.State = infoResponse.State; plantBox.Humidity = new CaptorValue(infoResponse.HumidityMin, infoResponse.HumidityMax, plantBox.Humidity?.Value ?? 0); plantBox.Luminosity = new CaptorValue(infoResponse.LuminosityMin, infoResponse.LuminosityMax, plantBox.Luminosity?.Value ?? 0); plantBox.Temperature = new CaptorValue(infoResponse.TemperatureMin, infoResponse.TemperatureMax, plantBox.Temperature?.Value ?? 0); + + plantBox.UpdateState(); + + Broker.PlantBoxesManager.Add(plantBox); } protected override void CaptorsCommand(CommandStream commandStream, CommandPacket packet) @@ -59,6 +63,8 @@ namespace PlantBox.Broker plantBox.Temperature.Value = captorsResponse.Temperature; plantBox.TankLevel = captorsResponse.Tank; + plantBox.UpdateState(); + plantBox.HistoricManager.Add(new CaptorsValue(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value)); }