97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using RobotNet10.RobotApp.Client.Shared.Devices;
|
|
using RobotNet10.RobotApp.Devices;
|
|
|
|
namespace RobotNet10.RobotApp.Hubs;
|
|
|
|
/// <summary>
|
|
/// SignalR Hub cho DeviceProvider - cung cấp real-time device information
|
|
/// </summary>
|
|
[Authorize]
|
|
public class DeviceHub(IDeviceProvider deviceProvider) : Hub
|
|
{
|
|
/// <summary>
|
|
/// Lấy tất cả devices
|
|
/// </summary>
|
|
public Task<DeviceDto[]> GetAllDevices()
|
|
{
|
|
var devices = deviceProvider.GetAllDevices();
|
|
return Task.FromResult(devices.Select(MapToDto).ToArray());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy device theo ID
|
|
/// </summary>
|
|
public Task<DeviceDto?> GetDevice(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
return Task.FromResult(device != null ? MapToDto(device) : null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy devices theo type
|
|
/// </summary>
|
|
public Task<DeviceDto[]> GetDevicesByType(DeviceType deviceType)
|
|
{
|
|
var serverType = deviceType;
|
|
var devices = deviceProvider.GetDevicesByType(serverType);
|
|
return Task.FromResult(devices.Select(MapToDto).ToArray());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy số lượng devices
|
|
/// </summary>
|
|
public Task<int> GetDeviceCount()
|
|
{
|
|
return Task.FromResult(deviceProvider.GetDeviceCount());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Map DeviceBase sang DeviceDto
|
|
/// </summary>
|
|
private static DeviceDto MapToDto(DeviceBase device)
|
|
{
|
|
return new DeviceDto
|
|
{
|
|
DeviceId = device.DeviceId,
|
|
DeviceName = device.DeviceName,
|
|
DeviceType = device.Type,
|
|
Description = device.Description,
|
|
Status = device.Status,
|
|
IsConnected = device.IsConnected,
|
|
LastUpdateTime = device.LastUpdateStateTime,
|
|
LastConnectedTime = device.LastConnectedTime,
|
|
LastDisconnectedTime = device.LastDisconnectedTime,
|
|
LastError = device.LastError?.Message,
|
|
ReconnectAttemptCount = device.ReconnectAttemptCount,
|
|
AutoReconnectEnabled = device.AutoReconnectEnabled,
|
|
ReconnectDelayMs = device.ReconnectDelayMs,
|
|
MaxReconnectAttempts = device.MaxReconnectAttempts,
|
|
PropertyDescriptions = device.PropertyDescriptions.Select(MapPropertyDescription).ToList(),
|
|
Properties = device.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Map PropertyDescription sang PropertyDescriptionDto
|
|
/// </summary>
|
|
private static PropertyDescription MapPropertyDescription(PropertyDescription prop)
|
|
{
|
|
return new PropertyDescription
|
|
{
|
|
Key = prop.Key,
|
|
DisplayName = prop.DisplayName,
|
|
Description = prop.Description,
|
|
DataType = prop.DataType,
|
|
Unit = prop.Unit,
|
|
DefaultValue = prop.DefaultValue,
|
|
IsReadOnly = prop.IsReadOnly,
|
|
DisplayOrder = prop.DisplayOrder,
|
|
Category = prop.Category,
|
|
Format = prop.Format
|
|
};
|
|
}
|
|
}
|
|
|