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 CacheTable cacheTelemetry; private readonly CacheTable cacheWells; public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration) { mapper = mapperConfiguration.CreateMapper(); cacheTelemetry = cacheDb.GetCachedTable((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public int GetOrCreateTemetryIdByUid(string uid) => GetOrCreateTelemetryByUid(uid).Id; public int? GetidWellByTelemetryUid(string uid) => GetWellByTelemetryUid(uid)?.Id; public double GetTimezoneOffsetByTelemetryId(int idTelemetry) => cacheTelemetry.FirstOrDefault(t => t.Id == idTelemetry).Info.TimeZoneOffsetTotalHours; public void UpdateInfo(string uid, TelemetryInfoDto info) { var telemetry = GetOrCreateTelemetryByUid(uid); telemetry.Info = mapper.Map(info); cacheTelemetry.Upsert(telemetry); } public Telemetry GetTelemetryByidWell(int idWell) { var well = cacheWells.FirstOrDefault(w => w.Id == idWell); if (well is null) return null; var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id); if (telemetry is null) return null; return telemetry; } private Well GetWellByTelemetryUid(string uid) { var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty); if (tele is null) return null; return cacheWells.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, }); } }