274 lines
12 KiB
C#
274 lines
12 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;
|
|
using static AgroBase.Models.Enuns;
|
|
|
|
namespace AgroMonitor.Forms
|
|
{
|
|
public partial class frmMonitoramento : Form
|
|
{
|
|
Timer tmrLeituras = new Timer();
|
|
DateTime MomentoLog = DateTime.Now;
|
|
private MapasModel Mapa;
|
|
Dictionary<byte, List<LoRaProtocoloTransmissaoModel>> Logs = new Dictionary<byte, List<LoRaProtocoloTransmissaoModel>>();
|
|
int ticks = 0;
|
|
|
|
public frmMonitoramento()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void frmMonitoramento_Load(object sender, EventArgs e)
|
|
{
|
|
tmrLeituras.Interval = 1000;
|
|
tmrLeituras.Tick += tmrLeituras_Tick;
|
|
tmrLeituras.Start();
|
|
|
|
Mapa = new MapasModel()
|
|
{
|
|
pnlMapa = pnlMapa,
|
|
lblMapa = lblMapa,
|
|
};
|
|
|
|
Logs = new Dictionary<byte, List<LoRaProtocoloTransmissaoModel>>(VariaveisMonitoramento.EquipamentosConectadosLogs);
|
|
}
|
|
|
|
private void tmrLeituras_Tick(object sender, EventArgs e)
|
|
{
|
|
string EquipamentoSelecionado = cmbEquipamento.Text;
|
|
if (cmbEquipamento.Items.Count != VariaveisMonitoramento.EquipamentosConectados.Count())
|
|
{
|
|
cmbEquipamento.Items.Clear();
|
|
cmbEquipamento.Items.AddRange(VariaveisMonitoramento.EquipamentosConectados.Keys.Select(x => x.ToString()).ToArray());
|
|
if (cmbEquipamento.Items.Contains(EquipamentoSelecionado))
|
|
{
|
|
cmbEquipamento.SelectedIndex = cmbEquipamento.Items.IndexOf(EquipamentoSelecionado);
|
|
}
|
|
}
|
|
|
|
if (cmbEquipamento.SelectedIndex > -1)
|
|
{
|
|
AtualizarDadosTela();
|
|
}
|
|
|
|
// Verifica atualizações nos equipamentos conectados
|
|
VerificarAtualizacoesEquipamentos();
|
|
|
|
if (ticks % (tmrLeituras.Interval * VariaveisEquipamento.TempoEntrePingsConexao) == 0)
|
|
{
|
|
byte EnderecoBase = VariaveisMonitoramento.ComunicacaoLoRa.parametros.address;
|
|
GPSService.EnviarCoordenadasParaMapa(GPSService.UltimaLeitura.Latitude, GPSService.UltimaLeitura.Longitude, GPSService.UltimaLeitura.Orientacao, chbFoco.Checked, Convert.ToInt32(EnderecoBase));
|
|
foreach (var Equipamento in VariaveisMonitoramento.EquipamentosConectados.Where(x => x.Key != EnderecoBase))
|
|
{
|
|
LoRaService.EnviarDadosPosicao(EnderecoBase, Equipamento.Key);
|
|
}
|
|
}
|
|
|
|
ticks++;
|
|
}
|
|
|
|
private void VerificarAtualizacoesEquipamentos()
|
|
{
|
|
foreach (var key in VariaveisMonitoramento.EquipamentosConectados.Keys)
|
|
{
|
|
// Verifica se o equipamento foi atualizado
|
|
if (!Logs.ContainsKey(key) || !EquipamentosIguais(VariaveisMonitoramento.EquipamentosConectadosLogs[key], Logs[key]))
|
|
{
|
|
// Se sim, consuma o último valor (último log)
|
|
ConsumirUltimoValor(VariaveisMonitoramento.EquipamentosConectadosLogs[key]);
|
|
// Atualiza o estado anterior da lista EquipamentosConectados
|
|
Logs[key] = new List<LoRaProtocoloTransmissaoModel>(VariaveisMonitoramento.EquipamentosConectadosLogs[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool EquipamentosIguais(List<LoRaProtocoloTransmissaoModel> listaAtual, List<LoRaProtocoloTransmissaoModel> listaAnterior)
|
|
{
|
|
// Verifica se as listas são iguais
|
|
return listaAtual.SequenceEqual(listaAnterior);
|
|
}
|
|
|
|
private void ConsumirUltimoValor(List<LoRaProtocoloTransmissaoModel> equipamento)
|
|
{
|
|
// Consumir o último valor (último log) do equipamento atualizado
|
|
if (equipamento.Count > 0)
|
|
{
|
|
var ultimoLog = equipamento[equipamento.Count - 1];
|
|
|
|
GPSService.EnviarCoordenadasParaMapa(ultimoLog.Coordenadas.Latitude, ultimoLog.Coordenadas.Longitude, ultimoLog.Coordenadas.Orientacao, chbFoco.Checked, Convert.ToInt32(ultimoLog.EnderecoCarro));
|
|
}
|
|
}
|
|
|
|
private void cmbEquipamento_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
MomentoLog = MomentoLog.AddSeconds(-15);
|
|
AtualizarDadosTela();
|
|
}
|
|
|
|
private void AtualizarDadosTela()
|
|
{
|
|
byte Endereco = Convert.ToByte(cmbEquipamento.Text);
|
|
|
|
if (Endereco == VariaveisMonitoramento.ComunicacaoLoRa.parametros.address)
|
|
{
|
|
MomentoLog = DateTime.Now;
|
|
|
|
txtLeitura.Text = MomentoLog.ToString("ddd HH:mm:ss");
|
|
txtOrientacao.Text = GPSService.UltimaLeitura.Orientacao.ToString("0.00");
|
|
txtLatitude.Text = GPSService.UltimaLeitura.Latitude.ToString();
|
|
txtLongitude.Text = GPSService.UltimaLeitura.Longitude.ToString();
|
|
|
|
txtStatusOperacao.Text = "";
|
|
txtStatusCarro.Text = "";
|
|
txtTempoDecorrido.Text = "";
|
|
txtTempoEstimado.Text = "";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbProgressoOperacao, 0);
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbProgressoRua, 0);
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbBateria, 0);
|
|
lblBateriaDados.Text = "0,00%";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbReservatorio, 0);
|
|
lblReservatorioDados.Text = "0,00%";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbHerbicidaAplicado, 0);
|
|
lblHerbicidaDados.Text = "0,00 L (0,00 ml/erva)";
|
|
lblErvasIdentificadas.Text = "0 ervas";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbPressao, 0);
|
|
lblPressaoDados.Text = "0,00 psi";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbTemperaturaMotores, 0);
|
|
lblTemperaturaMotoresDados.Text = "0,00 ºC";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbTemperaturaCampo, 0);
|
|
lblTemperaturaCampoDados.Text = "0,00 ºC";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbVelocidade, 0);
|
|
lblVelocidadeDados.Text = "0,00 km/h";
|
|
}
|
|
else if (Endereco > 0x00 && VariaveisMonitoramento.EquipamentosConectados.Any(x => x.Key == Endereco))
|
|
{
|
|
var Equipamento = VariaveisMonitoramento.EquipamentosConectados[Endereco];
|
|
|
|
if (Equipamento.Momento > MomentoLog)
|
|
{
|
|
MomentoLog = Equipamento.Momento;
|
|
|
|
txtLeitura.Text = Equipamento.Momento.ToString("ddd HH:mm:ss");
|
|
txtOrientacao.Text = Equipamento.Coordenadas.Orientacao.ToString("0.00");
|
|
txtLatitude.Text = Equipamento.Coordenadas.Latitude.ToString();
|
|
txtLongitude.Text = Equipamento.Coordenadas.Longitude.ToString();
|
|
|
|
txtStatusOperacao.Text = Equipamento.Operacao.statusOperacao.ToString();
|
|
txtStatusCarro.Text = Equipamento.Operacao.statusCarro.ToString();
|
|
txtTempoDecorrido.Text = TimeSpan.FromSeconds(Equipamento.Operacao.TempoOperacao).ToString(@"hh\:mm\:ss");
|
|
txtTempoEstimado.Text = Equipamento.Operacao.TempoEstimado;
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbProgressoOperacao, Equipamento.Operacao.PercentualOperacao);
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbProgressoRua, Equipamento.Operacao.PercentualRua);
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbBateria, Equipamento.Sensores.PercentualBateria);
|
|
lblBateriaDados.Text = $"{Equipamento.Sensores.PercentualBateria.ToString("0.00")}%";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbReservatorio, Equipamento.Sensores.PercentualReservatorio);
|
|
lblReservatorioDados.Text = $"{Equipamento.Sensores.PercentualReservatorio.ToString("0.00")}%";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbHerbicidaAplicado, Equipamento.Sensores.HerbicidaConsumido);
|
|
lblHerbicidaDados.Text = $"{Equipamento.Sensores.HerbicidaConsumido.ToString("0.00")} L ({Equipamento.Sensores.HerbicidaPorErva.ToString("0.00")} ml/erva)";
|
|
lblErvasIdentificadas.Text = $"{Equipamento.Sensores.ErvasIdentificadas} ervas";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbPressao, Equipamento.Sensores.PressaoLinha);
|
|
lblPressaoDados.Text = $"{Equipamento.Sensores.PressaoLinha.ToString("0.00")} psi";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbTemperaturaMotores, Equipamento.Sensores.TemperaturaMotores);
|
|
lblTemperaturaMotoresDados.Text = $"{Equipamento.Sensores.TemperaturaMotores.ToString("0.00")} ºC";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbTemperaturaCampo, Equipamento.Sensores.TemperaturaCampo);
|
|
lblTemperaturaCampoDados.Text = $"{Equipamento.Sensores.TemperaturaCampo.ToString("0.00")} ºC";
|
|
FuncoesGlobais.PreencheValorProgressBar(pgbVelocidade, Equipamento.Sensores.Velocidade);
|
|
lblVelocidadeDados.Text = $"{Equipamento.Sensores.Velocidade.ToString("0.00")} km/h";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void btnCarregar_Click(object sender, EventArgs e)
|
|
{
|
|
Mapa.btnCarregar_Click(sender, e, "", true);
|
|
}
|
|
|
|
private void btnControle_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
EnviarComandoControleEquipamento(e.KeyCode);
|
|
}
|
|
|
|
private void btnControle_KeyUp(object sender, KeyEventArgs e)
|
|
{
|
|
EnviarComandoControleEquipamento(Keys.Escape);
|
|
}
|
|
|
|
private void EnviarComandoControleEquipamento(Keys Tecla, T_Code Disp = T_Code.Vzo)
|
|
{
|
|
byte Endereco = Convert.ToByte(cmbEquipamento.Text);
|
|
|
|
Dictionary<Keys, Keys> DeParaTeclas = new Dictionary<Keys, Keys>()
|
|
{
|
|
{ Keys.W, Keys.Up },
|
|
{ Keys.S, Keys.Down },
|
|
{ Keys.A, Keys.Left },
|
|
{ Keys.D, Keys.Right },
|
|
{ Keys.Escape, Keys.Escape },
|
|
};
|
|
|
|
if (!DeParaTeclas.ContainsKey(Tecla))
|
|
{
|
|
return;
|
|
}
|
|
|
|
Keys[] TeclasDir = { Keys.A, Keys.D };
|
|
Keys[] TeclasMov = { Keys.W, Keys.S };
|
|
|
|
OperacaoControleBaseModel Controle = new OperacaoControleBaseModel()
|
|
{
|
|
Momento = DateTime.Now,
|
|
Tecla = DeParaTeclas[Tecla],
|
|
ControleMov = pnlControle.Controls.OfType<CheckBox>().Where(x => x.Name.Contains("Mov")).ToDictionary(x => x.Name.Split('_')[1], x => x.Checked),
|
|
ControleDir = pnlControle.Controls.OfType<CheckBox>().Where(x => x.Name.Contains("Dir")).ToDictionary(x => x.Name.Split('_')[1], x => x.Checked),
|
|
RPM = Convert.ToInt32(nudRPM.Value),
|
|
AnguloMP = Convert.ToInt32(nudAngulo.Value),
|
|
VelocidadeMP = Convert.ToInt32(nudVelocidadeMP.Value),
|
|
Dispositivo = Disp,
|
|
Emergencia = btnParar.Text == "Retomar"
|
|
};
|
|
|
|
if (Disp == T_Code.Vzo)
|
|
{
|
|
if (Tecla == Keys.Escape || (!TeclasDir.Contains(Tecla) && !TeclasMov.Contains(Tecla)))
|
|
{
|
|
Controle.Dispositivo = T_Code.Vzo;
|
|
}
|
|
else if (TeclasDir.Contains(Tecla))
|
|
{
|
|
Controle.Dispositivo = T_Code.Dir;
|
|
}
|
|
else if (TeclasMov.Contains(Tecla))
|
|
{
|
|
Controle.Dispositivo = T_Code.Mov;
|
|
}
|
|
}
|
|
|
|
LoRaService.EnviarDadosComandoControle(Controle, Endereco);
|
|
}
|
|
|
|
private void btnParar_Click(object sender, EventArgs e)
|
|
{
|
|
if (btnParar.Text == "Parar")
|
|
{
|
|
btnParar.Text = "Retomar";
|
|
EnviarComandoControleEquipamento(Keys.Escape);
|
|
}
|
|
else
|
|
{
|
|
btnParar.Text = "Parar";
|
|
EnviarComandoControleEquipamento(Keys.Escape);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|