238 lines
8.3 KiB
C#
238 lines
8.3 KiB
C#
namespace ExchangeHub
|
|
{
|
|
using ExchangeHub.Contracts;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNet.SignalR.Client;
|
|
|
|
[ClassInterface(ClassInterfaceType.None)]
|
|
[ComSourceInterfaces(typeof(IExchangeEvents))]
|
|
[Guid("2ABFAC6C-9F3C-4702-9E08-4979A26577EF")]
|
|
[ProgId("ExchangeHub.ExchangeConnection")]
|
|
public class ExchangeConnection : IExchangeHubMaster
|
|
{
|
|
public const string GROUP_KEY = "GroupNameKey";
|
|
public const string HUB_NAME = "ExchangeHub";
|
|
public const string STR_EMPTY = "";
|
|
|
|
private static readonly string GroupName = Environment.MachineName;
|
|
|
|
private readonly ICollection<IDisposable> disposables;
|
|
|
|
private HubConnection connection;
|
|
private IHubProxy proxy;
|
|
private string outputFile;
|
|
|
|
public ExchangeConnection()
|
|
=> this.disposables = new List<IDisposable>();
|
|
|
|
public event VoidEventHandler Disposed;
|
|
public event DoubleEventHandler StartFlow;
|
|
public event VoidEventHandler StopFlow;
|
|
public event IntEventHandler StartReferenceMessurement;
|
|
public event DoubleEventHandler StopReferenceMessurement;
|
|
public event StringEventHandler StateChanged;
|
|
|
|
public void Connect(string url)
|
|
{
|
|
this.connection = new HubConnection(url, queryString: $"{GROUP_KEY}={Environment.MachineName}");
|
|
this.connection.Closed += this.Connection_Closed;
|
|
this.connection.ConnectionSlow += this.Connection_ConnectionSlow;
|
|
this.connection.Error += this.Connection_Error;
|
|
this.connection.Received += this.Connection_Received;
|
|
this.connection.Reconnected += this.Connection_Reconnected;
|
|
this.connection.Reconnecting += this.Connection_Reconnecting;
|
|
this.connection.StateChanged += this.Connection_StateChanged;
|
|
|
|
this.proxy = this.connection.CreateHubProxy(HUB_NAME);
|
|
|
|
this.disposables.Add(this.proxy.On<string>( nameof(this.Disposed), this.ReceiveDisposed));
|
|
this.disposables.Add(this.proxy.On<double, string>( nameof(this.StartFlow), this.FlowStarted));
|
|
this.disposables.Add(this.proxy.On<string>( nameof(this.StopFlow), this.FlowStopped));
|
|
this.disposables.Add(this.proxy.On<int, string>( nameof(this.StartReferenceMessurement), this.MessurementStarted));
|
|
this.disposables.Add(this.proxy.On<double, string>( nameof(this.StopReferenceMessurement), this.MessurementStopped));
|
|
|
|
try
|
|
{
|
|
this.connection
|
|
.Start()
|
|
.Wait();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.NotifyStateChanged(e.ToString());
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
try
|
|
{
|
|
this.SendDisposed();
|
|
|
|
this.connection.Closed -= this.Connection_Closed;
|
|
this.connection.ConnectionSlow -= this.Connection_ConnectionSlow;
|
|
this.connection.Error -= this.Connection_Error;
|
|
this.connection.Received -= this.Connection_Received;
|
|
this.connection.Reconnected -= this.Connection_Reconnected;
|
|
this.connection.Reconnecting -= this.Connection_Reconnecting;
|
|
this.connection.StateChanged -= this.Connection_StateChanged;
|
|
|
|
foreach (var disposable in this.disposables)
|
|
{
|
|
disposable.Dispose();
|
|
}
|
|
|
|
Task.Factory.StartNew(this.connection.Dispose);
|
|
|
|
this.NotifyStateChanged($"Connection disconnected.");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.NotifyStateChanged(e.ToString());
|
|
}
|
|
}
|
|
|
|
public void SendDisposed(string _ = STR_EMPTY)
|
|
{
|
|
if (this.connection?.State == ConnectionState.Connected)
|
|
{
|
|
this.proxy.Invoke<string>(nameof(this.Disposed), GroupName);
|
|
this.NotifyStateChanged($"{nameof(SendDisposed)}");
|
|
}
|
|
}
|
|
|
|
public void SendStartFlow(double volumeM3H, string _ = STR_EMPTY)
|
|
{
|
|
if (this.connection?.State == ConnectionState.Connected)
|
|
{
|
|
this.proxy.Invoke(nameof(this.StartFlow), volumeM3H, GroupName);
|
|
this.NotifyStateChanged($"{nameof(SendStartFlow)}");
|
|
}
|
|
}
|
|
|
|
public void SendStopFlow(string _ = STR_EMPTY)
|
|
{
|
|
if (this.connection?.State == ConnectionState.Connected)
|
|
{
|
|
this.proxy.Invoke(nameof(this.StopFlow), GroupName);
|
|
this.NotifyStateChanged($"{nameof(SendStopFlow)}");
|
|
}
|
|
}
|
|
|
|
public void SendStartReferenceMessurement(int durationSeconds, string _ = STR_EMPTY)
|
|
{
|
|
if (this.connection?.State == ConnectionState.Connected)
|
|
{
|
|
this.proxy.Invoke(nameof(this.StartReferenceMessurement), durationSeconds, GroupName);
|
|
this.NotifyStateChanged($"{nameof(SendStartReferenceMessurement)}");
|
|
}
|
|
}
|
|
|
|
public void SendStopReferenceMessurement(double volumeM3H = 0D, string _ = STR_EMPTY)
|
|
{
|
|
if (this.connection?.State == ConnectionState.Connected)
|
|
{
|
|
this.proxy.Invoke(nameof(this.StopReferenceMessurement), volumeM3H, GroupName);
|
|
this.NotifyStateChanged($"{nameof(SendStopReferenceMessurement)}");
|
|
}
|
|
}
|
|
|
|
private void ReceiveDisposed(string groupName)
|
|
{
|
|
if (GroupName == groupName)
|
|
{
|
|
this.Disposed?.Invoke(groupName);
|
|
this.NotifyStateChanged($"{nameof(Disposed)}");
|
|
}
|
|
}
|
|
|
|
private void FlowStarted(double volumeM3H, string groupName)
|
|
{
|
|
if (GroupName == groupName)
|
|
{
|
|
this.StartFlow?.Invoke(volumeM3H, groupName);
|
|
this.NotifyStateChanged($"{nameof(FlowStarted)} -> {volumeM3H}m³/h");
|
|
}
|
|
}
|
|
|
|
private void FlowStopped(string groupName)
|
|
{
|
|
if (GroupName == groupName)
|
|
{
|
|
this.StopFlow?.Invoke(groupName);
|
|
this.NotifyStateChanged($"{nameof(FlowStopped)}");
|
|
}
|
|
}
|
|
|
|
private void MessurementStarted(int seconds, string groupName)
|
|
{
|
|
if (GroupName == groupName)
|
|
{
|
|
this.StartReferenceMessurement?.Invoke(seconds, groupName);
|
|
this.NotifyStateChanged($"{nameof(MessurementStarted)} -> {seconds}s");
|
|
}
|
|
}
|
|
|
|
private void MessurementStopped(double volumeMessured, string groupName)
|
|
{
|
|
if (GroupName == groupName)
|
|
{
|
|
this.StopReferenceMessurement?.Invoke(volumeMessured, groupName);
|
|
this.NotifyStateChanged($"{nameof(MessurementStopped)} -> {volumeMessured}m³/h");
|
|
}
|
|
}
|
|
|
|
private void Connection_Closed()
|
|
{
|
|
this.NotifyStateChanged("HubConnection closed!");
|
|
}
|
|
|
|
private void Connection_ConnectionSlow()
|
|
{
|
|
this.NotifyStateChanged("HubConnection is about to timeout!");
|
|
}
|
|
|
|
private void Connection_Error(Exception e)
|
|
{
|
|
this.NotifyStateChanged(e.ToString());
|
|
}
|
|
|
|
private void Connection_Received(string data)
|
|
{
|
|
this.NotifyStateChanged(data);
|
|
}
|
|
|
|
private void Connection_Reconnected()
|
|
{
|
|
this.NotifyStateChanged("HubConnection reconnected!");
|
|
}
|
|
|
|
private void Connection_Reconnecting()
|
|
{
|
|
this.NotifyStateChanged("HubConnection reconnecting!");
|
|
}
|
|
|
|
private void Connection_StateChanged(StateChange state)
|
|
{
|
|
this.NotifyStateChanged($"{state.OldState} -> {state.NewState}");
|
|
}
|
|
|
|
private void NotifyStateChanged(string text)
|
|
{
|
|
try
|
|
{
|
|
this.StateChanged?.Invoke(text, GroupName);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.StateChanged?.Invoke(e.ToString(), GroupName);
|
|
}
|
|
}
|
|
}
|
|
}
|