public class RebootCameraService : BackgroundService
{
private readonly Timer _timer;
private readonly string _cameraUrl = "http://ip:port/ISAPI/System/reboot"; / Replace 'ip' and 'port' with actual values
public RebootCameraService()
{
/ Initialize timer
_timer = new Timer(DoWork, null, Timeout.Infinite, Timeout.Infinite);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
/ Start the timer immediately and then repeat every 24 hours
_timer.Change(TimeSpan.Zero, TimeSpan.FromDays(1));
return Task.CompletedTask;
}
private async void DoWork(object state)
{
var request = new HttpRequestMessage(HttpMethod.Put, _cameraUrl);
/ Set credentials for Digest Authentication
var credentials = new NetworkCredential("username", "password"); / Replace with actual credentials
var cache = new CredentialCache();
cache.Add(new Uri(_cameraUrl), "Digest", credentials);
var handler = new HttpClientHandler { Credentials = cache };
using (var client = new HttpClient(handler))
{
try
{
var response = await client.SendAsync(request);
/ Log response or handle it accordingly
}
catch (Exception ex)
{
/ Handle exceptions
}
}
}
public override async Task StopAsync(CancellationToken stoppingToken)
{
_timer?.Change(Timeout.Infinite, 0);
await base.StopAsync(stoppingToken);
}
}