End Broker

This commit is contained in:
2019-04-24 19:27:40 +02:00
parent a1e1d43a6f
commit e3cc6392a7
12 changed files with 518 additions and 23 deletions

View File

@@ -0,0 +1,92 @@
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)
{
throw new NotImplementedException();
}
protected override void PingCommand(CommandStream commandStream, CommandPacket packet)
{
var ping = new PingCommand().Deserialize(packet.Arguments);
commandStream.Send(ping.ToCommandPacket(packet.ID));
}
}
}