91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
namespace Common.Hardware.SIRT
|
|
{
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class SIRTFactory
|
|
{
|
|
private readonly ConcurrentDictionary<string, SIRTBroadcaster> connections
|
|
= new ConcurrentDictionary<string, SIRTBroadcaster>();
|
|
|
|
public event Action<string> Logging;
|
|
|
|
public void CloseConnection(string portName)
|
|
{
|
|
if (this.connections.TryGetValue(portName, out var connection))
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
|
|
public void CloseConnections()
|
|
{
|
|
foreach (var connection in this.GetConnections())
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
|
|
public void DequeueTasks(uint address)
|
|
{
|
|
foreach (var connection in this.GetConnections())
|
|
{
|
|
connection.DequeueTasks(address);
|
|
}
|
|
}
|
|
|
|
public void OpenConnections()
|
|
{
|
|
foreach (var connection in this.GetConnections())
|
|
{
|
|
connection.Open();
|
|
}
|
|
}
|
|
|
|
public void ConnectSIRT(string portName, Action<SIRTBroadcaster> callback)
|
|
{
|
|
if (!this.connections.TryGetValue(portName, out var broadcaster))
|
|
{
|
|
broadcaster = new SIRTBroadcaster(portName);
|
|
broadcaster.Logging += message => this.Logging?.Invoke(message);
|
|
|
|
if (!this.connections.TryAdd(portName, broadcaster))
|
|
{
|
|
broadcaster = null;
|
|
}
|
|
}
|
|
|
|
callback(broadcaster);
|
|
broadcaster?.Close();
|
|
broadcaster?.Open();
|
|
}
|
|
|
|
public void EnqueueTask(SIRTTask task)
|
|
{
|
|
foreach (var connection in this.GetConnections())
|
|
{
|
|
connection.EnqueueTask(task);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<string> GetPortNames()
|
|
=> this.connections.Keys.ToArray();
|
|
|
|
public void RemoveConnections()
|
|
{
|
|
foreach (var key in this.connections.Keys.ToArray())
|
|
{
|
|
if (this.connections.TryRemove(key, out var broadcaster))
|
|
{
|
|
broadcaster.RemoveConnection();
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerable<SIRTBroadcaster> GetConnections()
|
|
=> this.connections.Values.ToArray();
|
|
}
|
|
}
|