В модуль витрины добавлен отдельный запрос статусов

This commit is contained in:
se_ga 2026-03-02 09:11:40 +03:00
parent 8d576c3af2
commit 481e87ec3b
2 changed files with 83 additions and 3 deletions

View File

@ -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<Dictionary<int, BoxStatus>> 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;
}
}
}

View File

@ -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<Dictionary<int, BoxStatus>> GetStatusesAsync()
{
byte[] buffer = new byte[1024];
Dictionary<int, BoxStatus> keyValuePairs = new Dictionary<int, BoxStatus>();
//очистка пол сек
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, //Готов к выдаче