DD.WellWorkover.Cloud/SaubPanelOnlineSender/CloudDataSender.cs
2021-04-07 18:01:56 +05:00

52 lines
1.6 KiB
C#

using AsbCloudApp.Data;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace SaubPanelOnlineSender
{
public class CloudDataSender
{
public CloudDataSender() { }
public string OnlineUrl => "http://localhost:5000/api/telemetry/0A:00:27:00:11:12/data";
public void Send(DataSaubBaseDto data)
{
var request = MakeRequest();
using var streamWriter = request.GetRequestStream();
var requestBodyJson = MakeRequestBody(data);
var dataBytes = Encoding.UTF8.GetBytes(requestBodyJson);
streamWriter.Write(dataBytes, 0, dataBytes.Length);
var response = request.GetResponse();
streamWriter.Close();
}
private HttpWebRequest MakeRequest()
{
var request = WebRequest.CreateHttp(OnlineUrl);
request.Method = "POST";
request.Timeout = 4900;
request.ContentType = "application/json";
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.ServicePoint.Expect100Continue = false;
return request;
}
private string MakeRequestBody(DataSaubBaseDto dataSaub)
{
var telemetry = new TelemetryDataDto
{
Date = DateTime.Now,
DataSaub = new List<DataSaubBaseDto> { dataSaub },
HmiVersion = "3.1",
UserName = "online sender",
};
return Newtonsoft.Json.JsonConvert.SerializeObject(telemetry);
}
}
}