|
|
|
@ -7,28 +7,172 @@ using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IoT.Shared.Infrastructure
|
|
|
|
|
{
|
|
|
|
|
public class NodeService : BaseNodeService
|
|
|
|
|
public class NodeService : IHostedService, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public NodeService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration)
|
|
|
|
|
private string _notifyHost;
|
|
|
|
|
private HubConnection Connection;
|
|
|
|
|
private readonly IServiceProvider applicationServices;
|
|
|
|
|
private readonly IConfiguration _cfg;
|
|
|
|
|
public string ConnectionId { get; private set; }
|
|
|
|
|
|
|
|
|
|
public NodeService(IServiceProvider applicationServices, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
this.applicationServices = applicationServices;
|
|
|
|
|
this._cfg = configuration;
|
|
|
|
|
this.ConnectionId = Guid.NewGuid().ToBase62();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
this.Connect();
|
|
|
|
|
await Task.Delay(10 * 1000).ConfigureAwait(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
|
|
|
|
|
public void Connect()
|
|
|
|
|
{
|
|
|
|
|
if (this._cfg.GetValue<bool>("notify:enabled", false))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("notify is enabled");
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("notify is disabled");
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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["sn"]}";
|
|
|
|
|
Console.WriteLine($"init connection for {url}");
|
|
|
|
|
if (this.Connection != null)
|
|
|
|
|
{
|
|
|
|
|
this.Connection.DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
this.Connection = new HubConnectionBuilder().WithUrl(url).Build();
|
|
|
|
|
this.Connection.Closed += ReConnect;
|
|
|
|
|
this.Connection.On(Methods.ServerToClient, (string method, string message, string fromConnectionId) => this.OnServerToClient(method, message, fromConnectionId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ServerToClient(string method, string message, string fromConnectionId)
|
|
|
|
|
{
|
|
|
|
|
this.OnServerToClient(method, method, fromConnectionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
|
|
|
|
|
public void ClientToServer(string method, string message, string fromConnectionId = null)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (this.Connection != null && this.Connection.State == HubConnectionState.Connected)
|
|
|
|
|
{
|
|
|
|
|
this.Connection.SendAsync(Methods.ClientToServer, method, message, fromConnectionId ?? this._cfg["sn"]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{_notifyHost} not connected");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnConnected()
|
|
|
|
|
/////////////////////////////
|
|
|
|
|
public void OnConnected()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{_notifyHost} OnConnected");
|
|
|
|
|
this.UpdateServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
|
|
|
|
|
public override void OnServerToClient(string method, string message, string fromConnectionId)
|
|
|
|
|
public void OnServerToClient(string method, string message, string fromConnectionId)
|
|
|
|
|
{
|
|
|
|
|
using var scope = this.applicationServices.CreateScope();
|
|
|
|
|
if (method == Methods.HealthCheckRequest)
|
|
|
|
@ -249,11 +393,11 @@ namespace IoT.Shared.Infrastructure
|
|
|
|
|
{
|
|
|
|
|
var model = message.FromJson<EditTimerModel>();
|
|
|
|
|
var nodeRepo = scope.ServiceProvider.GetService<IRepository<Node>>();
|
|
|
|
|
var timerRepo = scope.ServiceProvider.GetService<IRepository<Timer>>();
|
|
|
|
|
var timerRepo = scope.ServiceProvider.GetService<IRepository<IoTTimer>>();
|
|
|
|
|
var timer = timerRepo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
|
|
|
if (timer == null)
|
|
|
|
|
{
|
|
|
|
|
timer = new Timer
|
|
|
|
|
timer = new IoTTimer
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
};
|
|
|
|
@ -262,19 +406,19 @@ namespace IoT.Shared.Infrastructure
|
|
|
|
|
timer.FromDto(model);
|
|
|
|
|
timer.NodeId = nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == model.NodeNumber).Id;
|
|
|
|
|
timerRepo.SaveChanges();
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityUpdatedEvent<Timer>(timer));
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityUpdatedEvent<IoTTimer>(timer));
|
|
|
|
|
this.ClientToServer(Methods.EditTimerResponse, message);
|
|
|
|
|
}
|
|
|
|
|
else if (method == Methods.DeleteTimerRequest)
|
|
|
|
|
{
|
|
|
|
|
var model = message.FromJson<EditTimerModel>();
|
|
|
|
|
var timerRepo = scope.ServiceProvider.GetService<IRepository<Timer>>();
|
|
|
|
|
var timerRepo = scope.ServiceProvider.GetService<IRepository<IoTTimer>>();
|
|
|
|
|
var timer = timerRepo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
timerRepo.Delete(timer);
|
|
|
|
|
timerRepo.SaveChanges();
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityDeletedEvent<Timer>(timer));
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityDeletedEvent<IoTTimer>(timer));
|
|
|
|
|
}
|
|
|
|
|
this.ClientToServer(Methods.DeleteTimerResponse, message);
|
|
|
|
|
}
|
|
|
|
@ -282,11 +426,11 @@ namespace IoT.Shared.Infrastructure
|
|
|
|
|
{
|
|
|
|
|
var model = message.FromJson<EditTiggerModel>();
|
|
|
|
|
var nodeRepo = scope.ServiceProvider.GetService<IRepository<Node>>();
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<Tigger>>();
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<IoTTigger>>();
|
|
|
|
|
var entity = repo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
entity = new Tigger
|
|
|
|
|
entity = new IoTTigger
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
};
|
|
|
|
@ -295,19 +439,19 @@ namespace IoT.Shared.Infrastructure
|
|
|
|
|
entity.FromDto(model);
|
|
|
|
|
entity.NodeId = nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == model.NodeNumber).Id;
|
|
|
|
|
repo.SaveChanges();
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityUpdatedEvent<Tigger>(entity));
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityUpdatedEvent<IoTTigger>(entity));
|
|
|
|
|
this.ClientToServer(Methods.EditTiggerResponse, message);
|
|
|
|
|
}
|
|
|
|
|
else if (method == Methods.DeleteTiggerRequest)
|
|
|
|
|
{
|
|
|
|
|
var model = message.FromJson<EditTiggerModel>();
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<Tigger>>();
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<IoTTigger>>();
|
|
|
|
|
var entity = repo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
repo.Delete(entity);
|
|
|
|
|
repo.SaveChanges();
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityDeletedEvent<Tigger>(entity));
|
|
|
|
|
scope.ServiceProvider.GetService<IEventPublisher>().Publish(new EntityDeletedEvent<IoTTigger>(entity));
|
|
|
|
|
}
|
|
|
|
|
this.ClientToServer(Methods.DeleteTiggerResponse, message);
|
|
|
|
|
}
|
|
|
|
|