From 2878ef9748f6478ef44e4b5ba07a24e9f0954083 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Mon, 13 Dec 2021 11:12:40 +0800 Subject: [PATCH] heart beat --- S3Demo/Helper/HttpHelper.cs | 44 ++++++++++++++---------- S3Demo/Model/DataModel.cs | 2 +- S3Demo/Model/Uploader/S3UploadRequest.cs | 4 ++- S3Demo/Program.cs | 36 +++++++++++++++++++ 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/S3Demo/Helper/HttpHelper.cs b/S3Demo/Helper/HttpHelper.cs index 4525060..9ad543e 100644 --- a/S3Demo/Helper/HttpHelper.cs +++ b/S3Demo/Helper/HttpHelper.cs @@ -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; } } diff --git a/S3Demo/Model/DataModel.cs b/S3Demo/Model/DataModel.cs index 3d5a234..9c9bf45 100644 --- a/S3Demo/Model/DataModel.cs +++ b/S3Demo/Model/DataModel.cs @@ -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 { diff --git a/S3Demo/Model/Uploader/S3UploadRequest.cs b/S3Demo/Model/Uploader/S3UploadRequest.cs index 63081b6..9cfa0f8 100644 --- a/S3Demo/Model/Uploader/S3UploadRequest.cs +++ b/S3Demo/Model/Uploader/S3UploadRequest.cs @@ -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(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}"); } } diff --git a/S3Demo/Program.cs b/S3Demo/Program.cs index 0250ca6..778793a 100644 --- a/S3Demo/Program.cs +++ b/S3Demo/Program.cs @@ -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(); } + + /// + /// 心跳 + /// + 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(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); + } + } + } /// /// 创建文件