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}");
|
||||
var reconnecting = false;
|
||||
await clientWebSocket.ConnectAsync(tickerUri, CancellationToken.None);
|
||||
Console.WriteLine("Connected to TickerHook WebSocket.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error connecting to TickerHook WebSocket: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
var lastMessage = string.Empty;
|
||||
var consecutiveMessageCount = 0;
|
||||
var isRecording = false;
|
||||
var currentSongName = string.Empty;
|
||||
var buffer = new byte[1024];
|
||||
|
||||
using var clientWebSocket = new ClientWebSocket();
|
||||
while (clientWebSocket.State == WebSocketState.Open)
|
||||
{
|
||||
WebSocketReceiveResult result;
|
||||
try
|
||||
{
|
||||
await clientWebSocket.ConnectAsync(tickerUri, CancellationToken.None);
|
||||
Console.WriteLine("Connected to TickerHook WebSocket.");
|
||||
result = await clientWebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error connecting to TickerHook WebSocket: {ex.Message}");
|
||||
return;
|
||||
Console.WriteLine($"Error receiving message: {ex.Message}");
|
||||
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;
|
||||
try
|
||||
{
|
||||
result = await clientWebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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;
|
||||
consecutiveMessageCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
consecutiveMessageCount = 1;
|
||||
lastMessage = message;
|
||||
}
|
||||
|
||||
if (reconnecting)
|
||||
if (consecutiveMessageCount >= 2 && !message.Contains("SELECT FROM") && !isRecording)
|
||||
{
|
||||
await Task.Delay(10000);
|
||||
await ConnectWebSocket();
|
||||
currentSongName = message;
|
||||
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)
|
||||
{
|
||||
Task.Run(() => StartAudioRecording(songName));
|
||||
|
|
Loading…
Reference in a new issue