DD.WellWorkover.Cloud/SyncDicts/Program.cs
2021-04-23 10:21:25 +05:00

113 lines
3.7 KiB
C#

using AsbCloudApp.Data;
using AsbSaubDbModel.V3;
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
namespace SyncDicts
{
class Program
{
/// <summary>
/// Описывает процесс синхронизации словарей БД
/// </summary>
/// <param name="args"></param>
static void Main(/*string[] args*/)
{
bool res;
var context = new ArchiveDbContext(@"c:\temp\default.sqlite3");
// sync Events
var events = context.EventsDictionary.ToList()
.Select(e => new EventDto
{
EventType = e.EventType,
Id = e.Id,
IdCategory = e.CategoryId,
IdSound = e.SoundId,
Message = e.MessageTemplate,
Tag = e.Tag,
});
var info = new TelemetryInfoDto
{
Caption = "скв 32",
Deposit = "мр 2",
Cluster = "куст 22",
TimeZoneId = TimeZoneInfo.Local.Id,
TimeZoneOffsetTotalHours = TimeZoneInfo.Local.BaseUtcOffset.TotalHours,
Date = DateTime.Now,
};
var users = context.Users.ToList()
.Select(u => new TelemetryUserDto
{
Id = u.Id,
Level = u.Level,
Name = u.Name,
Patronymic = u.Patronymic,
Surname = u.Surname,
});
var messages = context.Messages.Take(1024).ToList()
.Select(m => new TelemetryMessageDto
{
Id = m.Id,
Date = DateTime.UnixEpoch.AddSeconds(m.TimeStamp),
IdEvent = m.EventItemId,
IdTelemetryUser = m.UserId,
State = m.State,
Arg0 = m.Arg0,
Arg1 = m.Arg1,
Arg2 = m.Arg2,
Arg3 = m.Arg3,
});
//res = Send("http://127.0.0.1:5000/api/telemetry/asdasd/event", events);
//res = Send("http://127.0.0.1:5000/api/telemetry/asdasd/info", info);
//res = Send("http://127.0.0.1:5000/api/telemetry/asdasd/user", users);
res = Send("http://127.0.0.1:5000/api/telemetry/asdasd/message", messages);
}
private static bool Send<T>(string url, T obj)
{
// sending data
var requestBodyJson = JsonSerializer.Serialize(obj);
var data = Encoding.UTF8.GetBytes(requestBodyJson);
var request = MakeRequest(url);
using var streamWriter = request.GetRequestStream();
streamWriter.Write(data);
streamWriter.Close();
// getting response
if (request.GetResponse() is not HttpWebResponse response)
{//FAILED
return false;
}
if (response.StatusCode == HttpStatusCode.OK)
{// sent succesfilly
return true;
}
else
{//failed
return false;
}
}
private static HttpWebRequest MakeRequest(string url, string contentType = "application/json")
{
var request = WebRequest.CreateHttp(url);
request.Method = "POST";
request.Timeout = 4900;
request.ContentType = contentType;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.ServicePoint.Expect100Continue = false;
return request;
}
}
}