824 lines
36 KiB
C#
824 lines
36 KiB
C#
using AgroBase.Forms;
|
|
using AgroBase.Services;
|
|
using Emgu.CV.ML;
|
|
using Emgu.CV.Structure;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static AgroBase.Models.Enuns;
|
|
|
|
namespace AgroBase.Models.Modules
|
|
{
|
|
public class SensoriamentoModel : DispositivoBaseModel
|
|
{
|
|
private bool _MensagemConexaoRecebida { get; set; } = false;
|
|
public bool MensagemConexaoRecebida(bool Reset = false)
|
|
{
|
|
if (Reset)
|
|
{
|
|
_MensagemConexaoRecebida = false;
|
|
}
|
|
return _MensagemConexaoRecebida;
|
|
}
|
|
|
|
public static int _TaxaAmostragem { get; set; } = 500;
|
|
public bool Conectado { get; set; } = false;
|
|
public static int LogsManter { get; set; } = 60;
|
|
public static PinoutDataModel Pinout { get; set; }
|
|
public static bool ExpansorGPIO { get; set; } = false;
|
|
public static List<FuncoesPinout> Funcoes { get; set; } = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.SDA,
|
|
FuncoesPinout.SCL,
|
|
FuncoesPinout.Geral,
|
|
};
|
|
public List<SensorModel> Sensores { get; set; }
|
|
public List<SensorSinaleiroModel> Sinaleiros { get; set; }
|
|
public string CameraID_Caminho { get; set; }
|
|
public Bitmap ultimoFrameCaminho { get; set; }
|
|
public string CameraID_SoloE { get; set; }
|
|
public Bitmap ultimoFrameSoloE { get; set; }
|
|
public string CameraID_SoloD { get; set; }
|
|
public Bitmap ultimoFrameSoloD { get; set; }
|
|
public double BateriaCargaConsumida { get; set; }
|
|
|
|
|
|
public static SensoriamentoModel CarregarParametrosIniciais()
|
|
{
|
|
return new SensoriamentoModel()
|
|
{
|
|
Sinaleiros = new List<SensorSinaleiroModel>()
|
|
{
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDMN",
|
|
Descricao = "Sinaleiro indicação de modo Manual ativo",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = Variaveis.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDAT",
|
|
Descricao = "Sinaleiro indicação de modo Automático ativo",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = Variaveis.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDOP",
|
|
Descricao = "Sinaleiro indicação do status da operação ativa",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = Variaveis.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
},
|
|
Sensores = new List<SensorModel>()
|
|
{
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVET",
|
|
Descricao = "Temperatura Motor Esquerdo Trás",
|
|
Prioridade = 0,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
DelayAmostragem = 200,
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "V offset", "-1.038" },
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVEF",
|
|
Descricao = "Temperatura Motor Esquerdo Frente",
|
|
Prioridade = 0,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
DelayAmostragem = 200,
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "V offset", "-1.038" },
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVDT",
|
|
Descricao = "Temperatura Motor Direito Trás",
|
|
Prioridade = 0,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
DelayAmostragem = 200,
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "V offset", "-1.038" },
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVDF",
|
|
Descricao = "Temperatura Motor Direito Frente",
|
|
Prioridade = 0,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
DelayAmostragem = 200,
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "V offset", "-1.038" },
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB3V",
|
|
Prioridade = 10,
|
|
Descricao = "Corrente no Barramento 3V",
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "A",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "Endereço I2C", "0x44" },
|
|
new string[] { "CI", "219" }
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB5V",
|
|
Prioridade = 10,
|
|
Descricao = "Corrente no Barramento 5V",
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "A",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "Endereço I2C", "0x41" },
|
|
new string[] { "CI", "219" }
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB7V2",
|
|
Prioridade = 10,
|
|
Descricao = "Corrente no Barramento 7,2V",
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "A",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "Endereço I2C", "0x40" },
|
|
new string[] { "CI", "219" }
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB12V",
|
|
Prioridade = 10,
|
|
Descricao = "Corrente no Barramento 12V",
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "A",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "Endereço I2C", "0x43" },
|
|
new string[] { "CI", "226" }
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TBAT",
|
|
Descricao = "Temperatura da bateria",
|
|
Prioridade = 5,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "V offset", "0.0000" },
|
|
},
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "SUESQ",
|
|
Descricao = "Sensor Ultrassom Esquerdo",
|
|
Prioridade = 2,
|
|
Componente = S_Code.sULT,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Echo,
|
|
FuncoesPinout.Trigger,
|
|
},
|
|
DelayAmostragem = 1000,
|
|
UnidadeMedida = "cm",
|
|
Parametros = new List<string[]>(),
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "SUDIR",
|
|
Descricao = "Sensor Ultrassom Direito",
|
|
Prioridade = 2,
|
|
Componente = S_Code.sULT,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Echo,
|
|
FuncoesPinout.Trigger,
|
|
},
|
|
DelayAmostragem = 1000,
|
|
UnidadeMedida = "cm",
|
|
Parametros = new List<string[]>(),
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "SMAG",
|
|
Descricao = "Sensor Magnetômetro",
|
|
Prioridade = 15,
|
|
Componente = S_Code.sMAG,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
DelayAmostragem = 500,
|
|
UnidadeMedida = "º",
|
|
Parametros = new List<string[]>(),
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "SNR_F",
|
|
Descricao = "Sensor Sonar Frontal",
|
|
Prioridade = 20,
|
|
Componente = S_Code.sSNR,
|
|
Aferir = true,
|
|
Inicializado = false,
|
|
Testando = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Echo,
|
|
FuncoesPinout.Trigger,
|
|
FuncoesPinout.ServoX,
|
|
FuncoesPinout.ServoY,
|
|
},
|
|
DelayAmostragem = 1000,
|
|
UnidadeMedida = "cm/º",
|
|
Parametros = new List<string[]>()
|
|
{
|
|
new string[] { "Angulo X Acrescentar", "5" },
|
|
new string[] { "Angulo Y Acrescentar", "30" },
|
|
},
|
|
LimiteMinimo = 40,
|
|
LimiteMaximo = 9999,
|
|
},
|
|
|
|
},
|
|
};
|
|
}
|
|
|
|
public List<string> ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
List<string> Config = Sensores.Where(x => x.Aferir).Select(x => x.ProtocoloConfiguracaoSensor(Pinout, Conectar)).ToList();
|
|
Config.AddRange(Sinaleiros.Select(x => x.ProtocoloConfiguracaoSinaleiro(Pinout, Conectar)).ToList());
|
|
string ProtocoloMOD =
|
|
((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
"MD" + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
_TaxaAmostragem.ToString() + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(Pinout, Funcoes);
|
|
Config.Add(ProtocoloMOD);
|
|
return Config;
|
|
}
|
|
|
|
public void RecebeProtocoloSerial(string Protocolo)
|
|
{
|
|
// #ET;00000;00000;00000;000
|
|
if (!string.IsNullOrEmpty(Protocolo) && Protocolo[0].ToString() == SerialService.BeginSensor)
|
|
{
|
|
string[] Dados = Protocolo.Replace(SerialService.BeginSensor, "").Split(SerialService.EndLine[0])[0].Split(';');
|
|
int cod = 0;
|
|
int.TryParse(Dados[0], out cod);
|
|
if (cod == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
S_Code TipoSensor = (S_Code)(cod);
|
|
SensorModel Sensor = Sensores.FirstOrDefault(x => x.ID == Dados[1]);
|
|
if (Sensor == null)
|
|
{
|
|
if (Dados[1] == "MD")
|
|
{
|
|
Conectado = Dados[2] == "1";
|
|
_MensagemConexaoRecebida = true;
|
|
}
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
bool AtualizaGrafico = false;
|
|
|
|
switch (TipoSensor)
|
|
{
|
|
case S_Code.sCFG:
|
|
Sensor.Inicializado = Dados[2] == "1";
|
|
break;
|
|
case S_Code.sCOR:
|
|
{
|
|
double vBarramento = -1;
|
|
double.TryParse(Dados[2].Replace(".", ","), out vBarramento);
|
|
double vShunt = -1;
|
|
double.TryParse(Dados[3].Replace(".", ","), out vShunt);
|
|
double corrente = -1;
|
|
double.TryParse(Dados[4].Replace(".", ","), out corrente);
|
|
double potencia = -1;
|
|
double.TryParse(Dados[5].Replace(".", ","), out potencia);
|
|
|
|
Sensor.Leituras = new List<double>()
|
|
{
|
|
corrente,
|
|
vBarramento,
|
|
potencia,
|
|
vShunt,
|
|
};
|
|
|
|
if (Sensor.ID.Contains("AB"))
|
|
{
|
|
frmInstancial.frmAcompanhamento.AtualizarSensores(Sensor.ID.Replace("AB", "Barramento ") + ": " + vBarramento.ToString("0.00") + " V " + corrente.ToString("0.00") + " mA");
|
|
}
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
case S_Code.sTEN:
|
|
{
|
|
double leitura = -1;
|
|
double.TryParse(Dados[2].Replace(".", ","), out leitura);
|
|
|
|
double Vcc = 3.3;
|
|
double Voffset = double.Parse(Sensor.Parametros[0][1]);
|
|
double R1 = double.Parse(Sensor.Parametros[1][1].Replace(".", ","));
|
|
double R2 = double.Parse(Sensor.Parametros[2][1].Replace(".", ","));
|
|
double Razao = (R1 + R2) / (double)R2;
|
|
double voltageV = (leitura * (Vcc / 4095.0)) + (Voffset / Razao); // Convertendo o valor ADC para tensão
|
|
double realVoltage = voltageV * Razao;
|
|
|
|
Sensor.Leituras = new List<double>()
|
|
{
|
|
realVoltage,
|
|
leitura,
|
|
voltageV,
|
|
};
|
|
|
|
if (Sensor.Descricao.ToLower().Contains("bateria"))
|
|
{
|
|
frmInstancial.frmAcompanhamento.AtualizarSensores("Tensão atual: " + realVoltage.ToString("0.00") + "V - Mín: " + Sensor.ValorMinimo.ToString("0.00") + "V - Máx: " + Sensor.ValorMaximo.ToString("0.00") + "V");
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.PorcentagemBateria = (realVoltage / Sensor.LimiteMaximo) * 100;
|
|
if (Variaveis.OperacaoEmAndamento.Sensoriamento.PorcentagemBateria < 0)
|
|
{
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.PorcentagemBateria = 0;
|
|
}
|
|
}
|
|
|
|
AtualizaGrafico = true;
|
|
|
|
break;
|
|
}
|
|
case S_Code.sTMP:
|
|
{
|
|
double temp = -1;
|
|
double.TryParse(Dados[2].Replace(".", ","), out temp);
|
|
|
|
double Tc = FuncoesMatematicas.CalcularTemperatura(temp, double.Parse(Sensor.Parametros[0][1].Replace(".", ",")));
|
|
|
|
Sensor.Leituras = new List<double>() { Tc };
|
|
|
|
if (Sensor.Descricao.ToLower().Contains("bateria"))
|
|
{
|
|
frmInstancial.frmAcompanhamento.AtualizarSensores(Tc.ToString("0.00") + " ºC");
|
|
}
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
case S_Code.sULT:
|
|
{
|
|
double distancia = -1;
|
|
double.TryParse(Dados[2].Replace(".", ","), out distancia);
|
|
int duracao = -1;
|
|
int.TryParse(Dados[3], out duracao);
|
|
|
|
Sensor.Leituras = new List<double>()
|
|
{
|
|
distancia,
|
|
duracao,
|
|
};
|
|
|
|
if (Sensor.ID == "SUESQ")
|
|
{
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.DistanciaEsquerda = distancia;
|
|
}
|
|
else if (Sensor.ID == "SUDIR")
|
|
{
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.DistanciaDireita = distancia;
|
|
}
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
case S_Code.sMAG:
|
|
{
|
|
int x = -1;
|
|
int.TryParse(Dados[2], out x);
|
|
int y = -1;
|
|
int.TryParse(Dados[3], out y);
|
|
int z = -1;
|
|
int.TryParse(Dados[4], out z);
|
|
double angulo = -1;
|
|
double.TryParse(Dados[5].Replace(".", ","), out angulo);
|
|
|
|
Sensor.Leituras = new List<double>()
|
|
{
|
|
angulo,
|
|
x,
|
|
y,
|
|
z,
|
|
};
|
|
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.AnguloCarroBussola = angulo;
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
case S_Code.sSNR:
|
|
{
|
|
int distancia = -1;
|
|
int.TryParse(Dados[2], out distancia);
|
|
int angulo = -1;
|
|
int.TryParse(Dados[3], out angulo);
|
|
int posicao = -1;
|
|
int.TryParse(Dados[4], out posicao);
|
|
|
|
Sensor.Leituras = new List<double>()
|
|
{
|
|
distancia,
|
|
angulo,
|
|
posicao,
|
|
};
|
|
/*string protocolo = Dados[2];
|
|
int posicao = -1;
|
|
int.TryParse(protocolo.Split('_')[0], out posicao);
|
|
var Leituras = new Dictionary<int, int>();
|
|
foreach (var leitura in protocolo.Split('_')[1].Trim().Split('-'))
|
|
{
|
|
if (leitura == "")
|
|
{
|
|
break;
|
|
}
|
|
int distancia = int.Parse(leitura.Split(',')[0]);
|
|
int angulo = int.Parse(leitura.Split(',')[1]);
|
|
Leituras.Add(angulo, distancia);
|
|
}*/
|
|
|
|
if (Sensor.ID == "SNR_F")
|
|
{
|
|
var posArr = Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.Deteccoes.FirstOrDefault(x => x.Posicao == posicao);
|
|
if (posArr == null)
|
|
{
|
|
posArr = new SonarRegistroModel()
|
|
{
|
|
Posicao = posicao,
|
|
Leituras = new List<SonarLeituraModel>()
|
|
};
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.Deteccoes.Add(posArr);
|
|
}
|
|
var leituras = posArr.Leituras;
|
|
|
|
if (false && posicao != Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.PosicaoAtual && Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.Deteccoes.Any(x => x.Posicao == posicao))
|
|
{
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.Deteccoes.First(x => x.Posicao == posicao).Leituras.Clear();
|
|
}
|
|
|
|
if (leituras.Any(x => x.Angulo == angulo))
|
|
{
|
|
leituras.First(x => x.Angulo == angulo).Momento = DateTime.Now;
|
|
leituras.First(x => x.Angulo == angulo).Distancia = distancia;
|
|
}
|
|
else
|
|
{
|
|
leituras.Add(new SonarLeituraModel(angulo, distancia));
|
|
}
|
|
/*foreach (var leitura in Leituras)
|
|
{
|
|
if (leituras.Any(x => x.Angulo == leitura.Key))
|
|
{
|
|
leituras.First(x => x.Angulo == leitura.Key).Momento = DateTime.Now;
|
|
leituras.First(x => x.Angulo == leitura.Key).Distancia = leitura.Value;
|
|
}
|
|
else
|
|
{
|
|
leituras.Add(new SonarLeituraModel(leitura.Key, leitura.Value));
|
|
}
|
|
}*/
|
|
|
|
|
|
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.DistanciaEsquerda = Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.DistanciaEsquerda;
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.DistanciaDireita = Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.DistanciaDireita;
|
|
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.PosicaoAtual = posicao;
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.AtualizarSonar();
|
|
|
|
if (
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.DesvioNecessario &&
|
|
Variaveis.OperacaoEmAndamento.Sensoriamento.SonarFrontal.DirecaoDesvio == Direcao.Parado &&
|
|
Variaveis.OperacaoEmAndamento.DispMov.Dados.Conectado
|
|
)
|
|
{
|
|
foreach (var _motor in Variaveis.OperacaoEmAndamento.DispMov.Dados.Motores)
|
|
{
|
|
string ProtocoloParada = _motor.ProtocoloComando(Direcao.Frente, true);
|
|
Variaveis.OperacaoEmAndamento.DispMov.AdicionarMensagemFila(ProtocoloParada);
|
|
}
|
|
}
|
|
}
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
if (Sensor.Leituras[0] > Sensor.ValorMaximo)
|
|
{
|
|
Sensor.ValorMaximo = Sensor.Leituras[0];
|
|
}
|
|
else if (Sensor.Leituras[0] < Sensor.ValorMinimo)
|
|
{
|
|
Sensor.ValorMinimo = Sensor.Leituras[0];
|
|
}
|
|
|
|
MovSensoriamentoMotor Motor =
|
|
Variaveis.DispositivosConectados.Any(x => x.Dispositivo == T_Code.Mov) ?
|
|
((MovimentacaoModel)(Variaveis.DispositivosConectados.FirstOrDefault(x => x.Dispositivo == T_Code.Mov).Dados)).Motores.FirstOrDefault(x => Sensor.ID.Contains(x.ID)) :
|
|
null;
|
|
if (Motor != null)
|
|
{
|
|
Motor.AtualizarSensoresMotor(Sensor);
|
|
}
|
|
|
|
if (AtualizaGrafico)
|
|
{
|
|
Sensor.LogGrafico.Add(new SenSensorimanetoGrafico()
|
|
{
|
|
Momento = DateTime.Now,
|
|
ComponenteID = Sensor.ID,
|
|
Componente = Sensor.Componente,
|
|
Leitura1 = Sensor.Leituras.Count() > 0 ? Sensor.Leituras[0] : -1,
|
|
Leitura2 = Sensor.Leituras.Count() > 1 ? Sensor.Leituras[1] : -1,
|
|
Leitura3 = Sensor.Leituras.Count() > 2 ? Sensor.Leituras[2] : -1,
|
|
Leitura4 = Sensor.Leituras.Count() > 3 ? Sensor.Leituras[3] : -1,
|
|
AlarmeMinima = Sensor.Leituras[0] < Sensor.LimiteMinimo,
|
|
AlarmeMaxima = Sensor.Leituras[0] > Sensor.LimiteMaximo,
|
|
});
|
|
|
|
if (Sensor.LogGrafico.Count() > LogsManter)
|
|
{
|
|
Sensor.LogGrafico.RemoveAt(0);
|
|
}
|
|
|
|
if (Variaveis.DispositivosConectados.Any(x => x.Dispositivo == T_Code.Dir))
|
|
{
|
|
//((frmDirPlot)(frmPrincipal.Dispositivos.First(x => x.Dispositivo == T_Code.Dir).frmPlot)).AtualizaGrafico();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public string GetTaxaAmostragem()
|
|
{
|
|
return _TaxaAmostragem.ToString("00000");
|
|
}
|
|
|
|
public void SetTaxaAmostragem(int TaxaAmostragem)
|
|
{
|
|
_TaxaAmostragem = TaxaAmostragem;
|
|
}
|
|
|
|
public bool GetStatusConexao()
|
|
{
|
|
return Conectado;
|
|
}
|
|
|
|
public void IniciarRegistrador()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public class SensorModel
|
|
{
|
|
public string ID { get; set; }
|
|
public int Prioridade { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public bool Aferir { get; set; } = false;
|
|
public int DelayAmostragem { get; set; } = 500;
|
|
public bool Inicializado { get; set; } = false;
|
|
public bool Testando { get; set; } = false;
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public List<string[]> Parametros { get; set;} = new List<string[]>();
|
|
|
|
public string UnidadeMedida { get; set; }
|
|
public List<double> Leituras { get; set; }
|
|
public double ValorMinimo { get; set; }
|
|
public double ValorMaximo { get; set; }
|
|
public double LimiteMaximo { get; set; }
|
|
public double LimiteMinimo { get; set; }
|
|
|
|
public List<SenSensorimanetoGrafico> LogGrafico = new List<SenSensorimanetoGrafico>();
|
|
|
|
public string ProtocoloConfiguracaoSensor(PinoutDataModel Pinout, bool Conectar)
|
|
{
|
|
var _pinout = new PinoutDataModel()
|
|
{
|
|
ExpansorGPIO = Pinout.ExpansorGPIO,
|
|
EnderecoExpansor = Pinout.EnderecoExpansor,
|
|
Pinos = Pinout.Pinos.ToList(),
|
|
};
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == ID).ToList();
|
|
string Protocolo =
|
|
((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
((int)Componente).ToString() + SerialService.SplitParams +
|
|
(Aferir ? "1" : "0") + SerialService.SplitParams +
|
|
DelayAmostragem.ToString() + SerialService.SplitParams +
|
|
Prioridade.ToString() + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(_pinout, Funcoes);
|
|
foreach (var Parametro in Parametros)
|
|
{
|
|
Protocolo += SerialService.SplitParams + Parametro[1];
|
|
}
|
|
return Protocolo;
|
|
}
|
|
}
|
|
|
|
public class SensorSinaleiroModel
|
|
{
|
|
public string ID { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public SensorSinaleiroComportamentoModel Comportamento { get; set; }
|
|
|
|
public string ProtocoloConfiguracaoSinaleiro(PinoutDataModel Pinout, bool Conectar)
|
|
{
|
|
var _pinout = new PinoutDataModel()
|
|
{
|
|
ExpansorGPIO = Pinout.ExpansorGPIO,
|
|
EnderecoExpansor = Pinout.EnderecoExpansor,
|
|
Pinos = Pinout.Pinos.ToList(),
|
|
};
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == ID).ToList();
|
|
string Protocolo = ((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(_pinout, Funcoes);
|
|
return Protocolo;
|
|
}
|
|
|
|
public string ProtocoloComando()
|
|
{
|
|
string ProtocoloSinaleiro =
|
|
((int)F_Code.Cmd).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
(int)Comportamento.statusLED + SerialService.SplitParams +
|
|
Comportamento.QtdPiscadas.ToString() + SerialService.SplitParams +
|
|
Comportamento.TempoMs.ToString() + SerialService.SplitParams +
|
|
Comportamento.PausaMs.ToString();
|
|
|
|
return ProtocoloSinaleiro;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class SensorSinaleiroComportamentoModel
|
|
{
|
|
public StatusLED statusLED { get; set; }
|
|
public int QtdPiscadas { get; set; } = 0;
|
|
public int TempoMs { get; set; } = 0;
|
|
public int PausaMs { get; set; } = 0;
|
|
}
|
|
|
|
public class SenSensorimanetoGrafico
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ComponenteID { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public double Leitura1 { get; set; }
|
|
public double Leitura2 { get; set; }
|
|
public double Leitura3 { get; set; }
|
|
public double Leitura4 { get; set; }
|
|
public bool AlarmeMaxima { get; set; }
|
|
public bool AlarmeMinima { get; set; }
|
|
}
|
|
|
|
|
|
}
|