diff --git a/projects/Infrastructure/Web/SignalR/BasePageHub.cs b/projects/Infrastructure/Web/SignalR/BasePageHub.cs index 22e06ce2..33a8a6d3 100644 --- a/projects/Infrastructure/Web/SignalR/BasePageHub.cs +++ b/projects/Infrastructure/Web/SignalR/BasePageHub.cs @@ -25,12 +25,12 @@ namespace Infrastructure.Web.SignalR return base.OnDisconnectedAsync(exception); } - public virtual void ServerToClient(string group, string method, string message, string connectionId = null) + public virtual void ServerToClient(string group, string method, string message, string fromConnectinId = null) { - Clients.Group(group).SendAsync(method, message, connectionId); + Clients.Group(group).SendAsync(method, message, fromConnectinId); } - public virtual void ClientToServer(string method, string message, string connectionId) + public virtual void OnClientToServer(string method, string message, string connectionId) { } } diff --git a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs index d4eb808c..a5c73d59 100644 --- a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs +++ b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs @@ -55,7 +55,7 @@ namespace IoT.Shared.Infrastructure InitConnection(); } Connection.StartAsync().Wait(); - this.Connected(); + this.OnConnected(); } else { @@ -80,7 +80,7 @@ namespace IoT.Shared.Infrastructure } } - public virtual void Connected() + public virtual void OnConnected() { } @@ -118,9 +118,29 @@ namespace IoT.Shared.Infrastructure } 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) + { + Task.Run(() => + { + try + { + this.Connection.SendAsync("ClientToServer", method, message); + } + catch (Exception ex) + { + ex.PrintStack(); + } + }); + } + public void Dispose() { this._tokenSource.Cancel(); diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs index 6ff9affb..450992ea 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; namespace FBeeService @@ -19,7 +18,7 @@ namespace FBeeService { } - public override void Connected() + public override void OnConnected() { using (var scope = this.applicationServices.CreateScope()) { @@ -27,34 +26,18 @@ namespace FBeeService var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList(); foreach (var item in deviceInfos) { - this.SendDeviceInfo(item); + this.ClientToServer("UpdateDeviceInfo", item.ToJson()); } var deviceRepo = scope.ServiceProvider.GetService>(); var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList(); foreach (var item in devices) { - this.SendDevice(item); + this.ClientToServer("UpdateDevice", item.ToJson()); } } } - public void SendDeviceInfo(DeviceInfo deviceInfo) - { - Task.Run(() => - { - try - { - Console.WriteLine("fbee service> send device info to server"); - this.Connection.SendAsync("ClientToServer", "UpdateDeviceInfo", deviceInfo.ToJson(), null); - } - catch (Exception ex) - { - ex.PrintStack(); - } - }); - } - public void SendDevice(Device device) { Task.Run(() => @@ -71,21 +54,52 @@ namespace FBeeService }); } - public override void ConnectionOn() + public override void OnServerToClient(string method, string message, string fromConnectionId) { - this.Connection.On("ServerToClient", (string path, string queryString, string connectionId) => + if (method == "GetDeviceInfo") { + var infoNumber = message; using (var scope = this.applicationServices.CreateScope()) { - var serviceProvider = scope.ServiceProvider; - var cfg = serviceProvider.GetService(); - var port = cfg["server.urls"].Split(':')[2]; - var url = $"http://localhost:{port}{path}{queryString}"; - var httpClient = serviceProvider.GetService().CreateClient(); - var result = httpClient.GetStringAsync(url).Result; - this.Connection.SendAsync("ClientToServer", "ApiCallback", result, connectionId); + var deviceInfoRepo = scope.ServiceProvider.GetService>(); + var deviceInfo = deviceInfoRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == infoNumber); + if (deviceInfo == null) + { + throw new Exception($"not has infoNumber:{infoNumber}"); + } + this.ClientToServer("UpdateDeviceInfo", deviceInfo.ToJson()); } - }); + } } + + //public override void ConnectionOn() + //{ + // this.Connection.On("GetDeviceInfo", (string infoNumber) => + // { + // using (var scope = this.applicationServices.CreateScope()) + // { + // var deviceInfoRepo = scope.ServiceProvider.GetService>(); + // var deviceInfo = deviceInfoRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == infoNumber); + // if (deviceInfo == null) + // { + // throw new Exception($"not has infoNumber:{infoNumber}"); + // } + // this.SendDeviceInfo(deviceInfo); + // } + // }); + // //this.Connection.On("CallApi", (string path, string queryString, string connectionId) => + // //{ + // // using (var scope = this.applicationServices.CreateScope()) + // // { + // // var serviceProvider = scope.ServiceProvider; + // // var cfg = serviceProvider.GetService(); + // // var port = cfg["server.urls"].Split(':')[2]; + // // var url = $"http://localhost:{port}{path}{queryString}"; + // // var httpClient = serviceProvider.GetService().CreateClient(); + // // var result = httpClient.GetStringAsync(url).Result; + // // this.Connection.SendAsync("ClientToServer", "ApiCallback", result, connectionId); + // // } + // //}); + //} } } \ No newline at end of file diff --git a/projects/IoTCenter/Services/PageHub.cs b/projects/IoTCenter/Services/PageHub.cs index 4ffc8c25..a1f53735 100644 --- a/projects/IoTCenter/Services/PageHub.cs +++ b/projects/IoTCenter/Services/PageHub.cs @@ -2,6 +2,7 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.SignalR; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Readers; using System; @@ -18,7 +19,7 @@ namespace IoTCenter.Services this._applicationService = applicationService; } - public override void ClientToServer(string method, string message, string connectionId) + public override void OnClientToServer(string method, string message, string connectionId) { if (method == "UpdateDeviceInfo") { @@ -28,10 +29,6 @@ namespace IoTCenter.Services { this.UpdateDevice(message); } - else if (message == "ApiCallback") - { - this.ApiCallback(message, connectionId); - } } private void UpdateDeviceInfo(string message) @@ -64,42 +61,62 @@ namespace IoTCenter.Services 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 - // { - // }; - // } - //} + try + { + Console.WriteLine("iot center> receive device"); + 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) + { + this.ServerToClient(newDevice.ConnectId, "GetDeviceInfo", newDevice.InfoNumber); + throw new Exception("need device info"); + } + 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().Include(o => o.Data).FirstOrDefault(o => o.Number == newDevice.Number); + if (device == null) + { + device = new Device().From(newDevice); + device.InfoId = deviceInfo.Id; + device.NodeId = node.Id; + deviceRepo.Add(device); + } + else + { + foreach (var newData in newDevice.Data) + { + var data = device.Data.FirstOrDefault(o => o.Key == newData.Key); + if (data == null) + { + data = new Data().From(data); + data.DeviceId = device.Id; + device.Data.Add(data); + } + data.From(newDevice); + data.DeviceId = device.Id; + } + } + deviceRepo.SaveChanges(); + } + } + catch (Exception ex) + { + ex.PrintStack(); + } } public void ApiCallback(string message, string connectionId)