Initial commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using RobotNet10.RobotApp.Client.Shared.Devices;
|
||||
using RobotNet10.RobotApp.Devices;
|
||||
using RobotNet10.Shared.Sensor;
|
||||
using System.Linq;
|
||||
|
||||
namespace RobotNet10.RobotApp.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// SignalR Hub cho Battery device - cung cấp real-time battery data
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
public class BatteryHub(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 IBattery)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DeviceInfoDto
|
||||
{
|
||||
DeviceId = device.DeviceId,
|
||||
DeviceName = device.DeviceName
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lấy thông tin battery theo device ID
|
||||
/// </summary>
|
||||
public async Task<BatteryState?> GetBatteryData(string deviceId)
|
||||
{
|
||||
var device = deviceProvider.GetDevice(deviceId);
|
||||
if (device is not IBattery battery)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var data = await battery.ReadBatteryStateAsync();
|
||||
return SanitizeBatteryState(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sanitize BatteryState to ensure all double values are valid for JSON serialization
|
||||
/// Replaces NaN and Infinity with 0.0
|
||||
/// </summary>
|
||||
private static BatteryState SanitizeBatteryState(BatteryState state)
|
||||
{
|
||||
var sanitized = state;
|
||||
|
||||
// Sanitize single double values
|
||||
sanitized.Voltage = SanitizeFloat(state.Voltage);
|
||||
sanitized.Current = SanitizeFloat(state.Current);
|
||||
sanitized.Charge = SanitizeFloat(state.Charge);
|
||||
sanitized.Capacity = SanitizeFloat(state.Capacity);
|
||||
sanitized.DesignCapacity = SanitizeFloat(state.DesignCapacity);
|
||||
sanitized.Percentage = SanitizeFloat(state.Percentage);
|
||||
|
||||
// Sanitize double arrays
|
||||
if (state.CellVoltage != null && state.CellVoltage.Length > 0)
|
||||
{
|
||||
sanitized.CellVoltage = state.CellVoltage.Select(SanitizeFloat).ToArray();
|
||||
}
|
||||
|
||||
if (state.CellTemperature != null && state.CellTemperature.Length > 0)
|
||||
{
|
||||
sanitized.CellTemperature = state.CellTemperature.Select(SanitizeFloat).ToArray();
|
||||
}
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sanitize a double value: replace NaN and Infinity with 0.0
|
||||
/// </summary>
|
||||
private static double SanitizeFloat(double value)
|
||||
{
|
||||
if (double.IsNaN(value) || double.IsInfinity(value))
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user