From 481e87ec3bdc94648a090e1d7841bc14224fe871 Mon Sep 17 00:00:00 2001 From: se_ga Date: Mon, 2 Mar 2026 09:11:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C?= =?UTF-8?q?=20=D0=B2=D0=B8=D1=82=D1=80=D0=B8=D0=BD=D1=8B=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BE=D1=82=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=81=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scenes/StandTester.cs | 16 ++++- .../PrizeStand/PrizeStandController.cs | 70 ++++++++++++++++++- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/Assets/Scenes/StandTester.cs b/Assets/Scenes/StandTester.cs index e9f4b08..b281851 100644 --- a/Assets/Scenes/StandTester.cs +++ b/Assets/Scenes/StandTester.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; using UnityEditor.Experimental.GraphView; using UnityEngine; @@ -40,12 +41,14 @@ public class StandTester : MonoBehaviour Debug.Log($"Îòêðûòèå. Result {arg1}; Message {arg2}"); } + + Task> awaitStatuses; void Update() { if (!initialized) return; if (Input.GetKeyDown(KeyCode.Alpha0)) { - + awaitStatuses = Task.Run(PrizeStandController.GetStatusesAsync); } if (Input.GetKeyDown(KeyCode.Alpha1)) { @@ -63,5 +66,16 @@ public class StandTester : MonoBehaviour _ = PrizeStandController.OpenBoxAsync(3); } + if (awaitStatuses != null && awaitStatuses.IsCompleted) + { + string res = ""; + foreach (var kv in awaitStatuses.Result) + { + res += $"{kv.Key} : {kv.Value}\n"; + } + Debug.Log(res); + awaitStatuses = null; + } + } } diff --git a/Assets/Scripts/PrizeStand/PrizeStandController.cs b/Assets/Scripts/PrizeStand/PrizeStandController.cs index cf2ed64..0426864 100644 --- a/Assets/Scripts/PrizeStand/PrizeStandController.cs +++ b/Assets/Scripts/PrizeStand/PrizeStandController.cs @@ -100,7 +100,7 @@ public static class PrizeStandController return; } - //очистка пол сук + //очистка пол сек await Task.Run(() => { byte[] buf = new byte[1024]; @@ -265,6 +265,72 @@ public static class PrizeStandController return status; } + public static async Task> GetStatusesAsync() + { + byte[] buffer = new byte[1024]; + Dictionary keyValuePairs = new Dictionary(); + + //очистка пол сек + await Task.Run(() => + { + byte[] buf = new byte[1024]; + var sw = Stopwatch.StartNew(); + while (sw.Elapsed.TotalSeconds < 0.5) + { + ComNativeWrapper.Serial_ReadLineUTF8_bytes(handle, buf, buf.Length); + } + }); + + + SendRequestStatusCommand(); + + + int boxesRecieved = 0; + var sw = Stopwatch.StartNew(); + + while (sw.Elapsed.TotalSeconds < 3 && boxesRecieved < boxesCount) + { + //ждать пока всё не считаем + int entryLength = await Task.Run(() => ComNativeWrapper.Serial_ReadLineUTF8_bytes(handle, buffer, buffer.Length)); + + if (entryLength == 0) //ничего не пришло + { + continue; + } + else if (entryLength < 0) //ошибка + { + _logger?.Log($"Ошибка при считывании статуса ячеек={entryLength} :: {ComNativeWrapper.GetLastError()}"); + continue; + } + + + string entry = Encoding.UTF8.GetString(buffer, 0, entryLength).Trim(); + if (!entry.StartsWith("BOX")) + { + continue; + } + + var ParseResult = ParseBoxStatus(entry); + UnityEngine.Debug.Log($"{ParseResult.BoxNumber} : {ParseResult.Status}"); + + if (ParseResult.BoxNumber == -1) + { + _logger?.Log($"[Модуль витрины. Ошибка получения статуса ячейки]\n{ParseResult.Message}"); + } + else + { + boxesRecieved++; + keyValuePairs[ParseResult.BoxNumber] = ParseResult.Status; + } + } + + if (sw.Elapsed.TotalSeconds >= 3) UnityEngine.Debug.Log("Вышли по таймауту"); + + + return keyValuePairs; + } + + private static (int BoxNumber, BoxStatus Status, string Message) ParseBoxStatus(string entryString) { int number = -1; @@ -316,7 +382,7 @@ public static class PrizeStandController #endregion } -enum BoxStatus +public enum BoxStatus { //Статусы ДО открытия OK, //Готов к выдаче