You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT/IoT.Shared/Infrastructure/BaseDeviceService.cs

61 lines
1.8 KiB

using Infrastructure.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace IoT.Shared.Infrastructure
{
public abstract class BaseDeviceService : IDeviceService
{
protected readonly IHostingEnvironment _env;
protected readonly IConfiguration _configuration;
protected readonly IHttpClientFactory _httpClientFactory;
protected readonly IServiceProvider _applicationServices;
protected CancellationTokenSource _tokenSource;
public BaseDeviceService(IHostingEnvironment env, IServiceProvider applicationServices, IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
this._env = env;
this._applicationServices = applicationServices;
this._configuration = configuration;
this._httpClientFactory = httpClientFactory;
this._tokenSource = new CancellationTokenSource();
}
public virtual void Start()
{
Task.Run(async () =>
{
while (!_tokenSource.IsCancellationRequested)
{
try
{
Execute();
}
catch (Exception ex)
{
ex.PrintStack();
}
await Task.Delay(this._configuration.GetValue<int>("timer.seconds", 60) * 1000);
}
});
}
public virtual void Execute()
{
}
public virtual void Stop()
{
}
public virtual void Dispose()
{
this._tokenSource.Cancel();
this.Stop();
}
}
}