Files
PlantBox/PlantBox.Broker/ClientManager.cs

109 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlantBox.Shared.Communication;
using PlantBox.Shared.Communication.Commands;
namespace PlantBox.Broker
{
class ClientManager : TcpManager
{
public ClientManager(Broker broker) : base(broker)
{
}
protected override string LogPrefix => "Client";
protected override int ListeningPort => Connection.TCP_CLIENT_PORT;
protected override void CaptorsCommand(CommandStream commandStream, CommandPacket packet)
{
CaptorsRequest captorsRequest = new CaptorsRequest().Deserialize(packet.Arguments);
ulong id = packet.ID;
PlantBox plantBox = Broker.PlantBoxesManager[id];
if (plantBox == null)
{
throw new InvalidOperationException($"This PlantBox (${id}) does not exist");
}
var response = new CaptorsResponse(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value, plantBox.TankLevel);
commandStream.Send(response.ToCommandPacket(id));
}
protected override void HistoricCommand(CommandStream commandStream, CommandPacket packet)
{
HistoricRequest historicRequest = new HistoricRequest().Deserialize(packet.Arguments);
ulong id = packet.ID;
PlantBox plantBox = Broker.PlantBoxesManager[id];
if (plantBox == null)
{
throw new InvalidOperationException($"This PlantBox (${id}) does not exist");
}
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");
}
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()
);
commandStream.Send(response.ToCommandPacket(id));
}
protected override void InfoCommand(CommandStream commandStream, CommandPacket packet)
{
InfoRequest infoRequest = new InfoRequest().Deserialize(packet.Arguments);
ulong id = packet.ID;
PlantBox plantBox = Broker.PlantBoxesManager[id];
if (plantBox == null)
{
throw new InvalidOperationException($"This PlantBox (${id}) does not exist");
}
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
);
commandStream.Send(response.ToCommandPacket(id));
}
protected override void PingCommand(CommandStream commandStream, CommandPacket packet)
{
var ping = new PingCommand().Deserialize(packet.Arguments);
commandStream.Send(ping.ToCommandPacket(packet.ID));
}
}
}