diff --git a/projects/Application/Domain/Statistic.cs b/projects/Application/Domain/Statistic.cs index 6b463dd0..c511fc38 100644 --- a/projects/Application/Domain/Statistic.cs +++ b/projects/Application/Domain/Statistic.cs @@ -9,7 +9,7 @@ namespace Application.Domain { public class Statistic:BaseEntity { - public string Group { get; set; } + public string Key { get; set; } public string Value { get; set; } } } diff --git a/projects/IoTNode/DeviceServices/BaseDeviceService.cs b/projects/IoTNode/DeviceServices/BaseDeviceService.cs index e39f4e8c..c4ab28d2 100644 --- a/projects/IoTNode/DeviceServices/BaseDeviceService.cs +++ b/projects/IoTNode/DeviceServices/BaseDeviceService.cs @@ -66,8 +66,57 @@ namespace IoTNode.DeviceServices { return Task.CompletedTask; } + public IoTProduct UpdateProduct(string productName, string productNumber, string path, string productIcon) + { + var scope = _applicationServices.CreateScope(); + // + var productRepo = scope.ServiceProvider.GetService>(); + var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == productNumber); + if (product == null) + { + product = new IoTProduct + { + Id = $"productid-{productNumber}".ToGuid(), + Number = productNumber, + Name = productName, + Path = path, + Image = $"/images/{productIcon}.svg" + }; + if (!string.IsNullOrEmpty(path)) + { + var iotNodeClient = scope.ServiceProvider.GetService(); + product.ApiJson = iotNodeClient.GetApiJson(path); + //OpenApiService.UpdateApi(product); + } + productRepo.Add(product); + productRepo.SaveChanges(); + } + // + if (this.GetSetting("notify:enabled") == "true") + { + try + { + var settingRepo = scope.ServiceProvider.GetRequiredService(); + var factory = scope.ServiceProvider.GetRequiredService(); + var notifyHost = settingRepo.GetValue("notify:host"); + var url = $"{notifyHost}/Server/HasProduct/{productNumber}"; + var httpClient = factory.CreateClient(); + var response = httpClient.GetStringAsync(url).Result.FromJson(); + if (!response) + { + scope.ServiceProvider.GetRequiredService().ClientToServer($"{nameof(IoTProduct)}EntityUpdated", product, null); + } + } + catch (Exception ex) + { + ex.PrintStack(); + scope.ServiceProvider.GetRequiredService>().LogError(ex.ToString()); + } + + } + return product; + } - //start public void DeleteDevice(Guid id) { using var scope = _applicationServices.CreateScope(); @@ -95,13 +144,6 @@ namespace IoTNode.DeviceServices return repo.ReadOnlyTable().FirstOrDefault(o => o.Number == number); } - public IoTData GetIoTData(string deviceNumber, DataKeys key) - { - using var scope = _applicationServices.CreateScope(); - var repo = scope.ServiceProvider.GetRequiredService>(); - var name = key.GetName(); - return repo.ReadOnlyTable().FirstOrDefault(o => o.IoTDevice.Number == deviceNumber && o.Key == name); - } public string GetIoTDataValue(Guid deviceId, DataKeys key) { using var scope = _applicationServices.CreateScope(); @@ -112,18 +154,6 @@ namespace IoTNode.DeviceServices .Select(o=>o.Value) .FirstOrDefault(); } - - public string GetIoTDataValue(string deviceNumber, DataKeys key) - { - using var scope = _applicationServices.CreateScope(); - var repo = scope.ServiceProvider.GetRequiredService>(); - var name = key.GetName(); - return repo.ReadOnlyTable() - .Where(o => o.IoTDevice.Number == deviceNumber && o.Key == name) - .Select(o=>o.Value) - .FirstOrDefault(); - - } public void AddIoTData(Guid deviceId, DataKeys key, object value, string description = "") { using var scope = _applicationServices.CreateScope(); @@ -158,7 +188,6 @@ namespace IoTNode.DeviceServices this.UpdateIoTDataValue(data, value); repo.SaveChanges(); } - //end private void UpdateIoTDataValue(IoTData data,object value) { @@ -210,71 +239,5 @@ namespace IoTNode.DeviceServices }; return data; } - public IoTProduct UpdateProduct(string productName, string productNumber, string path, string productIcon) - { - var scope = _applicationServices.CreateScope(); - // - var productRepo = scope.ServiceProvider.GetService>(); - var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == productNumber); - if (product == null) - { - product = new IoTProduct - { - Id = $"productid-{productNumber}".ToGuid(), - Number = productNumber, - Name = productName, - Path = path, - Image = $"/images/{productIcon}.svg" - }; - if (!string.IsNullOrEmpty(path)) - { - var iotNodeClient = scope.ServiceProvider.GetService(); - product.ApiJson = iotNodeClient.GetApiJson(path); - //OpenApiService.UpdateApi(product); - } - productRepo.Add(product); - productRepo.SaveChanges(); - } - // - if (this.GetSetting("notify:enabled") == "true") - { - try - { - var settingRepo = scope.ServiceProvider.GetRequiredService(); - var factory = scope.ServiceProvider.GetRequiredService(); - var notifyHost = settingRepo.GetValue("notify:host"); - var url = $"{notifyHost}/Server/HasProduct/{productNumber}"; - var httpClient = factory.CreateClient(); - var response = httpClient.GetStringAsync(url).Result.FromJson(); - if (!response) - { - scope.ServiceProvider.GetRequiredService().ClientToServer($"{nameof(IoTProduct)}EntityUpdated", product, null); - } - } - catch (Exception ex) - { - ex.PrintStack(); - scope.ServiceProvider.GetRequiredService>().LogError(ex.ToString()); - } - - } - return product; - } - - public void SendToServer(string method, object data) - { - Debug.WriteLine("send device to server"); - using var scope = _applicationServices.CreateScope(); - var iotNodeClient = scope.ServiceProvider.GetService(); - iotNodeClient.ClientToServer(method, data, null); - } - - public void SendDataToServer(IoTData data) - { - using var scope = _applicationServices.CreateScope(); - var iotNodeClient = scope.ServiceProvider.GetService(); - iotNodeClient.ClientToServer($"Edit{typeof(IoTData).Name}", data.To(), null); - } - } } diff --git a/projects/IoTNode/Services/IoTNodeEventHandler.cs b/projects/IoTNode/Services/IoTNodeEventHandler.cs index 926dde19..54d2d2f5 100644 --- a/projects/IoTNode/Services/IoTNodeEventHandler.cs +++ b/projects/IoTNode/Services/IoTNodeEventHandler.cs @@ -21,11 +21,13 @@ namespace IoTNode.Services IEventHander>, IEventHander>, IEventHander> - { private readonly ILogger _logger; + private readonly IHubContext _hub; + private readonly OnvifService _onvifService; + private readonly IoTNodeClient _ioTNodeClient; public IoTNodeEventHandler(ILogger logger, @@ -59,14 +61,17 @@ namespace IoTNode.Services this._ioTNodeClient.Connect(); } } + public void Handle(EntityInsertedEvent message) { this.Notify(message); } + public void Handle(EntityUpdatedEvent message) { this.Notify(message); } + public void Handle(EntityDeletedEvent message) { this.Notify(message); @@ -93,7 +98,7 @@ namespace IoTNode.Services { try { - _hub.Clients.Group("page").SendAsync(Methods.ServerToClient, $"{typeof(T).Name}{message.Arg}", message.Data.ToJson(true), null,null); + _hub.Clients.Group("page").SendAsync(Methods.ServerToClient, $"{typeof(T).Name}{message.Arg}", message.Data.ToJson(true), null, null); } catch (Exception ex) { @@ -108,7 +113,7 @@ namespace IoTNode.Services { try { - this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}",message.Data,null); + this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}", message.Data, null); } catch (Exception ex) {