NetActionExecutionModels.cs 9.67 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
using Rcs.Domain.Enums;

namespace Rcs.Application.Services.PathFind.Models;

/// <summary>
/// 网络动作执行上下文(用于Redis缓存)
/// @author zzy
/// </summary>
public class NetActionExecutionContext
{
    /// <summary>
    /// 上下文ID(唯一标识)
    /// </summary>
    public string ContextId { get; set; } = string.Empty;

    /// <summary>
    /// 机器人ID
    /// </summary>
    public Guid RobotId { get; set; }

    /// <summary>
    /// 任务ID
    /// </summary>
    public Guid TaskId { get; set; }

    /// <summary>
    /// 子任务ID
    /// </summary>
    public Guid SubTaskId { get; set; }

    /// <summary>
    /// 当前路口段索引
    /// </summary>
    public int JunctionIndex { get; set; }

    /// <summary>
    /// 当前资源段索引
    /// </summary>
    public int ResourceIndex { get; set; }

    /// <summary>
    /// 网络动作ID
    /// </summary>
    public Guid NetActionId { get; set; }

    /// <summary>
    /// 动作在节点中的位置(Start=起点, End=终点)
    /// </summary>
    public NetActionPosition ActionPosition { get; set; }

    /// <summary>
    /// 执行状态
    /// </summary>
    public NetActionExecutionStatus ExecutionStatus { get; set; }

    /// <summary>
    /// 响应等待类型
    /// </summary>
    public ResponseWaitType ResponseWaitType { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 执行时间
    /// </summary>
    public DateTime? ExecutedAt { get; set; }

    /// <summary>
    /// 完成时间
    /// </summary>
    public DateTime? CompletedAt { get; set; }

    /// <summary>
    /// 执行结果消息
    /// </summary>
    public string? ResultMessage { get; set; }

    /// <summary>
    /// 是否成功
    /// </summary>
    public bool? IsSuccess { get; set; }

    /// <summary>
    /// 异步回调令牌(用于异步响应验证)
    /// </summary>
    public string? AsyncCallbackToken { get; set; }

    /// <summary>
    /// 过期时间(用于自动清理)
    /// </summary>
    public TimeSpan Expiry { get; set; } = TimeSpan.FromMinutes(30);

    /// <summary>
    /// 异步回调超时时间(秒),超过此时间未收到回调则视为超时
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public int AsyncTimeoutSeconds { get; set; } = 300;

    /// <summary>
    /// 重试次数
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public int RetryCount { get; set; } = 0;

    /// <summary>
    /// 最大重试次数(来自网络动作配置的RepeatCount,-1表示无限重试)
    /// @author zzy
    /// 2026-02-07 更新:使用网络动作配置
    /// </summary>
    public int MaxRetryCount { get; set; } = 3;

    /// <summary>
    /// 重试间隔时间(毫秒,来自网络动作配置的IntervalTimeMs)
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public int RetryIntervalMs { get; set; } = 5000;

    /// <summary>
    /// 上次重试时间
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public DateTime? LastRetryAt { get; set; }

    /// <summary>
    /// 检查是否已超时
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public bool IsTimeout => ExecutionStatus == NetActionExecutionStatus.WaitingAsyncResponse
        && ExecutedAt.HasValue
        && DateTime.UtcNow > ExecutedAt.Value.AddSeconds(AsyncTimeoutSeconds);

    /// <summary>
    /// 检查是否可以重试(考虑重试间隔)
    /// @author zzy
    /// 2026-02-07 新增
    /// </summary>
    public bool CanRetry
    {
        get
        {
            // 检查是否达到最大重试次数(-1表示无限重试)
            if (MaxRetryCount != -1 && RetryCount >= MaxRetryCount)
            {
                return false;
            }

            // 检查重试间隔
            if (LastRetryAt.HasValue)
            {
                var nextRetryTime = LastRetryAt.Value.AddMilliseconds(RetryIntervalMs);
                if (DateTime.UtcNow < nextRetryTime)
                {
                    return false;
                }
            }

            return true;
        }
    }
}

/// <summary>
/// 网络动作在节点中的位置
/// @author zzy
/// </summary>
public enum NetActionPosition
{
    /// <summary>
    /// 起始节点动作
    /// </summary>
    Start = 1,

    /// <summary>
    /// 终止节点动作
    /// </summary>
    End = 2
}

/// <summary>
/// 网络动作执行状态
/// @author zzy
/// </summary>
public enum NetActionExecutionStatus
{
    /// <summary>
    /// 待执行
    /// </summary>
    Pending = 0,

    /// <summary>
    /// 执行中
    /// </summary>
    Executing = 1,

    /// <summary>
    /// 等待异步响应
    /// </summary>
    WaitingAsyncResponse = 2,

    /// <summary>
    /// 执行成功
    /// </summary>
    Success = 3,

    /// <summary>
    /// 执行失败
    /// </summary>
    Failed = 4,

    /// <summary>
    /// 已取消
    /// </summary>
    Cancelled = 5
}

/// <summary>
/// 网络动作执行请求
/// @author zzy
/// </summary>
public class NetActionExecuteRequest
{
    /// <summary>
    /// 网络动作ID
    /// </summary>
    public Guid NetActionId { get; set; }

    /// <summary>
    /// 上下文ID
    /// </summary>
    public string ContextId { get; set; } = string.Empty;

    /// <summary>
    /// 机器人ID
    /// </summary>
    public Guid RobotId { get; set; }

    /// <summary>
    /// 机器人编码
    /// </summary>
    public string RobotCode { get; set; } = string.Empty;

    /// <summary>
    /// 任务ID
    /// </summary>
    public Guid TaskId { get; set; }

    /// <summary>
    /// 任务编码
    /// </summary>
    public string TaskCode { get; set; } = string.Empty;

    /// <summary>
    /// 子任务ID
    /// </summary>
    public Guid SubTaskId { get; set; }

    /// <summary>
    /// 当前节点编码
    /// </summary>
    public string NodeCode { get; set; } = string.Empty;

    /// <summary>
    /// 当前节点ID
    /// </summary>
    public Guid NodeId { get; set; }
}

/// <summary>
/// 网络动作执行结果
/// @author zzy
/// </summary>
public class NetActionExecuteResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 是否需要等待异步响应
    /// </summary>
    public bool IsAsyncWaiting { get; set; }

    /// <summary>
    /// 上下文ID(用于异步回调关联)
    /// </summary>
    public string ContextId { get; set; } = string.Empty;
}

/// <summary>
/// 异步回调请求模型(固定格式,供第三方系统回调使用)
/// @author zzy
/// </summary>
public class NetActionCallbackRequest
{
    /// <summary>
    /// 上下文ID(由RCS生成,第三方系统原样返回)
    /// </summary>
    public string ContextId { get; set; } = string.Empty;

    /// <summary>
    /// 回调令牌(用于验证回调合法性)
    /// </summary>
    public string CallbackToken { get; set; } = string.Empty;

    /// <summary>
    /// 执行结果(true=成功, false=失败)
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 结果消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 业务数据(可选,由第三方系统返回的业务相关数据)
    /// </summary>
    public Dictionary<string, object>? BusinessData { get; set; }

    /// <summary>
    /// 时间戳(ISO 8601格式)
    /// </summary>
    public string Timestamp { get; set; } = DateTime.UtcNow.ToString("O");
}

/// <summary>
/// 异步回调响应模型
/// @author zzy
/// </summary>
public class NetActionCallbackResponse
{
    /// <summary>
    /// 是否成功接收回调
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 响应消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 处理后的动作(如:Continue=继续下发下一段, Retry=重试当前动作, Abort=中止任务)
    /// </summary>
    public string Action { get; set; } = "Continue";
}

/// <summary>
/// 路径段网络动作检查请求
/// @author zzy
/// </summary>
public class SegmentNetActionCheckRequest
{
    /// <summary>
    /// 机器人ID
    /// </summary>
    public Guid RobotId { get; set; }

    /// <summary>
    /// 任务ID
    /// </summary>
    public Guid TaskId { get; set; }

    /// <summary>
    /// 子任务ID
    /// </summary>
    public Guid SubTaskId { get; set; }

    /// <summary>
    /// 当前路口段索引
    /// </summary>
    public int JunctionIndex { get; set; }

    /// <summary>
    /// 当前资源段索引
    /// </summary>
    public int ResourceIndex { get; set; }

    /// <summary>
    /// 路径段
    /// </summary>
    public List<PathSegmentWithCode> Segments { get; set; } = new();
}

/// <summary>
/// 路径段网络动作检查结果
/// @author zzy
/// </summary>
public class SegmentNetActionCheckResult
{
    /// <summary>
    /// 是否有网络动作需要执行
    /// </summary>
    public bool HasNetAction { get; set; }

    /// <summary>
    /// 上下文ID(如果有网络动作)
    /// </summary>
    public string ContextId { get; set; } = string.Empty;

    /// <summary>
    /// 是否可以继续发送下一段
    /// </summary>
    public bool CanContinue { get; set; }

    /// <summary>
    /// 需要等待异步响应
    /// </summary>
    public bool IsWaitingAsync { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;
}