End Broker
This commit is contained in:
77
PlantBox.Broker/ServerManager.cs
Normal file
77
PlantBox.Broker/ServerManager.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using PlantBox.Shared.Communication;
|
||||
using PlantBox.Shared.Communication.Commands;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PlantBox.Broker
|
||||
{
|
||||
class ServerManager : TcpManager
|
||||
{
|
||||
public ServerManager(Broker broker) : base(broker)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override string LogPrefix => "Server";
|
||||
protected override int ListeningPort => Connection.TCP_SERVER_PORT;
|
||||
|
||||
protected override void InfoCommand(CommandStream commandStream, CommandPacket packet)
|
||||
{
|
||||
InfoResponse infoResponse = new InfoResponse().Deserialize(packet.Arguments);
|
||||
ulong id = packet.ID;
|
||||
PlantBox plantBox = Broker.PlantBoxesManager[id];
|
||||
|
||||
if (plantBox == null)
|
||||
{
|
||||
plantBox = new PlantBox
|
||||
{
|
||||
HistoricManager = new HistoricManager()
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected override void CaptorsCommand(CommandStream commandStream, CommandPacket packet)
|
||||
{
|
||||
CaptorsResponse captorsResponse = new CaptorsResponse().Deserialize(packet.Arguments);
|
||||
ulong id = packet.ID;
|
||||
PlantBox plantBox = Broker.PlantBoxesManager[id];
|
||||
|
||||
if (plantBox == null)
|
||||
{
|
||||
Log($"Received captors info from non-registered PlantBox ({id}), ignoring it");
|
||||
return;
|
||||
}
|
||||
|
||||
plantBox.LastMeasureDate = DateTime.Now;
|
||||
plantBox.Humidity.Value = captorsResponse.Humidity;
|
||||
plantBox.Luminosity.Value = captorsResponse.Luminosity;
|
||||
plantBox.Temperature.Value = captorsResponse.Temperature;
|
||||
plantBox.TankLevel = captorsResponse.Tank;
|
||||
|
||||
plantBox.HistoricManager.Add(new CaptorsValue(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value));
|
||||
}
|
||||
|
||||
protected override void HistoricCommand(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user