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.

105 lines
4.2 KiB

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<IRepository<DeviceInfo>>();
var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList();
foreach (var item in deviceInfos)
{
this.ClientToServer("UpdateDeviceInfo", item.ToJson());
}
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList();
foreach (var item in devices)
{
this.ClientToServer("UpdateDevice", item.ToJson());
}
}
}
public void SendDevice(Device device)
{
Task.Run(() =>
{
try
{
Console.WriteLine("send device to server");
this.Connection.SendAsync("ClientToServer", "UpdateDevice", device.ToJson(), null);
}
catch (Exception ex)
{
ex.PrintStack();
}
});
}
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<IRepository<DeviceInfo>>();
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<IRepository<DeviceInfo>>();
// 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<IConfiguration>();
// // var port = cfg["server.urls"].Split(':')[2];
// // var url = $"http://localhost:{port}{path}{queryString}";
// // var httpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient();
// // var result = httpClient.GetStringAsync(url).Result;
// // this.Connection.SendAsync("ClientToServer", "ApiCallback", result, connectionId);
// // }
// //});
//}
}
}