From cb6acbfa082662844f0b95163e9e9fd057f34f48 Mon Sep 17 00:00:00 2001 From: KhasanovAM Date: Tue, 15 Apr 2025 10:07:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Log.cs | 24 ++++++++++++++++++ Program.cs | 71 ++++++++++++++++++------------------------------------ 2 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 Log.cs diff --git a/Log.cs b/Log.cs new file mode 100644 index 0000000..d97dc18 --- /dev/null +++ b/Log.cs @@ -0,0 +1,24 @@ +namespace SendNotify +{ + internal class Log + { + public static void Logger(string str) + { + string outdir = Environment.CurrentDirectory + @"\logs\"; + if (!Directory.Exists(outdir)) Directory.CreateDirectory(outdir); + string filename = $"{DateTime.Now.Day}_{DateTime.Now.Month}_{DateTime.Now.Year}_SendNotify"; + + foreach (FileInfo file in new DirectoryInfo(outdir).GetFiles()) + { + if (Convert.ToDateTime(file.LastWriteTime) < DateTime.Now.AddDays(-30)) + file.Delete(); + } + + using (FileStream aFile = new FileStream($@"{outdir}\{filename}.log", FileMode.Append, FileAccess.Write)) + using (StreamWriter sw = new StreamWriter(aFile)) + { + sw.WriteLine(DateTime.Now + " - " + str); + } + } + } +} diff --git a/Program.cs b/Program.cs index 85398e7..6108e33 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ -using System.Data.SqlClient; +using SendNotify; +using System.Data.SqlClient; using System.Net; using System.Text; internal class Program @@ -8,7 +9,6 @@ internal class Program { while (true) { - DateTime now = DateTime.Now; string? gotifyUrl = null; string? appToken_izhstal = null; string? proxyUrl = null; @@ -33,28 +33,21 @@ internal class Program if (gotifyUrl == null || appToken_izhstal == null || proxyUrl == null || proxyUsername == null || proxyPassword == null) { Console.WriteLine("Ошибка: не все параметры указаны в config.txt"); - Log("Ошибка: не все параметры указаны в config.txt"); + Log.Logger("Ошибка: не все параметры указаны в config.txt"); } else - { - CheckStatusAsync(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword); - - if (now.Hour == 4 && now.Date != lastDate.Date) - { - TestMSG(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword); - lastDate = now; - } - } + NotifyPollingAsync(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword); Thread.Sleep(60000); } } - - private static async void CheckStatusAsync(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword) + private static async void NotifyPollingAsync(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword) { + #region check furnace 250 + DateTime now = DateTime.Now; var result = CheckFurnace250Async().Result; if (!result.status) { @@ -66,18 +59,21 @@ internal class Program }; await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword); } - } + #endregion - private static async void TestMSG(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword) - { - var notification = new + #region test msg to check + if (now.Hour == 8 && now.Date != lastDate.Date) { - title = "Test notify", - message = "IS OK", - priority = 3 - }; - await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword); - + var notification = new + { + title = "Test notify", + message = "IS OK", + priority = 3 + }; + await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword); + lastDate = now; + } + #endregion } private static async Task<(bool status, string msg)> CheckFurnace250Async() @@ -154,43 +150,24 @@ internal class Program if (response.IsSuccessStatusCode) { Console.WriteLine($"{DateTime.Now} - Отправлено сообщение."); - Log($"Отправлено сообщение."); + Log.Logger($"Отправлено сообщение."); } else { Console.WriteLine($"Ошибка: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); - Log($"Ошибка: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); + Log.Logger($"Ошибка: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); } } catch (HttpRequestException ex) { Console.WriteLine($"Ошибка подключения: {ex.Message}"); - Log($"Ошибка подключения: {ex.Message}"); + Log.Logger($"Ошибка подключения: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"Детали: {ex.InnerException.Message}"); - Log($"Детали: {ex.InnerException.Message}"); + Log.Logger($"Детали: {ex.InnerException.Message}"); } } } } - - private static void Log(string str) - { - string outdir = Environment.CurrentDirectory + @"\logs\"; - if (!Directory.Exists(outdir)) Directory.CreateDirectory(outdir); - string filename = $"{DateTime.Now.Day}_{DateTime.Now.Month}_{DateTime.Now.Year}_SendNotify"; - - foreach (FileInfo file in new DirectoryInfo(outdir).GetFiles()) - { - if (Convert.ToDateTime(file.LastWriteTime) < DateTime.Now.AddDays(-30)) - file.Delete(); - } - - using (FileStream aFile = new FileStream($@"{outdir}\{filename}.log", FileMode.Append, FileAccess.Write)) - using (StreamWriter sw = new StreamWriter(aFile)) - { - sw.WriteLine(DateTime.Now + " - " + str); - } - } } \ No newline at end of file