247 lines
9.1 KiB
C#
247 lines
9.1 KiB
C#
using AgroBase.Properties;
|
|
using AgroBase.Services;
|
|
using CefSharp.WinForms;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Models
|
|
{
|
|
public class MapasModel
|
|
{
|
|
public ChromiumWebBrowser chromiumWebBrowser { get; set; }
|
|
public Panel pnlMapa { get; set; }
|
|
private PictureBox picLoading { get; set; }
|
|
public Label lblMapa { get; set; }
|
|
public MapasService mapaService { get; set; } = new MapasService();
|
|
public List<List<GPSModel>> TrajetoriaMapa { get; set; } = new List<List<GPSModel>>();
|
|
public List<string> RuasPercorrer { get; set; } = new List<string>();
|
|
|
|
private AsyncTaskTimerModel tmrCarregamento;
|
|
|
|
|
|
public void btnCarregar_Click(object sender, EventArgs e, string caminhoArquivo = "", bool monitoramento = false)
|
|
{
|
|
string script = monitoramento ? PythonService.ScriptMapFollower : PythonService.ScriptMapConverter;
|
|
mapaService.CarregarMapa(script, MapasVariaveisModel.NomeArquivoMapaInterativo, MapasVariaveisModel.NomeArquivoMapaInterativoSaida, caminhoArquivo);
|
|
|
|
if (mapaService.MapaUrl != "")
|
|
{
|
|
if (picLoading == null)
|
|
{
|
|
picLoading = new PictureBox();
|
|
}
|
|
picLoading.Dock = DockStyle.Fill;
|
|
picLoading.BackColor = System.Drawing.Color.Transparent;
|
|
picLoading.Image = Resources.loading;
|
|
picLoading.Name = "picLoading";
|
|
picLoading.SizeMode = PictureBoxSizeMode.Zoom;
|
|
picLoading.Visible = true;
|
|
if (pnlMapa != null)
|
|
{
|
|
pnlMapa.Controls.Add(picLoading);
|
|
}
|
|
tmrCarregamento?.Dispose();
|
|
tmrCarregamento = new AsyncTaskTimerModel("tmrCarregamento", tmrCarregamento_Tick, 500, pnlMapa);
|
|
tmrCarregamento.Start();
|
|
}
|
|
}
|
|
|
|
public void CarregarMapaGPS()
|
|
{
|
|
if (picLoading == null)
|
|
{
|
|
picLoading = new PictureBox()
|
|
{
|
|
SizeMode = PictureBoxSizeMode.Zoom,
|
|
Location = new System.Drawing.Point(0, 0),
|
|
Size = new System.Drawing.Size(pnlMapa.Width, pnlMapa.Height)
|
|
};
|
|
pnlMapa.Controls.Add(picLoading);
|
|
}
|
|
mapaService.CarregarMapaGPS(MapasVariaveisModel.NomeArquivoMapaGPS);
|
|
|
|
tmrCarregamento?.Dispose();
|
|
tmrCarregamento = new AsyncTaskTimerModel("tmrCarregamento", tmrCarregamento_Tick, 500, pnlMapa);
|
|
tmrCarregamento.Start();
|
|
}
|
|
|
|
private async Task tmrCarregamento_Tick()
|
|
{
|
|
try
|
|
{
|
|
lock (mapaService.pythonProcess)
|
|
{
|
|
if (mapaService.pythonProcess == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (mapaService.pythonProcess != null && !mapaService.pythonProcess.HasExited)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Processo finalizado ou não associado
|
|
picLoading.Visible = false;
|
|
mapaService.Carregado = true;
|
|
|
|
if (mapaService.NomeArquivoMapa == MapasVariaveisModel.NomeArquivoMapaInterativo)
|
|
{
|
|
mapaService.CarregarDadosMapa();
|
|
if (lblMapa != null)
|
|
{
|
|
lblMapa.Text = "Mapa carregado: " + mapaService.NomeArquivos;
|
|
}
|
|
|
|
PopularTrajetoriaMapa();
|
|
}
|
|
|
|
IniciarProcessamento();
|
|
|
|
lock (mapaService.pythonProcess)
|
|
{
|
|
mapaService.pythonProcess.Dispose();
|
|
mapaService.pythonProcess = null;
|
|
}
|
|
|
|
tmrCarregamento.Dispose();
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
Console.WriteLine($"Erro ao verificar o estado do processo: {ex.Message}");
|
|
picLoading.Visible = false;
|
|
tmrCarregamento.Dispose();
|
|
}
|
|
}
|
|
|
|
public void PopularTrajetoriaMapa()
|
|
{
|
|
if (mapaService.DadosMapa != null)
|
|
{
|
|
double OrientacaoRuas = -999;
|
|
//DistanciaTotal = 0;
|
|
/*if (!RuasPercorrer.Any())
|
|
{
|
|
RuasPercorrer = mapaService.DadosMapa.features.Select(x => x.geometry.id).ToList();
|
|
}*/
|
|
TrajetoriaMapa = new List<List<GPSModel>>();
|
|
|
|
foreach (var ID in RuasPercorrer)
|
|
{
|
|
var rua = mapaService.DadosMapa.features.FirstOrDefault(x => ID == x.geometry.id);
|
|
if (rua != null)
|
|
{
|
|
TrajetoriaMapa.Add(new List<GPSModel>());
|
|
foreach (var ponto in rua.geometry.coordinates)
|
|
{
|
|
GPSModel item = new GPSModel()
|
|
{
|
|
Longitude = ponto[0],
|
|
Latitude = ponto[1],
|
|
};
|
|
TrajetoriaMapa[TrajetoriaMapa.Count - 1].Add(item);
|
|
/*if (Trajetoria[Trajetoria.Count - 1].Count > 1)
|
|
{
|
|
DistanciaTotal += GPSService.DistanciaEntrePontos(Trajetoria[Trajetoria.Count - 1][Trajetoria[Trajetoria.Count - 1].Count - 2], item);
|
|
}*/
|
|
}
|
|
|
|
var Rua = TrajetoriaMapa[TrajetoriaMapa.Count - 1];
|
|
if (OrientacaoRuas == -999)
|
|
{
|
|
OrientacaoRuas = GPSUtils.CalcularOrientacao(Rua[Rua.Count - 1], Rua[Rua.Count - 2]);
|
|
}
|
|
else
|
|
{
|
|
double MargemErroAngulo = 45;
|
|
double orientacaoRua = GPSUtils.CalcularOrientacao(Rua[Rua.Count - 1], Rua[Rua.Count - 2]);
|
|
double anguloOposto = (orientacaoRua + 180) % 360;
|
|
if ((anguloOposto + MargemErroAngulo) >= OrientacaoRuas && (anguloOposto - MargemErroAngulo) <= OrientacaoRuas)
|
|
{
|
|
Rua.Reverse();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (TrajetoriaMapa.Any())
|
|
{
|
|
Variaveis.OperacaoEmAndamento.Trajetoria = new TrajetoriaMapaOperacaoModel(TrajetoriaMapa);
|
|
Variaveis.OperacaoEmAndamento.Trajetoria.ProjetarTrajetoriaFixa();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void IniciarProcessamento()
|
|
{
|
|
//APIService.Inicializar();
|
|
|
|
chromiumWebBrowser = new ChromiumWebBrowser(mapaService.MapaUrl);
|
|
pnlMapa.Controls.Add(chromiumWebBrowser);
|
|
chromiumWebBrowser.Dock = DockStyle.Fill;
|
|
}
|
|
|
|
public void AtualizarRuasSelecionadas()
|
|
{
|
|
List<string> ids = new List<string>();
|
|
bool idsMensagem = false;
|
|
var Topico = Variaveis.MqttService.Topicos.FirstOrDefault(x => x.Topico == MapasVariaveisModel.TopicoSelecaoRuasMapa);
|
|
var Mensagem = Topico.Mensagens.LastOrDefault();
|
|
if (Mensagem != null && !Mensagem.Mensagem.Contains("[]"))
|
|
{
|
|
Mensagem.Mensagem = Mensagem.Mensagem.Replace("\n", "");
|
|
Mensagem.Mensagem = Mensagem.Mensagem.Replace("\"", "");
|
|
if (!string.IsNullOrEmpty(Mensagem.Mensagem))
|
|
{
|
|
ids = JsonConvert.DeserializeObject<int[]>(Mensagem.Mensagem).Select(x => x.ToString()).ToList();
|
|
idsMensagem = true;
|
|
}
|
|
}
|
|
if (idsMensagem && ids.Count != RuasPercorrer.Count)
|
|
{
|
|
RuasPercorrer = ids;
|
|
PopularTrajetoriaMapa();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class MapaFeatureCollectionModel
|
|
{
|
|
public string type { get; set; }
|
|
public List<MapaFeatureModel> features { get; set; }
|
|
}
|
|
|
|
public class MapaFeatureModel
|
|
{
|
|
public string type { get; set; }
|
|
public MapaFeaturePropertiesModel properties { get; set; }
|
|
public MapaFeatureGeometryModel geometry { get; set; }
|
|
}
|
|
|
|
public class MapaFeaturePropertiesModel
|
|
{
|
|
public string Id { get; set; }
|
|
public string Name { get; set; }
|
|
public double Length { get; set; }
|
|
public double Dist1 { get; set; }
|
|
public double Dist2 { get; set; }
|
|
}
|
|
|
|
public class MapaFeatureGeometryModel
|
|
{
|
|
public string id { get; set; }
|
|
public string type { get; set; }
|
|
public List<List<double>> coordinates { get; set; }
|
|
}
|
|
|
|
|
|
|
|
}
|