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/IoTNode/Services/IoTNodeClient.cs

388 lines
15 KiB

using Application.Domain.Entities;
using Infrastructure.Application.Services.Settings;
using Infrastructure.Data;
using Infrastructure.Domain;
using Infrastructure.Events;
using Infrastructure.Extensions;
using IoT.Shared.Application.Models;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace IoT.Shared.Services
{
public class IoTNodeClient : IHostedService, IDisposable
{
private bool disposed = false;
private string _notifyHost;
private HubConnection Connection;
private readonly IHostApplicationLifetime _lifetime;
private readonly IServiceProvider _applicationServices;
private readonly ILogger<IoTNodeClient> _logger;
private string _mac;
public IoTNodeClient(IHostApplicationLifetime lifetime, IServiceProvider applicationServices, ILogger<IoTNodeClient> logger)
{
this._mac = Helper.Instance.GetMacAddress();
this._lifetime = lifetime;
this._applicationServices = applicationServices;
this._logger = logger;
}
private string GetSetting(string name)
{
using var scope = _applicationServices.CreateScope();
return scope.ServiceProvider.GetRequiredService<ISettingService>().GetValue(name);
}
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;
}
public void Connect()
{
var code = GetSetting("code");
if (code != Helper.Instance.MacEncrypt(this._mac))
{
this._logger.LogError($"err: code {code} does not match mac {_mac}");
return;
}
var enable = GetSetting("notify:enabled");
if (enable == "true")
{
this._logger.LogDebug("notify is enabled");
try
{
var host = this.GetSetting("notify:host");
if (Connection == null)
{
this._logger.LogDebug("connection is null");
InitConnection();
}
if (Connection.State == HubConnectionState.Disconnected)
{
this._logger.LogDebug("start connect");
if (this._notifyHost != host)
{
InitConnection();
}
Connection.StartAsync().Wait();
this._logger.LogDebug($"{_notifyHost} OnConnected");
this.OnConnected();
}
else
{
if (this._notifyHost != host)
{
this.ReConnect(null);
}
else
{
this._logger.LogDebug($"connection has connected");
}
}
}
catch (Exception ex)
{
this._logger.LogError(ex, ex.Message);
}
try
{
var now = DateTime.Now;
if (now.Hour == 0 && now.Minute == 0 && now.Second > 0 && now.Second <= 10)
{
this._logger.LogInformation("upload at 00:00 everyday");
this.OnConnected();
}
}
catch (Exception ex)
{
this._logger.LogError(ex, ex.Message);
}
}
else
{
this._logger.LogDebug("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 = GetSetting("notify:host");
var url = $"{this._notifyHost}/hub?type=node&group={GetSetting("sn")}";
this._logger.LogDebug($"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 to, string from) => this.OnServerToClient(method, message, to, from));
}
public void ServerToClient(string method, string message, string to, string from)
{
this.OnServerToClient(method, message, to, from);
}
public void ClientToServer(string method, object data, string to, string from = null)
{
Task.Run(() =>
{
try
{
if (this.Connection != null && this.Connection.State == HubConnectionState.Connected)
{
this.Connection.SendAsync(Methods.ClientToServer, method, data.ToJson(), to, from ?? this.GetSetting("sn"));
}
else
{
this._logger.LogWarning($"{_notifyHost} not connected");
}
}
catch (Exception ex)
{
ex.PrintStack();
}
});
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
this.Close();
}
disposed = true;
}
public void OnConnected()
{
//上传网关
this.UpdateEntityIdList<IoTGateway>(null, $"{nameof(IoTGateway)}{nameof(EntityUpdated<IoTGateway>)}");
//上传产品
this.UpdateEntityIdList<IoTProduct>(null, $"{nameof(IoTProduct)}{nameof(EntityUpdated<IoTProduct>)}");
//上传设备Id列表、设备
this.UpdateEntityIdList<IoTDevice>(Methods.UpdateDeviceIdList, $"{nameof(IoTDevice)}{nameof(EntityUpdated<IoTDevice>)}");
//上传数据
this.UpdateEntityIdList<IoTData>(null, $"{nameof(IoTData)}EntityUpdated", null, o => !o.Hidden);
}
public void OnServerToClient(string method, string message, string to, string from)
{
try
{
using var scope = this._applicationServices.CreateScope();
var dataService = scope.ServiceProvider.GetService<DataService>();
var eventPublisher = scope.ServiceProvider.GetService<IEventPublisher>();
if (method == Methods.StopNode)
{
this._lifetime.StopApplication();
}
else if (method == Methods.UploadNode)
{
this.OnConnected();
}
else if (method == $"Edit{nameof(IoTGateway)}")//服务端编辑节点
{
var model = message.FromJson<EditIoTGatewayModel>();
dataService.Edit<IoTGateway, EditIoTGatewayModel>(model);
this.ClientToServer(method, model, null);
}
else if (method == $"Edit{nameof(IoTDevice)}")//服务端编辑设备
{
var model = message.FromJson<EditIoTDeviceModel>();
dataService.Edit<IoTDevice, EditIoTDeviceModel>(model);
this.ClientToServer(method, model, null);
}
else if (method == $"Delete{nameof(IoTDevice)}")//服务端删除设备
{
var model = message.FromJson<EditIoTDeviceModel>();
dataService.Delete<IoTDevice, EditIoTDeviceModel>(model);
this.ClientToServer(method, model, null);
}
else if (method == $"{nameof(IoTData)}{nameof(EntityDeleted<IoTData>)}" || method == $"{nameof(IoTData)}{nameof(EntityUpdated<IoTData>)}")//服务端插入或更新数据
{
var model = message.FromJson<EditIoTDataModel>();
dataService.Edit<IoTData, EditIoTDataModel>(model);
this.ClientToServer(method, model, null);
}
else if (method == $"{nameof(IoTData)}{nameof(EntityDeleted<IoTData>)}")//服务端删除数据
{
var model = message.FromJson<EditIoTDataModel>();
dataService.Delete<IoTData, EditIoTDataModel>(model);
this.ClientToServer(method, model, null);
}
else if (method == Methods.ExecApiRequest)
{
var cfg = scope.ServiceProvider.GetService<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}{message.FromJson<string>()}";
var httpClient = scope.ServiceProvider.GetService<IHttpClientFactory>().CreateClient();
var result = httpClient.GetStringAsync(url).Result;
this.ClientToServer(Methods.ExecApiResponse, result, from, to);
}
else if (method == Methods.UpdateCamera)
{
var model = message.FromJson<EditIoTDataModel>();
dataService.Edit<IoTData, EditIoTDataModel>(model);
this.ClientToServer("EditData", model, null);
}
}
catch (Exception ex)
{
ex.PrintStack();
}
}
public void UpdateEntityIdList<T>(string updateIdListMethod, string updateEntityMethod, Func<IQueryable<T>, IQueryable<T>> include = null, Func<T, bool> predicate = null) where T : BaseEntity
{
using var scope = this._applicationServices.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var query = repo.ReadOnlyTable();
if (include != null)
{
query = include(query);
}
if (predicate != null)
{
query = query.Where(predicate).AsQueryable();
}
var entities = query.ToList();
if (!string.IsNullOrEmpty(updateIdListMethod))
{
this.ClientToServer(updateIdListMethod, entities.Select(o => o.Id).ToList(), null);
}
if (!string.IsNullOrEmpty(updateEntityMethod))
{
foreach (var entity in entities)
{
this.ClientToServer(updateEntityMethod, entity, null);
}
}
}
public string GetApiJson(string prefix)
{
try
{
using var scope = _applicationServices.CreateScope();
var serviceProvider = scope.ServiceProvider;
var cfg = serviceProvider.GetService<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}/swagger/v1/swagger.json";
var hc = serviceProvider.GetService<IHttpClientFactory>().CreateClient();
var result = hc.GetStringAsync(url).Result;
var json = JsonConvert.DeserializeObject(result) as JObject;
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
var names = paths.Properties().Select(o => o.Name).ToList();
foreach (var item in names)
{
if (!item.StartsWith(prefix))
{
paths.Remove(item);
}
}
var tags = json.Properties().FirstOrDefault(o => o.Name == "tags").Value as JArray;
var names2 = tags.Select(o => (o as JObject).Properties().FirstOrDefault(o => o.Name == "name").Value.ToString()).ToList();
foreach (var item in names2)
{
if (item != prefix.Trim('/'))
{
tags.Remove(tags.FirstOrDefault(o => (o as JObject).Properties().FirstOrDefault(o => o.Name == "name").Value.ToString() != prefix.Trim('/')));
}
}
var realResult = JsonConvert.SerializeObject(json);
return realResult;
}
catch (Exception ex)
{
ex.PrintStack();
return null;
}
}
private void Delay(int commandDelay)
{
using var scope = _applicationServices.CreateScope();
var serviceProvider = scope.ServiceProvider;
var settingService = serviceProvider.GetService<ISettingService>();
var delay = 0;
try
{
delay = Convert.ToInt32(settingService.GetValue("delay"));
}
catch (Exception ex)
{
ex.PrintStack();
this._logger.LogError(ex.ToString());
}
if (commandDelay > 0)
{
delay += commandDelay;
}
if (delay > 0)
{
Thread.Sleep(delay);
}
}
}
}