Improve ticker handling to stop recording
This commit is contained in:
parent
426268b512
commit
1e8fe94e3e
|
@ -32,78 +32,93 @@ class Program
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task ConnectWebSocket()
|
private static async Task ConnectWebSocket()
|
||||||
|
{
|
||||||
|
var tickerUri = new Uri($"ws://{WebsocketAddress}:{WebsocketPort}");
|
||||||
|
var reconnecting = false;
|
||||||
|
|
||||||
|
var lastMessage = string.Empty;
|
||||||
|
var consecutiveMessageCount = 0;
|
||||||
|
var isRecording = false;
|
||||||
|
var currentSongName = string.Empty;
|
||||||
|
|
||||||
|
// Flag to track if we need to check for "MUSIC SELECT!!"
|
||||||
|
var shouldCheckForMusicSelect = false;
|
||||||
|
|
||||||
|
using var clientWebSocket = new ClientWebSocket();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var tickerUri = new Uri($"ws://{WebsocketAddress}:{WebsocketPort}");
|
await clientWebSocket.ConnectAsync(tickerUri, CancellationToken.None);
|
||||||
var reconnecting = false;
|
Console.WriteLine("Connected to TickerHook WebSocket.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error connecting to TickerHook WebSocket: {ex.Message}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var lastMessage = string.Empty;
|
var buffer = new byte[1024];
|
||||||
var consecutiveMessageCount = 0;
|
|
||||||
var isRecording = false;
|
|
||||||
var currentSongName = string.Empty;
|
|
||||||
|
|
||||||
using var clientWebSocket = new ClientWebSocket();
|
while (clientWebSocket.State == WebSocketState.Open)
|
||||||
|
{
|
||||||
|
WebSocketReceiveResult result;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await clientWebSocket.ConnectAsync(tickerUri, CancellationToken.None);
|
result = await clientWebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||||
Console.WriteLine("Connected to TickerHook WebSocket.");
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Error connecting to TickerHook WebSocket: {ex.Message}");
|
Console.WriteLine($"Error receiving message: {ex.Message}");
|
||||||
return;
|
reconnecting = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = new byte[1024];
|
var message = Encoding.UTF8.GetString(buffer, 0, result.Count).Trim().ToUpper();
|
||||||
|
Console.WriteLine($"Received message: {message}");
|
||||||
|
|
||||||
while (clientWebSocket.State == WebSocketState.Open)
|
if (message == lastMessage && !message.Contains("SELECT FROM"))
|
||||||
{
|
{
|
||||||
WebSocketReceiveResult result;
|
consecutiveMessageCount++;
|
||||||
try
|
}
|
||||||
{
|
else
|
||||||
result = await clientWebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
{
|
||||||
}
|
consecutiveMessageCount = 1;
|
||||||
catch (Exception ex)
|
lastMessage = message;
|
||||||
{
|
|
||||||
Console.WriteLine($"Error receiving message: {ex.Message}");
|
|
||||||
reconnecting = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var message = Encoding.UTF8.GetString(buffer, 0, result.Count).Trim().ToUpper();
|
|
||||||
Console.WriteLine($"Received message: {message}");
|
|
||||||
|
|
||||||
if (message == lastMessage && !message.Contains("SELECT FROM"))
|
|
||||||
{
|
|
||||||
consecutiveMessageCount++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
consecutiveMessageCount = 1;
|
|
||||||
lastMessage = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (consecutiveMessageCount >= 2 && !message.Contains("SELECT FROM") && !isRecording)
|
|
||||||
{
|
|
||||||
currentSongName = message;
|
|
||||||
Console.WriteLine("Starting recording...");
|
|
||||||
StartRecording(currentSongName);
|
|
||||||
isRecording = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isRecording || (!message.EndsWith("CLEAR!") && !message.EndsWith("FAILED.."))) continue;
|
|
||||||
Console.WriteLine("Stopping recording...");
|
|
||||||
StopRecording(currentSongName);
|
|
||||||
isRecording = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reconnecting)
|
if (consecutiveMessageCount >= 2 && !message.Contains("SELECT FROM") && !isRecording)
|
||||||
{
|
{
|
||||||
await Task.Delay(10000);
|
currentSongName = message;
|
||||||
await ConnectWebSocket();
|
Console.WriteLine("Starting recording...");
|
||||||
|
StartRecording(currentSongName);
|
||||||
|
isRecording = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRecording)
|
||||||
|
{
|
||||||
|
if (shouldCheckForMusicSelect && message.Contains("MUSIC SELECT!!"))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Stopping recording...");
|
||||||
|
StopRecording(currentSongName);
|
||||||
|
isRecording = false;
|
||||||
|
shouldCheckForMusicSelect = false;
|
||||||
|
}
|
||||||
|
else if (message.EndsWith("CLEAR!") || message.EndsWith("FAILED.."))
|
||||||
|
{
|
||||||
|
// Set the flag to check the next message for "MUSIC SELECT!!"
|
||||||
|
shouldCheckForMusicSelect = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (reconnecting)
|
||||||
|
{
|
||||||
|
await Task.Delay(10000);
|
||||||
|
await ConnectWebSocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void StartRecording(string songName)
|
private static void StartRecording(string songName)
|
||||||
{
|
{
|
||||||
Task.Run(() => StartAudioRecording(songName));
|
Task.Run(() => StartAudioRecording(songName));
|
||||||
|
|
Loading…
Reference in a new issue