forked from ddrilling/AsbCloudServer
CS2-119: Added timezone info receive method in TelemetryService
This commit is contained in:
parent
88f9236b8a
commit
53dbb2d959
17
AsbCloudApp/Data/TimeZoneInfo.cs
Normal file
17
AsbCloudApp/Data/TimeZoneInfo.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
namespace AsbCloudApp.Data
|
||||||
|
{
|
||||||
|
public class TimeZoneInfo
|
||||||
|
{
|
||||||
|
public string Sunrise { get; set; }
|
||||||
|
public double Lng { get; set; }
|
||||||
|
public double Lat { get; set; }
|
||||||
|
public string CountryCode { get; set; }
|
||||||
|
public double GmtOffset { get; set; }
|
||||||
|
public double RawOffset { get; set; }
|
||||||
|
public string Sunset { get; set; }
|
||||||
|
public string TimezoneId { get; set; }
|
||||||
|
public double DstOffset { get; set; }
|
||||||
|
public string CountryName { get; set; }
|
||||||
|
public string Time { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using TimeZoneInfo = AsbCloudApp.Data.TimeZoneInfo;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Services
|
||||||
{
|
{
|
||||||
@ -12,6 +13,7 @@ namespace AsbCloudApp.Services
|
|||||||
int GetOrCreateTemetryIdByUid(string uid);
|
int GetOrCreateTemetryIdByUid(string uid);
|
||||||
double GetTimezoneOffsetByTelemetryId(int idTelemetry);
|
double GetTimezoneOffsetByTelemetryId(int idTelemetry);
|
||||||
Task UpdateInfoAsync(string uid, TelemetryInfoDto info, CancellationToken token);
|
Task UpdateInfoAsync(string uid, TelemetryInfoDto info, CancellationToken token);
|
||||||
|
Task<TimeZoneInfo> GetTimeZoneInfoAsync(int idWell, CancellationToken token);
|
||||||
Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto telemetryTimeZoneInfo, CancellationToken token);
|
Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto telemetryTimeZoneInfo, CancellationToken token);
|
||||||
int? GetIdTelemetryByIdWell(int idWell);
|
int? GetIdTelemetryByIdWell(int idWell);
|
||||||
int Merge(IEnumerable<int> telemetryIds);
|
int Merge(IEnumerable<int> telemetryIds);
|
||||||
|
@ -7,8 +7,11 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using TimeZoneInfo = AsbCloudApp.Data.TimeZoneInfo;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services
|
namespace AsbCloudInfrastructure.Services
|
||||||
{
|
{
|
||||||
@ -18,6 +21,8 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
private readonly CacheTable<Well> cacheWells;
|
private readonly CacheTable<Well> cacheWells;
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
private readonly ITelemetryTracker telemetryTracker;
|
private readonly ITelemetryTracker telemetryTracker;
|
||||||
|
private readonly string timeZoneApiUrl = "http://api.geonames.org/timezoneJSON";
|
||||||
|
private readonly string timezoneApiUserName = "asbautodrilling";
|
||||||
|
|
||||||
public TelemetryService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker,
|
public TelemetryService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker,
|
||||||
CacheDb cacheDb)
|
CacheDb cacheDb)
|
||||||
@ -84,11 +89,39 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
if (isTimeZoneToUpdate)
|
if (isTimeZoneToUpdate)
|
||||||
telemetry.TelemetryTimeZone = new TelemetryTimeZone()
|
telemetry.TelemetryTimeZone = new TelemetryTimeZone()
|
||||||
{
|
{
|
||||||
Hours = info.TimeZoneOffsetTotalHours, // TODO: Добавить получение имени часового пояса
|
Hours = info.TimeZoneOffsetTotalHours,
|
||||||
TimeZoneId = info.TimeZoneId
|
TimeZoneId = info.TimeZoneId
|
||||||
};
|
};
|
||||||
|
|
||||||
await cacheTelemetry.UpsertAsync(telemetry, token);
|
await cacheTelemetry.UpsertAsync(telemetry, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TimeZoneInfo> GetTimeZoneInfoAsync(int idWell, CancellationToken token)
|
||||||
|
{
|
||||||
|
var well = await db.Wells.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
using var client = new HttpClient();
|
||||||
|
|
||||||
|
var latitude = $"{well.Latitude}".Replace(',', '.');
|
||||||
|
var longitude = $"{well.Longitude}".Replace(',', '.');
|
||||||
|
|
||||||
|
var url =
|
||||||
|
$"{timeZoneApiUrl}?lat={latitude}&lng={longitude}&username={timezoneApiUserName}";
|
||||||
|
|
||||||
|
var response = await client.GetAsync(url, token).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var responseJson = await response.Content.ReadAsStringAsync(token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
var timeZoneInfo = JsonSerializer.Deserialize<TimeZoneInfo>(responseJson, options);
|
||||||
|
|
||||||
|
return timeZoneInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto timeZoneInfo,
|
public async Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto timeZoneInfo,
|
||||||
@ -97,7 +130,8 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
var telemetry = GetOrCreateTelemetryByUid(uid);
|
var telemetry = GetOrCreateTelemetryByUid(uid);
|
||||||
telemetry.TelemetryTimeZone = timeZoneInfo.Adapt<TelemetryTimeZone>();
|
telemetry.TelemetryTimeZone = timeZoneInfo.Adapt<TelemetryTimeZone>();
|
||||||
|
|
||||||
await cacheTelemetry.UpsertAsync(telemetry, token);
|
await cacheTelemetry.UpsertAsync(telemetry, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int? GetIdTelemetryByIdWell(int idWell)
|
public int? GetIdTelemetryByIdWell(int idWell)
|
||||||
|
12
AsbCloudWebApi/Docs/Timezone info api credentials.md
Normal file
12
AsbCloudWebApi/Docs/Timezone info api credentials.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# url
|
||||||
|
https://www.geonames.org/commercial-webservices.html
|
||||||
|
|
||||||
|
# учетная запись для аутентификации на сайте
|
||||||
|
Имя пользователя: asbautodrilling
|
||||||
|
Пароль: asbautodrilling1!
|
||||||
|
|
||||||
|
# имя пользователя для запросов
|
||||||
|
asbautodrilling
|
||||||
|
|
||||||
|
# Пример запроса
|
||||||
|
http://api.geonames.org/timezoneJSON?lat=60.8705722222222&lng=70.3811888888889&username=asbautodrilling
|
Loading…
Reference in New Issue
Block a user