CustomProtocolService.cs
4.54 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
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.Services.Protocol;
using Rcs.Cyaninetech.Services;
using Rcs.Domain.Entities;
using Rcs.Domain.Extensions;
using Rcs.Domain.Models.VDA5050;
namespace Rcs.Infrastructure.Services.Protocol;
/// <summary>
/// 自定义协议服务实现
/// 通过LanYin服务与机器人通信
/// @author zzy
/// </summary>
public class CustomProtocolService : IProtocolService
{
private readonly ILogger<CustomProtocolService> _logger;
private readonly ILanYinService _lanYinService;
public CustomProtocolService(
ILogger<CustomProtocolService> logger,
ILanYinService lanYinService)
{
_logger = logger;
_lanYinService = lanYinService;
}
/// <summary>
/// 支持的协议类型:自定义协议
/// </summary>
public ProtocolType ProtocolType => ProtocolType.Custom;
/// <summary>
/// 发送订单指令(自定义协议暂不支持)
/// </summary>
public async Task<ApiResponse> SendOrderAsync(Robot robot, RobotTask task, CancellationToken ct = default)
{
var areaCode = task.EndLocation?.StorageArea?.AreaCode;
if (string.IsNullOrEmpty(areaCode))
{
return ApiResponse.Failed($"CustomProtocol - 未找到库区编码,任务: {task.TaskCode}");
}
var locationCode = task.BeginLocation?.LocationCode;
if (string.IsNullOrEmpty(locationCode))
{
return ApiResponse.Failed($"CustomProtocol - 未找到起始库位编码,任务: {task.TaskCode}");
}
var request = new Rcs.Cyaninetech.Models.LanYinDispatchTaskRequest
{
location_id = locationCode,
area = areaCode
};
var res = await _lanYinService.DispatchTaskAsync(request, ct);
if (res.Success)
{
if (!string.IsNullOrEmpty(res.Data.RunningId))
{
task.Relation = res.Data.RunningId;
}
return ApiResponse.Successful();
}
return ApiResponse.Failed($"发送任务失败:: {res.Message},{res.Data}");
}
/// <summary>
/// 取消指定订单
/// </summary>
public async Task CancelOrderAsync(Robot robot, string? orderId, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 取消订单,机器人: {SerialNumber}, 订单: {OrderId}",
robot.RobotSerialNumber, orderId);
var result = await _lanYinService.CancelTaskByRobotAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
throw new BusinessException($"取消订单失败: {result.Message}");
}
}
/// <summary>
/// 取消机器人所有任务
/// </summary>
public async Task CancelRobotTasksAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 取消所有任务,机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.CancelTaskByRobotAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
throw new BusinessException($"取消任务失败: {result.Message}");
}
}
/// <summary>
/// 发送即时动作指令(自定义协议暂不支持)
/// </summary>
public Task SendInstantActionAsync(Robot robot, InstantAction actions, CancellationToken ct = default)
{
_logger.LogWarning("自定义协议暂不支持发送InstantAction指令,机器人: {SerialNumber}", robot.RobotSerialNumber);
return Task.CompletedTask;
}
/// <summary>
/// 复位机器人
/// </summary>
public async Task ResetRobotAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 复位机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.ResetRobotAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
throw new BusinessException($"复位机器人失败: {result.Message}");
}
}
/// <summary>
/// 确认异常
/// </summary>
public async Task ConfirmExceptionAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 确认异常,机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.ConfirmExceptionAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
throw new BusinessException($"确认异常失败: {result.Message}");
}
}
}