agrobot_base/AgroBase/AgroBase/Models/MapasModel.cs

145 lines
4.9 KiB
C#

using AgroBase.Properties;
using AgroBase.Services;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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; } = new PictureBox();
public Label lblMapa { get; set; } = new Label();
public MapasService mapaService { get; set; } = new MapasService();
public List<List<GPSModel>> Trajetoria { get; set; } = new List<List<GPSModel>>();
public List<string> RuasPercorrer { get; set; } = new List<string>();
public int IdxRuaAtual { get; set; } = 0;
public double DistanciaTotal { get; set; } = 0;
public void btnCarregar_Click(object sender, EventArgs e)
{
mapaService.CarregarMapa(MapasVariaveisModel.NomeArquivoMapaInterativo, MapasVariaveisModel.NomeArquivoMapaInterativoSaida);
picLoading.Dock = DockStyle.Fill;
picLoading.BackColor = System.Drawing.Color.Transparent;
picLoading.Image = Resources.loading;
picLoading.Name = "picLoading";
picLoading.SizeMode = PictureBoxSizeMode.Zoom;
picLoading.Visible = true;
pnlMapa.Controls.Add(picLoading);
Timer tmr = new Timer() { Interval = 500 };
tmr.Tick += Tmr_Tick;
tmr.Start();
}
public void CarregarMapaGPS()
{
mapaService.CarregarMapaGPS(MapasVariaveisModel.NomeArquivoMapaGPS);
Timer tmr = new Timer() { Interval = 500 };
tmr.Tick += Tmr_Tick;
tmr.Start();
}
private void Tmr_Tick(object sender, EventArgs e)
{
if (mapaService.pythonProcess.HasExited)
{
((Timer)sender).Stop();
picLoading.Visible = false;
if (mapaService.NomeArquivoMapa == MapasVariaveisModel.NomeArquivoMapaInterativo)
{
mapaService.CarregarDadosMapa();
lblMapa.Text = "Mapa carregado: " + mapaService.NomeArquivos;
PopularTrajetoria();
}
IniciarProcessamento();
}
}
public void PopularTrajetoria()
{
if (mapaService.DadosMapa != null)
{
DistanciaTotal = 0;
var IDs = RuasPercorrer.Any() ? RuasPercorrer : mapaService.DadosMapa.features.Select(x => x.geometry.id).ToList();
Trajetoria = new List<List<GPSModel>>();
foreach (var rua in mapaService.DadosMapa.features.Where(x => IDs.Contains(x.geometry.id)))
{
Trajetoria.Add(new List<GPSModel>());
foreach (var ponto in rua.geometry.coordinates)
{
GPSModel item = new GPSModel()
{
Longitude = ponto[0],
Latitude = ponto[1],
};
Trajetoria[Trajetoria.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);
}
}
}
}
}
private void IniciarProcessamento()
{
APIService.Inicializar();
GPSService.IniciarRecepcaoDados();
chromiumWebBrowser = new ChromiumWebBrowser(mapaService.MapaUrl);
pnlMapa.Controls.Add(chromiumWebBrowser);
chromiumWebBrowser.Dock = DockStyle.Fill;
}
public void PararProcessamento()
{
APIService.Parar();
GPSService.PararRecepcaoDados();
}
}
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; }
}
}