Рефакторинг
This commit is contained in:
24
Log.cs
Normal file
24
Log.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
71
Program.cs
71
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user