|
|
|
@ -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<IRepository<Device>>();
|
|
|
|
|
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<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);
|
|
|
|
|
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);
|
|
|
|
|
// // }
|
|
|
|
|
// //});
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|