| | 1 | | using LGDXRobotCloud.API.Repositories; |
| | 2 | | using LGDXRobotCloud.API.Services.Common; |
| | 3 | | using LGDXRobotCloud.API.Services.Navigation; |
| | 4 | | using LGDXRobotCloud.Data.DbContexts; |
| | 5 | | using LGDXRobotCloud.Data.Entities; |
| | 6 | | using LGDXRobotCloud.Data.Models.Redis; |
| | 7 | | using LGDXRobotCloud.Protos; |
| | 8 | | using LGDXRobotCloud.Utilities.Enums; |
| | 9 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 10 | | using Microsoft.EntityFrameworkCore; |
| | 11 | |
|
| | 12 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 13 | |
|
| | 14 | | public interface IAutoTaskSchedulerService |
| | 15 | | { |
| | 16 | | Task RunSchedulerNewAutoTaskAsync(int realmId, Guid? robotId); |
| | 17 | | Task RunSchedulerRobotNewJoinAsync(Guid robotId); |
| | 18 | | Task<bool> RunSchedulerRobotReadyAsync(Guid robotId); |
| | 19 | |
|
| | 20 | | Task ReleaseRobotAsync(Guid robotId); |
| | 21 | |
|
| | 22 | | Task AutoTaskAbortAsync(Guid robotId, int taskId, string token, AutoTaskAbortReason autoTaskAbortReason); |
| | 23 | | Task<bool> AutoTaskAbortApiAsync(int taskId); |
| | 24 | | Task AutoTaskNextAsync(Guid robotId, int taskId, string token); |
| | 25 | | Task<bool> AutoTaskNextApiAsync(Guid robotId, int taskId, string token); |
| | 26 | | } |
| | 27 | |
|
| 0 | 28 | | public class AutoTaskSchedulerService( |
| 0 | 29 | | IAutoTaskPathPlannerService autoTaskPathPlanner, |
| 0 | 30 | | IAutoTaskRepository autoTaskRepository, |
| 0 | 31 | | IEmailService emailService, |
| 0 | 32 | | IOnlineRobotsService onlineRobotsService, |
| 0 | 33 | | IRobotService robotService, |
| 0 | 34 | | ITriggerService triggerService, |
| 0 | 35 | | LgdxContext context |
| 0 | 36 | | ) : IAutoTaskSchedulerService |
| | 37 | | { |
| 0 | 38 | | private readonly IAutoTaskPathPlannerService _autoTaskPathPlanner = autoTaskPathPlanner ?? throw new ArgumentNullExcep |
| 0 | 39 | | private readonly IAutoTaskRepository _autoTaskRepository = autoTaskRepository ?? throw new ArgumentNullException(nameo |
| 0 | 40 | | private readonly IEmailService _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService)); |
| 0 | 41 | | private readonly IOnlineRobotsService _onlineRobotsService = onlineRobotsService ?? throw new ArgumentNullException(na |
| 0 | 42 | | private readonly IRobotService _robotService = robotService ?? throw new ArgumentNullException(nameof(robotService)); |
| 0 | 43 | | private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer |
| 0 | 44 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 45 | |
|
| | 46 | | private async Task AddAutoTaskJourney(AutoTask autoTask) |
| 0 | 47 | | { |
| 0 | 48 | | var autoTaskJourney = new AutoTaskJourney |
| 0 | 49 | | { |
| 0 | 50 | | AutoTaskId = autoTask.Id, |
| 0 | 51 | | CurrentProgressId = autoTask.CurrentProgressId |
| 0 | 52 | | }; |
| 0 | 53 | | await _context.AutoTasksJourney.AddAsync(autoTaskJourney); |
| 0 | 54 | | await _context.SaveChangesAsync(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | private async Task<RobotClientsAutoTask?> GenerateTaskDetail(AutoTask task, bool continueAutoTask = false) |
| 0 | 58 | | { |
| 0 | 59 | | var progress = await _context.Progresses.AsNoTracking() |
| 0 | 60 | | .Where(p => p.Id == task.CurrentProgressId) |
| 0 | 61 | | .Select(p => new { p.Name }) |
| 0 | 62 | | .FirstOrDefaultAsync(); |
| 0 | 63 | | if (!continueAutoTask) |
| 0 | 64 | | { |
| | 65 | | // Notify the updated task |
| 0 | 66 | | var flowName = await _context.Flows.AsNoTracking() |
| 0 | 67 | | .Where(f => f.Id == task.FlowId) |
| 0 | 68 | | .Select(f => f.Name) |
| 0 | 69 | | .FirstOrDefaultAsync(); |
| 0 | 70 | | string? robotName = null; |
| 0 | 71 | | if (task.AssignedRobotId != null) |
| 0 | 72 | | { |
| 0 | 73 | | robotName = await _context.Robots.AsNoTracking() |
| 0 | 74 | | .Where(r => r.Id == task.AssignedRobotId) |
| 0 | 75 | | .Select(r => r.Name) |
| 0 | 76 | | .FirstOrDefaultAsync(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | // Send the updated to the Redis queue |
| 0 | 80 | | var data = new AutoTaskUpdate |
| 0 | 81 | | { |
| 0 | 82 | | Id = task.Id, |
| 0 | 83 | | Name = task.Name, |
| 0 | 84 | | Priority = task.Priority, |
| 0 | 85 | | FlowId = task.FlowId ?? 0, |
| 0 | 86 | | FlowName = flowName ?? "Deleted Flow", |
| 0 | 87 | | RealmId = task.RealmId, |
| 0 | 88 | | AssignedRobotId = task.AssignedRobotId, |
| 0 | 89 | | AssignedRobotName = robotName, |
| 0 | 90 | | CurrentProgressId = task.CurrentProgressId, |
| 0 | 91 | | CurrentProgressName = progress!.Name, |
| 0 | 92 | | }; |
| 0 | 93 | | await _autoTaskRepository.AutoTaskHasUpdateAsync(task.RealmId, data); |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if (task.CurrentProgressId == (int)ProgressState.Completed || task.CurrentProgressId == (int)ProgressState.Aborted) |
| 0 | 97 | | { |
| | 98 | | // Return immediately if the task is completed / aborted |
| 0 | 99 | | return new RobotClientsAutoTask |
| 0 | 100 | | { |
| 0 | 101 | | TaskId = task.Id, |
| 0 | 102 | | TaskName = task.Name ?? string.Empty, |
| 0 | 103 | | TaskProgressId = task.CurrentProgressId, |
| 0 | 104 | | TaskProgressName = progress!.Name ?? string.Empty, |
| 0 | 105 | | Paths = { }, |
| 0 | 106 | | NextToken = string.Empty, |
| 0 | 107 | | }; |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | var flowDetail = await _context.FlowDetails.AsNoTracking() |
| 0 | 111 | | .Where(fd => fd.FlowId == task.FlowId && fd.Order == (int)task.CurrentProgressOrder!) |
| 0 | 112 | | .FirstOrDefaultAsync(); |
| 0 | 113 | | if (!continueAutoTask && flowDetail!.TriggerId != null) |
| 0 | 114 | | { |
| | 115 | | // Fire the trigger |
| 0 | 116 | | await _triggerService.InitialiseTriggerAsync(task, flowDetail); |
| 0 | 117 | | } |
| | 118 | |
|
| 0 | 119 | | List<RobotClientsPath> paths = []; |
| | 120 | | try |
| 0 | 121 | | { |
| 0 | 122 | | paths = await _autoTaskPathPlanner.GeneratePath(task); |
| 0 | 123 | | } |
| 0 | 124 | | catch (Exception) |
| 0 | 125 | | { |
| 0 | 126 | | await AutoTaskAbortSqlAsync(task.Id); |
| 0 | 127 | | await _emailService.SendAutoTaskAbortEmailAsync(task.Id, AutoTaskAbortReason.PathPlanner); |
| 0 | 128 | | await AddAutoTaskJourney(task); |
| 0 | 129 | | return null; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | string nextToken = task.NextToken ?? string.Empty; |
| 0 | 133 | | if (flowDetail?.AutoTaskNextControllerId != (int)AutoTaskNextController.Robot) |
| 0 | 134 | | { |
| | 135 | | // API has the control |
| 0 | 136 | | nextToken = string.Empty; |
| 0 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | return new RobotClientsAutoTask |
| 0 | 140 | | { |
| 0 | 141 | | TaskId = task.Id, |
| 0 | 142 | | TaskName = task.Name ?? string.Empty, |
| 0 | 143 | | TaskProgressId = task.CurrentProgressId, |
| 0 | 144 | | TaskProgressName = progress!.Name ?? string.Empty, |
| 0 | 145 | | Paths = { paths }, |
| 0 | 146 | | NextToken = nextToken, |
| 0 | 147 | | }; |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public async Task RunSchedulerNewAutoTaskAsync(int realmId, Guid? robotId) |
| 0 | 151 | | { |
| | 152 | | // Find any robot that is idle |
| 0 | 153 | | if (robotId == null) |
| 0 | 154 | | { |
| 0 | 155 | | robotId = await _autoTaskRepository.SchedulerHoldAnyRobotAsync(realmId); |
| 0 | 156 | | if (robotId != null) |
| 0 | 157 | | { |
| 0 | 158 | | await RunSchedulerRobotReadyAsync(robotId.Value); |
| | 159 | | // Release the robot when it obtains the task |
| 0 | 160 | | } |
| 0 | 161 | | } |
| | 162 | | else |
| 0 | 163 | | { |
| 0 | 164 | | if (await _autoTaskRepository.SchedulerHoldRobotAsync(realmId, robotId.Value)) |
| 0 | 165 | | { |
| 0 | 166 | | await RunSchedulerRobotReadyAsync(robotId.Value); |
| | 167 | | // Release the robot when it obtains the task |
| 0 | 168 | | } |
| 0 | 169 | | } |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | private async Task<AutoTask?> GetRunningAutoTaskSqlAsync(Guid robotId) |
| 0 | 173 | | { |
| 0 | 174 | | return await _context.AutoTasks.AsNoTracking() |
| 0 | 175 | | .Include(t => t.AutoTaskDetails) |
| 0 | 176 | | .Where(t => t.AssignedRobotId == robotId) |
| 0 | 177 | | .Where(t => !LgdxHelper.AutoTaskStaticStates.Contains(t.CurrentProgressId)) |
| 0 | 178 | | .OrderByDescending(t => t.Priority) // In case the robot has multiple running task by mistake |
| 0 | 179 | | .ThenByDescending(t => t.AssignedRobotId) |
| 0 | 180 | | .ThenBy(t => t.Id) |
| 0 | 181 | | .FirstOrDefaultAsync(); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | public async Task RunSchedulerRobotNewJoinAsync(Guid robotId) |
| 0 | 185 | | { |
| 0 | 186 | | if (await _onlineRobotsService.GetPauseAutoTaskAssignmentAsync(robotId)) |
| 0 | 187 | | { |
| 0 | 188 | | return; |
| | 189 | | } |
| | 190 | |
|
| 0 | 191 | | var runningAutoTask = await GetRunningAutoTaskSqlAsync(robotId); |
| 0 | 192 | | if (runningAutoTask != null) |
| 0 | 193 | | { |
| | 194 | | // Send the running task to the robot |
| 0 | 195 | | var task = await GenerateTaskDetail(runningAutoTask, true); |
| 0 | 196 | | if (task != null) |
| 0 | 197 | | { |
| 0 | 198 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 199 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, robotId, task); |
| 0 | 200 | | } |
| 0 | 201 | | return; |
| | 202 | | } |
| | 203 | | // Assign the task to the robot |
| 0 | 204 | | await RunSchedulerRobotReadyAsync(robotId); |
| 0 | 205 | | } |
| | 206 | |
|
| | 207 | | private async Task<AutoTask?> GetNextAutoTaskSqlAsync(Guid robotId, int realmId) |
| 0 | 208 | | { |
| 0 | 209 | | AutoTask? task = null; |
| 0 | 210 | | using var transaction = await _context.Database.BeginTransactionAsync(); |
| | 211 | | try |
| 0 | 212 | | { |
| | 213 | | // Get waiting task |
| 0 | 214 | | task = await _context.AutoTasks.FromSql( |
| 0 | 215 | | $@"SELECT * FROM ""Automation.AutoTasks"" AS T |
| 0 | 216 | | WHERE T.""CurrentProgressId"" = {(int)ProgressState.Waiting} AND (T.""AssignedRobotId"" = {robotId} OR T.""A |
| 0 | 217 | | ORDER BY T.""Priority"" DESC, T.""AssignedRobotId"" DESC, T.""Id"" |
| 0 | 218 | | LIMIT 1 FOR UPDATE SKIP LOCKED" |
| 0 | 219 | | ).FirstOrDefaultAsync(); |
| 0 | 220 | | if (task == null) |
| 0 | 221 | | { |
| 0 | 222 | | return null; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | // Get flow detail |
| 0 | 226 | | var flowDetail = await _context.FlowDetails |
| 0 | 227 | | .Where(f => f.FlowId == task!.FlowId) |
| 0 | 228 | | .Select(f => new |
| 0 | 229 | | { |
| 0 | 230 | | f.ProgressId, |
| 0 | 231 | | f.Order |
| 0 | 232 | | }) |
| 0 | 233 | | .OrderBy(f => f.Order) |
| 0 | 234 | | .FirstOrDefaultAsync(); |
| | 235 | |
|
| | 236 | | // Update task |
| 0 | 237 | | task!.AssignedRobotId = robotId; |
| 0 | 238 | | task.CurrentProgressId = flowDetail!.ProgressId; |
| 0 | 239 | | task.CurrentProgressOrder = flowDetail.Order; |
| 0 | 240 | | task.NextToken = LgdxHelper.GenerateMd5Hash($"{robotId} {task.Id} {task.CurrentProgressId} {DateTime.UtcNow}"); |
| 0 | 241 | | await _context.SaveChangesAsync(); |
| 0 | 242 | | await transaction.CommitAsync(); |
| 0 | 243 | | return task; |
| | 244 | | } |
| 0 | 245 | | catch (Exception) |
| 0 | 246 | | { |
| 0 | 247 | | await transaction.RollbackAsync(); |
| 0 | 248 | | } |
| 0 | 249 | | return null; |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | public async Task<bool> RunSchedulerRobotReadyAsync(Guid robotId) |
| 0 | 253 | | { |
| 0 | 254 | | if (await _onlineRobotsService.GetPauseAutoTaskAssignmentAsync(robotId)) |
| 0 | 255 | | { |
| 0 | 256 | | return false; |
| | 257 | | } |
| | 258 | |
|
| 0 | 259 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 260 | | var newTask = await GetNextAutoTaskSqlAsync(robotId, realmId); |
| 0 | 261 | | if (newTask != null) |
| 0 | 262 | | { |
| 0 | 263 | | await AddAutoTaskJourney(newTask); |
| | 264 | | // Send the running task to the robot |
| 0 | 265 | | var task = await GenerateTaskDetail(newTask); |
| 0 | 266 | | if (task != null) |
| 0 | 267 | | { |
| 0 | 268 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, robotId, task); |
| 0 | 269 | | } |
| | 270 | | // Has new task |
| 0 | 271 | | return true; |
| | 272 | | } |
| 0 | 273 | | return false; |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | public async Task ReleaseRobotAsync(Guid robotId) |
| 0 | 277 | | { |
| 0 | 278 | | var realmId = _robotService.GetRobotRealmIdAsync(robotId).Result ?? 0; |
| 0 | 279 | | await _autoTaskRepository.SchedulerReleaseRobotAsync(realmId, robotId); |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | private async Task<AutoTask?> AutoTaskAbortSqlAsync(int taskId, Guid? robotId = null, string? token = null) |
| 0 | 283 | | { |
| 0 | 284 | | AutoTask? task = null; |
| 0 | 285 | | using var transaction = await _context.Database.BeginTransactionAsync(); |
| | 286 | | try |
| 0 | 287 | | { |
| | 288 | | // Get task |
| 0 | 289 | | if (robotId == null && string.IsNullOrWhiteSpace(token)) |
| 0 | 290 | | { |
| | 291 | | // From API |
| 0 | 292 | | task = await _context.AutoTasks.FromSql( |
| 0 | 293 | | $@"SELECT * FROM ""Automation.AutoTasks"" AS T |
| 0 | 294 | | WHERE T.""Id"" = {taskId} |
| 0 | 295 | | LIMIT 1 FOR UPDATE NOWAIT" |
| 0 | 296 | | ).FirstOrDefaultAsync(); |
| 0 | 297 | | } |
| | 298 | | else |
| 0 | 299 | | { |
| 0 | 300 | | task = await _context.AutoTasks.FromSql( |
| 0 | 301 | | $@"SELECT * FROM ""Automation.AutoTasks"" AS T |
| 0 | 302 | | WHERE T.""Id"" = {taskId} AND T.""AssignedRobotId"" = {robotId} AND T.""NextToken"" = {token} |
| 0 | 303 | | LIMIT 1 FOR UPDATE NOWAIT" |
| 0 | 304 | | ).FirstOrDefaultAsync(); |
| 0 | 305 | | } |
| 0 | 306 | | if (task == null) |
| 0 | 307 | | { |
| 0 | 308 | | return null; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | // Update task |
| 0 | 312 | | task!.CurrentProgressId = (int)ProgressState.Aborted; |
| 0 | 313 | | task.CurrentProgressOrder = null; |
| 0 | 314 | | task.NextToken = null; |
| 0 | 315 | | await _context.SaveChangesAsync(); |
| 0 | 316 | | await transaction.CommitAsync(); |
| 0 | 317 | | } |
| 0 | 318 | | catch (Exception) |
| 0 | 319 | | { |
| 0 | 320 | | await transaction.RollbackAsync(); |
| 0 | 321 | | } |
| 0 | 322 | | return task; |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | private async Task DeleteTriggerRetries(int taskId) |
| 0 | 326 | | { |
| 0 | 327 | | var count = await _context.TriggerRetries.Where(tr => tr.AutoTaskId == taskId).CountAsync(); |
| 0 | 328 | | if (count > 0) |
| 0 | 329 | | { |
| 0 | 330 | | await _context.TriggerRetries.Where(tr => tr.AutoTaskId == taskId).ExecuteDeleteAsync(); |
| 0 | 331 | | } |
| 0 | 332 | | } |
| | 333 | |
|
| | 334 | | public async Task AutoTaskAbortAsync(Guid robotId, int taskId, string token, AutoTaskAbortReason autoTaskAbortReason) |
| 0 | 335 | | { |
| 0 | 336 | | var task = await AutoTaskAbortSqlAsync(taskId, robotId, token); |
| 0 | 337 | | if (task == null) |
| 0 | 338 | | { |
| 0 | 339 | | return; |
| | 340 | | } |
| | 341 | |
|
| 0 | 342 | | await DeleteTriggerRetries(taskId); |
| 0 | 343 | | await _emailService.SendAutoTaskAbortEmailAsync(taskId, autoTaskAbortReason); |
| 0 | 344 | | await AddAutoTaskJourney(task); |
| | 345 | | // Allow update the task to rabbitmq |
| 0 | 346 | | var sendTask = await GenerateTaskDetail(task); |
| 0 | 347 | | if (!await RunSchedulerRobotReadyAsync(robotId)) |
| 0 | 348 | | { |
| | 349 | | // No new task, send the aborted task to idle the robot |
| 0 | 350 | | if (sendTask != null) |
| 0 | 351 | | { |
| 0 | 352 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 353 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, robotId, sendTask); |
| 0 | 354 | | } |
| 0 | 355 | | } |
| 0 | 356 | | } |
| | 357 | |
|
| | 358 | | public async Task<bool> AutoTaskAbortApiAsync(int taskId) |
| 0 | 359 | | { |
| 0 | 360 | | var task = await AutoTaskAbortSqlAsync(taskId); |
| 0 | 361 | | if (task == null) |
| 0 | 362 | | { |
| 0 | 363 | | return false; |
| | 364 | | } |
| | 365 | |
|
| 0 | 366 | | await DeleteTriggerRetries(taskId); |
| 0 | 367 | | await _emailService.SendAutoTaskAbortEmailAsync(taskId, AutoTaskAbortReason.UserApi); |
| 0 | 368 | | await AddAutoTaskJourney(task); |
| | 369 | | // Allow update the task to rabbitmq |
| 0 | 370 | | var sendTask = await GenerateTaskDetail(task); |
| 0 | 371 | | if (task.AssignedRobotId != null && !await RunSchedulerRobotReadyAsync(task.AssignedRobotId.Value)) |
| 0 | 372 | | { |
| | 373 | | // No new task, send the aborted task to idle the robot |
| 0 | 374 | | if (sendTask != null) |
| 0 | 375 | | { |
| 0 | 376 | | var realmId = await _robotService.GetRobotRealmIdAsync(task.AssignedRobotId!.Value) ?? 0; |
| 0 | 377 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, task.AssignedRobotId!.Value, sendTask); |
| 0 | 378 | | } |
| 0 | 379 | | } |
| 0 | 380 | | return true; |
| 0 | 381 | | } |
| | 382 | |
|
| | 383 | | private async Task<AutoTask?> AutoTaskNextSqlAsync(Guid robotId, int taskId, string token) |
| 0 | 384 | | { |
| 0 | 385 | | AutoTask? task = null; |
| 0 | 386 | | using var transaction = await _context.Database.BeginTransactionAsync(); |
| | 387 | | try |
| 0 | 388 | | { |
| | 389 | | // Get waiting task |
| 0 | 390 | | task = await _context.AutoTasks.FromSql( |
| 0 | 391 | | $@"SELECT * FROM ""Automation.AutoTasks"" AS T |
| 0 | 392 | | WHERE T.""Id"" = {taskId} AND T.""AssignedRobotId"" = {robotId} AND T.""NextToken"" = {token} |
| 0 | 393 | | LIMIT 1 FOR UPDATE NOWAIT" |
| 0 | 394 | | ).FirstOrDefaultAsync(); |
| 0 | 395 | | if (task == null) |
| 0 | 396 | | { |
| 0 | 397 | | return null; |
| | 398 | | } |
| | 399 | |
|
| | 400 | | // Get flow detail |
| 0 | 401 | | var flowDetail = await _context.FlowDetails.AsNoTracking() |
| 0 | 402 | | .Where(f => f.FlowId == task!.FlowId) |
| 0 | 403 | | .Where(f => f.Order > task!.CurrentProgressOrder) |
| 0 | 404 | | .Select(f => new |
| 0 | 405 | | { |
| 0 | 406 | | f.ProgressId, |
| 0 | 407 | | f.Order |
| 0 | 408 | | }) |
| 0 | 409 | | .OrderBy(f => f.Order) |
| 0 | 410 | | .FirstOrDefaultAsync(); |
| | 411 | |
|
| | 412 | | // Update task |
| 0 | 413 | | if (flowDetail != null) |
| 0 | 414 | | { |
| 0 | 415 | | task!.CurrentProgressId = flowDetail!.ProgressId; |
| 0 | 416 | | task.CurrentProgressOrder = flowDetail.Order; |
| 0 | 417 | | task.NextToken = LgdxHelper.GenerateMd5Hash($"{robotId} {task.Id} {task.CurrentProgressId} {DateTime.UtcNow}"); |
| 0 | 418 | | } |
| | 419 | | else |
| 0 | 420 | | { |
| 0 | 421 | | task!.CurrentProgressId = (int)ProgressState.Completed; |
| 0 | 422 | | task.CurrentProgressOrder = null; |
| 0 | 423 | | task.NextToken = null; |
| 0 | 424 | | } |
| | 425 | |
|
| 0 | 426 | | await _context.SaveChangesAsync(); |
| 0 | 427 | | await transaction.CommitAsync(); |
| 0 | 428 | | } |
| 0 | 429 | | catch (Exception) |
| 0 | 430 | | { |
| 0 | 431 | | await transaction.RollbackAsync(); |
| 0 | 432 | | } |
| 0 | 433 | | return task; |
| 0 | 434 | | } |
| | 435 | |
|
| | 436 | | public async Task AutoTaskNextAsync(Guid robotId, int taskId, string token) |
| 0 | 437 | | { |
| 0 | 438 | | var task = await AutoTaskNextSqlAsync(robotId, taskId, token); |
| 0 | 439 | | if (task == null) |
| 0 | 440 | | { |
| 0 | 441 | | return; |
| | 442 | | } |
| | 443 | |
|
| 0 | 444 | | await AddAutoTaskJourney(task); |
| 0 | 445 | | if (task.CurrentProgressId == (int)ProgressState.Completed && await RunSchedulerRobotReadyAsync(robotId)) |
| 0 | 446 | | { |
| | 447 | | // Allow update the task to rabbitmq |
| 0 | 448 | | await GenerateTaskDetail(task); |
| | 449 | | // Completed task, and new task available |
| 0 | 450 | | return; |
| | 451 | | } |
| | 452 | |
|
| | 453 | | // Send the running task / complete task to the robot |
| 0 | 454 | | var sendTask = await GenerateTaskDetail(task); |
| 0 | 455 | | if (sendTask != null) |
| 0 | 456 | | { |
| 0 | 457 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 458 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, robotId, sendTask); |
| 0 | 459 | | } |
| 0 | 460 | | } |
| | 461 | |
|
| | 462 | | public async Task<bool> AutoTaskNextApiAsync(Guid robotId, int taskId, string token) |
| 0 | 463 | | { |
| 0 | 464 | | var task = await AutoTaskNextSqlAsync(robotId, taskId, token); |
| 0 | 465 | | if (task == null) |
| 0 | 466 | | { |
| 0 | 467 | | return false; |
| | 468 | | } |
| | 469 | |
|
| 0 | 470 | | await AddAutoTaskJourney(task); |
| 0 | 471 | | if (task.CurrentProgressId == (int)ProgressState.Completed && await RunSchedulerRobotReadyAsync(robotId)) |
| 0 | 472 | | { |
| | 473 | | // Allow update the task to rabbitmq |
| 0 | 474 | | await GenerateTaskDetail(task); |
| | 475 | | // Completed task, and new task available |
| 0 | 476 | | return true; |
| | 477 | | } |
| | 478 | |
|
| | 479 | | // Send the running task / complete task to the robot |
| 0 | 480 | | var sendTask = await GenerateTaskDetail(task); |
| 0 | 481 | | if (sendTask != null) |
| 0 | 482 | | { |
| 0 | 483 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 484 | | await _autoTaskRepository.AddAutoTaskAsync(realmId, robotId, sendTask); |
| 0 | 485 | | } |
| 0 | 486 | | return true; |
| 0 | 487 | | } |
| | 488 | | } |