using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.SignalR; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Readers; using System; using System.Linq; namespace IoTCenter.Services { public class PageHub : BasePageHub { private readonly IServiceProvider _applicationService; public PageHub(IServiceProvider applicationService) { this._applicationService = applicationService; } public override void ClientToServer(string method, string message, string connectionId) { if (method == "UpdateDeviceInfo") { this.UpdateDeviceInfo(message); } else if (method == "UpdateDevice") { this.UpdateDevice(message); } else if (message == "ApiCallback") { this.ApiCallback(message, connectionId); } } private void UpdateDeviceInfo(string message) { var newDeviceInfo = message.FromJson(); using (var scope = this._applicationService.CreateScope()) { var deviceInfoRepo = scope.ServiceProvider.GetService>(); var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == newDeviceInfo.Number); if (deviceInfo == null) { deviceInfo = new DeviceInfo().From(newDeviceInfo); deviceInfoRepo.Add(deviceInfo); if (!string.IsNullOrEmpty(deviceInfo.ApiJson)) { this.UpdateApi(deviceInfo); } deviceInfoRepo.SaveChanges(); } } } private void UpdateDevice(string message) { //var newDevice = message.FromJson(); //using (var scope = this._applicationService.CreateScope()) //{ // var deviceInfoRepo = scope.ServiceProvider.GetService>(); // var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == newDevice.InfoNumber); // if (deviceInfo == null) // { // deviceInfo = new DeviceInfo // { // Name = newDevice.Name, // Number = newDevice.InfoNumber // }; // deviceInfoRepo.Add(deviceInfo); // deviceInfoRepo.SaveChanges(); // } // var nodeRepo = scope.ServiceProvider.GetService>(); // var node = nodeRepo.Table().FirstOrDefault(o => o.Number == newDevice.NodeNumber); // if (node == null) // { // node = new Node // { // Number = newDevice.NodeNumber, // Name = newDevice.NodeNumber // }; // nodeRepo.Add(node); // nodeRepo.SaveChanges(); // } // var deviceRepo = scope.ServiceProvider.GetService>(); // var device = deviceRepo.Table().FirstOrDefault(o => o.Number == newDevice.Number); // if (device == null) // { // device = new Device // { // }; // } //} } public void ApiCallback(string message, string connectionId) { if (!string.IsNullOrEmpty(connectionId)) { this.ServerToClient(connectionId, "ApiCallback", message); } } private void UpdateApi(DeviceInfo deviceInfo) { try { var reader = new OpenApiStringReader(new OpenApiReaderSettings { }).Read(deviceInfo.ApiJson, out OpenApiDiagnostic diagnostic); foreach (var path in reader.Paths) { foreach (var operation in path.Value.Operations) { if (!deviceInfo.Apis.Any(o => o.Name == operation.Value.Summary)) { var postion = path.Key.LastIndexOf('/') + 1; var api = new Api { Path = path.Key.Substring(0, postion), Command = path.Key.Substring(postion), Name = operation.Value.Summary, Method = operation.Key.ToString() }; deviceInfo.Apis.Add(api); foreach (var parameter in operation.Value.Parameters) { if (!api.Parameters.Any(o => o.Name == parameter.Name)) { api.Parameters.Add(new Parameter { Name = parameter.Name, Description = parameter.Description, Required = parameter.Required, Type = parameter.Schema.Type, Minimum = parameter.Schema.Minimum?.ToString(), Maxinum = parameter.Schema.Maximum?.ToString() }); } } } } } } catch (Exception ex) { ex.PrintStack(); } } } }