Different types of units. Writing values, relay unit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace ESPServer
|
||||
{
|
||||
class UnitDataWriter
|
||||
{
|
||||
const int UNIT_TARGET_PORT = 7000;
|
||||
|
||||
private readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
UnitsDatabase db;
|
||||
|
||||
public UnitDataWriter(UnitsDatabase db, RegistersContainer registers)
|
||||
{
|
||||
this.db = db;
|
||||
registers.OnRegisterWritten += OnRegisterWritten;
|
||||
}
|
||||
|
||||
private void OnRegisterWritten(int address, float value)
|
||||
{
|
||||
logger.Debug("Register written, address = " + address.ToString() + " value = " + value.ToString());
|
||||
var unit = db.GetUnit(address);
|
||||
if (unit == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var payload = unit.GetWriteCommand(address, value);
|
||||
if (payload == null)
|
||||
{
|
||||
logger.Info("Write of address " + address + " is not supported by unit " + unit.id);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var udpClient = new UdpClient())
|
||||
{
|
||||
udpClient.Connect(unit.ip, UNIT_TARGET_PORT);
|
||||
udpClient.Send(payload, payload.Length);
|
||||
udpClient.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user