独立出与海康RCS通信交互部分,可以有更多的精力关注业务层,更好的代码复用节约开发时间
- 包括RCS的消息推送:状态、告警、Sync等的处理
- 提供对RCS API接口的调用:创建、继续、取消任务,释放AGV等
完全插件式使用
- 使用默认的HikAGVService
public static IMvcBuilder AddHikRCSIntegration(this IMvcBuilder builder, Action<HikRCSOptions> options)
{
builder.Services.AddOptions();
builder.Services.Configure<HikRCSOptions>(options);
builder.Services.AddTransient<IHikAGVService, HikAGVService>();
builder.AddApplicationPart(typeof(HikRCSController).Assembly);
return builder;
}
- 使用自定义实现的IHikAGVService
public static IMvcBuilder AddHikRCSIntegration<T>(this IMvcBuilder builder, Action<HikRCSOptions> options)
where T : class, IHikAGVService
{
builder.Services.AddOptions();
builder.Services.Configure<HikRCSOptions>(options);
builder.Services.AddTransient<IHikAGVService, T>();
builder.AddApplicationPart(typeof(HikRCSController).Assembly);
return builder;
}
-
创建一个测试使用的WebAPI项目,如下
using HikRCS.AspNetCore.Extensions; using MediatR; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddMediatR(typeof(Program).Assembly); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } // 添加回调控制器的跨域支持 // 调用必须放在UseRouting之后,但在UseAuthorization之前,参考https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0#middleware-order app.ApplyHikRCSIntegration(); app.UseAuthorization(); app.MapControllers(); app.Run();
-
在AddControllers()后面也就是本HikRCSIntegration中实现的IMvcBuilder的扩展方法,如下
builder.Services .AddControllers() .AddHikRCSIntegration(options => { options.RCSUrl = "http://192.168.2.3"; //// default configuration //options.CreateTaskRouter = ":8182/rcms/services/rest/hikRpcService/genAgvSchedulingTask"; //options.ContinueTaskRouter = ":8182/rcms/services/rest/hikRpcService/continueTask"; //options.CancelTaskRouter = ":8182/rcms/services/rest/hikRpcService/cancelTask"; //options.GetTaskStatusRouter = ":8182/rcms/services/rest/hikRpcService/queryTaskStatus"; //options.GetRobotStatusRouter = ":8083/rcms-dps/rest/queryAgvStatus"; //options.FreeRobotRouter = ":8182/rcms/services/rest/hikRpcService/freeRobot"; //options.StopRobotRouter = ":8182/rcms/services/rest/hikRpcService/stopRobot"; //options.ResumeRobotRouter = ":8182/rcms/services/rest/hikRpcService/resumeRobot"; });
-
现实MediatR的
INotificationHandler<HikRCSCallEvent>
,如下HikRCSCallCommand
using HikRCS.AspNetCore.Models; using MediatR; namespace HikRCSIntegration.Test.MediatorCommand { public class HikRCSCallCommand : INotificationHandler<HikRCSCallEvent> { private readonly ILogger<HikRCSCallCommand> _logger; public HikRCSCallCommand(ILogger<HikRCSCallCommand> logger) { _logger = logger; } /// <summary> /// 处理状态回调逻辑 /// </summary> /// <param name="notification"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task Handle(HikRCSCallEvent notification, CancellationToken cancellationToken) { _logger.LogInformation($"Method: {notification.Method}"); return Task.CompletedTask; } } }
HikRCSWarnCommand
using HikRCS.AspNetCore.Models; using MediatR; namespace HikRCSIntegration.Test.MediatorCommand { public class HikRCSWarnCommand : INotificationHandler<HikRCSWarnEvent> { private readonly ILogger<HikRCSCallCommand> _logger; public HikRCSWarnCommand(ILogger<HikRCSCallCommand> logger) { _logger = logger; } /// <summary> /// 告警回调逻辑 /// </summary> /// <param name="notification"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task Handle(HikRCSWarnEvent notification, CancellationToken cancellationToken) { if (notification.WarnDescs.Any()) { foreach (var item in notification.WarnDescs) { _logger.LogInformation($"Begin Date: {item.beginDate}, Task code: {item.taskCode}, Warn content: {item.warnContent}"); } } return Task.CompletedTask; } } }
-
欧克,下面来验证一下结果
分别使用丝袜哥调用两个提供给HikRCS的两个接口
curl -X 'POST' \ 'http://localhost:5244/restapi/HikIntegration/Status' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{ "reqCode": "string", "reqTime": "string", "cooX": "string", "cooY": "string", "currentPositionCode": "string", "data": "string", "mapCode": "string", "mapDataCode": "string", "method": "RCS回调.method=测试", "podCode": "string", "podDir": "string", "robotCode": "string", "taskCode": "string", "wbCode": "string" }'
curl -X 'POST' \ 'http://localhost:5244/restapi/HikIntegration/Warn' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{ "reqCode": "string", "reqTime": "string", "clientCode": "string", "tokenCode": "string", "warnInfos": [ { "robotCode": "034", "beginDate": "2022-06-18T13:56:30.521Z", "warnContent": "爆炸告警测试", "taskCode": "001122334455" } ] }'
查看结果
info: HikRCSIntegration.Test.MediatorCommand.HikRCSCallCommand[0] Method: RCS回调.method=测试 info: HikRCSIntegration.Test.MediatorCommand.HikRCSCallCommand[0] Begin Date: 2022/6/18 13:56:30, Task code: 001122334455, Warn content: 爆炸告警测试