using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Infrastructure; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading.Tasks; namespace FBeeService { public class ClientService : BaseClientService { public ClientService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration) { } public override void OnConnected() { using (var scope = this.applicationServices.CreateScope()) { var deviceInfoRepo = scope.ServiceProvider.GetService>(); var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList(); foreach (var deviceInfo in deviceInfos) { this.SendDeviceInfo(deviceInfo); } var deviceRepo = scope.ServiceProvider.GetService>(); var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList(); foreach (var device in devices) { this.SendDevice(device); } } } public override void OnServerToClient(string method, string message, string fromConnectionId) { if (method == "GetDeviceInfo") { var infoNumber = message; 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); } } } public void SendDevice(Device device) { Task.Run(() => { try { Console.WriteLine("send device to server"); this.ClientToServer("UpdateDevice", device.ToJson()); } catch (Exception ex) { ex.PrintStack(); } }); } public void SendDeviceInfo(DeviceInfo deviceInfo) { Task.Run(() => { try { Console.WriteLine("send device to server"); this.ClientToServer("UpdateDeviceInfo", deviceInfo.ToJson()); } catch (Exception ex) { ex.PrintStack(); } }); } //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); // // } // //}); //} } }