Change broker response on invalid id

Now just send back a blank response
This commit is contained in:
2019-04-29 19:45:52 +02:00
parent 6ca735b574
commit 929b49a05c

View File

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