Добавил механизм удаления заготовок (ст.250 мониторинг)
This commit is contained in:
124
Program.cs
124
Program.cs
@@ -1,6 +1,7 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Oracle.ManagedDataAccess.Client;
|
||||
using SendNotify;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
@@ -8,18 +9,26 @@ using System.Text;
|
||||
internal class Program
|
||||
{
|
||||
private static DateTime lastDate = DateTime.MinValue;
|
||||
|
||||
|
||||
private static DateTime startProgramDT = DateTime.MinValue;
|
||||
public static bool statusRemovingBillets = false;
|
||||
private static CancellationTokenSource? removalCts;
|
||||
private static object removalLock = new object();
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
startProgramDT = DateTime.Now;
|
||||
var RemovingBillets = new RemovingBillets();
|
||||
|
||||
while (true)
|
||||
{
|
||||
string? gotifyUrl = null;
|
||||
string? appToken_izhstal = null;
|
||||
string? clientToken_izhstal = null;
|
||||
string? proxyUrl = null;
|
||||
string? proxyUsername = null;
|
||||
string? proxyPassword = null;
|
||||
string? checkNow = null;
|
||||
int targetPriority = -1;
|
||||
|
||||
var config = File.ReadAllLines("config.txt");
|
||||
|
||||
@@ -37,23 +46,52 @@ internal class Program
|
||||
proxyPassword = line.Substring("proxy_password=".Length);
|
||||
else if (line.StartsWith("check_now="))
|
||||
checkNow = line.Substring("check_now=".Length);
|
||||
else if (line.StartsWith("client_token_izhstal="))
|
||||
clientToken_izhstal = line.Substring("client_token_izhstal=".Length);
|
||||
else if (line.StartsWith("target_priority="))
|
||||
int.TryParse(line.Substring("target_priority=".Length), out targetPriority);
|
||||
}
|
||||
|
||||
if (gotifyUrl == null || appToken_izhstal == null || proxyUrl == null || proxyUsername == null || proxyPassword == null || checkNow == null)
|
||||
if (gotifyUrl == null || appToken_izhstal == null || proxyUrl == null || proxyUsername == null ||
|
||||
proxyPassword == null || checkNow == null || clientToken_izhstal == null || targetPriority == -1)
|
||||
{
|
||||
Console.WriteLine("Ошибка: не все параметры указаны в config.txt");
|
||||
Log.Logger("Ошибка: не все параметры указаны в config.txt");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
NotifyPollingAsync(gotifyUrl, appToken_izhstal, proxyUrl, proxyUsername, proxyPassword, checkNow);
|
||||
}
|
||||
var result = ReceiveMessagesAsync(gotifyUrl, clientToken_izhstal, proxyUrl, proxyUsername, proxyPassword, targetPriority).Result;
|
||||
|
||||
if (result.status && result.msg.Date > startProgramDT)
|
||||
{
|
||||
if (result.msg.Title == "L2_REM")
|
||||
{
|
||||
lock (removalLock)
|
||||
{
|
||||
if (result.msg.Message == "ON" && !statusRemovingBillets)
|
||||
{
|
||||
removalCts = new CancellationTokenSource();
|
||||
Task.Run(() => RemovingBillets.StartRemovalProcess(removalCts.Token));
|
||||
statusRemovingBillets = true;
|
||||
Log.Logger("Удаление заготовок: ЗАПУЩЕНО");
|
||||
}
|
||||
else if (result.msg.Message == "OFF" && statusRemovingBillets)
|
||||
{
|
||||
removalCts?.Cancel();
|
||||
statusRemovingBillets = false;
|
||||
Log.Logger("Удаление заготовок: ОСТАНОВЛЕНО");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.Sleep(60000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static async void NotifyPollingAsync(string url, string token, string proxyUrl, string proxyUsername, string proxyPassword, string checkNow)
|
||||
{
|
||||
#region test msg to check
|
||||
@@ -213,7 +251,8 @@ internal class Program
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.PostAsync(url, content);
|
||||
var requestUrl = $"{url}/message";
|
||||
HttpResponseMessage response = await client.PostAsync(requestUrl, content);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
@@ -238,5 +277,74 @@ internal class Program
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<(bool status, GotifyMessage msg)> ReceiveMessagesAsync(
|
||||
string url,
|
||||
string token,
|
||||
string proxyUrl,
|
||||
string proxyUsername,
|
||||
string proxyPassword,
|
||||
int targetPriority)
|
||||
{
|
||||
var proxy = new WebProxy(new Uri(proxyUrl))
|
||||
{
|
||||
Credentials = new NetworkCredential(proxyUsername, proxyPassword, "MECHEL")
|
||||
};
|
||||
|
||||
var handler = new HttpClientHandler()
|
||||
{
|
||||
Proxy = proxy,
|
||||
UseProxy = true,
|
||||
PreAuthenticate = true,
|
||||
UseDefaultCredentials = false
|
||||
};
|
||||
|
||||
using (var client = new HttpClient(handler))
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("X-Gotify-Key", token);
|
||||
var requestUrl = $"{url}/message?limit=100";
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.GetAsync(requestUrl);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var messages = await response.Content.ReadFromJsonAsync<GotifyMessagesResponse>();
|
||||
|
||||
var filtered = messages.Messages?
|
||||
.Where(m => m.Priority == targetPriority)
|
||||
.OrderByDescending(m => m.Date)
|
||||
.ToList();
|
||||
|
||||
if (filtered?.Count > 0)
|
||||
{
|
||||
var lastMsg = filtered.First();
|
||||
return (true, lastMsg);
|
||||
}
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Ошибка HTTP: {response.StatusCode}");
|
||||
return (false, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка: {ex.Message}");
|
||||
Log.Logger($"Ошибка получения сообщений: {ex.Message}");
|
||||
return (false, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GotifyMessagesResponse
|
||||
{
|
||||
public required List<GotifyMessage> Messages { get; set; }
|
||||
}
|
||||
|
||||
public class GotifyMessage
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user