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, //Готов к выдаче