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
|
|
|
|
|
{
|
2021-05-20 14:14:51 +05:00
|
|
|
|
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);
|
2021-05-17 12:53:30 +05:00
|
|
|
|
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;
|
2021-04-30 17:35:35 +05:00
|
|
|
|
|
2021-06-25 15:10:05 +05:00
|
|
|
|
public double GetTimezoneOffsetByTelemetryId(int idTelemetry) =>
|
|
|
|
|
cacheTelemetry.FirstOrDefault(t => t.Id == idTelemetry).Info.TimeZoneOffsetTotalHours;
|
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
public void UpdateInfo(string uid, TelemetryInfoDto info)
|
2021-04-09 17:59:07 +05:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
public int? GetIdTelemetryByIdWell(int idWell)
|
2021-05-17 12:53:30 +05:00
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
|
2021-05-17 12:53:30 +05:00
|
|
|
|
if (well is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
return well.IdTelemetry;
|
2021-05-17 12:53:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
|
2021-05-17 12:53:30 +05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|