Added periodical pings of all known units
This commit is contained in:
@@ -60,6 +60,8 @@ namespace ESPServer
|
|||||||
{
|
{
|
||||||
var unitReceiver = new UnitDataReceiver(config.RootElement.GetProperty("port").GetInt32(), db, registers);
|
var unitReceiver = new UnitDataReceiver(config.RootElement.GetProperty("port").GetInt32(), db, registers);
|
||||||
unitReceiver.Start();
|
unitReceiver.Start();
|
||||||
|
var unitPinger = new UnitPinger(db);
|
||||||
|
unitPinger.Start();
|
||||||
var modbusServer = new ModbusServer(registers);
|
var modbusServer = new ModbusServer(registers);
|
||||||
modbusServer.Port = config.RootElement.GetProperty("modbusPort").GetInt32();
|
modbusServer.Port = config.RootElement.GetProperty("modbusPort").GetInt32();
|
||||||
modbusServer.Run();
|
modbusServer.Run();
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using NLog;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace ESPServer
|
||||||
|
{
|
||||||
|
class UnitPinger
|
||||||
|
{
|
||||||
|
const int PING_INTERVAL = 30 * 1000; // 30 seconds
|
||||||
|
const int UNIT_TARGET_PORT = 7000;
|
||||||
|
|
||||||
|
private readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||||||
|
UnitsDatabase db;
|
||||||
|
|
||||||
|
public UnitPinger(UnitsDatabase db)
|
||||||
|
{
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
var t = new Thread(new ThreadStart(this.Run));
|
||||||
|
t.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Run()
|
||||||
|
{
|
||||||
|
logger.Info("Started UnitPinger");
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.Sleep(PING_INTERVAL);
|
||||||
|
var ips = db.GetUnitIPs();
|
||||||
|
foreach (var kv in ips)
|
||||||
|
{
|
||||||
|
var ip = kv.Value;
|
||||||
|
using (var udpClient = new UdpClient())
|
||||||
|
{
|
||||||
|
udpClient.Connect(ip, UNIT_TARGET_PORT);
|
||||||
|
var data = Encoding.ASCII.GetBytes("ping");
|
||||||
|
udpClient.Send(data, data.Length);
|
||||||
|
udpClient.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.Error(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -103,6 +103,22 @@ namespace ESPServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal Dictionary<string, string> GetUnitIPs()
|
||||||
|
{
|
||||||
|
var result = new Dictionary<string, string>();
|
||||||
|
lock (units)
|
||||||
|
{
|
||||||
|
foreach (var u in units)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(u.ip))
|
||||||
|
{
|
||||||
|
result[u.id] = u.ip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public Unit GetUnit(string id)
|
public Unit GetUnit(string id)
|
||||||
{
|
{
|
||||||
lock (units)
|
lock (units)
|
||||||
|
|||||||
Reference in New Issue
Block a user