forked from ddrilling/AsbCloudServer
50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using AutoMapper;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class TelemetryService : ITelemetryService
|
|
{
|
|
private readonly IMapper mapper;
|
|
private readonly ICacheTable<Telemetry> cacheTelemetry;
|
|
private readonly ICacheTable<Well> cacheWell;
|
|
|
|
public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration)
|
|
{
|
|
mapper = mapperConfiguration.CreateMapper();
|
|
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
|
|
cacheWell = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
|
|
}
|
|
|
|
public int GetOrCreateTemetryIdByUid(string uid)
|
|
=> GetOrCreateTelemetryByUid(uid).Id;
|
|
|
|
public int? GetWellIdByTelemetryUid(string uid)
|
|
=> GetWellByTelemetryUid(uid)?.Id;
|
|
|
|
public void UpdateInfo(string uid, TelemetryInfoDto info)
|
|
{
|
|
var telemetry = GetOrCreateTelemetryByUid(uid);
|
|
telemetry.Info = mapper.Map<TelemetryInfo>(info);
|
|
cacheTelemetry.Upsert(telemetry);
|
|
}
|
|
|
|
private Well GetWellByTelemetryUid(string uid)
|
|
{
|
|
var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty);
|
|
if (tele is null)
|
|
return null;
|
|
|
|
return cacheWell.FirstOrDefault(w => w?.IdTelemetry == tele.Id);
|
|
}
|
|
|
|
private Telemetry GetOrCreateTelemetryByUid(string uid)
|
|
=> cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty)
|
|
?? cacheTelemetry.Insert(new Telemetry { RemoteUid = uid, });
|
|
|
|
}
|
|
}
|