259 lines
10 KiB
C#
259 lines
10 KiB
C#
using AgroBase.Models;
|
|
using AgroBase.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Forms
|
|
{
|
|
public partial class frmLoraConfig : Form
|
|
{
|
|
LoRaParametrosModel Parametros = null;
|
|
AsyncTaskTimerModel tmrLeitura;
|
|
|
|
public frmLoraConfig()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async void frmLoraConfig_Load(object sender, EventArgs e)
|
|
{
|
|
tmrLeitura = new AsyncTaskTimerModel("tmrLeitura", tmrLeitura_Tick, 1000, this);
|
|
tmrLeitura.Start();
|
|
|
|
//Parametros = loraService.CarregarParametros();
|
|
if (LoRaService.Iniciado)
|
|
{
|
|
await AtualizarParametros();
|
|
}
|
|
}
|
|
|
|
private async Task AtualizarParametros()
|
|
{
|
|
(bool sucesso, LoRaParametrosModel par) = (false, null);
|
|
if (Variaveis.IsAgroMonitor)
|
|
{
|
|
(sucesso, par) = await Variaveis.ConexaoLoRa.RequisitarParametrosModuloAsync();
|
|
}
|
|
else
|
|
{
|
|
(sucesso, par) = await Variaveis.LoraService?.RequisitarParametrosModulo();
|
|
}
|
|
|
|
if (!sucesso)
|
|
{
|
|
MessageBox.Show("Erro ao requisitar parâmetros! Verifique o modo do módulo LoRa!");
|
|
}
|
|
|
|
Parametros = par.Clone();
|
|
|
|
txtEndereco.Text = ((int)Parametros.address).ToString();
|
|
txtCanal.Text = ((int)Parametros.channel).ToString();
|
|
|
|
cmbBaudRate.Items.Clear();
|
|
cmbBaudRate.Items.AddRange(LoRaService.CodigosBaudRates.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbBaudRate.SelectedItem = LoRaService.CodigosBaudRates.FirstOrDefault(x => x.Value == Parametros.baudRate).Key.ToString();
|
|
cmbTransmissao.Items.Clear();
|
|
cmbTransmissao.Items.AddRange(LoRaService.CodigosTipoTransmissao.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbTransmissao.SelectedItem = LoRaService.CodigosTipoTransmissao.FirstOrDefault(x => x.Value == Parametros.tranMode).Key.ToString();
|
|
cmbAirRate.Items.Clear();
|
|
cmbAirRate.Items.AddRange(LoRaService.CodigosAirRates.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbAirRate.SelectedItem = LoRaService.CodigosAirRates.FirstOrDefault(x => x.Value == Parametros.airRate).Key.ToString();
|
|
cmbPacketSize.Items.Clear();
|
|
cmbPacketSize.Items.AddRange(LoRaService.CodigosPacketSizes.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbPacketSize.SelectedItem = LoRaService.CodigosPacketSizes.FirstOrDefault(x => x.Value == Parametros.packetSize).Key.ToString();
|
|
cmbWorCycle.Items.Clear();
|
|
cmbWorCycle.Items.AddRange(LoRaService.CodigosWorCycles.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbWorCycle.SelectedItem = LoRaService.CodigosWorCycles.FirstOrDefault(x => x.Value == Parametros.worCycle).Key.ToString();
|
|
cmbPower.Items.Clear();
|
|
cmbPower.Items.AddRange(LoRaService.CodigosPowers.Keys.Select(x => x.ToString()).ToArray());
|
|
cmbPower.SelectedItem = LoRaService.CodigosPowers.FirstOrDefault(x => x.Value == Parametros.power).Key.ToString();
|
|
|
|
AtualizarDadosTela();
|
|
}
|
|
|
|
private async Task tmrLeitura_Tick()
|
|
{
|
|
if (LoRaService.Iniciado)
|
|
{
|
|
var MensagemDisponivel = Variaveis.ConexaoLoRa.MensagemPendente;
|
|
if (MensagemDisponivel != null)
|
|
{
|
|
AtualizarConsole(MensagemDisponivel.Remetente, MensagemDisponivel.Destinatario, MensagemDisponivel.Mensagem);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AtualizarDadosTela()
|
|
{
|
|
bool loraConectado = LoRaService.Iniciado;
|
|
|
|
cmbPorta.Items.Clear();
|
|
cmbPorta.Items.AddRange(SerialPort.GetPortNames().ToArray());
|
|
if (Parametros.PortaCOM != "")
|
|
{
|
|
cmbPorta.SelectedItem = Parametros.PortaCOM;
|
|
}
|
|
|
|
gpbParametros.Enabled = loraConectado;
|
|
gpbTestar.Enabled = loraConectado;
|
|
btnConectar.Text = loraConectado ? "Desconectar" : "Conectar";
|
|
btnSalvar.Text = loraConectado ? "Editar" : "Salvar";
|
|
btnConectar.Enabled = cmbPorta.SelectedIndex > -1;
|
|
|
|
btnLerParametros.Enabled = loraConectado;
|
|
btnGravarParametros.Enabled = loraConectado;
|
|
|
|
btnConectar.Enabled = false;
|
|
btnSalvar.Enabled = false;
|
|
}
|
|
|
|
private async void btnLerParametros_Click(object sender, EventArgs e)
|
|
{
|
|
if (LoRaService.Iniciado)
|
|
{
|
|
await AtualizarParametros();
|
|
}
|
|
}
|
|
|
|
private async void btnGravarParametros_Click(object sender, EventArgs e)
|
|
{
|
|
if (LoRaService.Iniciado)
|
|
{
|
|
Parametros.baudRate = LoRaService.CodigosBaudRates.FirstOrDefault(x => x.Key.ToString() == cmbBaudRate.Text).Value;
|
|
Parametros.tranMode = LoRaService.CodigosTipoTransmissao.FirstOrDefault(x => x.Key.ToString() == cmbTransmissao.Text).Value;
|
|
Parametros.address = Convert.ToByte(txtEndereco.Text);
|
|
Parametros.channel = Convert.ToByte(txtCanal.Text);
|
|
Parametros.airRate = LoRaService.CodigosAirRates.FirstOrDefault(x => x.Key.ToString() == cmbAirRate.Text).Value;
|
|
Parametros.packetSize = LoRaService.CodigosPacketSizes.FirstOrDefault(x => x.Key.ToString() == cmbPacketSize.Text).Value;
|
|
Parametros.worCycle = LoRaService.CodigosWorCycles.FirstOrDefault(x => x.Key.ToString() == cmbWorCycle.Text).Value;
|
|
Parametros.power = LoRaService.CodigosPowers.FirstOrDefault(x => x.Key.ToString() == cmbPower.Text).Value;
|
|
|
|
await ConfigurarModulo();
|
|
}
|
|
}
|
|
|
|
private async Task ConfigurarModulo()
|
|
{
|
|
(bool Sucesso, LoRaParametrosModel par) = (false, null);
|
|
if (Variaveis.IsAgroMonitor)
|
|
{
|
|
(Sucesso, par) = await Variaveis.ConexaoLoRa.ConfigurarModuloAsync(Parametros);
|
|
}
|
|
else
|
|
{
|
|
(Sucesso, par) = await Variaveis.LoraService?.RedefinirParametros(Parametros);
|
|
}
|
|
if (Sucesso)
|
|
{
|
|
MessageBox.Show("Módulo configurado com sucesso!");
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Erro ao configurar módulo!");
|
|
}
|
|
await AtualizarParametros();
|
|
}
|
|
|
|
private void btnEnviar_Click(object sender, EventArgs e)
|
|
{
|
|
if (LoRaService.Iniciado)
|
|
{
|
|
bool valido = byte.TryParse(txtEnderecoDestino.Text, out byte destinatario);
|
|
if (!valido)
|
|
{
|
|
MessageBox.Show("Preencha o destinatario corretamente!");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// Remove espaços extras e divide por espaço
|
|
string[] partes = txtMensagem.Text.Trim().Split(' ');
|
|
|
|
// Converte cada string para byte
|
|
List<byte> payload = new List<byte>();
|
|
payload.AddRange(LoRaSerializer.CanPrefix(LoRaEspService.DadosLoRaParse.DadosLivre));
|
|
foreach (var parte in partes)
|
|
{
|
|
if (byte.TryParse(parte, System.Globalization.NumberStyles.HexNumber, null, out byte valor))
|
|
{
|
|
if (payload.Count < 8)
|
|
payload.Add(valor);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show($"Valor inválido: '{parte}'", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (Variaveis.IsAgroMonitor)
|
|
{
|
|
LoRaService.EnviarDadosLoRa(payload.ToArray(), destinatario);
|
|
}
|
|
else
|
|
{
|
|
Variaveis.LoraService.EnviarDadosLoRaViaCAN(payload.ToArray());
|
|
}
|
|
|
|
MessageBox.Show("Dados enviados com sucesso: " + FuncoesGlobais.ConverterComandoBytesParaTexto(payload.ToArray()));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Erro ao enviar: {ex.Message}", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
AtualizarConsole(Parametros.address, destinatario, txtMensagem.Text);
|
|
}
|
|
}
|
|
|
|
private void txtMensagem_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
btnEnviar_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
private void AtualizarConsole(byte Rementente, byte Destinatario, string Mensagem)
|
|
{
|
|
richTextBox1.Text += "Em " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") +
|
|
" de: " + Convert.ToInt32(Rementente.ToString("X")).ToString("00") +
|
|
" para " + Convert.ToInt32(Destinatario.ToString("X")).ToString("00") +
|
|
": " + Mensagem + Environment.NewLine;
|
|
txtMensagem.Text = "";
|
|
txtMensagem.Focus();
|
|
}
|
|
|
|
private void btnConectar_Click(object sender, EventArgs e)
|
|
{
|
|
//LoRaService.AtualizarPortaCOM(new SerialPort() { PortName = Parametros.PortaCOM, BaudRate = 9600 });
|
|
AtualizarDadosTela();
|
|
}
|
|
|
|
private void btnSalvar_Click(object sender, EventArgs e)
|
|
{
|
|
//loraService.SalvarParametros();
|
|
AtualizarDadosTela();
|
|
}
|
|
|
|
private void cmbPorta_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (cmbPorta.Text != Parametros.PortaCOM)
|
|
{
|
|
btnConectar_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
private void cmbPorta_Click(object sender, EventArgs e)
|
|
{
|
|
AtualizarDadosTela();
|
|
}
|
|
}
|
|
}
|