370 lines
14 KiB
C#
370 lines
14 KiB
C#
using AgroBase.Forms.IHM;
|
|
using AgroBase.Models;
|
|
using AgroBase.Models.Components;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static AgroBase.Models.Enums;
|
|
|
|
namespace AgroBase.Forms.Operacoes
|
|
{
|
|
public partial class frmInicioOperacao : Form
|
|
{
|
|
string CaminhoOperacoesSistema = Variaveis.CaminhoSistema + Variaveis.CaminhoOperacoesSalvas;
|
|
frmDialogoMapa frmDialogo = new frmDialogoMapa();
|
|
List<string> ruasSelecionadas = new List<string>();
|
|
string pathOperacaoSelecionada = "";
|
|
|
|
public frmInicioOperacao()
|
|
{
|
|
InitializeComponent();
|
|
|
|
FuncoesGlobais.DefinirEventosPicBtn(picAtualizar, "Erro ao atualizar lista de arquivos!", async () =>
|
|
{
|
|
LoadDirectoryFiles(txtCaminho.Text);
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picVoltar, "Erro ao obter lista de arquivos anterior!", async () =>
|
|
{
|
|
string diretorioPai = Path.GetDirectoryName(txtCaminho.Text);
|
|
|
|
if (diretorioPai != null)
|
|
{
|
|
LoadDirectoryFiles(diretorioPai);
|
|
}
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picMapa, "Erro ao carregar pré-visualização da operação!", async () =>
|
|
{
|
|
frmDialogo.Show();
|
|
await frmDialogo.AtualizarRuasSelecionadas(ruasSelecionadas);
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picFechar, "", async () =>
|
|
{
|
|
this.Close();
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picIniciar, "Erro ao iniciar operação!", async () =>
|
|
{
|
|
if (Variaveis.OperacaoEmAndamento.Sensoriamento.Operacao.OperacaoLiberada)
|
|
{
|
|
var res = CustomDialog.ShowDialog(
|
|
"Iniciar Operação",
|
|
"Deseja iniciar a operação selecionada?",
|
|
MessageBoxIcon.Question,
|
|
new Dictionary<(string, DialogResult?), Action>()
|
|
{
|
|
{ ("Sim", DialogResult.Yes), () => { } },
|
|
{ ("Não", DialogResult.No), () => { } },
|
|
}
|
|
);
|
|
if (res == DialogResult.Yes)
|
|
{
|
|
frmDialogo.Show();
|
|
await frmDialogo.AtualizarRuasSelecionadas(ruasSelecionadas);
|
|
frmDialogo.IniciarOperacao();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CustomDialog.ShowDialog(
|
|
"Conexão de módulos pendentes",
|
|
$"{Variaveis.OperacaoEmAndamento.Sensoriamento.Operacao.ErroOperacaoLiberada}",
|
|
MessageBoxIcon.Warning,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } },
|
|
}
|
|
);
|
|
}
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picExcluir, "Erro ao excluir operação!", async () =>
|
|
{
|
|
if (pathOperacaoSelecionada.Contains(CaminhoOperacoesSistema))
|
|
{
|
|
var res = CustomDialog.ShowDialog(
|
|
"Excluir Operação",
|
|
"Tem certeza que deseja excluir a operação do sistema?",
|
|
MessageBoxIcon.Question,
|
|
new Dictionary<(string, DialogResult?), Action>()
|
|
{
|
|
{ ("Sim", DialogResult.Yes), () => { } },
|
|
{ ("Não", DialogResult.No), () => { } },
|
|
}
|
|
);
|
|
if (res == DialogResult.Yes)
|
|
{
|
|
File.Delete(pathOperacaoSelecionada);
|
|
|
|
CustomDialog.ShowDialog(
|
|
"Excluir Operação",
|
|
"Operação excluída da memória do equipamento com sucesso!",
|
|
MessageBoxIcon.Information,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } },
|
|
}
|
|
);
|
|
|
|
LoadDirectoryFiles(txtCaminho.Text);
|
|
}
|
|
}
|
|
});
|
|
FuncoesGlobais.DefinirEventosPicBtn(picSalvar, "Erro ao salvar operação!", async () =>
|
|
{
|
|
if (!pathOperacaoSelecionada.Contains(CaminhoOperacoesSistema))
|
|
{
|
|
string fileName = Path.GetFileName(pathOperacaoSelecionada);
|
|
|
|
if (!Directory.Exists(CaminhoOperacoesSistema))
|
|
{
|
|
Directory.CreateDirectory(CaminhoOperacoesSistema);
|
|
}
|
|
|
|
File.Copy(pathOperacaoSelecionada, CaminhoOperacoesSistema + fileName);
|
|
|
|
CustomDialog.ShowDialog(
|
|
"Copiar Operação",
|
|
"Operação copiada da mídia para a memória do equipamento com sucesso!",
|
|
MessageBoxIcon.Information,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } },
|
|
}
|
|
);
|
|
}
|
|
});
|
|
|
|
cmbMidias.SelectedIndexChanged += cmbMidias_SelectedIndexChanged;
|
|
}
|
|
|
|
private void frmInicioOperacao_Load(object sender, EventArgs e)
|
|
{
|
|
PopulateDrivesComboBox();
|
|
AtualizarOpLiberada();
|
|
}
|
|
|
|
private void PopulateDrivesComboBox()
|
|
{
|
|
cmbMidias.Items.Clear();
|
|
var drives = DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => d.Name);
|
|
cmbMidias.Items.Add("Local");
|
|
cmbMidias.Items.AddRange(drives.ToArray());
|
|
|
|
cmbMidias.SelectedIndex = 0;
|
|
}
|
|
|
|
private void cmbMidias_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
ComboBox cmb = (ComboBox)sender;
|
|
if (cmb.SelectedItem != null)
|
|
{
|
|
string selectedDrive = cmb.SelectedItem.ToString();
|
|
LoadDirectoryFiles(selectedDrive == "Local" ? CaminhoOperacoesSistema : selectedDrive);
|
|
}
|
|
}
|
|
|
|
private void LoadDirectoryFiles(string path)
|
|
{
|
|
pathOperacaoSelecionada = "";
|
|
|
|
FlowLayoutPanel flowPanel = fpnlPastas;
|
|
flowPanel.Controls.Clear();
|
|
try
|
|
{
|
|
var directories = Directory.GetDirectories(path);
|
|
var files = Directory.GetFiles(path);
|
|
|
|
foreach (var directory in directories)
|
|
{
|
|
AddItemToFlowPanel(flowPanel, directory, true);
|
|
}
|
|
|
|
foreach (var file in files)
|
|
{
|
|
AddItemToFlowPanel(flowPanel, file, false);
|
|
}
|
|
|
|
txtCaminho.Text = path;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
CustomDialog.ShowDialog(
|
|
"Erro",
|
|
$"Erro ao listar arquivos e pastas: {ex.Message}",
|
|
MessageBoxIcon.Error,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } },
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
private async Task CarregarDadosOperacao(string path)
|
|
{
|
|
try
|
|
{
|
|
frmDialogo = new frmDialogoMapa();
|
|
frmDialogo.Show();
|
|
frmDialogo.Hide();
|
|
bool sucesso = Variaveis.OperacaoEmAndamento.CarregarParametrizacaoOperacao(frmDialogo.pnlMapa, frmDialogo.lblMapaCarregado, path);
|
|
|
|
bool condicaoOperacaoCarregada() => Variaveis.OperacaoEmAndamento.Trajetoria?.CorredorAtual != null;
|
|
bool resultado = await FuncoesGlobais.AguardarCondicaoAsync(condicaoOperacaoCarregada, 15000, 200);
|
|
|
|
if (!resultado)
|
|
{
|
|
throw new Exception("Erro ao carregar a trajetória do mapa!");
|
|
}
|
|
|
|
ruasSelecionadas = new List<string>();
|
|
|
|
foreach (var chb in fpnlInformacoes.Controls.OfType<CheckBox>())
|
|
{
|
|
string disp = chb.Name.Replace("chb", "");
|
|
T_Code Dispositivo = (T_Code)Enum.Parse(typeof(T_Code), disp);
|
|
chb.Checked = Variaveis.OperacaoEmAndamento.Parametros.ModulosMandatorios.Any(x => x.Dispositivo == Dispositivo && x.Mandatorio);
|
|
}
|
|
|
|
txtModoOperacao.Text = (Variaveis.OperacaoEmAndamento.Parametros?.Modo ?? ModoOperacao.NaoDefinido).ToString();
|
|
txtDescricao.Text = Variaveis.OperacaoEmAndamento.Descricao;
|
|
txtCriadoEm.Text = new FileInfo(path).CreationTime.ToString("dd/MM/yyyy HH:mm");
|
|
txtRuasPercorrer.Text = (Variaveis.OperacaoEmAndamento.Trajetoria?.Corredores?.Count() ?? 0).ToString();
|
|
txtDistanciaTotal.Text = (Variaveis.OperacaoEmAndamento.Trajetoria?.DistanciaTotal ?? 0).ToString("0.00");
|
|
string tempo = Variaveis.OperacaoEmAndamento.Trajetoria?.TempoEstimadoOperacao;
|
|
txtTempoEstimado.Text = tempo;
|
|
|
|
pathOperacaoSelecionada = path;
|
|
|
|
AtualizarOpLiberada();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
CustomDialog.ShowDialog(
|
|
"Erro",
|
|
$"Erro ao carregar operação!{Environment.NewLine}{ex.Message}",
|
|
MessageBoxIcon.Error,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } },
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
private void AddItemToFlowPanel(FlowLayoutPanel flowPanel, string path, bool isDirectory)
|
|
{
|
|
bool isOperation = !isDirectory && Path.GetExtension(path) == ".opr";
|
|
|
|
if (!isDirectory && !isOperation)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string pnlNome = "fpnl_" + path.Split('/')[path.Split('/').Length - 1];
|
|
|
|
// Criação do FlowLayoutPanel para o item
|
|
FlowLayoutPanel itemPanel = new FlowLayoutPanel
|
|
{
|
|
Size = new Size(150, 170),
|
|
Margin = new Padding(5),
|
|
Name = pnlNome,
|
|
//BorderStyle = path == pathOperacaoSelecionada ? BorderStyle.FixedSingle : BorderStyle.None
|
|
};
|
|
|
|
// Criação do PictureBox
|
|
PictureBox pictureBox = new PictureBox
|
|
{
|
|
Size = new Size(144, 100),
|
|
Image = isDirectory ? Properties.Resources.pasta : isOperation ? Properties.Resources.cana : GetIconForFile(path),
|
|
SizeMode = PictureBoxSizeMode.Zoom,
|
|
Margin = new Padding(3),
|
|
Cursor = Cursors.Hand
|
|
};
|
|
|
|
// Criação do RichTextBox
|
|
RichTextBox richTextBox = new RichTextBox
|
|
{
|
|
Size = new Size(144, 49),
|
|
Text = Path.GetFileName(path),
|
|
ReadOnly = true,
|
|
BorderStyle = BorderStyle.None,
|
|
BackColor = Color.White,
|
|
Margin = new Padding(3),
|
|
};
|
|
richTextBox.SelectAll();
|
|
richTextBox.SelectionAlignment = HorizontalAlignment.Center;
|
|
|
|
pictureBox.Click += (s, args) =>
|
|
{
|
|
if (isDirectory)
|
|
{
|
|
LoadDirectoryFiles(path);
|
|
}
|
|
else if (isOperation)
|
|
{
|
|
fpnlPastas.Controls.OfType<FlowLayoutPanel>().ToList().ForEach(flowLayoutPanel =>
|
|
{
|
|
flowLayoutPanel.BorderStyle = BorderStyle.None;
|
|
});
|
|
|
|
FuncoesGlobais.ExecutarMetodoComVerificacaoCrossThreadAsync(fpnlPastas, async () =>
|
|
{
|
|
this.Cursor = Cursors.WaitCursor;
|
|
await CarregarDadosOperacao(path);
|
|
((FlowLayoutPanel)fpnlPastas.Controls.Find(pnlNome, false).First()).BorderStyle = BorderStyle.FixedSingle;
|
|
this.Cursor = Cursors.Default;
|
|
});
|
|
}
|
|
};
|
|
|
|
// Adiciona os controles ao itemPanel
|
|
itemPanel.Controls.Add(pictureBox);
|
|
itemPanel.Controls.Add(richTextBox);
|
|
|
|
// Adiciona o itemPanel ao flowPanel
|
|
flowPanel.Controls.Add(itemPanel);
|
|
}
|
|
|
|
private Image GetIconForFile(string filePath)
|
|
{
|
|
// Método para obter o ícone do arquivo
|
|
Icon icon = Icon.ExtractAssociatedIcon(filePath);
|
|
return icon.ToBitmap();
|
|
}
|
|
|
|
private void frmInicioOperacao_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (!frmDialogo.IsDisposed)
|
|
{
|
|
frmDialogo.Close();
|
|
}
|
|
}
|
|
|
|
private void btnStatusOperacao_Click(object sender, EventArgs e)
|
|
{
|
|
AtualizarOpLiberada();
|
|
CustomDialog.ShowDialog(
|
|
"Status",
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.Operacao.ErroOperacaoLiberada,
|
|
MessageBoxIcon.Information,
|
|
new Dictionary<(string, dynamic), Action>()
|
|
{
|
|
{ ("OK", null), () => { } }
|
|
}
|
|
);
|
|
}
|
|
|
|
private void AtualizarOpLiberada()
|
|
{
|
|
lblStatusOperacao.Text = "LIBERADO: " + (Variaveis.OperacaoEmAndamento.Sensoriamento.Operacao.OperacaoLiberada ? "SIM" : "NÃO");
|
|
lblStatusOperacao.ForeColor = Variaveis.OperacaoEmAndamento.Sensoriamento.Operacao.OperacaoLiberada ? Color.DarkGreen : Color.DarkRed;
|
|
}
|
|
|
|
}
|
|
}
|