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;
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<double>(), Array.Empty<double>(), Array.Empty<double>());
}
HistoricManager historic = plantBox.HistoricManager;
IReadOnlyList<CaptorsValue> 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<CaptorsValue> 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));
}