CustomProtocolService.cs
7.65 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
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.Services;
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;
private readonly IUnifiedTrafficControlService _unifiedTrafficControlService;
public CustomProtocolService(
ILogger<CustomProtocolService> logger,
ILanYinService lanYinService,
IUnifiedTrafficControlService unifiedTrafficControlService)
{
_logger = logger;
_lanYinService = lanYinService;
_unifiedTrafficControlService = unifiedTrafficControlService;
}
/// <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)
{
_logger.LogError($"取消订单失败: {result.Message}");
}
}
/// <summary>
/// 取消机器人所有任务
/// </summary>
/// <summary>
/// 取消机器人所有任务
/// @author zzy
/// </summary>
public async Task CancelRobotTasksAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 取消所有任务,机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.CancelTaskByRobotAsync(robot.RobotSerialNumber, ct);
await ConfirmExceptionAsync(robot, ct);
if (!result.Success)
{
_logger.LogError($"取消任务失败: {result.Message}");
}
// 释放该机器人的所有路径锁
// @author zzy
// @date 2026-01-30
var releasedLockCount = await _unifiedTrafficControlService.ReleaseAllRobotLocksAsync(robot.RobotId);
_logger.LogInformation("CustomProtocol - 任务取消,释放机器人路径锁: RobotId={RobotId}, ReleasedCount={Count}",
robot.RobotId, releasedLockCount);
}
/// <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);
await ConfirmExceptionAsync(robot, ct);
if (!result.Success)
{
_logger.LogError($"复位机器人失败: {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)
{
_logger.LogError($"确认异常失败: {result.Message}");
}
}
public async Task RobotPauseAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 暂停,机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.PauseRobotAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
_logger.LogError($"机器人暂停失败: {result.Message}");
}
}
public async Task RobotUnPauseAsync(Robot robot, CancellationToken ct = default)
{
_logger.LogInformation("CustomProtocol - 取消暂停,机器人: {SerialNumber}", robot.RobotSerialNumber);
var result = await _lanYinService.UnPauseRobotAsync(robot.RobotSerialNumber, ct);
if (!result.Success)
{
_logger.LogError($"机器人取消暂停失败: {result.Message}");
}
}
public async Task<ApiResponse> ReLocationAsync(Robot robot,string mapCode, double x, double y, double theta, CancellationToken ct = default)
{
var result = await _lanYinService.RelocateRobotAsync(robot.RobotSerialNumber,Convert.ToInt32(mapCode),x,y,theta, ct);
if (!result.Success)
{
_logger.LogError($"机器人重定位失败: {result.Message}");
return ApiResponse.Failed($"机器人重定位失败: {result.Message}");
}
return ApiResponse.Successful();
}
/// <summary>
/// 发送下一段路径指令(自定义协议暂不支持分段下发)
/// </summary>
/// <param name="robot">机器人实体</param>
/// <param name="task">任务实体</param>
/// <param name="ct">取消令牌</param>
/// <returns>操作响应</returns>
public Task<ApiResponse> SendNextSegmentAsync(Robot robot, RobotSubTask currentSubTask, CancellationToken ct = default, bool reExec = false)
{
_logger.LogWarning("自定义协议暂不支持分段下发下一段路径指令,机器人: {SerialNumber}, 任务: {TaskCode}",
robot.RobotSerialNumber, currentSubTask.SubTaskId);
return Task.FromResult(ApiResponse.Failed("自定义协议暂不支持分段下发功能"));
}
public Task<ApiResponse> SendNextSegmentAsync(string robotManufacturer, string robotSerialNumber, CancellationToken ct = default, bool reExec = false)
{
return Task.FromResult(ApiResponse.Failed("自定义协议暂不支持分段下发功能"));
}
}