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.

66 lines
2.1 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.Net.Http;
using System.Threading.Tasks;
namespace FBeeService
{
public class ClientService : BaseClientService
{
public ClientService(IServiceProvider applicationServices, IConfiguration configuration) : base(applicationServices, configuration)
{
}
public override void Connected()
{
using (var scope = this.applicationServices.CreateScope())
{
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var devices = deviceRepo.Table().Include(o => o.Data).ToList();
foreach (var device in devices)
{
this.Notify(device);
}
}
}
public void Notify(Device device)
{
Task.Run(() =>
{
try
{
Console.WriteLine("notify to server");
this.Connection.SendAsync("ClientToServer", device.ToJson());
}
catch (Exception ex)
{
ex.PrintStack();
}
});
}
public override void ConnectionOn()
{
this.Connection.On("ServerToClient", (string path, string queryString) =>
{
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();
}
});
}
}
}