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,11 +64,14 @@ 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; HistoricManager historic = plantBox.HistoricManager;
IReadOnlyList<CaptorsValue> captorsValues; IReadOnlyList<CaptorsValue> captorsValues;
@@ -88,13 +96,15 @@ namespace PlantBox.Broker
throw new InvalidOperationException("How did you just got here? Even 『 』can't"); throw new InvalidOperationException("How did you just got here? Even 『 』can't");
} }
var response = new HistoricResponse response = new HistoricResponse
( (
DateTime.Now - plantBox.LastMeasureDate, DateTime.Now - plantBox.LastMeasureDate,
FillArray(captorsValues.Select(x => x.Humidity).ToArray(), count), FillArray(captorsValues.Select(x => x.Humidity).ToArray(), count),
FillArray(captorsValues.Select(x => x.Luminosity).ToArray(), count), FillArray(captorsValues.Select(x => x.Luminosity).ToArray(), count),
FillArray(captorsValues.Select(x => x.Temperature).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();
var response = new InfoResponse response = new InfoResponse
( (
plantBox.Name, plantBox.Type, plantBox.State, plantBox.Name, plantBox.Type, plantBox.State,
plantBox.Humidity.Min, plantBox.Humidity.Max, plantBox.Humidity.Min, plantBox.Humidity.Max,
plantBox.Luminosity.Min, plantBox.Luminosity.Max, plantBox.Luminosity.Min, plantBox.Luminosity.Max,
plantBox.Temperature.Min, plantBox.Temperature.Max plantBox.Temperature.Min, plantBox.Temperature.Max
); );
}
commandStream.Send(response.ToCommandPacket(id)); commandStream.Send(response.ToCommandPacket(id));
} }