diff --git a/PlantBox.Broker/ClientManager.cs b/PlantBox.Broker/ClientManager.cs index 5ea2895..1b0972e 100644 --- a/PlantBox.Broker/ClientManager.cs +++ b/PlantBox.Broker/ClientManager.cs @@ -23,12 +23,17 @@ namespace PlantBox.Broker ulong id = packet.ID; PlantBox plantBox = Broker.PlantBoxesManager[id]; + CaptorsResponse response; + if (plantBox == null) { - throw new InvalidOperationException($"This PlantBox (${id}) does not exist"); + response = new CaptorsResponse(0, 0, 0, 0); + } + else + { + response = new CaptorsResponse(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value, plantBox.TankLevel); } - var response = new CaptorsResponse(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value, plantBox.TankLevel); commandStream.Send(response.ToCommandPacket(id)); } @@ -59,42 +64,47 @@ namespace PlantBox.Broker PlantBox plantBox = Broker.PlantBoxesManager[id]; int count = historicRequest.Number; + HistoricResponse response; + if (plantBox == null) { - throw new InvalidOperationException($"This PlantBox (${id}) does not exist"); + response = new HistoricResponse(TimeSpan.MinValue, Array.Empty(), Array.Empty(), Array.Empty()); } - - HistoricManager historic = plantBox.HistoricManager; - - IReadOnlyList captorsValues; - switch (historicRequest.Interval) + else { - case HistoricInterval.Minutely: - captorsValues = historic.MinutesHistoric; - break; - case HistoricInterval.Hourly: - captorsValues = historic.HoursHistoric; - break; - case HistoricInterval.Daily: - captorsValues = historic.DaysHistoric; - break; - case HistoricInterval.Weekly: - captorsValues = historic.WeeksHistoric; - break; - case HistoricInterval.Monthly: - captorsValues = historic.MonthsHistoric; - break; - default: - throw new InvalidOperationException("How did you just got here? Even 『 』can't"); + HistoricManager historic = plantBox.HistoricManager; + + IReadOnlyList captorsValues; + switch (historicRequest.Interval) + { + case HistoricInterval.Minutely: + captorsValues = historic.MinutesHistoric; + break; + case HistoricInterval.Hourly: + captorsValues = historic.HoursHistoric; + break; + case HistoricInterval.Daily: + captorsValues = historic.DaysHistoric; + break; + case HistoricInterval.Weekly: + captorsValues = historic.WeeksHistoric; + break; + case HistoricInterval.Monthly: + captorsValues = historic.MonthsHistoric; + break; + default: + throw new InvalidOperationException("How did you just got here? Even 『 』can't"); + } + + response = new HistoricResponse + ( + DateTime.Now - plantBox.LastMeasureDate, + FillArray(captorsValues.Select(x => x.Humidity).ToArray(), count), + FillArray(captorsValues.Select(x => x.Luminosity).ToArray(), count), + FillArray(captorsValues.Select(x => x.Temperature).ToArray(), count) + ); } - var response = new HistoricResponse - ( - DateTime.Now - plantBox.LastMeasureDate, - 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)); } @@ -104,20 +114,24 @@ namespace PlantBox.Broker ulong id = packet.ID; PlantBox plantBox = Broker.PlantBoxesManager[id]; + InfoResponse response; + if (plantBox == null) { - throw new InvalidOperationException($"This PlantBox (${id}) does not exist"); + response = new InfoResponse("", default, default, 0, 0, 0, 0, 0, 0); } + else + { + plantBox.UpdateState(); - plantBox.UpdateState(); - - var response = new InfoResponse - ( - plantBox.Name, plantBox.Type, plantBox.State, - plantBox.Humidity.Min, plantBox.Humidity.Max, - plantBox.Luminosity.Min, plantBox.Luminosity.Max, - plantBox.Temperature.Min, plantBox.Temperature.Max - ); + response = new InfoResponse + ( + plantBox.Name, plantBox.Type, plantBox.State, + plantBox.Humidity.Min, plantBox.Humidity.Max, + plantBox.Luminosity.Min, plantBox.Luminosity.Max, + plantBox.Temperature.Min, plantBox.Temperature.Max + ); + } commandStream.Send(response.ToCommandPacket(id)); }