Former-commit-id: 79ef37484ac1b273240d58cd9a2d8af60a1035ac
TangShanKaiPing
wanggang 6 years ago
parent ebf2d57da3
commit 40bb8f4a62

@ -25,12 +25,12 @@ namespace Infrastructure.Web.SignalR
return base.OnDisconnectedAsync(exception); 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)
{ {
} }
} }

@ -55,7 +55,7 @@ namespace IoT.Shared.Infrastructure
InitConnection(); InitConnection();
} }
Connection.StartAsync().Wait(); Connection.StartAsync().Wait();
this.Connected(); this.OnConnected();
} }
else 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 = new HubConnectionBuilder().WithUrl(url).Build();
this.Connection.Closed += ReConnect; this.Connection.Closed += ReConnect;
this.Connection.On("ServerToClient", (string method, string message, string fromConnectionId) => this.OnServerToClient(method, message, fromConnectionId));
this.ConnectionOn(); 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() public void Dispose()
{ {
this._tokenSource.Cancel(); this._tokenSource.Cancel();

@ -8,7 +8,6 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FBeeService namespace FBeeService
@ -19,7 +18,7 @@ namespace FBeeService
{ {
} }
public override void Connected() public override void OnConnected()
{ {
using (var scope = this.applicationServices.CreateScope()) using (var scope = this.applicationServices.CreateScope())
{ {
@ -27,34 +26,18 @@ namespace FBeeService
var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList(); var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList();
foreach (var item in deviceInfos) foreach (var item in deviceInfos)
{ {
this.SendDeviceInfo(item); this.ClientToServer("UpdateDeviceInfo", item.ToJson());
} }
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList(); var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList();
foreach (var item in devices) 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) public void SendDevice(Device device)
{ {
Task.Run(() => 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()) using (var scope = this.applicationServices.CreateScope())
{ {
var serviceProvider = scope.ServiceProvider; var deviceInfoRepo = scope.ServiceProvider.GetService<IRepository<DeviceInfo>>();
var cfg = serviceProvider.GetService<IConfiguration>(); var deviceInfo = deviceInfoRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == infoNumber);
var port = cfg["server.urls"].Split(':')[2]; if (deviceInfo == null)
var url = $"http://localhost:{port}{path}{queryString}"; {
var httpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient(); throw new Exception($"not has infoNumber:{infoNumber}");
var result = httpClient.GetStringAsync(url).Result; }
this.Connection.SendAsync("ClientToServer", "ApiCallback", result, connectionId); 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);
// // }
// //});
//}
} }
} }

@ -2,6 +2,7 @@
using Infrastructure.Data; using Infrastructure.Data;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Infrastructure.Web.SignalR; using Infrastructure.Web.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Readers;
using System; using System;
@ -18,7 +19,7 @@ namespace IoTCenter.Services
this._applicationService = applicationService; 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") if (method == "UpdateDeviceInfo")
{ {
@ -28,10 +29,6 @@ namespace IoTCenter.Services
{ {
this.UpdateDevice(message); this.UpdateDevice(message);
} }
else if (message == "ApiCallback")
{
this.ApiCallback(message, connectionId);
}
} }
private void UpdateDeviceInfo(string message) private void UpdateDeviceInfo(string message)
@ -64,42 +61,62 @@ namespace IoTCenter.Services
private void UpdateDevice(string message) private void UpdateDevice(string message)
{ {
//var newDevice = message.FromJson<Device>(); try
//using (var scope = this._applicationService.CreateScope()) {
//{ Console.WriteLine("iot center> receive device");
// var deviceInfoRepo = scope.ServiceProvider.GetService<IRepository<DeviceInfo>>(); var newDevice = message.FromJson<Device>();
// var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == newDevice.InfoNumber); using (var scope = this._applicationService.CreateScope())
// if (deviceInfo == null) {
// { var deviceInfoRepo = scope.ServiceProvider.GetService<IRepository<DeviceInfo>>();
// deviceInfo = new DeviceInfo var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == newDevice.InfoNumber);
// { if (deviceInfo == null)
// Name = newDevice.Name, {
// Number = newDevice.InfoNumber this.ServerToClient(newDevice.ConnectId, "GetDeviceInfo", newDevice.InfoNumber);
// }; throw new Exception("need device info");
// deviceInfoRepo.Add(deviceInfo); }
// deviceInfoRepo.SaveChanges(); var nodeRepo = scope.ServiceProvider.GetService<IRepository<Node>>();
// } var node = nodeRepo.Table().FirstOrDefault(o => o.Number == newDevice.NodeNumber);
// var nodeRepo = scope.ServiceProvider.GetService<IRepository<Node>>(); if (node == null)
// var node = nodeRepo.Table().FirstOrDefault(o => o.Number == newDevice.NodeNumber); {
// if (node == null) node = new Node
// { {
// node = new Node Number = newDevice.NodeNumber,
// { Name = newDevice.NodeNumber
// Number = newDevice.NodeNumber, };
// Name = newDevice.NodeNumber nodeRepo.Add(node);
// }; nodeRepo.SaveChanges();
// nodeRepo.Add(node); }
// nodeRepo.SaveChanges(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
// } var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == newDevice.Number);
// var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); if (device == null)
// var device = deviceRepo.Table().FirstOrDefault(o => o.Number == newDevice.Number); {
// if (device == null) device = new Device().From(newDevice);
// { device.InfoId = deviceInfo.Id;
// device = new Device 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) public void ApiCallback(string message, string connectionId)

Loading…
Cancel
Save