IRobotCacheService.cs 5.57 KB
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Rcs.Domain.Entities;

namespace Rcs.Application.Services
{
    /// <summary>
    /// 机器人错误信息
    /// @author zzy
    /// </summary>
    public class RobotError
    {
        public string ErrorType { get; set; } = string.Empty;
        public string ErrorLevel { get; set; } = string.Empty;
        public string ErrorDescription { get; set; } = string.Empty;
    }

    /// <summary>
    /// 机器人缓存服务接口 - 负责机器人状态和位置的Redis缓存操作
    /// @author zzy
    /// </summary>
    public interface IRobotCacheService
    {
        /// <summary>
        /// 更新机器人状态到Redis(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        Task UpdateStatusAsync(string manufacturer, string serialNumber, RobotStatus? status, OnlineStatus? online,
            int? batteryLevel, bool? driving, bool? paused, bool? charging, OperatingMode? operatingMode, string? errors);

        /// <summary>
        /// 更新机器人位置到Redis(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        Task UpdateLocationAsync(string manufacturer, string serialNumber, Guid? mapId, Guid? nodeId,
            double? x, double? y, double? theta);

        /// <summary>
        /// 从Redis获取机器人状态(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        Task<RobotStatusCache?> GetStatusAsync(string manufacturer, string serialNumber);

        /// <summary>
        /// 从Redis获取机器人位置(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        Task<RobotLocationCache?> GetLocationAsync(string manufacturer, string serialNumber);

        /// <summary>
        /// 获取所有在线机器人标识(制造商:序列号)
        /// @author zzy
        /// </summary>
        Task<IEnumerable<string>> GetOnlineRobotKeysAsync();

        /// <summary>
        /// 获取所有空闲机器人标识(制造商:序列号)
        /// @author zzy
        /// </summary>
        Task<IEnumerable<string>> GetIdleRobotKeysAsync();

        /// <summary>
        /// 根据参数修改位置信息的值
        /// </summary>
        /// <param name="manufacturer"></param>
        /// <param name="serialNumber"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<bool> SetLocationValueAsync(string manufacturer, string serialNumber, string field, string value);
        /// <summary>
        /// 获取所有需要持久化的机器人状态数据(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        Task<IEnumerable<(string Manufacturer, string SerialNumber, RobotStatusCache Status, RobotLocationCache Location)>> GetAllRobotCacheDataAsync();

        /// <summary>
        /// 从Redis获取机器人基础信息(使用制造商+序列号作为唯一标识)
        /// @author zzy
        /// </summary>
        /// <param name="manufacturer">制造商</param>
        /// <param name="serialNumber">序列号</param>
        /// <returns>机器人基础信息缓存,不存在则返回null</returns>
        Task<RobotBasicCache?> GetBasicAsync(string manufacturer, string serialNumber);

        /// <summary>
        /// 获取所有启用机器人的完整缓存数据(基础信息+状态+位置)
        /// @author zzy
        /// </summary>
        Task<IEnumerable<(RobotBasicCache Basic, RobotStatusCache? Status, RobotLocationCache? Location)>> GetAllActiveRobotCacheAsync();
    }

    /// <summary>
    /// 机器人状态缓存数据
    /// @author zzy
    /// </summary>
    public class RobotStatusCache
    {
        public RobotStatus Status { get; set; }
        public OnlineStatus Online { get; set; }
        public int? BatteryLevel { get; set; }
        public bool Driving { get; set; }
        public bool Paused { get; set; }
        public bool Charging { get; set; }
        public OperatingMode OperatingMode { get; set; }
        public List<RobotError>? Errors { get; set; }
        public DateTime UpdatedAt { get; set; }
    }

    /// <summary>
    /// 机器人位置缓存数据
    /// @author zzy
    /// </summary>
    public class RobotLocationCache
    {
        public Guid? MapId { get; set; }
        public Guid? NodeId { get; set; }
        public double? X { get; set; }
        public double? Y { get; set; }
        public double? Theta { get; set; }
        public List<List<double>>? Path { get; set; }
        public DateTime UpdatedAt { get; set; }
    }

    /// <summary>
    /// 机器人基础信息缓存数据
    /// @author zzy
    /// </summary>
    public class RobotBasicCache
    {
        public string RobotId { get; set; } = string.Empty;
        public string RobotCode { get; set; } = string.Empty;
        public string RobotName { get; set; } = string.Empty;
        public string RobotVersion { get; set; } = string.Empty;
        public string ProtocolName { get; set; } = string.Empty;
        public string ProtocolVersion { get; set; } = string.Empty;
        public int ProtocolType { get; set; }
        public string RobotManufacturer { get; set; } = string.Empty;
        public string RobotSerialNumber { get; set; } = string.Empty;
        public int RobotType { get; set; }
        public string? IpAddress { get; set; }
        public double CoordinateScale { get; set; }
        public bool Active { get; set; }
    }
}