agrobot_base/AgroBase/AgroBase/Forms/Sensoriamento/frmSenCamera.cs

190 lines
6.0 KiB
C#

using AgroBase.Models;
using AgroBase.Services;
using Microsoft.Web.WebView2.WinForms;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Windows.Forms;
namespace AgroBase.Forms.Sensoriamento
{
public partial class frmSenCamera : Form
{
private CameraService cameraService = new CameraService();
private WebView2 browser = null;
private Timer tmrLeitura;
private int MaxLeituras = 10;
private string VideoPorta = VariaveisPortas.CameraCaminho.ToString();
private string VideoUrl = "line_angle";
private bool MostrarDebug = true;
private string ArquivoLeitura = "leitura_angulo";
public frmSenCamera()
{
InitializeComponent();
}
private void frmSenCamera_Load(object sender, EventArgs e)
{
AtualizaListaCameras();
}
private void frmSenCamera_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
Variaveis.MqttServiceLocal.UnsubscribeAsync(cameraService._mqttTopico).GetAwaiter().GetResult();
cameraService.pythonProcess?.Kill();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void TmrLeitura_Tick(object sender, EventArgs e)
{
ListarObjetosDetectados();
}
private void btnAtualizar_Click(object sender, EventArgs e)
{
AtualizaListaCameras();
}
private void btnSalvar_Click(object sender, EventArgs e)
{
if (btnSalvar.Text == "Editar")
{
btnSalvar.Text = "Salvar";
}
else
{
btnSalvar.Text = "Editar";
cameraService.cameraIndex = cmbCameras.SelectedIndex;
}
btnIniciar.Enabled = btnSalvar.Text == "Editar" && cmbCameras.SelectedIndex > 0;
cmbCameras.Enabled = btnSalvar.Text == "Salvar";
}
private void btnIniciar_Click(object sender, EventArgs e)
{
/*cameraService.ArquivoLeitura = ArquivoLeitura;
cameraService.IniciarCamera(
PythonService.ScriptLineAngle,
new string[] {
MaxLeituras.ToString(),
VideoPorta,
VideoUrl,
ArquivoLeitura,
MostrarDebug ? "1" : "0",
VideoUrl
},
VideoPorta,
VideoUrl,
VideoUrl,
new System.Timers.Timer()
);
tmrLeitura = new Timer() { Interval = 200 };
tmrLeitura.Tick += TmrLeitura_Tick;
tmrLeitura.Start();*/
}
private void AtualizaListaCameras()
{
cameraService.cameraIndex = cmbCameras.SelectedIndex > 0 ? cmbCameras.SelectedIndex : 0;
cmbCameras.Items.Clear();
cmbCameras.Items.AddRange(CameraService.ObterListaCameras().Keys.ToArray());
if (cmbCameras.Items.Count == 1)
{
cameraService.cameraIndex = 0;
}
cmbCameras.SelectedIndex = cameraService.cameraIndex;
}
private void ListarObjetosDetectados()
{
if (browser == null)
{
browser = new WebView2();
browser.Source = new Uri(cameraService.URLcamera[0]);
this.pnlCamera.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
this.pnlLeituras.Controls.Clear();
int i = 0;
var MqttTopico = Variaveis.MqttServiceLocal.Topicos.FirstOrDefault(x => x.Topico == VideoUrl);
CameraCoordenadasFrameModel leitura = null;
var Mensagem = MqttTopico.Mensagens.LastOrDefault(x => x.Momento > cameraService.IniciadoEm);
if (Mensagem != null)
{
try
{
leitura = JsonConvert.DeserializeObject<CameraCoordenadasFrameModel>(Mensagem.Mensagem);
}
catch
{
}
}
/*foreach (var leitura in leituras)
{
GroupBox gpb = new GroupBox()
{
Name = "gpbLeitura" + i,
Size = new Size(120, 170),
Text = "Leitura " + (i + 1),
Location = new Point(3, 3 + (i * 190))
};
Label _lblMomento = new Label()
{
Name = "lblLeitura" + i + "Momento",
Size = new Size(100, 20),
Text = "Momento: " + leitura.timestamp,
Location = new Point(6, 20)
};
Label _lblAngulo = new Label()
{
Name = "lblLeitura" + i + "Angulo",
Size = new Size(100, 20),
Text = "Angulo: " + leitura.angulo.ToString("0.00") + "º",
Location = new Point(6, 40)
};
Label _lblEsquerda = new Label()
{
Name = "lblLeitura" + i + "Esquerda",
Size = new Size(100, 20),
Text = "Esquerda: " + leitura.distancia_esquerda,
Location = new Point(6, 60)
};
Label _lblDireita = new Label()
{
Name = "lblLeitura" + i + "Direita",
Size = new Size(100, 20),
Text = "Direita: " + leitura.distancia_direita,
Location = new Point(6, 80)
};
gpb.Controls.Add(_lblMomento);
gpb.Controls.Add(_lblAngulo);
gpb.Controls.Add(_lblEsquerda);
gpb.Controls.Add(_lblDireita);
pnlLeituras.Controls.Add(gpb);
i++;
}*/
}
}
}