agrobot_base/AgroBase/AgroBase/Forms/frmInstancial.cs

173 lines
5.2 KiB
C#

using AgroBase.Forms.IHM;
using AgroBase.Models;
using AgroBase.Services;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AgroBase.Forms
{
public partial class frmInstancial : Form
{
public static bool DebugMode = true;
public static bool Fechando = false;
public static AsyncTaskTimerModel tmrVarreduraDispositivos;
public static AsyncTaskTimerModel tmrMonitoramentoRecursos;
public static frmPrincipal frmPrincipal = new frmPrincipal();
public static frmJoystick frmJoystick = new frmJoystick();
public static frmIHM frmIHM = new frmIHM()
{
FormBorderStyle = FormBorderStyle.None,
//WindowState = FormWindowState.Maximized,
//TopMost = true
};
public frmInstancial()
{
InitializeComponent();
ThreadPool.SetMinThreads(workerThreads: 50, completionPortThreads: 50);
WifiService.IniciarRotinas();
APIService.IniciarRotinas();
GeneralJoystick.IniciarRotinas();
VersionamentoService.IniciarRotinas();
EncerrarProcessosPython();
}
private void frmInstancial_Shown(object sender, EventArgs e)
{
if (DebugMode)
{
frmIHM.FormClosing += FrmIHM_FormClosing;
frmPrincipal.FormClosing += frmPrincipal_FormClosing;
frmPrincipal.Show();
}
else
{
frmIHM.FormClosing += frmPrincipal_FormClosing;
frmIHM.Show();
}
this.Hide();
}
private void frmInstancial_Load(object sender, EventArgs e)
{
FuncoesGlobais.AddApplicationToStartup();
IniciarConexaoMqtt();
Control control = new Control();
if (DebugMode)
{
control = frmPrincipal;
}
else
{
control = frmIHM;
}
tmrVarreduraDispositivos = new AsyncTaskTimerModel("tmrVarreduraDispositivos", tmrVarreduraDispositivos_Tick, SerialService.InteraloVerificacao, null, 15000);
tmrVarreduraDispositivos.Start();
tmrMonitoramentoRecursos = new AsyncTaskTimerModel("tmrMonitoramentoRecursos", tmrMonitoramentoRecursos_Tick, 500, null);
tmrMonitoramentoRecursos.Start();
}
private static async Task tmrVarreduraDispositivos_Tick()
{
await SerialService.RealizarVarreduraPortasUSB();
}
private async void IniciarConexaoMqtt()
{
await Variaveis.MqttService.ConnectAsync();
}
public static async Task EncerrarProcessos()
{
Fechando = true;
EthernetService.ConfigurarIP(false, false);
AsyncTaskTimerModel.PararTimers();
foreach (var Disp in Variaveis.DispositivosConectados)
{
Disp.EnviarProtocolosConfiguracao(false);
Disp.SalvarParametros(Disp.Dados);
}
while (!FilaService.Liberado)
{
await Task.Delay(500);
}
EncerrarProcessosPython();
Environment.Exit(0);
}
private static void EncerrarProcessosPython()
{
foreach (var processo in Process.GetProcessesByName("python"))
{
try
{
// Verifica os argumentos do processo
string argumentos = processo.StartInfo.Arguments;
if (argumentos.Contains("--source=" + Variaveis.NomeAplicacao) || true)
{
processo.Kill(); // Encerra apenas os processos marcados pelo sistema
}
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao encerrar processo: {ex.Message}");
}
}
}
private async void frmPrincipal_FormClosing(object sender, FormClosingEventArgs e)
{
await EncerrarProcessos();
e.Cancel = true;
}
private void FrmIHM_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
frmIHM.Hide();
}
private static async Task tmrMonitoramentoRecursos_Tick()
{
// Threads ativas no processo
int activeThreads = Process.GetCurrentProcess().Threads.Count;
// Status do Thread Pool
ThreadPool.GetAvailableThreads(out int workerThreads, out int completionPortThreads);
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads);
// Exibe os dados
Console.WriteLine($"Active Threads: {activeThreads}");
Console.WriteLine($"ThreadPool (Worker): {maxWorkerThreads - workerThreads} / {maxWorkerThreads}");
Console.WriteLine($"ThreadPool (IO): {maxCompletionPortThreads - completionPortThreads} / {maxCompletionPortThreads}");
Console.WriteLine(new string('-', 40));
}
}
}