feat: Introduce explicit start, stop, and status commands, including minimum recording duration.
This commit is contained in:
@@ -140,6 +140,28 @@ public static class DaemonService
|
||||
else
|
||||
await orchestrator.ProcessStartRecordingAsync();
|
||||
}
|
||||
else if (cmd == 5) // STATUS
|
||||
{
|
||||
bool json = pipeToStdout; // buffer[1] == 1 is json
|
||||
bool isRecording = stateTracker.IsRecording();
|
||||
string stateStr = isRecording ? "Recording" : "Idle";
|
||||
|
||||
if (json)
|
||||
{
|
||||
var start = stateTracker.GetRecordingStartTime();
|
||||
double durationMs = 0;
|
||||
if (isRecording && start.HasValue)
|
||||
{
|
||||
durationMs = (DateTime.UtcNow - start.Value).TotalMilliseconds;
|
||||
}
|
||||
var jsonStr = $"{{\"state\": \"{stateStr}\", \"duration\": {Math.Round(durationMs)}}}";
|
||||
await client.SendAsync(System.Text.Encoding.UTF8.GetBytes(jsonStr), SocketFlags.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
await client.SendAsync(System.Text.Encoding.UTF8.GetBytes(stateStr), SocketFlags.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -59,4 +59,5 @@ public interface IRecordingStateTracker
|
||||
void SetRecording(int pid);
|
||||
void ClearRecording();
|
||||
bool IsRecording();
|
||||
DateTime? GetRecordingStartTime();
|
||||
}
|
||||
|
||||
@@ -14,15 +14,15 @@ public class StateTracker : IRecordingStateTracker
|
||||
public void SetRecording(int ffmpegPid)
|
||||
{
|
||||
Logger.LogDebug($"Setting recording state with PID {ffmpegPid}");
|
||||
File.WriteAllText(StateFilePath, ffmpegPid.ToString());
|
||||
File.WriteAllText(StateFilePath, $"{ffmpegPid}\n{DateTime.UtcNow.Ticks}");
|
||||
}
|
||||
|
||||
public int? GetRecordingPid()
|
||||
{
|
||||
if (File.Exists(StateFilePath))
|
||||
{
|
||||
var content = File.ReadAllText(StateFilePath).Trim();
|
||||
if (int.TryParse(content, out var pid))
|
||||
var lines = File.ReadAllLines(StateFilePath);
|
||||
if (lines.Length > 0 && int.TryParse(lines[0], out var pid))
|
||||
{
|
||||
Logger.LogDebug($"Read recording PID {pid} from state file");
|
||||
return pid;
|
||||
@@ -31,6 +31,19 @@ public class StateTracker : IRecordingStateTracker
|
||||
return null;
|
||||
}
|
||||
|
||||
public DateTime? GetRecordingStartTime()
|
||||
{
|
||||
if (File.Exists(StateFilePath))
|
||||
{
|
||||
var lines = File.ReadAllLines(StateFilePath);
|
||||
if (lines.Length > 1 && long.TryParse(lines[1], out var ticks))
|
||||
{
|
||||
return new DateTime(ticks, DateTimeKind.Utc);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ClearRecording()
|
||||
{
|
||||
if (File.Exists(StateFilePath))
|
||||
|
||||
@@ -58,6 +58,19 @@ public class TranscriptionOrchestrator : ITranscriptionOrchestrator
|
||||
|
||||
Logger.LogDebug("Received STOP command");
|
||||
var config = _configProvider.LoadConfig();
|
||||
|
||||
var startTime = _stateTracker.GetRecordingStartTime();
|
||||
if (startTime.HasValue)
|
||||
{
|
||||
var duration = (DateTime.UtcNow - startTime.Value).TotalMilliseconds;
|
||||
if (duration < config.MinRecordingDuration)
|
||||
{
|
||||
Logger.LogDebug($"Recording duration {duration}ms is less than min {config.MinRecordingDuration}ms. Discarding.");
|
||||
ProcessAbortAsync();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_notifications.PlaySound(config.StopSoundPath);
|
||||
_notifications.Notify("Toak", "Transcribing...");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user