Files
laatzen/Common/SIRTBroadcaster.Host/SIRTBroadcasterHost.cs
T
2023-08-22 14:58:05 +02:00

154 lines
4.8 KiB
C#

namespace SIRTBroadcaster.Host
{
using Microsoft.AspNet.SignalR.Client;
using SIRTBroadcaster.Abstraction;
using System;
using System.Collections.Generic;
/// <summary>
/// Implements a SignalR host connection.
/// Operates from and to SIRT without saving any data.
/// </summary>
public class SIRTBroadcasterHost : ISIRTBroadcaster, ISIRTBroadcasterHost
{
/// <summary>
/// This collection holds all signed events to the hub.
/// On dispose they are also disposed.
/// </summary>
private readonly ICollection<IDisposable> disposables;
/// <summary>
/// SIRT listener object.
/// </summary>
private readonly ISIRTListener sirtListener;
/// <summary>
/// Hub connection object.
/// </summary>
private readonly HubConnection hubConnection;
/// <summary>
/// Hub proxy object. Handels the hub endpoints.
/// </summary>
private readonly IHubProxy hubProxy;
/// <summary>
/// Returns the hub connection state.
/// </summary>
private bool hubConnected;
/// <summary>
/// Returns the SIRT connection state.
/// </summary>
private bool sirtConnected;
/// <summary>
/// Initializes all private members needed to start message handling.
/// Starts a hub connection.
/// Also starts sirt connection.
/// </summary>
/// <param name="sirtConnection"> - model containing informations about the sirt.</param>
public SIRTBroadcasterHost(SIRTConnection sirtConnection)
{
this.disposables = new List<IDisposable>();
this.sirtListener = new SIRTListener(sirtConnection);
this.hubConnection = new HubConnection(SIRTFixed.SIRT_URL);
this.hubProxy = this.hubConnection.CreateHubProxy(SIRTFixed.SIRT_HUB);
this.ConfigureHubConnection();
this.ConfigureSIRTListener();
}
/// <summary>
/// Disposes all disposables in this instance and clears all collections.
/// </summary>
public void Dispose()
{
try
{
this.sirtListener.Dispose();
foreach (var disposable in this.disposables)
{
disposable?.Dispose();
}
this.disposables.Clear();
this.hubConnection.Dispose();
}
catch (Exception)
{
// TODO: Exception logging.
}
}
/// <summary>
/// Sends the message to the SignalR hub if the hub is connected and the message is not null.
/// </summary>
/// <param name="message"> - to send.</param>
public void MessageReceived(SIRTMessage message)
{
if (this.hubConnected && message != null)
{
this.hubProxy.Invoke(nameof(ISIRTBroadcaster.MessageReceived), message);
}
}
/// <summary>
/// Sends the message directly to the SIRT if the SIRT is connected and the message is not null.
/// </summary>
/// <param name="message"> - to send.</param>
public void SendMessage(SIRTMessage message)
{
if (this.sirtConnected && message != null)
{
this.sirtListener.SendData(message.ToByteArray());
}
}
/// <summary>
/// Initializes the hub connection.
/// </summary>
private void ConfigureHubConnection()
{
this.disposables.Add(this.hubProxy.On<SIRTMessage>(nameof(ISIRTBroadcaster.SendMessage), this.SendMessage));
this.hubConnection.StateChanged += (args) =>
{
this.hubConnected = args.NewState == ConnectionState.Connected;
};
try
{
this.hubConnection
.Start()
.ConfigureAwait(true)
.GetAwaiter()
.GetResult();
}
catch (Exception)
{
// TODO: exception logging.
}
}
/// <summary>
/// Initializes the SIRT connection.
/// </summary>
private void ConfigureSIRTListener()
{
if (this.sirtListener.StartListening())
{
this.sirtListener.MessageReceived += this.MessageReceived;
this.sirtListener.SendData(new byte[]
{
/// SIRT init string.
0x55, 0x84, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x80, 0x80, 0x01, 0x90, 0xA0, 0x16
});
this.sirtConnected = true;
}
}
}
}