Update Broker
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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<CaptorsValue> _minutesHistoric;
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CaptorsValue> MinutesHistoric => _minutesHistoric.AsReadOnly();
|
||||
|
||||
[JsonProperty(PropertyName = nameof(HoursHistoric))]
|
||||
private List<CaptorsValue> _hoursHistoric;
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CaptorsValue> HoursHistoric => _hoursHistoric.AsReadOnly();
|
||||
|
||||
[JsonProperty(PropertyName = nameof(DaysHistoric))]
|
||||
private List<CaptorsValue> _daysHistoric;
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CaptorsValue> DaysHistoric => _daysHistoric.AsReadOnly();
|
||||
|
||||
[JsonProperty(PropertyName = nameof(WeeksHistoric))]
|
||||
private List<CaptorsValue> _weeksHistoric;
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CaptorsValue> WeeksHistoric => _weeksHistoric.AsReadOnly();
|
||||
|
||||
[JsonProperty(PropertyName = nameof(MonthsHistoric))]
|
||||
private List<CaptorsValue> _monthsHistoric;
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CaptorsValue> MonthsHistoric => _monthsHistoric.AsReadOnly();
|
||||
|
||||
public HistoricManager()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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...");
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user