mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 10:10:48 +01:00
Add event type selection for event viewer
This commit is contained in:
@@ -15,7 +15,7 @@ public class EventViewModel : ObservableObject
|
|||||||
public EventViewModel(EventData eventData)
|
public EventViewModel(EventData eventData)
|
||||||
{
|
{
|
||||||
Time = $"{DateTime.Now:HH:mm:ss.fff}";
|
Time = $"{DateTime.Now:HH:mm:ss.fff}";
|
||||||
Type = eventData?.EventType.ToUpper() ?? string.Empty;
|
Type = eventData?.EventType?.ToUpper() ?? string.Empty;
|
||||||
Uri = eventData?.Uri ?? string.Empty;
|
Uri = eventData?.Uri ?? string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using BlossomiShymae.GrrrLCU;
|
using Avalonia.Collections;
|
||||||
|
using BlossomiShymae.GrrrLCU;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
@@ -8,8 +9,11 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Websocket.Client;
|
using Websocket.Client;
|
||||||
|
|
||||||
namespace Needlework.Net.ViewModels.Pages.Websocket;
|
namespace Needlework.Net.ViewModels.Pages.Websocket;
|
||||||
@@ -25,37 +29,76 @@ public partial class WebsocketViewModel : PageBase
|
|||||||
[ObservableProperty] private bool _isTail = false;
|
[ObservableProperty] private bool _isTail = false;
|
||||||
[ObservableProperty] private EventViewModel? _selectedEventLog = null;
|
[ObservableProperty] private EventViewModel? _selectedEventLog = null;
|
||||||
|
|
||||||
|
[ObservableProperty] private IAvaloniaList<string> _eventTypes = new AvaloniaList<string>();
|
||||||
|
[ObservableProperty] private string _eventType = "OnJsonApiEvent";
|
||||||
|
|
||||||
private Dictionary<string, EventMessage> _events = [];
|
private Dictionary<string, EventMessage> _events = [];
|
||||||
|
|
||||||
public WebsocketClient? Client { get; set; }
|
public WebsocketClient? Client { get; set; }
|
||||||
|
|
||||||
|
public List<IDisposable> ClientDisposables = [];
|
||||||
|
|
||||||
|
private readonly object _tokenLock = new();
|
||||||
|
public CancellationTokenSource TokenSource { get; set; } = new();
|
||||||
|
|
||||||
|
public HttpClient HttpClient { get; }
|
||||||
|
|
||||||
public IReadOnlyList<EventViewModel> FilteredEventLog => string.IsNullOrWhiteSpace(Search) ? EventLog : [.. EventLog.Where(x => x.Key.Contains(Search, StringComparison.InvariantCultureIgnoreCase))];
|
public IReadOnlyList<EventViewModel> FilteredEventLog => string.IsNullOrWhiteSpace(Search) ? EventLog : [.. EventLog.Where(x => x.Key.Contains(Search, StringComparison.InvariantCultureIgnoreCase))];
|
||||||
|
|
||||||
public WebsocketViewModel() : base("Event Viewer", "plug", -100)
|
public WebsocketViewModel(HttpClient httpClient) : base("Event Viewer", "plug", -100)
|
||||||
{
|
{
|
||||||
|
HttpClient = httpClient;
|
||||||
EventLog.CollectionChanged += (s, e) => OnPropertyChanged(nameof(FilteredEventLog));
|
EventLog.CollectionChanged += (s, e) => OnPropertyChanged(nameof(FilteredEventLog));
|
||||||
var thread = new Thread(InitializeWebsocket) { IsBackground = true };
|
Task.Run(async () =>
|
||||||
thread.Start();
|
{
|
||||||
|
await InitializeEventTypes();
|
||||||
|
InitializeWebsocket();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task InitializeEventTypes()
|
||||||
|
{
|
||||||
|
var file = await HttpClient.GetStringAsync("https://raw.githubusercontent.com/dysolix/hasagi-types/refs/heads/main/lcu-events.d.ts");
|
||||||
|
var matches = EventTypesRegex().Matches(file);
|
||||||
|
Avalonia.Threading.Dispatcher.UIThread.Invoke(() => EventTypes.AddRange(matches.Select(m => m.Groups[1].Value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeWebsocket()
|
private void InitializeWebsocket()
|
||||||
{
|
{
|
||||||
while (true)
|
lock (_tokenLock)
|
||||||
{
|
{
|
||||||
try
|
if (Client != null)
|
||||||
{
|
{
|
||||||
var client = Connector.CreateLcuWebsocketClient();
|
foreach (var disposable in ClientDisposables)
|
||||||
client.EventReceived.Subscribe(OnMessage);
|
disposable.Dispose();
|
||||||
client.DisconnectionHappened.Subscribe(OnDisconnection);
|
ClientDisposables.Clear();
|
||||||
client.ReconnectionHappened.Subscribe(OnReconnection);
|
Client.Dispose();
|
||||||
|
|
||||||
client.Start();
|
|
||||||
client.Send(new EventMessage(EventRequestType.Subscribe, EventKinds.OnJsonApiEvent));
|
|
||||||
Client = client;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
catch (Exception) { }
|
TokenSource.Cancel();
|
||||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
var tokenSource = new CancellationTokenSource();
|
||||||
|
var thread = new Thread(() =>
|
||||||
|
{
|
||||||
|
while (!tokenSource.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var client = Connector.CreateLcuWebsocketClient();
|
||||||
|
ClientDisposables.Add(client.EventReceived.Subscribe(OnMessage));
|
||||||
|
ClientDisposables.Add(client.DisconnectionHappened.Subscribe(OnDisconnection));
|
||||||
|
ClientDisposables.Add(client.ReconnectionHappened.Subscribe(OnReconnection));
|
||||||
|
|
||||||
|
client.Start();
|
||||||
|
client.Send(new EventMessage(EventRequestType.Subscribe, new EventKind() { Prefix = EventType }));
|
||||||
|
Client = client;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception) { }
|
||||||
|
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
{ IsBackground = true };
|
||||||
|
thread.Start();
|
||||||
|
TokenSource = tokenSource;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,10 +127,13 @@ public partial class WebsocketViewModel : PageBase
|
|||||||
|
|
||||||
private void OnDisconnection(DisconnectionInfo info)
|
private void OnDisconnection(DisconnectionInfo info)
|
||||||
{
|
{
|
||||||
Trace.WriteLine($"-- Disconnection --\nType:{info.Type}\nSubProocol:{info.SubProtocol}\nCloseStatus:{info.CloseStatus}\nCloseStatusDescription:{info.CloseStatusDescription}\nExceptionMessage:{info?.Exception?.Message}\n:InnerException:{info?.Exception?.InnerException}");
|
Trace.WriteLine($"-- Disconnection --\nType:{info.Type}\nSubProtocol:{info.SubProtocol}\nCloseStatus:{info.CloseStatus}\nCloseStatusDescription:{info.CloseStatusDescription}\nExceptionMessage:{info?.Exception?.Message}\n:InnerException:{info?.Exception?.InnerException}");
|
||||||
Client?.Dispose();
|
InitializeWebsocket();
|
||||||
var thread = new Thread(InitializeWebsocket) { IsBackground = true };
|
}
|
||||||
thread.Start();
|
|
||||||
|
partial void OnEventTypeChanged(string value)
|
||||||
|
{
|
||||||
|
InitializeWebsocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMessage(EventMessage message)
|
private void OnMessage(EventMessage message)
|
||||||
@@ -122,4 +168,7 @@ public partial class WebsocketViewModel : PageBase
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[GeneratedRegex("\"(.*?)\":")]
|
||||||
|
public static partial Regex EventTypesRegex();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,22 @@
|
|||||||
<Grid RowDefinitions="*,auto,*" Margin="16">
|
<Grid RowDefinitions="*,auto,*" Margin="16">
|
||||||
<Border Grid.Row="0"
|
<Border Grid.Row="0"
|
||||||
Padding="0 0 0 8">
|
Padding="0 0 0 8">
|
||||||
<Grid RowDefinitions="auto,*" ColumnDefinitions="*">
|
<Grid RowDefinitions="auto,auto,*" ColumnDefinitions="*">
|
||||||
|
<Grid Grid.Row="0"
|
||||||
|
Grid.Column="0"
|
||||||
|
RowDefinitions="*">
|
||||||
|
<ComboBox ItemsSource="{Binding EventTypes}"
|
||||||
|
SelectedItem="{Binding EventType}"
|
||||||
|
Grid.Row="0"
|
||||||
|
HorizontalAlignment="Stretch"
|
||||||
|
HorizontalContentAlignment="Left"/>
|
||||||
|
</Grid>
|
||||||
<Grid
|
<Grid
|
||||||
Grid.Row="0"
|
Grid.Row="1"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
RowDefinitions="*"
|
RowDefinitions="*"
|
||||||
ColumnDefinitions="auto,*,auto,auto">
|
ColumnDefinitions="auto,*,auto,auto"
|
||||||
|
Margin="0 8 0 0">
|
||||||
<Button Margin="0 0 8 0"
|
<Button Margin="0 0 8 0"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
@@ -37,7 +47,7 @@
|
|||||||
Content="Tail"
|
Content="Tail"
|
||||||
IsChecked="{Binding IsTail}"/>
|
IsChecked="{Binding IsTail}"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<ListBox Grid.Row="1"
|
<ListBox Grid.Row="2"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Name="EventViewer"
|
Name="EventViewer"
|
||||||
Margin="0 8 0 0"
|
Margin="0 8 0 0"
|
||||||
|
|||||||
Reference in New Issue
Block a user