312 lines
11 KiB
C#
312 lines
11 KiB
C#
using AgroBase.Models;
|
|
using AgroBase.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Forms
|
|
{
|
|
public partial class frmEthernet : Form
|
|
{
|
|
private string _Interface = "";
|
|
private bool _VarreduraEmAndamento = false;
|
|
|
|
public frmEthernet()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void frmEthernet_Load(object sender, EventArgs e)
|
|
{
|
|
var interfaces = EthernetService.ObterNomesInterfaces();
|
|
cmbInterface.Items.AddRange(interfaces.ToArray());
|
|
|
|
if (interfaces.Any(x => x.Contains("Ativa")))
|
|
cmbInterface.SelectedIndex = interfaces.IndexOf(interfaces.First(x => x.Contains("Ativa")));
|
|
}
|
|
|
|
private void PreencherDados()
|
|
{
|
|
txtGateway.Text = EthernetService.ObterIpGateway(_Interface);
|
|
txtIpAtual.Text = EthernetService.ObterIpAtual(_Interface);
|
|
txtSubmask.Text = EthernetService.ObterSubnetMaskAtual(_Interface);
|
|
chbIpFixo.Checked = EthernetService.ObterTipoConfiguracaoIP(_Interface).Contains("Fixo");
|
|
|
|
if (!string.IsNullOrEmpty(txtIpAtual.Text) && !_VarreduraEmAndamento)
|
|
{
|
|
var ip = txtIpAtual.Text.Split('.');
|
|
if (ip.Length > 2)
|
|
{
|
|
txtFaixaIP.Text = $"{ip[0]}.{ip[1]}.{ip[2]}";
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnDefinirIp_Click(object sender, EventArgs e)
|
|
{
|
|
string ipNovo = txtIpAtual.Text;
|
|
string gateway = txtGateway.Text;
|
|
string subnetMask = txtSubmask.Text;
|
|
|
|
Task.Run(async () =>
|
|
{
|
|
// Aqui substituímos "Ethernet" pelo nome correto da interface que o PC está usando
|
|
if (chbIpFixo.Checked)
|
|
{
|
|
await EthernetService.DefinirIpFixo(_Interface, ipNovo, subnetMask, gateway, true);
|
|
}
|
|
else
|
|
{
|
|
await EthernetService.DefinirIpDinamico(_Interface, true);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void chbIpFixo_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
txtGateway.Enabled = chbIpFixo.Checked;
|
|
txtIpAtual.Enabled = chbIpFixo.Checked;
|
|
txtSubmask.Enabled = chbIpFixo.Checked;
|
|
}
|
|
|
|
private void cmbInterface_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
_Interface = cmbInterface.Text.Split('(')[0].Trim();
|
|
PreencherDados();
|
|
}
|
|
|
|
private async void btnIniciarVarredura_Click(object sender, EventArgs e)
|
|
{
|
|
if (btnIniciarVarredura.Text.Contains("Iniciar"))
|
|
{
|
|
btnIniciarVarredura.Text = "Parar Varredura";
|
|
if (await IniciarVarreduraAsync())
|
|
{
|
|
btnIniciarVarredura.Text = "Iniciar Varredura";
|
|
MessageBox.Show($"Verificação concluída com {tvwEncontrados.Nodes.Count} endereços encontrados!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_VarreduraEmAndamento = false;
|
|
btnIniciarVarredura.Text = "Iniciar Varredura";
|
|
MessageBox.Show($"Verificação encerrada com {tvwEncontrados.Nodes.Count} endereços encontrados!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
private async Task<bool> IniciarVarreduraAsync()
|
|
{
|
|
string faixaIP = txtFaixaIP.Text.Trim(); // Ex: "192.168.0"
|
|
if (string.IsNullOrWhiteSpace(faixaIP))
|
|
{
|
|
MessageBox.Show("Insira uma faixa de IP válida.");
|
|
return false;
|
|
}
|
|
|
|
_VarreduraEmAndamento = true;
|
|
|
|
tvwEscaneados.Nodes.Clear();
|
|
tvwEncontrados.Nodes.Clear();
|
|
|
|
// Escaneia de 1 a 254 na faixa fornecida
|
|
for (int i = 1; i <= 254; i++)
|
|
{
|
|
if (!_VarreduraEmAndamento)
|
|
{
|
|
break;
|
|
}
|
|
|
|
string ip = $"{faixaIP}.{i}";
|
|
|
|
// Atualiza o TreeView com o IP que está sendo escaneado
|
|
TreeNode nodeEscaneado = new TreeNode(ip);
|
|
TreeNode nodeEncontrado = new TreeNode(ip);
|
|
|
|
bool isAlive = await EthernetService.PingAsync(ip);
|
|
|
|
if (isAlive)
|
|
{
|
|
nodeEscaneado.ForeColor = Color.Green;
|
|
// Atualiza o TreeView dos dispositivos encontrados
|
|
tvwEncontrados.Nodes.Add(nodeEncontrado);
|
|
}
|
|
else
|
|
{
|
|
nodeEscaneado.ForeColor = Color.Red;
|
|
}
|
|
|
|
tvwEscaneados.Nodes.Add(nodeEscaneado);
|
|
lblEscaneados.Text = $"Endereços escaneados {(i + 1)}";
|
|
}
|
|
|
|
_VarreduraEmAndamento = false;
|
|
|
|
AtualizarIPsEncontrados();
|
|
|
|
return tvwEncontrados.Nodes.Count == 254;
|
|
}
|
|
|
|
private void AtualizarIPsEncontrados()
|
|
{
|
|
cmbIP_Mov.Items.Clear();
|
|
cmbIP_Dir.Items.Clear();
|
|
|
|
foreach (TreeNode node in tvwEncontrados.Nodes)
|
|
{
|
|
cmbIP_Mov.Items.Add(node.Text);
|
|
cmbIP_Dir.Items.Add(node.Text);
|
|
}
|
|
|
|
gpbMov.Enabled = cmbIP_Mov.Items.Count > 0;
|
|
gpbDir.Enabled = cmbIP_Dir.Items.Count > 0;
|
|
|
|
if (tvwEncontrados.Nodes.Count > 0)
|
|
{
|
|
cmbIP_Mov.SelectedIndex = 0;
|
|
cmbIP_Dir.SelectedIndex = 0;
|
|
}
|
|
}
|
|
|
|
private async void btnVerificarAlteracoes_Click(object sender, EventArgs e)
|
|
{
|
|
if (btnVerificarAlteracoes.Text.Contains("Verificar"))
|
|
{
|
|
if (tvwEncontrados.Nodes.Count > 0)
|
|
{
|
|
if (MessageBox.Show("Desconecte o dispositivo que deseja identificar da rede e então pressione OK para verificar", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Question) == DialogResult.OK)
|
|
{
|
|
btnVerificarAlteracoes.Text = "Cancelar Verificação";
|
|
if (await IniciarVarreduraAlteracoesAsync())
|
|
{
|
|
btnVerificarAlteracoes.Text = "Verificar Alterações";
|
|
MessageBox.Show($"Verificação concluída com {tvwAlterados.Nodes.Count} endereços alterados!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
btnVerificarAlteracoes.Text = "Verificar Alterações";
|
|
_VarreduraEmAndamento = false;
|
|
MessageBox.Show($"Verificação encerrada com {tvwAlterados.Nodes.Count} endereços alterados!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
private async Task<bool> IniciarVarreduraAlteracoesAsync()
|
|
{
|
|
_VarreduraEmAndamento = true;
|
|
|
|
tvwAlterados.Nodes.Clear();
|
|
|
|
int i = 0;
|
|
foreach (TreeNode node in tvwEncontrados.Nodes)
|
|
{
|
|
if (!_VarreduraEmAndamento)
|
|
{
|
|
break;
|
|
}
|
|
|
|
string ip = node.Text;
|
|
|
|
bool isAlive = await EthernetService.PingAsync(ip);
|
|
|
|
if (!isAlive)
|
|
{
|
|
tvwAlterados.Nodes.Add(ip);
|
|
}
|
|
i++;
|
|
}
|
|
|
|
_VarreduraEmAndamento = false;
|
|
|
|
return tvwEncontrados.Nodes.Count == i;
|
|
}
|
|
|
|
private async void cmbIP_Disp_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
ComboBox cmb = ((ComboBox)sender);
|
|
string name = cmb.Name.Replace("cmbIP_", "");
|
|
GroupBox gpb = FuncoesGlobais.FindControlRecursive<GroupBox>(this, "gpb" + name);
|
|
CheckBox chb = FuncoesGlobais.FindControlRecursive<CheckBox>(gpb, "chbConectado_" + name);
|
|
TextBox _txtGateway = FuncoesGlobais.FindControlRecursive<TextBox>(gpb, "txtGateway_" + name);
|
|
TextBox _txtSubmask = FuncoesGlobais.FindControlRecursive<TextBox>(gpb, "txtSubmask_" + name);
|
|
TextBox _txtIP = FuncoesGlobais.FindControlRecursive<TextBox>(gpb, "txtIP_" + name);
|
|
Button _btn = FuncoesGlobais.FindControlRecursive<Button>(gpb, "btnDefinirIP_" + name);
|
|
|
|
var deviceManager = new UsrDR302Service(cmb.Text);
|
|
chb.Checked = await deviceManager.CheckDeviceAsync();
|
|
_btn.Enabled = chb.Checked;
|
|
_txtGateway.Enabled = chb.Checked;
|
|
_txtSubmask.Enabled = chb.Checked;
|
|
_txtIP.Enabled = chb.Checked;
|
|
|
|
if (chb.Checked)
|
|
{
|
|
_txtGateway.Text = txtGateway.Text;
|
|
_txtSubmask.Text = txtSubmask.Text;
|
|
_txtIP.Text = cmb.Text;
|
|
}
|
|
}
|
|
|
|
private async void btnDefinirIP_Mov_Click(object sender, EventArgs e)
|
|
{
|
|
await ConfigurarDadosRedeConversor(cmbIP_Mov.Text, txtIP_Mov.Text, txtSubmask_Mov.Text, txtGateway_Mov.Text);
|
|
}
|
|
|
|
private async void btnDefinirIP_Dir_Click(object sender, EventArgs e)
|
|
{
|
|
await ConfigurarDadosRedeConversor(cmbIP_Dir.Text, txtIP_Dir.Text, txtSubmask_Dir.Text, txtGateway_Dir.Text);
|
|
}
|
|
|
|
|
|
private async Task ConfigurarDadosRedeConversor(string IPatual, string IPnovo, string subnetMask, string gateway)
|
|
{
|
|
var deviceManager = new UsrDR302Service(IPatual);
|
|
|
|
if (await deviceManager.ConnectAsync())
|
|
{
|
|
Console.WriteLine($"Conectado ao dispositivo no IP {IPatual}.");
|
|
|
|
if (await deviceManager.EnterATModeAsync())
|
|
{
|
|
Console.WriteLine("Entrou no modo AT.");
|
|
|
|
// Enviar comandos AT (exemplo para consultar a versão)
|
|
string response = await deviceManager.SendATCommandAsync("AT+VER");
|
|
Console.WriteLine("Resposta do comando AT+VER: " + response);
|
|
|
|
// Configurar IP, Máscara de Sub-rede e Gateway
|
|
bool configSuccess = await deviceManager.SetNetworkConfigAsync(IPnovo, subnetMask, gateway);
|
|
|
|
if (configSuccess)
|
|
{
|
|
Console.WriteLine("Configuração de rede definida com sucesso.");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Falha ao definir configuração de rede.");
|
|
}
|
|
|
|
// Sair do modo AT
|
|
if (await deviceManager.ExitATModeAsync())
|
|
{
|
|
Console.WriteLine("Saiu do modo AT e voltou ao modo transparente.");
|
|
}
|
|
}
|
|
deviceManager.Close();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Falha na conexão com o dispositivo.");
|
|
MessageBox.Show("Falha na conexão com o dispositivo.", "Conexão", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|