107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using RobotNet10.Shared.Geometry;
|
|
using RobotNet10.RobotApp.Devices;
|
|
using RobotNet10.Shared.Sensor;
|
|
using RobotNet10.RobotApp.Client.Shared.Devices;
|
|
|
|
namespace RobotNet10.RobotApp.Hubs;
|
|
|
|
/// <summary>
|
|
/// SignalR Hub cho IMU device - cung cấp real-time IMU data
|
|
/// </summary>
|
|
[Authorize]
|
|
public class InertialMeasurementUnitHub(IDeviceProvider deviceProvider) : Hub
|
|
{
|
|
/// <summary>
|
|
/// Lấy thông tin device (DeviceName) theo device ID
|
|
/// </summary>
|
|
public async Task<DeviceInfoDto?> GetDeviceInfo(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
if (device is not IInertialMeasurementUnit)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new DeviceInfoDto
|
|
{
|
|
DeviceId = device.DeviceId,
|
|
DeviceName = device.DeviceName
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy thông tin IMU theo device ID
|
|
/// </summary>
|
|
public async Task<Imu?> GetImuData(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
if (device is not IInertialMeasurementUnit imu)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await imu.ReadAllDataAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Đọc tất cả dữ liệu IMU
|
|
/// </summary>
|
|
public async Task<Imu?> ReadAllData(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
if (device is not IInertialMeasurementUnit imu)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await imu.ReadAllDataAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy dictionary properties hiện tại của device (để hiển thị trên UI)
|
|
/// </summary>
|
|
public Task<Dictionary<string, string>?> GetDeviceProperties(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
if (device is not IInertialMeasurementUnit)
|
|
{
|
|
return Task.FromResult<Dictionary<string, string>?>(null);
|
|
}
|
|
|
|
var props = device.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
|
|
return Task.FromResult<Dictionary<string, string>?>(props);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lấy danh sách mô tả properties của device (để hiển thị trên UI)
|
|
/// </summary>
|
|
public Task<List<PropertyDescription>?> GetDevicePropertyDescriptions(string deviceId)
|
|
{
|
|
var device = deviceProvider.GetDevice(deviceId);
|
|
if (device is not IInertialMeasurementUnit)
|
|
{
|
|
return Task.FromResult<List<PropertyDescription>?>(null);
|
|
}
|
|
|
|
var list = device.PropertyDescriptions
|
|
.Select(p => new PropertyDescription
|
|
{
|
|
Key = p.Key,
|
|
DisplayName = p.DisplayName,
|
|
Description = p.Description,
|
|
DataType = p.DataType,
|
|
Unit = p.Unit,
|
|
DefaultValue = p.DefaultValue,
|
|
IsReadOnly = p.IsReadOnly,
|
|
DisplayOrder = p.DisplayOrder,
|
|
Category = p.Category,
|
|
Format = p.Format
|
|
})
|
|
.ToList();
|
|
return Task.FromResult<List<PropertyDescription>?>(list);
|
|
}
|
|
}
|
|
|