DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/TelemetryService.cs

60 lines
2.0 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
2021-07-28 09:46:58 +05:00
using Mapster;
2021-04-07 18:01:56 +05:00
namespace AsbCloudInfrastructure.Services
{
public class TelemetryService : ITelemetryService
{
private readonly CacheTable<Telemetry> cacheTelemetry;
private readonly CacheTable<Well> cacheWells;
2021-04-07 18:01:56 +05:00
2021-07-28 09:46:58 +05:00
public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb)
2021-04-07 18:01:56 +05:00
{
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
2021-04-07 18:01:56 +05:00
}
2021-04-23 10:21:25 +05:00
public int GetOrCreateTemetryIdByUid(string uid)
=> GetOrCreateTelemetryByUid(uid).Id;
2021-04-07 18:01:56 +05:00
2021-07-27 14:43:30 +05:00
public int? GetidWellByTelemetryUid(string uid)
2021-04-23 10:21:25 +05:00
=> GetWellByTelemetryUid(uid)?.Id;
public double GetTimezoneOffsetByTelemetryId(int idTelemetry) =>
cacheTelemetry.FirstOrDefault(t => t.Id == idTelemetry).Info?.TimeZoneOffsetTotalHours??0d;
2021-04-23 10:21:25 +05:00
public void UpdateInfo(string uid, TelemetryInfoDto info)
{
2021-04-23 10:21:25 +05:00
var telemetry = GetOrCreateTelemetryByUid(uid);
2021-07-28 09:46:58 +05:00
telemetry.Info = info.Adapt<TelemetryInfo>();
2021-04-23 10:21:25 +05:00
cacheTelemetry.Upsert(telemetry);
2021-04-07 18:01:56 +05:00
}
public int? GetIdTelemetryByIdWell(int idWell)
{
2021-07-27 14:43:30 +05:00
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
if (well is null)
return null;
return well.IdTelemetry;
}
2021-04-23 10:21:25 +05:00
private Well GetWellByTelemetryUid(string uid)
2021-04-07 18:01:56 +05:00
{
2021-04-23 10:21:25 +05:00
var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty);
if (tele is null)
return null;
return cacheWells.FirstOrDefault(w => w?.IdTelemetry == tele.Id);
2021-04-07 18:01:56 +05:00
}
2021-04-23 10:21:25 +05:00
private Telemetry GetOrCreateTelemetryByUid(string uid)
=> cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty)
?? cacheTelemetry.Insert(new Telemetry { RemoteUid = uid, });
2021-04-07 18:01:56 +05:00
}
}