2024-07-15 19:02:36 +00:00
using AgroBase.Models ;
using AgroBase.Services ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Linq ;
2024-11-25 12:14:59 +00:00
using System.Threading.Tasks ;
2024-07-15 19:02:36 +00:00
using System.Windows.Forms ;
2024-07-26 19:54:25 +00:00
using static AgroBase . Models . Enums ;
2024-07-15 19:02:36 +00:00
namespace AgroMonitor.Forms
{
public partial class frmMonitoramento : Form
{
2024-11-25 12:14:59 +00:00
AsyncTaskTimerModel tmrLeituras ;
2025-02-03 19:54:39 +00:00
string EquipamentoSelecionado = "" ;
2024-07-15 19:02:36 +00:00
DateTime MomentoLog = DateTime . Now ;
2024-07-16 20:04:09 +00:00
private MapasModel Mapa ;
Dictionary < byte , List < LoRaProtocoloTransmissaoModel > > Logs = new Dictionary < byte , List < LoRaProtocoloTransmissaoModel > > ( ) ;
2024-10-04 20:08:48 +00:00
Keys UltimoComandoEnviado = Keys . None ;
2025-02-03 19:54:39 +00:00
bool ultimStatusoEmergencia = false ;
2024-07-15 19:02:36 +00:00
public frmMonitoramento ( )
{
InitializeComponent ( ) ;
}
private void frmMonitoramento_Load ( object sender , EventArgs e )
{
2024-11-25 19:26:00 +00:00
tmrLeituras = new AsyncTaskTimerModel ( "tmrLeituras" , tmrLeituras_Tick , 1000 , this ) ;
2024-07-15 19:02:36 +00:00
tmrLeituras . Start ( ) ;
2024-07-16 20:04:09 +00:00
2024-10-04 13:11:23 +00:00
cmbTipoMovimento . Items . Clear ( ) ;
cmbTipoMovimento . Items . AddRange ( Enum . GetNames ( typeof ( TipoMovimentoDirecional ) ) ) ;
cmbTipoMovimento . SelectedIndex = 0 ;
2024-07-16 20:04:09 +00:00
Mapa = new MapasModel ( )
{
pnlMapa = pnlMapa ,
lblMapa = lblMapa ,
} ;
2025-02-03 19:54:39 +00:00
lock ( VariaveisMonitoramento . _EquipamentosConectadosLogsLock )
{
Logs = new Dictionary < byte , List < LoRaProtocoloTransmissaoModel > > ( VariaveisMonitoramento . EquipamentosConectadosLogs ) ;
}
2024-07-15 19:02:36 +00:00
}
2024-11-25 12:14:59 +00:00
private async Task tmrLeituras_Tick ( )
2024-07-15 19:02:36 +00:00
{
2025-02-03 19:54:39 +00:00
if ( cmbEquipamento . Text ! = "" )
{
EquipamentoSelecionado = cmbEquipamento . Text ;
}
2024-07-15 19:02:36 +00:00
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 ( ) ;
}
2024-07-16 20:04:09 +00:00
// Verifica atualizações nos equipamentos conectados
VerificarAtualizacoesEquipamentos ( ) ;
}
private void VerificarAtualizacoesEquipamentos ( )
{
foreach ( var key in VariaveisMonitoramento . EquipamentosConectados . Keys )
{
2025-02-03 19:54:39 +00:00
lock ( VariaveisMonitoramento . _EquipamentosConectadosLogsLock )
2024-07-16 20:04:09 +00:00
{
2025-02-03 19:54:39 +00:00
// 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 ] ) ;
}
2024-07-16 20:04:09 +00:00
}
}
}
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 ] ;
2024-10-04 20:08:48 +00:00
int enderecoEquipamentoEmFoco = cmbEquipamento . SelectedIndex > - 1 ? Convert . ToInt32 ( cmbEquipamento . Text ) : Convert . ToInt32 ( Variaveis . ConexaoLoRa . parametros . address ) ;
2024-10-04 13:11:23 +00:00
int enderecoEquipamentoLog = Convert . ToInt32 ( ultimoLog . EnderecoCarro ) ;
bool foco = enderecoEquipamentoEmFoco = = enderecoEquipamentoLog & & chbFoco . Checked ;
GPSService . EnviarCoordenadasParaMapa ( ultimoLog . Coordenadas . Latitude , ultimoLog . Coordenadas . Longitude , ultimoLog . Coordenadas . Orientacao , foco , enderecoEquipamentoLog ) ;
2024-07-16 20:04:09 +00:00
}
2024-07-15 19:02:36 +00:00
}
private void cmbEquipamento_SelectedIndexChanged ( object sender , EventArgs e )
{
2025-02-03 19:54:39 +00:00
MomentoLog = MomentoLog . AddMinutes ( - 1 ) ;
2024-07-15 19:02:36 +00:00
AtualizarDadosTela ( ) ;
}
private void AtualizarDadosTela ( )
{
byte Endereco = Convert . ToByte ( cmbEquipamento . Text ) ;
2024-07-16 20:04:09 +00:00
2025-02-03 19:54:39 +00:00
if ( Endereco = = Variaveis . ConexaoLoRa . parametros . address & & false )
2024-07-15 19:02:36 +00:00
{
MomentoLog = DateTime . Now ;
txtLeitura . Text = MomentoLog . ToString ( "ddd HH:mm:ss" ) ;
2025-02-03 19:54:39 +00:00
txtOrientacao . Text = GPSService . UltimaLeitura . CursoVerdadeiro . ToString ( "0.00" ) ;
2024-07-15 19:02:36 +00:00
txtLatitude . Text = GPSService . UltimaLeitura . Latitude . ToString ( ) ;
txtLongitude . Text = GPSService . UltimaLeitura . Longitude . ToString ( ) ;
2024-10-03 18:21:30 +00:00
txtCodigoFalha . Text = "" ;
2024-07-15 19:02:36 +00:00
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 ] ;
2024-10-04 20:08:48 +00:00
bool Conectado = Equipamento . RecebidoEm . AddMilliseconds ( VariaveisEquipamento . TempoEntrePingsConexao ) > DateTime . Now ;
txtCarroConectado . Text = Conectado ? "Sim" : "Não" ;
2025-05-19 10:35:58 +00:00
if ( Equipamento . RecebidoEm > MomentoLog )
2024-07-15 19:02:36 +00:00
{
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 ( ) ;
2024-10-03 18:21:30 +00:00
txtCodigoFalha . Text = Equipamento . CodigoFalha . ToString ( ) ;
2024-07-15 19:02:36 +00:00
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" ;
}
}
2024-07-16 20:04:09 +00:00
2024-07-15 19:02:36 +00:00
}
2024-07-16 20:04:09 +00:00
private void btnCarregar_Click ( object sender , EventArgs e )
{
Mapa . btnCarregar_Click ( sender , e , "" , true ) ;
}
2024-07-19 18:04:31 +00:00
private void btnControle_KeyDown ( object sender , KeyEventArgs e )
{
2025-02-03 19:54:39 +00:00
EnviarComandoControleEquipamento ( e . KeyCode , ultimStatusoEmergencia ) ;
2024-07-19 18:04:31 +00:00
}
2024-07-16 20:04:09 +00:00
2024-07-19 18:04:31 +00:00
private void btnControle_KeyUp ( object sender , KeyEventArgs e )
{
2025-02-03 19:54:39 +00:00
EnviarComandoControleEquipamento ( Keys . Escape , ultimStatusoEmergencia ) ;
2024-07-19 18:04:31 +00:00
}
2024-07-16 20:04:09 +00:00
2025-02-03 19:54:39 +00:00
private void EnviarComandoControleEquipamento ( Keys Tecla , bool emergencia , T_Code Disp = T_Code . Vzo )
2024-07-19 18:04:31 +00:00
{
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 } ,
2024-10-04 20:08:48 +00:00
{ Keys . Space , Keys . Space } ,
2024-07-19 18:04:31 +00:00
} ;
2025-02-03 19:54:39 +00:00
if ( emergencia = = ultimStatusoEmergencia & & ( Tecla = = UltimoComandoEnviado | | ! DeParaTeclas . ContainsKey ( Tecla ) | | cmbEquipamento . SelectedIndex < 0 ) )
2024-07-19 18:04:31 +00:00
{
return ;
}
2024-10-04 20:08:48 +00:00
byte Endereco = Convert . ToByte ( cmbEquipamento . Text ) ;
if ( Endereco = = Variaveis . ConexaoLoRa . parametros . address )
{
return ;
}
UltimoComandoEnviado = Tecla ;
2024-07-19 18:04:31 +00:00
Keys [ ] TeclasDir = { Keys . A , Keys . D } ;
2024-10-04 20:08:48 +00:00
Keys [ ] TeclasMov = { Keys . W , Keys . S , Keys . Space } ;
2024-07-19 18:04:31 +00:00
OperacaoControleBaseModel Controle = new OperacaoControleBaseModel ( )
{
Momento = DateTime . Now ,
Tecla = DeParaTeclas [ Tecla ] ,
2024-10-03 18:21:30 +00:00
TipoMovimento = ( TipoMovimentoDirecional ) cmbTipoMovimento . SelectedIndex ,
PercentualVelocidadeSP = tkbVelocidadeSP . Value ,
AnguloMP = tkbAnguloMP . Value ,
VelocidadeMP = 50 ,
Dispositivo = Disp ,
2025-02-03 19:54:39 +00:00
Emergencia = emergencia
2024-07-19 18:04:31 +00:00
} ;
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 ) ;
2025-02-03 19:54:39 +00:00
ultimStatusoEmergencia = emergencia ;
2024-07-19 18:04:31 +00:00
}
private void btnParar_Click ( object sender , EventArgs e )
{
if ( btnParar . Text = = "Parar" )
{
2025-02-03 19:54:39 +00:00
EnviarComandoControleEquipamento ( Keys . Escape , true ) ;
2024-07-19 18:04:31 +00:00
btnParar . Text = "Retomar" ;
}
else
{
2025-02-03 19:54:39 +00:00
EnviarComandoControleEquipamento ( Keys . Escape , false ) ;
2024-07-19 18:04:31 +00:00
btnParar . Text = "Parar" ;
}
}
2024-10-03 18:21:30 +00:00
private void btnVerFalha_Click ( object sender , EventArgs e )
{
tvwFalhas . Visible = ! tvwFalhas . Visible ;
if ( ! tvwFalhas . Visible )
{
return ;
}
2025-02-03 19:54:39 +00:00
byte Endereco = 0x00 ;
byte . TryParse ( cmbEquipamento . Text , out Endereco ) ;
2024-10-03 18:21:30 +00:00
if ( Endereco > 0x00 & & VariaveisMonitoramento . EquipamentosConectados . Any ( x = > x . Key = = Endereco ) )
{
var Equipamento = VariaveisMonitoramento . EquipamentosConectados [ Endereco ] ;
OperacaoModel . PopularTreeView ( tvwFalhas , Equipamento . CodigoFalha ) ;
}
else
{
2025-02-03 19:54:39 +00:00
tvwFalhas . Visible = false ;
2024-10-03 18:21:30 +00:00
MessageBox . Show ( "Selecione um equipamento para ver o código de falhas" , "Equipamento não selecionado" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
2024-10-04 20:08:48 +00:00
private void tkbVelocidadeSP_ValueChanged ( object sender , EventArgs e )
{
double Percentual = tkbVelocidadeSP . Value ;
int RPM = Convert . ToInt32 ( ( Percentual / 100.0 ) * VariaveisEquipamento . RPM_Max_Roda ) ;
2024-12-06 20:19:59 +00:00
double Velocidade = FuncoesMatematicas . CalculaVelocidadeRPM ( RPM ) ;
2024-10-04 20:08:48 +00:00
lblVelocidadeSP . Text = $"Velocidade: {tkbVelocidadeSP.Value}% {Velocidade.ToString(" 0.00 ")} km/h" ;
}
private void tkbAnguloMP_ValueChanged ( object sender , EventArgs e )
{
lblAngulo . Text = $"Angulo: {tkbAnguloMP.Value}°" ;
}
2024-07-15 19:02:36 +00:00
}
}