159 lines
5.5 KiB
C#
159 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using SocketIOClient;
|
|
using BarRaider.SdTools;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
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;
|
|
});
|
|
|
|
Task.Run(async () => await socket.ConnectAsync());
|
|
//Task.Run(ShortPoll);
|
|
}
|
|
|
|
|
|
//public async Task ShortPoll()
|
|
//{
|
|
// while (true)
|
|
// {
|
|
// await GetMetadata();
|
|
// await Task.Delay(TimeSpan.FromSeconds(5)); await Task.Delay(TimeSpan.FromSeconds(5));
|
|
|
|
// }
|
|
//}
|
|
|
|
public List<CollectionMetaData> Collections { get; private set; } = new List<CollectionMetaData>();
|
|
public int SelectedCollection { get; private set; } = -1;
|
|
public int PageIndex { get; private set; } = 0;
|
|
//private async Task GetMetadata()
|
|
//{
|
|
// try
|
|
// {
|
|
// var response = await httpClient.GetAsync("meta");
|
|
// if (response.IsSuccessStatusCode)
|
|
// {
|
|
// var json = await response.Content.ReadAsStringAsync();
|
|
// dynamic collections = JsonConvert.DeserializeObject(json);
|
|
// collections = collections.collections;
|
|
// Collections = JsonConvert.DeserializeObject<List<CollectionMetaData>>(collections.ToString());
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// //Logger.Instance.LogMessage(TracingLevel.INFO, $"Error pinging ClipTrim API: {ex.Message}");
|
|
// return;
|
|
// }
|
|
|
|
//}
|
|
|
|
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)
|
|
{
|
|
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;
|
|
|
|
//var response = await httpClient.PostAsync("playback/start", new StringContent(JsonConvert.SerializeObject(metadata), Encoding.UTF8, "application/json"));
|
|
//if (!response.IsSuccessStatusCode)
|
|
//{
|
|
// //Logger.Instance.LogMessage(TracingLevel.INFO, $"Error playing clip: {response.ReasonPhrase}");
|
|
//}
|
|
}
|
|
}
|
|
}
|