CS2-119: Added timezone info receive method in TelemetryService

This commit is contained in:
KharchenkoVladimir 2021-11-16 17:14:36 +05:00
parent 88f9236b8a
commit 53dbb2d959
4 changed files with 68 additions and 3 deletions

View 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; }
}
}

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using TimeZoneInfo = AsbCloudApp.Data.TimeZoneInfo;
namespace AsbCloudApp.Services
{
@ -12,6 +13,7 @@ namespace AsbCloudApp.Services
int GetOrCreateTemetryIdByUid(string uid);
double GetTimezoneOffsetByTelemetryId(int idTelemetry);
Task UpdateInfoAsync(string uid, TelemetryInfoDto info, CancellationToken token);
Task<TimeZoneInfo> GetTimeZoneInfoAsync(int idWell, CancellationToken token);
Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto telemetryTimeZoneInfo, CancellationToken token);
int? GetIdTelemetryByIdWell(int idWell);
int Merge(IEnumerable<int> telemetryIds);

View File

@ -7,8 +7,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using System.Text.Json;
using System.Net.Http;
using System.Threading;
using Microsoft.EntityFrameworkCore;
using TimeZoneInfo = AsbCloudApp.Data.TimeZoneInfo;
namespace AsbCloudInfrastructure.Services
{
@ -18,6 +21,8 @@ namespace AsbCloudInfrastructure.Services
private readonly CacheTable<Well> cacheWells;
private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker;
private readonly string timeZoneApiUrl = "http://api.geonames.org/timezoneJSON";
private readonly string timezoneApiUserName = "asbautodrilling";
public TelemetryService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker,
CacheDb cacheDb)
@ -84,11 +89,39 @@ namespace AsbCloudInfrastructure.Services
if (isTimeZoneToUpdate)
telemetry.TelemetryTimeZone = new TelemetryTimeZone()
{
Hours = info.TimeZoneOffsetTotalHours, // TODO: Добавить получение имени часового пояса
Hours = info.TimeZoneOffsetTotalHours,
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,
@ -97,7 +130,8 @@ namespace AsbCloudInfrastructure.Services
var telemetry = GetOrCreateTelemetryByUid(uid);
telemetry.TelemetryTimeZone = timeZoneInfo.Adapt<TelemetryTimeZone>();
await cacheTelemetry.UpsertAsync(telemetry, token);
await cacheTelemetry.UpsertAsync(telemetry, token)
.ConfigureAwait(false);
}
public int? GetIdTelemetryByIdWell(int idWell)

View 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