145 lines
4.9 KiB
C#
145 lines
4.9 KiB
C#
using BarRaider.SdTools;
|
|
using Newtonsoft.Json;
|
|
using SocketIOClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace ClipTrimDotNet.Client
|
|
{
|
|
public class ClipTrimClient
|
|
{
|
|
private static ClipTrimClient? instance;
|
|
public static ClipTrimClient Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = new ClipTrimClient();
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
//private HttpClient httpClient;
|
|
private SocketIO socket;
|
|
|
|
public int PortNumber { get; set; } = 5010;
|
|
|
|
public ClipTrimClient()
|
|
{
|
|
//httpClient = new HttpClient()
|
|
//{
|
|
// BaseAddress = new Uri("http://localhost:5010/"),
|
|
// Timeout = TimeSpan.FromSeconds(10)
|
|
//};
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"Starting ClipTrimClient on port {PortNumber}");
|
|
socket = new SocketIO(new Uri($"http://localhost:5010/"));
|
|
socket.Options.AutoUpgrade = false;
|
|
socket.Options.ConnectionTimeout = TimeSpan.FromSeconds(10);
|
|
socket.Options.Reconnection = true;
|
|
socket.On("full_data", ctx =>
|
|
{
|
|
try
|
|
{
|
|
var response = ctx.GetValue<List<CollectionMetaData>>(0);
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"full_data event {JsonConvert.SerializeObject(response)}");
|
|
Collections = response!;
|
|
//Logger.Instance.LogMessage(TracingLevel.INFO, $"Collections {JsonConvert.SerializeObject(Collections)}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"full_data error {ex.ToString()}");
|
|
}
|
|
return Task.CompletedTask;
|
|
});
|
|
socket.On("collection_updated", ctx =>
|
|
{
|
|
try
|
|
{
|
|
var response = ctx.GetValue<CollectionMetaData>(0)!;
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"collection_updated event {JsonConvert.SerializeObject(response)}");
|
|
int index = Collections.FindIndex(x => x.Id == response.Id);
|
|
if (index != -1)
|
|
{
|
|
Collections[index] = response;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
});
|
|
|
|
socket.OnDisconnected += (sender, e) =>
|
|
{
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"Socket disconnected: {e}");
|
|
Task.Run(async () => await Connect());
|
|
};
|
|
Task.Run(async () => await Connect());
|
|
}
|
|
|
|
public async Task Connect()
|
|
{
|
|
while (!socket.Connected)
|
|
{
|
|
try
|
|
{
|
|
await socket.ConnectAsync();
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<CollectionMetaData> Collections { get; private set; } = new List<CollectionMetaData>();
|
|
public int SelectedCollection { get; private set; } = -1;
|
|
public int PageIndex { get; private set; } = 0;
|
|
|
|
public List<string> GetCollectionNames()
|
|
{
|
|
//await GetMetadata();
|
|
return Collections.Select(x => x.Name).ToList();
|
|
}
|
|
|
|
public void SetSelectedCollectionByName(string name)
|
|
{
|
|
SelectedCollection = Collections.FindIndex(x => x.Name == name);
|
|
if (SelectedCollection != -1)
|
|
{
|
|
PageIndex = 0;
|
|
}
|
|
}
|
|
|
|
public ClipMetadata? GetClipByPagedIndex(int index)
|
|
{
|
|
SelectedCollection = Collections.FindIndex(x => x.Name == GlobalSettings.Instance.ProfileName);
|
|
if (SelectedCollection == -1) return null;
|
|
int clipIndex = PageIndex * 10 + index;
|
|
var collection = Collections[SelectedCollection];
|
|
if (clipIndex >= 0 && clipIndex < collection.Clips.Count)
|
|
{
|
|
return collection.Clips[clipIndex];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public async void PlayClip(ClipMetadata? metadata)
|
|
{
|
|
if (metadata == null) return;
|
|
Logger.Instance.LogMessage(TracingLevel.INFO, $"playing clip:");
|
|
await socket.EmitAsync("play_clip", new List<object>() { metadata });
|
|
}
|
|
}
|
|
}
|