81 lines
2.2 KiB
C#
81 lines
2.2 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 SIRTBroadcaster ConnectSIRT(string comPort)
|
|
{
|
|
if (!this.connections.TryGetValue(comPort, out var connection))
|
|
{
|
|
connection = new SIRTBroadcaster(comPort);
|
|
|
|
if (this.connections.TryAdd(comPort, connection))
|
|
{
|
|
connection.Logging += message => this.Logging?.Invoke(message);
|
|
connection.Open();
|
|
}
|
|
else
|
|
{
|
|
connection = null;
|
|
this.Logging?.Invoke($"Cann't connect SIRT to {comPort}");
|
|
}
|
|
}
|
|
|
|
return connection;
|
|
}
|
|
|
|
public void EnqueueTask(SIRTTask task)
|
|
{
|
|
foreach (var connection in this.GetConnections())
|
|
{
|
|
connection.EnqueueTask(task);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<string> GetPortNames() => this.connections.Keys.ToArray();
|
|
|
|
private IEnumerable<SIRTBroadcaster> GetConnections() => this.connections.Values.ToArray();
|
|
}
|
|
}
|