mirror of
https://github.com/maker-community/ElectronBot.DotNet.git
synced 2026-10-20 23:58:11 +08:00
添加异步发送数据代码
This commit is contained in:
+1
-2
@@ -10,11 +10,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="1.4.1" />
|
||||
</ItemGroup>
|
||||
<!-- CsWinRT properties -->
|
||||
<PropertyGroup>
|
||||
<CsWinRTComponent>true</CsWinRTComponent>
|
||||
<CsWinRTWindowsMetadata>10.0.19041.0</CsWinRTWindowsMetadata>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<GenerateDocumentationFile>False</GenerateDocumentationFile>
|
||||
|
||||
@@ -20,6 +20,7 @@ using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using Verdure.ElectronBot.GrpcService;
|
||||
using Windows.Media.Playback;
|
||||
using Windows.ApplicationModel.Background;
|
||||
using Services;
|
||||
|
||||
namespace ElectronBot.BraincasePreview;
|
||||
|
||||
@@ -147,6 +148,8 @@ public partial class App : Application
|
||||
|
||||
services.AddSingleton<IActionExpressionProviderFactory, ActionExpressionProviderFactory>();
|
||||
|
||||
services.AddSingleton<EmoticonActionFrameService>();
|
||||
|
||||
|
||||
services.AddGrpcClient<ElectronBotActionGrpc.ElectronBotActionGrpcClient>(o =>
|
||||
{
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Verdure.ElectronBot.Core.Models;
|
||||
using ElectronBot.BraincasePreview.Models;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using Services;
|
||||
using Verdure.ElectronBot.Core.Models;
|
||||
using Windows.Devices.Enumeration;
|
||||
using Windows.Graphics.Imaging;
|
||||
using Windows.Media.Devices;
|
||||
@@ -237,7 +233,7 @@ public class EbHelper
|
||||
|
||||
var dataMeta = mat2.Data;
|
||||
|
||||
Marshal.Copy(dataMeta, data, 0, 240 * 240 * 3);
|
||||
Marshal.Copy(dataMeta, data, 0, 240 * 240 * 3);
|
||||
}
|
||||
var frame = new EmoticonActionFrame(
|
||||
data, true, action.J1, action.J2, action.J3, action.J4, action.J5, action.J6);
|
||||
@@ -338,6 +334,9 @@ public class EbHelper
|
||||
Marshal.Copy(dataMeta, data, 0, 240 * 240 * 3);
|
||||
|
||||
EmojiPlayHelper.Current.Enqueue(new EmoticonActionFrame(data));
|
||||
|
||||
var service = App.GetService<EmoticonActionFrameService>();
|
||||
_ = await service.SendToUsbDeviceAsync(new EmoticonActionFrame(data));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Concurrent;
|
||||
using ElectronBot.BraincasePreview.Helpers;
|
||||
using Verdure.ElectronBot.Core.Models;
|
||||
|
||||
namespace Services;
|
||||
public class EmoticonActionFrameService
|
||||
{
|
||||
private readonly ConcurrentQueue<(EmoticonActionFrame Data, TaskCompletionSource<bool> Tcs, CancellationToken Ct)> _queue = new();
|
||||
|
||||
private int _isSending;
|
||||
|
||||
public async Task<bool> SendToUsbDeviceAsync(EmoticonActionFrame data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
_queue.Enqueue((data, tcs, cancellationToken));
|
||||
|
||||
if (Interlocked.CompareExchange(ref _isSending, 1, 0) == 0)
|
||||
{
|
||||
_ = Task.Run(SendDataAsync, CancellationToken.None);
|
||||
}
|
||||
|
||||
return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task SendDataAsync()
|
||||
{
|
||||
while (_queue.TryDequeue(out var item))
|
||||
{
|
||||
var (data, tcs, cancellationToken) = item;
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
tcs.TrySetCanceled(cancellationToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
await Task.Delay(100).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
if (data != null)
|
||||
{
|
||||
ElectronBotHelper.Instance.PlayEmoticonActionFrame(data);
|
||||
tcs.TrySetResult(true);
|
||||
}
|
||||
//todo:
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Interlocked.Exchange(ref _isSending, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user