DD.WellWorkover.Cloud/SyncDicts/Program.cs

121 lines
4.3 KiB
C#

using SyncDicts.AsbCloudApi;
using System.Collections.Generic;
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 = "скв 111",
// Deposit = "мр 111",
// Cluster = "куст 111",
// 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,
// });
var cli = new AsbCloudApi.Client("https://localhost:5001/", new System.Net.Http.HttpClient());
var fileName = @"C:\temp\default.sqlite3";
var fileStream = System.IO.File.OpenRead(fileName);
var file = new FileParameter(fileStream, System.IO.Path.GetFileName(fileName));
cli.ApiTelemetryDbAsync("1", new List<FileParameter> { file }).Wait();
//var cli = new swaggerClient("https://localhost:5001/", new System.Net.Http.HttpClient());
//cli.InfoAsync("aaa",info).Wait();
//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;
}
}
}