From decaae0c1076fd9c9d76849652438453d336d00a Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 13 三月 2026 12:23:29 +0800
Subject: [PATCH] feat: create S7 simulator solution structure
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs | 461 ++++++++-------------------------------------------------
1 files changed, 68 insertions(+), 393 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
index b63717b..f6b1ac5 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -1,43 +1,65 @@
-锘縰sing HslCommunication;
-using Newtonsoft.Json;
-using Quartz;
-using System.Collections.Concurrent;
-using System.Net.Sockets;
-using System.Text.Json;
-using WIDESEAWCS_Common.HttpEnum;
-using WIDESEAWCS_Core;
+锘縰sing Quartz;
+using WIDESEA_Core;
+using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
-using WIDESEAWCS_Core.Http;
-using WIDESEAWCS_DTO.Stock;
-using WIDESEAWCS_DTO.TaskInfo;
-using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
-using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
-using WIDESEAWCS_QuartzJob.Service;
+using WIDESEAWCS_Tasks.Workflow.Abstractions;
+using WIDESEAWCS_Tasks.Workflow;
using WIDESEAWCS_Tasks.SocketServer;
namespace WIDESEAWCS_Tasks
{
+ /// <summary>
+ /// 鏈哄櫒浜轰换鍔′綔涓氾細璐熻矗璋冨害涓庣敓鍛藉懆鏈熺鐞嗭紝鍏蜂綋鐘舵�佹満娴佺▼浜ょ粰缂栨帓鍣ㄣ��
+ /// </summary>
[DisallowConcurrentExecution]
public class RobotJob : IJob
{
- private readonly TcpSocketServer _TcpSocket;
- private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
- private static int _eventSubscribedFlag;
+ private const int MaxTaskTotalNum = 48;
+ private static int _messageSubscribedFlag;
- private readonly ITaskService _taskService;
- private readonly IRobotTaskService _robottaskService;
- private readonly ITaskExecuteDetailService _taskExecuteDetailService;
- private readonly ITaskRepository _taskRepository;
- private readonly IRouterService _routerService;
+ private readonly RobotClientManager _clientManager;
+ private readonly RobotStateManager _stateManager;
+ private readonly IRobotMessageRouter _messageRouter;
+ private readonly RobotTaskProcessor _taskProcessor;
+ private readonly IRobotWorkflowOrchestrator _workflowOrchestrator;
- public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService RobottaskService, ITaskService TaskService)
+ public RobotJob(
+ TcpSocketServer tcpSocket,
+ IRobotTaskService robotTaskService,
+ ITaskService taskService,
+ ICacheService cache,
+ HttpClientHelper httpClientHelper)
{
- _TcpSocket = TcpSocket;
- _robottaskService = RobottaskService;
- this._taskService = TaskService;
+ _stateManager = new RobotStateManager(cache);
+
+ // 鏀跺彛 Socket 璁块棶锛屽悗缁嫢鏇挎崲閫氫俊瀹炵幇鍙渶鏇挎崲缃戝叧灞傘��
+ ISocketClientGateway socketGateway = new SocketClientGateway(tcpSocket);
+
+ _taskProcessor = new RobotTaskProcessor(socketGateway, _stateManager, robotTaskService, taskService, httpClientHelper);
+ _clientManager = new RobotClientManager(tcpSocket, _stateManager);
+
+ var simpleCommandHandler = new RobotSimpleCommandHandler(_taskProcessor);
+ var prefixCommandHandler = new RobotPrefixCommandHandler(robotTaskService, _taskProcessor, _stateManager, socketGateway);
+ _messageRouter = new RobotMessageHandler(socketGateway, _stateManager, cache, simpleCommandHandler, prefixCommandHandler);
+
+ _workflowOrchestrator = new RobotWorkflowOrchestrator(_stateManager, _clientManager, _taskProcessor, robotTaskService);
+
+ _clientManager.OnClientDisconnected += OnClientDisconnected;
+
+ // 鍏ㄥ眬鍙闃呬竴娆℃秷鎭簨浠讹紝淇濇寔鍘熸湁琛屼负銆�
+ if (System.Threading.Interlocked.CompareExchange(ref _messageSubscribedFlag, 1, 0) == 0)
+ {
+ tcpSocket.MessageReceived += _messageRouter.HandleMessageReceivedAsync;
+ Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 鏈哄櫒鎵婽CP娑堟伅浜嬩欢宸茶闃�");
+ }
+ }
+
+ private void OnClientDisconnected(object? sender, RobotSocketState state)
+ {
+ Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 瀹㈡埛绔凡鏂紑杩炴帴: {state.IPAddress}");
}
public async Task Execute(IJobExecutionContext context)
@@ -51,382 +73,35 @@
string ipAddress = robotCrane.IPAddress;
- // 鑾峰彇鎴栧垱寤虹姸鎬�
- RobotSocketState state = _socketStates.GetOrAdd(ipAddress, _ => new RobotSocketState
- {
- IPAddress = ipAddress,
- RobotCrane = robotCrane
- });
-
- // 鏇存柊璁惧淇℃伅
+ RobotSocketState state = _stateManager.GetOrCreateState(ipAddress, robotCrane);
state.RobotCrane = robotCrane;
- // 妫�鏌ユ槸鍚︽湁璇ュ鎴风杩炴帴
- var clientIds = _TcpSocket.GetClientIds();
- if (!clientIds.Contains(ipAddress))
+ try
{
- return;
- }
-
- // 璁㈤槄涓�娆� message 浜嬩欢锛堝叏灞�涓�娆★級
- if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
- {
- _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
- _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
- }
-
- if (!state.IsEventSubscribed)
- {
- _TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client);
- Task clientTask = _TcpSocket.HandleClientAsync(client, robotCrane.IPAddress, _TcpSocket._cts.Token, state);
- state.IsEventSubscribed = true;
- }
-
- // 鑾峰彇浠诲姟骞剁紦瀛樺埌鐘舵�佷腑
- Dt_RobotTask? task = GetTask(robotCrane);
- if (task != null)
- {
- state.CurrentTask = task;
- if (task.RobotTaskTotalNum != 48)
+ if (!_clientManager.EnsureClientSubscribed(ipAddress, robotCrane))
{
- // 澶勭悊姝e湪鎵ц鐨勪换鍔�
- if (state.RobotRunMode == 1 && state.RobotControlMode == 1)
- {
- await Task.Delay(1000);
- if ((state.CurrentAction == "Homed" || state.CurrentAction == "PickFinished" || state.CurrentAction == "PutFinished") && state.OperStatus == "Running")
- {
- // TODO 璇诲彇绾夸綋鐢垫睜鏉$爜锛屽彂閫佸彇鐢垫睜鎸囦护
- if (true)
- {
- // 妯℃嫙璇诲彇鏉$爜
- state.CellBarcode = new string[] { "CellBarcode1", "CellBarcode2", "CellBarcode3", "CellBarcode4" };
+ return;
+ }
- string taskString = $"Pickbattery,{task.RobotSourceAddress}";
- // 鍙戦�佷换鍔℃寚浠�
- bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
- }
- }
+ var task = _taskProcessor.GetTask(robotCrane);
+ if (task != null)
+ {
+ var latestState = _stateManager.GetState(ipAddress);
+ if (latestState == null)
+ {
+ return;
+ }
+
+ if (latestState.RobotTaskTotalNum < MaxTaskTotalNum)
+ {
+ await _workflowOrchestrator.ExecuteAsync(latestState, task, ipAddress);
}
}
}
-
- return;
- }
-
- /// <summary>
- /// 浜嬩欢锛氬鎴风鏂紑杩炴帴鏃惰Е鍙�
- /// </summary>
- /// <param name="clientId"></param>
- /// <returns></returns>
- private Task<string?> _TcpSocket_RobotReceived(string clientId)
- {
- _socketStates.TryRemove(clientId, out _);
- return Task.FromResult<string?>(null);
- }
-
- /// <summary>
- /// 浜嬩欢锛氭敹鍒版秷鎭椂瑙﹀彂
- /// </summary>
- /// <param name="message"></param>
- /// <param name="isJson"></param>
- /// <param name="client"></param>
- /// <param name="state"></param>
- /// <returns></returns>
- private async Task<string?> _TcpSocket_MessageReceived(string message, bool isJson, TcpClient client, RobotSocketState state)
- {
- string messageLower = message.ToLowerInvariant();
-
- if (await IsSimpleCommandAsync(messageLower, state))
+ catch (Exception)
{
- return null;
+ // 寮傚父澶勭悊宸插湪缁勪欢鍐呴儴杩涜锛孞ob 灞備繚鎸佸厹搴曞悶鍚愯涔夈��
}
-
- if (IsPrefixCommand(messageLower))
- {
- try
- {
- var parts = message.Split(',');
- if (parts.Length >= 1)
- {
- var cmd = parts[0].ToLowerInvariant();
- int[] positions = new int[4];
- for (int i = 1; i <= 4 && i < parts.Length; i++)
- {
- int.TryParse(parts[i], out positions[i - 1]);
- }
-
- if (cmd.StartsWith("pickfinished"))
- {
- StockDTO stockDTO = new StockDTO
- {
- SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
- SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
- TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
- TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
- Details = positions
- .Where(x => x > 0)
- .OrderBy(x => x)
- .Select((x, idx) => new StockDetailDTO
- {
- Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
- Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
- CellBarcode = state.CellBarcode[idx]
- })
- .ToList()
- };
- state.LastPickPositions = positions;
-
- var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), state.CurrentTask?.RobotTaskType == 2 ? nameof(ConfigKey.ChangePalletAsync) : nameof(ConfigKey.SplitPalletAsync));
-
- if (result.Status)
- {
- state.CurrentAction = "PickFinished";
- }
- }
- else if (cmd.StartsWith("putfinished"))
- {
- state.LastPutPositions = positions;
- if (state.CurrentTask?.RobotTaskType == 1)
- {
- // 鍙戦�佹暟鎹粰WMS缁勭洏/鎹㈢洏
- StockDTO stockDTO = new StockDTO
- {
- SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
- SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
- TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
- TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
- Details = positions
- .Where(x => x > 0)
- .OrderBy(x => x)
- .Select((x, idx) => new StockDetailDTO
- {
- Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
- Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
- CellBarcode = state.CellBarcode[idx]
- })
- .ToList()
- };
- var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), nameof(ConfigKey.GroupPalletAsync));
-
- if (result.Status)
- {
- state.CurrentAction = "PutFinished";
- }
- }
- }
- }
- }
- catch { }
-
- return null;
- }
-
- return null;
- }
-
- /// <summary>
- /// 鏈烘鎵嬬畝鍗曞懡浠ゅ鐞�
- /// </summary>
- /// <param name="message"></param>
- /// <param name="state"></param>
- /// <returns></returns>
- private async Task<bool> IsSimpleCommandAsync(string message, RobotSocketState state)
- {
- switch (message)
- {
- case "homing":
- state.CurrentAction = "Homing";
- return true;
-
- case "homed":
- state.CurrentAction = "Homed";
- return true;
-
- case "picking":
- state.CurrentAction = "Picking";
- return true;
-
- case "puting":
- state.CurrentAction = "Putting";
- return true;
-
- case "allpickfinished":
- state.CurrentAction = "AllPickFinished";
- if (state.CurrentTask?.RobotTaskType == 2 || state.CurrentTask?.RobotTaskType == 3)
- {
- await HandleInboundTaskAsync(state, useSourceAddress: true);
- }
- return true;
-
- case "allputfinished":
- state.CurrentAction = "AllPutFinished";
- if (state.CurrentTask?.RobotTaskType == 1)
- {
- await HandleInboundTaskAsync(state, useSourceAddress: false);
- }
- return true;
-
- case "running":
- state.OperStatus = "Running";
- return true;
-
- case "pausing":
- state.OperStatus = "Pausing";
- return true;
-
- case "warming":
- state.OperStatus = "Warming";
- return true;
-
- case "emstoping":
- state.OperStatus = "Emstoping";
- return true;
-
- case "runmode,1":
- state.RobotRunMode = 1;
- return true;
-
- case "runmode,2":
- state.RobotRunMode = 2;
- return true;
-
- case "controlmode,1":
- state.RobotControlMode = 1;
- return true;
-
- case "controlmode,2":
- state.RobotControlMode = 2;
- return true;
-
- case "armobject,1":
- state.RobotArmObject = 1;
- return true;
-
- case "armobject,0":
- state.RobotArmObject = 0;
- return true;
-
- default:
- return false;
- }
- }
-
- private async Task HandleInboundTaskAsync(RobotSocketState state, bool useSourceAddress)
- {
- var currentTask = state.CurrentTask;
- if (currentTask == null)
- {
- return;
- }
-
- string roadway = currentTask.RobotRoadway == "1" ? "GWSC001" : currentTask.RobotRoadway == "2" ? "HCSC001" : "SC001";
- int warehouseId = currentTask.RobotRoadway == "1" ? 1 : currentTask.RobotRoadway == "2" ? 2 : 3;
-
- CreateTaskDto taskDto = new CreateTaskDto
- {
- PalletCode = currentTask.RobotTargetAddressPalletCode ?? string.Empty,
- SourceAddress = currentTask.RobotTargetAddress ?? string.Empty,
- TargetAddress = currentTask.RobotTargetAddress ?? string.Empty,
- Roadway = roadway,
- WarehouseId = warehouseId,
- PalletType = 1,
- TaskType = 4
- };
-
- var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), taskDto.ToJsonString(), nameof(ConfigKey.CreateTaskInboundAsync));
- if (!result.Status)
- {
- return;
- }
-
- WMSTaskDTO taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.ToString() ?? string.Empty) ?? new WMSTaskDTO();
- var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
- if (!content.Status) return;
-
- var taskInfo = _taskService.QueryByTaskNum(taskDTO.TaskNum);
-
-
- string targetAddress = useSourceAddress ? taskDTO.SourceAddress : taskDTO.TargetAddress;
-
- IDevice? device = Storage.Devices.Where(x => x.DeviceProDTOs.Select(x => x.DeviceChildCode == taskDTO.SourceAddress).FirstOrDefault()).FirstOrDefault() ?? null;
- device?.Communicator.Write(nameof(ConveyorLineDBNameNew.Target), taskInfo.NextAddress);
- device?.Communicator.Write(nameof(ConveyorLineDBNameNew.TaskNo), taskDTO.TaskNum);
- device?.Communicator.Write(nameof(ConveyorLineDBNameNew.WCS_STB), 1);
- }
-
- /// <summary>
- /// 鏈烘鎵嬪墠缂�鍛戒护澶勭悊
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- private static bool IsPrefixCommand(string message)
- {
- return message.StartsWith("pickfinished") || message.StartsWith("putfinished");
- }
-
- private Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
- {
- return _robottaskService.QueryRobotCraneTask(robotCrane.DeviceCode);
}
}
-
- public class RobotSocketState
- {
- public string IPAddress { get; set; } = string.Empty;
-
- /// <summary>
- /// 鏄惁宸茶闃呮秷鎭簨浠�
- /// </summary>
- public bool IsEventSubscribed { get; set; }
-
- /// <summary>
- /// 鏈烘鎵嬭繍琛屾ā寮�
- /// </summary>
- public int? RobotRunMode { get; set; }
-
- /// <summary>
- /// 鏈烘鎵嬫帶鍒舵ā寮�
- /// </summary>
- public int? RobotControlMode { get; set; }
-
- /// <summary>
- /// 鏈烘鎵嬫姄鍙栧璞�
- /// </summary>
- public int? RobotArmObject { get; set; }
-
- /// <summary>
- /// 鏈烘鎵嬭澶囦俊鎭�
- /// </summary>
- public RobotCraneDevice? RobotCrane { get; set; }
-
- /// <summary>
- /// 褰撳墠鍔ㄤ綔
- /// </summary>
- public string? CurrentAction { get; set; }
-
- /// <summary>
- /// 褰撳墠鐘舵��
- /// </summary>
- public string? OperStatus { get; set; }
-
- /// <summary>
- /// 鍙栬揣瀹屾垚浣嶇疆
- /// </summary>
- public int[]? LastPickPositions { get; set; }
-
- /// <summary>
- /// 鏀捐揣瀹屾垚浣嶇疆
- /// </summary>
- public int[]? LastPutPositions { get; set; }
-
- /// <summary>
- /// 鎶撳彇浣嶇疆鏉$爜
- /// </summary>
- public string[] CellBarcode { get; set; }
-
- /// <summary>
- /// 褰撳墠鎶撳彇浠诲姟
- /// </summary>
- public Dt_RobotTask? CurrentTask { get; set; }
- }
-}
\ No newline at end of file
+}
--
Gitblit v1.9.3