56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using Toak.Core.Interfaces;
|
|
|
|
namespace Toak.Core;
|
|
|
|
public class StateTracker : IRecordingStateTracker
|
|
{
|
|
private readonly string _stateFilePath = Constants.Paths.StateFile;
|
|
|
|
public bool IsRecording()
|
|
{
|
|
return File.Exists(_stateFilePath);
|
|
}
|
|
|
|
public void SetRecording(int ffmpegPid)
|
|
{
|
|
Logger.LogDebug($"Setting recording state with PID {ffmpegPid}");
|
|
File.WriteAllText(_stateFilePath, $"{ffmpegPid}\n{DateTime.UtcNow.Ticks}");
|
|
}
|
|
|
|
public int? GetRecordingPid()
|
|
{
|
|
if (File.Exists(_stateFilePath))
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
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))
|
|
{
|
|
Logger.LogDebug("Clearing recording state file");
|
|
File.Delete(_stateFilePath);
|
|
}
|
|
}
|
|
}
|