|
@@ -3,13 +3,16 @@ using SqlSugarORM;
|
|
|
|
|
|
namespace EEMSServerCore.Hubs;
|
|
|
|
|
|
-internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider, SqlSugarCustom orm) : Hub, IClientCaller
|
|
|
+internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider, SqlSugarCustom orm, IUIProvider uiCaller) : Hub, IClientCaller
|
|
|
{
|
|
|
public override Task OnConnectedAsync()
|
|
|
{
|
|
|
if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
|
|
|
provider?.OnConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
|
|
|
|
|
|
+ if (deviceManager.TryGetDevice(Context.ConnectionId, out DeviceInfo? device) && device is not null && device.Guid is not null)
|
|
|
+ uiCaller.OnlineStatusNotify(device.Guid.Value, true);
|
|
|
+
|
|
|
return base.OnConnectedAsync();
|
|
|
}
|
|
|
|
|
@@ -18,7 +21,10 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
|
|
|
deviceManager.RemoveDevice(Context.ConnectionId, out DeviceInfo? device);
|
|
|
|
|
|
if (device is not null && device.Guid.HasValue)
|
|
|
+ {
|
|
|
ClientManager.DeviceClients.TryRemove(device.Guid.Value, out _);
|
|
|
+ uiCaller.OnlineStatusNotify(device.Guid.Value, false);
|
|
|
+ }
|
|
|
|
|
|
if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
|
|
|
provider?.OnDisConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
|
|
@@ -28,20 +34,30 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
|
|
|
|
|
|
public Task<Guid> RegisterDevice(DeviceInfo deviceInfo)
|
|
|
{
|
|
|
- deviceInfo.Guid ??= Guid.NewGuid();
|
|
|
deviceInfo.IP ??= string.Empty;
|
|
|
+ bool newDevice = (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty);
|
|
|
+ deviceInfo.Guid ??= Guid.NewGuid();
|
|
|
|
|
|
if (!orm.AddOrUpdate("Devices", deviceInfo, t => t.Guid == deviceInfo.Guid))
|
|
|
return Task.FromResult(Guid.Empty);
|
|
|
|
|
|
+
|
|
|
deviceManager.LoginDevice(Context.ConnectionId, deviceInfo);
|
|
|
ClientManager.DeviceClients[deviceInfo.Guid.Value] = Clients.Caller;
|
|
|
+
|
|
|
+ if (newDevice)
|
|
|
+ uiCaller.InsertNewDevice(deviceInfo);
|
|
|
+ else
|
|
|
+ uiCaller.UpdateDevice(deviceInfo);
|
|
|
+
|
|
|
return Task.FromResult(deviceInfo.Guid.Value);
|
|
|
}
|
|
|
|
|
|
public Task<bool> UpdateRealTimeData(Dictionary<string, object> realtimeData)
|
|
|
{
|
|
|
+ if (!deviceManager.TryGetDevice(Context.ConnectionId, out DeviceInfo? device) || device is null || device.Guid is null)
|
|
|
+ return Task.FromResult(false);
|
|
|
|
|
|
- return Task.FromResult(true);
|
|
|
+ return uiCaller.UpdateRealtimeData(device.Guid.Value, realtimeData);
|
|
|
}
|
|
|
}
|