Рефакторинг

This commit is contained in:
2025-04-15 10:07:07 +04:00
parent 5f1b51fd5f
commit cb6acbfa08
2 changed files with 48 additions and 47 deletions

24
Log.cs Normal file
View File

@@ -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);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Data.SqlClient; using SendNotify;
using System.Data.SqlClient;
using System.Net; using System.Net;
using System.Text; using System.Text;
internal class Program internal class Program
@@ -8,7 +9,6 @@ internal class Program
{ {
while (true) while (true)
{ {
DateTime now = DateTime.Now;
string? gotifyUrl = null; string? gotifyUrl = null;
string? appToken_izhstal = null; string? appToken_izhstal = null;
string? proxyUrl = null; string? proxyUrl = null;
@@ -33,28 +33,21 @@ internal class Program
if (gotifyUrl == null || appToken_izhstal == null || proxyUrl == null || proxyUsername == null || proxyPassword == null) if (gotifyUrl == null || appToken_izhstal == null || proxyUrl == null || proxyUsername == null || proxyPassword == null)
{ {
Console.WriteLine("Ошибка: не все параметры указаны в config.txt"); Console.WriteLine("Ошибка: не все параметры указаны в config.txt");
Log("Ошибка: не все параметры указаны в config.txt"); Log.Logger("Ошибка: не все параметры указаны в config.txt");
} }
else else
{ NotifyPollingAsync(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword);
CheckStatusAsync(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword);
if (now.Hour == 4 && now.Date != lastDate.Date)
{
TestMSG(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword);
lastDate = now;
}
}
Thread.Sleep(60000); Thread.Sleep(60000);
} }
} }
private static async void NotifyPollingAsync(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword)
private static async void CheckStatusAsync(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword)
{ {
#region check furnace 250
DateTime now = DateTime.Now;
var result = CheckFurnace250Async().Result; var result = CheckFurnace250Async().Result;
if (!result.status) if (!result.status)
{ {
@@ -66,18 +59,21 @@ internal class Program
}; };
await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword); await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword);
} }
} #endregion
private static async void TestMSG(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword) #region test msg to check
{ if (now.Hour == 8 && now.Date != lastDate.Date)
var notification = new
{ {
title = "Test notify", var notification = new
message = "IS OK", {
priority = 3 title = "Test notify",
}; message = "IS OK",
await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword); priority = 3
};
await SendNotify(url, token, notification, proxyUrl, proxyUsername, proxyPassword);
lastDate = now;
}
#endregion
} }
private static async Task<(bool status, string msg)> CheckFurnace250Async() private static async Task<(bool status, string msg)> CheckFurnace250Async()
@@ -154,43 +150,24 @@ internal class Program
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
Console.WriteLine($"{DateTime.Now} - Отправлено сообщение."); Console.WriteLine($"{DateTime.Now} - Отправлено сообщение.");
Log($"Отправлено сообщение."); Log.Logger($"Отправлено сообщение.");
} }
else else
{ {
Console.WriteLine($"Ошибка: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); 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) catch (HttpRequestException ex)
{ {
Console.WriteLine($"Ошибка подключения: {ex.Message}"); Console.WriteLine($"Ошибка подключения: {ex.Message}");
Log($"Ошибка подключения: {ex.Message}"); Log.Logger($"Ошибка подключения: {ex.Message}");
if (ex.InnerException != null) if (ex.InnerException != null)
{ {
Console.WriteLine($"Детали: {ex.InnerException.Message}"); 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);
}
}
} }