master
ZGGSONG 3 years ago
parent 92d9ce43ad
commit 2878ef9748

@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Net;
using System.Text;
@ -16,36 +17,43 @@ namespace S3Demo.Helper
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Method = "POST";
req.Timeout = 10000;
req.Timeout = 10000;
req.ContentType = "application/json";
req.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(postData);
byte[] data = Encoding.UTF8.GetBytes(postData);
req.ContentLength = data.Length;
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
result = reader.ReadToEnd();
return ex.Message.ToString();
}
return result;
}
}

@ -9,7 +9,7 @@ namespace S3Demo.Model
public class ScadaRequest
{
public string s3Flag { get; set; }
public DateTime dateTime { get; set; }
public string dateTime { get; set; }
}
public class ScadaResponse
{

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json;
using Amazon;
using Amazon.S3;
@ -60,8 +61,9 @@ namespace S3Demo.Model.Uploader
ScadaRequest req = new ScadaRequest();
ScadaResponse resp = new ScadaResponse();
req.s3Flag = res;
req.dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
resp = JsonConvert.DeserializeObject<ScadaResponse>(Helper.HttpHelper.PostUrl(url, JsonConvert.SerializeObject(req)));
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} s3Recv {RequestData.Keyname} ==>{res}");
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} S3Recv {RequestData.Keyname} ==>{res}");
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} ScadaRecv ==>{resp.code} & {resp.message}");
}
}

@ -4,6 +4,8 @@ using S3Demo.Model.Uploader;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using S3Demo.Model;
using Newtonsoft.Json;
namespace S3Demo
{
@ -33,6 +35,12 @@ namespace S3Demo
// 开始监控
watch.EnableRaisingEvents = true;
}
Thread th = new Thread(HeartBeatMethord)
{
Name = "HeartBeatMethord",
IsBackground = true
};
th.Start();
#endregion
#region 保存至数据库
@ -128,6 +136,34 @@ namespace S3Demo
}
Console.ReadKey();
}
/// <summary>
/// 心跳
/// </summary>
private static void HeartBeatMethord()
{
while (true)
{
//TODO: 优化无效数据包
try
{
ScadaRequest req = new ScadaRequest();
ScadaResponse resp = new ScadaResponse();
req.s3Flag = "heart beat";
req.dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
resp = JsonConvert.DeserializeObject<ScadaResponse>(Helper.HttpHelper.PostUrl("http://172.17.204.30:9000/api/tri/imagesupload", JsonConvert.SerializeObject(req)));
Console.WriteLine(resp);
}
catch
{
Console.WriteLine("连接异常");
}
finally
{
Thread.Sleep(5000);
}
}
}
/// <summary>
/// 创建文件

Loading…
Cancel
Save