Files
tbf/TBF/Rig/Network/AdapterJMS/Netadapter.cs
michal d2ae5f3e83 It works !!! ??
Add support for `System.Net.Sockets`, enhance JMS camera detection, and refactor adapter connection handling

- Added `System.Net.Sockets` as a project dependency and updated the configuration file accordingly.
- Enhanced timeout logic for JMS camera detection, improving reliability in identifying cameras.
- Refactored `NetAdapter` to support async connection handling with better initialization in simulate mode.
- Fixed IP setup for debugging and added new task management for asynchronous operations.
2025-05-30 15:34:21 +02:00

116 lines
3.6 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Common;
using Config.Entities;
using log4net;
using NHibernate;
using TBF.Rig.Network.Camera.CJMS11;
namespace TBF.Rig.Network.AdapterJMS
{
public class Netadapter : ComponentBase, GenericDevices.INetworkAdapter
{
private static readonly ILog log = LogManager.GetLogger(typeof(Netadapter));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly NetadapterCfg netadapterCfg;
IPAddress ipAddress;
IPAddress netMask;
IPAddress broadcastAddress;
int tcpPort = -1;
IList<TcpClient> tcpClients;
private IList<Task> connectAsyncList;
public IPAddress IPAddress { get { return ipAddress; } }
public IPAddress NetMask { get { return netMask; } }
public IPAddress BroadcastAddress { get { return broadcastAddress; } }
public IList<TcpClient> TcpClients { get { return tcpClients; } }
public int TcpPort { get { return tcpPort; } }
public TcpClient GetTcpClient(string ipAddressCJMS)
{
foreach (TcpClient tcpClient in tcpClients)
{
if (tcpClient.Client.RemoteEndPoint.ToString().Contains(ipAddressCJMS))
{
return tcpClient;
}
}
return null;
}
public IList<Task> TasksConnectAsync { get { return connectAsyncList; } }
public Netadapter() { }
public Netadapter(Generic.IComponentCfg cfg)
: base(cfg)
{
netadapterCfg = cfg as NetadapterCfg;
log.Warn(this.ToString());
tcpClients = new List<TcpClient>();
}
public override void Initialize()
{
if (DebugLevel == DebugMode.Simulate)
{
ipAddress = IPAddress.Parse("192.168.1.100");
netMask = IPAddress.Parse("255.255.255.0");
broadcastAddress = IPAddress.Parse("192.168.1.255");
return;
}
//TODO hold connections to cameras and other devices, close them when the adapter is removed.
ISession session = TBF.DB.CreateSession(DBKind.Config);
//TODO BUMI - get the port from the config file - defined by UI
if (tcpPort == -1) tcpPort = 32456; //default port
IList<Component> cmpntEntities = session.QueryOver<Component>()
.OrderBy(x => x. ItemNr).Asc
.List();
connectAsyncList = new List<Task>();
foreach (var cmpnt in cmpntEntities)
{
Rig.Generic.IComponentFactory factory = TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName);
/// Camera - jouined to a network adapter
if (factory is Rig.Network.Camera.CJMS11.Factory)
{
CameraCfg cmpntCfgFromCmpntEntity =
factory.CmpntCfgFromCmpntEntity(cmpnt) as Rig.Network.Camera.CJMS11.CameraCfg;
if (cmpntCfgFromCmpntEntity == null) continue;
// TcpClient tcpClient = new TcpClient();
// tcpClients.Add(tcpClient);
// connectAsyncList.Add(tcpClient.ConnectAsync(cmpntCfgFromCmpntEntity.IPAddressCJMS, tcpPort));
}
}
//Task.WaitAll(connectAsyncList.ToArray()); // run all tasks in parallel
// AdapterInfo.RefreshNetAdaptersInfo();
// AdapterInfo netadapter = AdapterInfo.GetNetAdapter(netadapterCfg.Description);
// ipAddress = netadapter.IPAddress;
// netMask = netadapter.NetMask;
//
// if (this.Cfg.DebugLevel != DebugMode.Simulate)
// {
// broadcastAddress = netadapter.GetBroadcastAddress();
// }
}
}
}