152 lines
6.7 KiB
C#
152 lines
6.7 KiB
C#
using AgroBase.Models;
|
|
using System;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Forms
|
|
{
|
|
public partial class frmA05Config : Form
|
|
{
|
|
public UltrasonicA05Configuracoes ConfiguracaoSensores = null;
|
|
bool Carregado = false;
|
|
|
|
public frmA05Config(UltrasonicA05Configuracoes config)
|
|
{
|
|
ConfiguracaoSensores = config;
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void frmA05Config_Load(object sender, EventArgs e)
|
|
{
|
|
cmbPosicao.Items.Clear();
|
|
cmbPosicao.Items.AddRange(Enum.GetNames(typeof(PosicaoSensorUltrassonico)));
|
|
|
|
cmbConfiguracao.Items.Clear();
|
|
cmbConfiguracao.Items.AddRange(Enum.GetNames(typeof(ConfiguracaoSensorUltrassonico)));
|
|
cmbConfiguracao.SelectedIndex = (int)(ConfiguracaoSensores?.Configuracao ?? 0);
|
|
}
|
|
|
|
private void CarregarDadosConfiguracao(bool padrao)
|
|
{
|
|
if (padrao)
|
|
{
|
|
ConfiguracaoSensorUltrassonico Config = (ConfiguracaoSensorUltrassonico)cmbConfiguracao.SelectedIndex;
|
|
ConfiguracaoSensores = UltrasonicA05Helper.CarregarConfiguracaoPadrao(Config);
|
|
}
|
|
|
|
rtbDescricao.Text = UltrasonicA05Helper.DescricaoConfiguracoes[ConfiguracaoSensores.Configuracao];
|
|
|
|
cmbSensor.Items.Clear();
|
|
cmbSensor.Items.AddRange(ConfiguracaoSensores.Sensores.Select(x => "Sensor " + x.ID.ToString("00")).ToArray());
|
|
cmbSensor.SelectedIndex = 0;
|
|
|
|
cmbSensor_SelectedIndexChanged(cmbSensor, null);
|
|
|
|
Carregado = true;
|
|
}
|
|
|
|
private void cmbConfiguracao_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
bool carregar = !Carregado || (Carregado && MessageBox.Show($"Deseja carregar a configuração {((ConfiguracaoSensorUltrassonico)cmbConfiguracao.SelectedIndex).ToString()}? As alterações atuais serão perdidas!", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
|
|
|
|
if (carregar)
|
|
{
|
|
CarregarDadosConfiguracao(Carregado);
|
|
}
|
|
}
|
|
|
|
private void cmbSensor_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
UltrasonicA05ConfiguracoesSensor Sensor = ConfiguracaoSensores.Sensores[cmbSensor.SelectedIndex];
|
|
cmbPosicao.SelectedIndex = (int)Sensor.Posicao;
|
|
chbLimiteMinimo.Checked = Sensor.LimiteMinimo;
|
|
txtLimiteMinimo.Text = Sensor.LimiteMinimoVal.ToString();
|
|
txtLimiteMinimo.Enabled = Sensor.LimiteMinimo;
|
|
txtMargemMinimo.Text = Sensor.MargemMinimo.ToString();
|
|
txtMargemMinimo.Enabled = Sensor.LimiteMinimo;
|
|
chbLimiteMaximo.Checked = Sensor.LimiteMaximo;
|
|
txtLimiteMaximo.Text = Sensor.LimiteMaximoVal.ToString();
|
|
txtLimiteMaximo.Enabled = Sensor.LimiteMaximo;
|
|
txtMargemMaximo.Text = Sensor.MargemMaximo.ToString();
|
|
txtMargemMaximo.Enabled = Sensor.LimiteMaximo;
|
|
rdbDesvio.Checked = Sensor.Desvio;
|
|
rdbParada.Checked = Sensor.Parada;
|
|
rdbSemAcao.Checked = !Sensor.Desvio && !Sensor.Parada;
|
|
}
|
|
|
|
private void btnSalvar_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void cmbPosicao_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
UltrasonicA05ConfiguracoesSensor Sensor = ConfiguracaoSensores.Sensores[cmbSensor.SelectedIndex];
|
|
UltrasonicA05ConfiguracoesSensor SensorPosicao = ConfiguracaoSensores.Sensores.FirstOrDefault(x => x.Posicao != PosicaoSensorUltrassonico.NaoConectado && (int)x.Posicao == cmbPosicao.SelectedIndex);
|
|
if (SensorPosicao != null && Sensor.ID != SensorPosicao.ID)
|
|
{
|
|
MessageBox.Show($"O sensor {SensorPosicao.ID.ToString("00")} já está configurado na posição {SensorPosicao.Posicao.ToString()}!");
|
|
cmbPosicao.SelectedIndex = (int)Sensor.Posicao;
|
|
}
|
|
else
|
|
{
|
|
foreach (var pnl in pnlImagem.Controls.OfType<Panel>().OrderBy(x => x.Name))
|
|
{
|
|
int id = int.Parse(pnl.Name.Replace("pnlP_", ""));
|
|
var _Sensor = ConfiguracaoSensores.Sensores.FirstOrDefault(x => (int)x.Posicao == id);
|
|
|
|
pnl.BackColor = _Sensor == null ? Color.Gray : Color.Red;
|
|
pnl.Tag = _Sensor?.ID.ToString("00"); // Armazena o ID no Tag do Panel
|
|
pnl.Paint += (s, _e) =>
|
|
{
|
|
// Pega o ID armazenado no Tag, se houver
|
|
string sensorID = pnl.Tag?.ToString();
|
|
if (!string.IsNullOrEmpty(sensorID))
|
|
{
|
|
using (Font font = new Font("Arial", 8, FontStyle.Bold))
|
|
{
|
|
SizeF textSize = _e.Graphics.MeasureString(sensorID, font);
|
|
float textX = (pnl.Width - textSize.Width) / 2;
|
|
float textY = (pnl.Height - textSize.Height) / 2;
|
|
|
|
_e.Graphics.DrawString(sensorID, font, Brushes.White, textX, textY);
|
|
}
|
|
}
|
|
};
|
|
|
|
pnl.Invalidate(); // Redesenha o Panel para aplicar as mudanças
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnSalvarSensor_Click(object sender, EventArgs e)
|
|
{
|
|
UltrasonicA05ConfiguracoesSensor Sensor = ConfiguracaoSensores.Sensores[cmbSensor.SelectedIndex];
|
|
Sensor.Posicao = (PosicaoSensorUltrassonico)cmbPosicao.SelectedIndex;
|
|
Sensor.LimiteMinimo = chbLimiteMinimo.Checked;
|
|
Sensor.LimiteMinimoVal = double.Parse(txtLimiteMinimo.Text);
|
|
Sensor.MargemMinimo = double.Parse(txtMargemMinimo.Text);
|
|
Sensor.LimiteMaximo = chbLimiteMaximo.Checked;
|
|
Sensor.LimiteMaximoVal = double.Parse(txtLimiteMaximo.Text);
|
|
Sensor.MargemMaximo = double.Parse(txtMargemMaximo.Text);
|
|
Sensor.Desvio = rdbDesvio.Checked;
|
|
Sensor.Parada = rdbParada.Checked;
|
|
}
|
|
|
|
private void chbLimiteMinimo_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
txtLimiteMinimo.Enabled = ((CheckBox)sender).Checked;
|
|
txtMargemMinimo.Enabled = ((CheckBox)sender).Checked;
|
|
}
|
|
|
|
private void chbLimiteMaximo_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
txtLimiteMaximo.Enabled = ((CheckBox)sender).Checked;
|
|
txtMargemMaximo.Enabled = ((CheckBox)sender).Checked;
|
|
}
|
|
}
|
|
}
|