You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs

150 lines
4.6 KiB

using Infrastructure.Extensions;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace IoT.Shared.Infrastructure
{
public class BaseClientService : IClientService
{
protected string _notifyHost;
protected HubConnection Connection;
protected readonly IServiceProvider applicationServices;
protected readonly IConfiguration _cfg;
protected CancellationTokenSource _tokenSource;
public string ConnectionId { get; private set; }
public BaseClientService(IServiceProvider applicationServices, IConfiguration configuration)
{
this.applicationServices = applicationServices;
this._cfg = configuration;
this._tokenSource = new CancellationTokenSource();
this.ConnectionId = Guid.NewGuid().ToBase62();
}
public void Start()
{
Task.Run(async () =>
{
while (!_tokenSource.IsCancellationRequested)
{
this.Connect();
await Task.Delay(this._cfg.GetValue<int>("timer.seconds", 60) * 1000);
}
});
}
public void Connect()
{
if (this._cfg.GetValue<bool>("notify:enabled", false))
{
try
{
if (Connection == null)
{
Console.WriteLine("connection is null");
InitConnection();
}
if (Connection.State == HubConnectionState.Disconnected)
{
Console.WriteLine("start connect");
if (this._notifyHost != this._cfg["notify:host"])
{
InitConnection();
}
Connection.StartAsync().Wait();
this.OnConnected();
}
else
{
if (this._notifyHost != this._cfg["notify:host"])
{
this.ReConnect(null);
}
else
{
Console.WriteLine($"connection has connected");
}
}
}
catch (Exception ex)
{
ex.PrintStack();
}
}
else
{
this.Close();
}
}
public virtual void OnConnected()
{
}
public virtual void ConnectionOn()
{
}
public void Close()
{
if (this.Connection != null)
{
if (this.Connection.State == HubConnectionState.Connected)
{
this.Connection.StopAsync();
}
this.Connection.DisposeAsync();
this.Connection = null;
}
}
private Task ReConnect(Exception arg)
{
this.Close();
this.Connect();
return Task.CompletedTask;
}
private void InitConnection()
{
this._notifyHost = this._cfg["notify:host"];
var url = $"http://{this._notifyHost}/hub?group={this._cfg["connectId"]}";
if (this.Connection != null)
{
this.Connection.DisposeAsync();
}
this.Connection = new HubConnectionBuilder().WithUrl(url).Build();
this.Connection.Closed += ReConnect;
this.Connection.On("ServerToClient", (string method, string message, string fromConnectionId) => this.OnServerToClient(method, message, fromConnectionId));
this.ConnectionOn();
}
public virtual void OnServerToClient(string method, string message, string fromConnectionId)
{
}
public void ClientToServer(string method, string message, string fromConnectionId = null)
{
Task.Run(() =>
{
try
{
this.Connection.SendAsync("OnClientToServer", method, message, fromConnectionId);
}
catch (Exception ex)
{
ex.PrintStack();
}
});
}
public void Dispose()
{
this._tokenSource.Cancel();
this.Close();
}
}
}