203 lines
6.7 KiB
C#
203 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Net;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using OperationControl.Helpers;
|
|
|
|
namespace OperationControl.ViewModels
|
|
{
|
|
public class NetworkConfigViewModel : INotifyPropertyChanged
|
|
{
|
|
public NetworkConfigViewModel()
|
|
{
|
|
Interfaces = new ObservableCollection<string>();
|
|
|
|
AtualizarCommand = new RelayCommand(_ => Atualizar(), _ => PodeAtualizar);
|
|
SalvarCommand = new RelayCommand(async _ => await SalvarAsync(), _ => PodeSalvar);
|
|
FecharCommand = new RelayCommand(_ => Fechar());
|
|
|
|
CarregarInterfaces();
|
|
}
|
|
|
|
// Lista de interfaces
|
|
public ObservableCollection<string> Interfaces { get; }
|
|
|
|
private string _interfaceSelecionada;
|
|
public string InterfaceSelecionada
|
|
{
|
|
get => _interfaceSelecionada;
|
|
set
|
|
{
|
|
if (_interfaceSelecionada != value)
|
|
{
|
|
_interfaceSelecionada = value;
|
|
OnPropertyChanged(nameof(InterfaceSelecionada));
|
|
OnPropertyChanged(nameof(PodeAtualizar));
|
|
OnPropertyChanged(nameof(PodeSalvar));
|
|
MensagemStatus = "Clique em Atualizar para ler os dados da interface selecionada.";
|
|
}
|
|
}
|
|
}
|
|
|
|
private string _ip;
|
|
public string Ip
|
|
{
|
|
get => _ip;
|
|
set { _ip = value; OnPropertyChanged(nameof(Ip)); OnPropertyChanged(nameof(PodeSalvar)); }
|
|
}
|
|
|
|
private string _subnet;
|
|
public string Subnet
|
|
{
|
|
get => _subnet;
|
|
set { _subnet = value; OnPropertyChanged(nameof(Subnet)); OnPropertyChanged(nameof(PodeSalvar)); }
|
|
}
|
|
|
|
private string _gateway;
|
|
public string Gateway
|
|
{
|
|
get => _gateway;
|
|
set { _gateway = value; OnPropertyChanged(nameof(Gateway)); OnPropertyChanged(nameof(PodeSalvar)); }
|
|
}
|
|
|
|
private string _mensagemStatus;
|
|
public string MensagemStatus
|
|
{
|
|
get => _mensagemStatus;
|
|
set { _mensagemStatus = value; OnPropertyChanged(nameof(MensagemStatus)); }
|
|
}
|
|
|
|
// Comandos
|
|
public ICommand AtualizarCommand { get; }
|
|
public ICommand SalvarCommand { get; }
|
|
public ICommand FecharCommand { get; }
|
|
|
|
private bool _isBusy;
|
|
public bool IsBusy
|
|
{
|
|
get => _isBusy;
|
|
set
|
|
{
|
|
_isBusy = value;
|
|
OnPropertyChanged(nameof(IsBusy));
|
|
OnPropertyChanged(nameof(PodeSalvar));
|
|
OnPropertyChanged(nameof(PodeAtualizar));
|
|
}
|
|
}
|
|
|
|
public bool PodeAtualizar => !IsBusy && !string.IsNullOrEmpty(InterfaceSelecionada);
|
|
|
|
public bool PodeSalvar => !IsBusy &&
|
|
!string.IsNullOrEmpty(InterfaceSelecionada) &&
|
|
!string.IsNullOrWhiteSpace(Ip) &&
|
|
!string.IsNullOrWhiteSpace(Subnet) &&
|
|
!string.IsNullOrWhiteSpace(Gateway);
|
|
|
|
private bool IpValido(string s) => IPAddress.TryParse(s, out _);
|
|
|
|
// Evento para a janela fechar de fora
|
|
public event Action<bool> RequisitarFechamento;
|
|
|
|
private void CarregarInterfaces()
|
|
{
|
|
Interfaces.Clear();
|
|
|
|
// TODO: trocar pelo seu método real pra listar as interfaces
|
|
// Exemplo:
|
|
// foreach (var iface in NetworkHelper.ListarInterfaces())
|
|
// Interfaces.Add(iface.NomeExibicao);
|
|
|
|
foreach (var i in EthernetService.ObterNomesInterfaces())
|
|
{
|
|
if (i.Contains("Ativa"))
|
|
Interfaces.Add(i.Split('(')[0].Trim());
|
|
}
|
|
|
|
if (Interfaces.Count > 0)
|
|
InterfaceSelecionada = Interfaces[0];
|
|
|
|
MensagemStatus = "Selecione a interface e clique em Atualizar para carregar os dados atuais.";
|
|
}
|
|
|
|
private void Atualizar()
|
|
{
|
|
if (string.IsNullOrEmpty(InterfaceSelecionada))
|
|
{
|
|
MensagemStatus = "Selecione uma interface primeiro.";
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// TODO: aqui você chama SEUS métodos reais
|
|
// var config = NetworkHelper.GetConfig(InterfaceSelecionada);
|
|
// Ip = config.Ip;
|
|
// Subnet = config.Subnet;
|
|
// Gateway = config.Gateway;
|
|
|
|
// MOCK por enquanto:
|
|
Ip = EthernetService.ObterIpAtual(InterfaceSelecionada);
|
|
Subnet = EthernetService.ObterSubnetMaskAtual(InterfaceSelecionada);
|
|
Gateway = EthernetService.ObterIpGateway(InterfaceSelecionada);
|
|
|
|
MensagemStatus = "Configuração atual carregada da interface selecionada.";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MensagemStatus = "Erro ao ler configuração da interface: " + ex.Message;
|
|
}
|
|
}
|
|
|
|
private async Task SalvarAsync()
|
|
{
|
|
if (!PodeSalvar) { MensagemStatus = "Preencha IP, Máscara e Gateway antes de salvar."; return; }
|
|
|
|
if (!IpValido(Ip) || !IpValido(Subnet) || !IpValido(Gateway))
|
|
{
|
|
MensagemStatus = "IP, máscara ou gateway inválidos. Ex: 192.168.0.50 / 255.255.255.0 / 192.168.0.1";
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
IsBusy = true;
|
|
MensagemStatus = "Salvando configuração...";
|
|
|
|
bool sucesso = await EthernetService.DefinirIpFixo(InterfaceSelecionada, Ip, Subnet, Gateway, false);
|
|
|
|
if (!sucesso)
|
|
{
|
|
MensagemStatus = "Falha ao aplicar configuração de rede.";
|
|
return;
|
|
}
|
|
|
|
MensagemStatus = "Configuração aplicada com sucesso.";
|
|
System.Windows.MessageBox.Show($"Configuração salva para {InterfaceSelecionada}...", "Rede", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
RequisitarFechamento?.Invoke(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MensagemStatus = "Erro ao salvar configuração de rede: " + ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
IsBusy = false;
|
|
}
|
|
}
|
|
|
|
private void Fechar()
|
|
{
|
|
// fecha sem "sucesso"
|
|
RequisitarFechamento?.Invoke(false);
|
|
}
|
|
|
|
// INotifyPropertyChanged
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
private void OnPropertyChanged(string nome)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nome));
|
|
}
|
|
}
|
|
} |