2564 lines
102 KiB
C#
2564 lines
102 KiB
C#
using AgroBase.Forms.IHM;
|
|
using AgroBase.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using static AgroBase.Models.Enums;
|
|
using System.IO.Ports;
|
|
using System.Globalization;
|
|
using System.Data;
|
|
|
|
namespace AgroBase.Models.Modules
|
|
{
|
|
public class SensoriamentoModel : DispositivoBaseModel
|
|
{
|
|
public T_Code Dispositivo { get; set; } = T_Code.Sen;
|
|
public string Modulo_ID { get; set; } = T_Code.Sen.ToString();
|
|
public byte _EnderecoCAN_Tx { get; set; } = 0x11;
|
|
public byte _EnderecoCAN_Rx { get; set; } = 0x12;
|
|
public int _TaxaAmostragem { get; set; } = 1000;
|
|
public bool RequisitarDados { get; set; } = true;
|
|
public static int PingsConsiderarConexao { get; set; } = 5;
|
|
public static int TempoLimiteConexao
|
|
{
|
|
get
|
|
{
|
|
return PingsConsiderarConexao * (Variaveis.OperacaoEmAndamento?.DispSen?._TaxaAmostragem ?? 1000);
|
|
}
|
|
}
|
|
public bool Conectado
|
|
{
|
|
get
|
|
{
|
|
return DadosLeitura.UltimoComandoRespondido.AddMilliseconds(TempoLimiteConexao * 2) >= DateTime.Now; // DadosLeitura.Conectado &&
|
|
}
|
|
}
|
|
|
|
public static PinoutDataModel Pinout { get; set; }
|
|
public static List<FuncoesPinout> Funcoes { get; set; } = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.SDA,
|
|
FuncoesPinout.SCL,
|
|
};
|
|
public List<SensorModel> Sensores { get; set; }
|
|
public List<SensorSinaleiroModel> Sinaleiros { get; set; }
|
|
public List<SensorReleModel> Reles { get; set; }
|
|
public List<SensorServoModel> Servos { get; set; }
|
|
public string CameraID_Caminho { get; set; } = "";
|
|
public List<string> CameraID_Solo { get; set; } = new List<string>();
|
|
public UltrasonicA05Configuracoes ConfiguracaoSensorUltrassom { get; set; }
|
|
public SensoriamentoMensagemJsonModel DadosLeitura { get; set; } = new SensoriamentoMensagemJsonModel();
|
|
private SenHandler _SenHandler = new SenHandler();
|
|
public LoRaEspService loRaService { get; set; } = new LoRaEspService();
|
|
public LoRaParametrosModel LoRaParametrosBase { get; set; } = new LoRaParametrosModel()
|
|
{
|
|
address = 0x01,
|
|
channel = 0x17,
|
|
tranMode = LoRaService.CodigosTipoTransmissao["Fixed"],
|
|
baudRate = LoRaService.CodigosBaudRates[9600],
|
|
airRate = LoRaService.CodigosAirRates[2.4],
|
|
packetSize = LoRaService.CodigosPacketSizes[200],
|
|
worCycle = LoRaService.CodigosWorCycles[4000],
|
|
power = LoRaService.CodigosPowers[22],
|
|
};
|
|
public CoulombCounterService ContadorCarga = new CoulombCounterService(VariaveisEquipamento.CorrenteMaximaBateria);
|
|
public AsyncTaskTimerModel tmrRegistrador;
|
|
private int _TempoReconexaoComponentes = 5;
|
|
private int _VezesLeituraReconexao = 0;
|
|
|
|
public static SensoriamentoModel CarregarParametrosIniciais()
|
|
{
|
|
var obj = new SensoriamentoModel()
|
|
{
|
|
_EnderecoCAN_Tx = 0x11,
|
|
_EnderecoCAN_Rx = 0x12,
|
|
_TaxaAmostragem = 1000,
|
|
RequisitarDados = true,
|
|
CameraID_Caminho = "",
|
|
CameraID_Solo = new List<string>(),
|
|
Sinaleiros = new List<SensorSinaleiroModel>()
|
|
{
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDMN",
|
|
ID_Num = 21,
|
|
Descricao = "Sinaleiro indicação de modo Manual ativo",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = VariaveisEquipamento.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDAT",
|
|
ID_Num = 22,
|
|
Descricao = "Sinaleiro indicação de modo Automático ativo",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = VariaveisEquipamento.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
new SensorSinaleiroModel()
|
|
{
|
|
ID = "LEDOP",
|
|
ID_Num = 23,
|
|
Descricao = "Sinaleiro indicação do status da operação ativa",
|
|
Componente = S_Code.sLED,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Sinaleiro,
|
|
},
|
|
Comportamento = VariaveisEquipamento.ComportamentoPorStatus(StatusOperacao.NaoIniciado)
|
|
},
|
|
},
|
|
Sensores = new List<SensorModel>()
|
|
{
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVET",
|
|
Descricao = "Temperatura Motor Esquerdo Trás",
|
|
ID_Num = 1,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Offset",
|
|
Valor = "0",
|
|
QtdBytes = 2,
|
|
Escalar = false,
|
|
Enviar = false,
|
|
}
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVEF",
|
|
Descricao = "Temperatura Motor Esquerdo Frente",
|
|
ID_Num = 2,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Offset",
|
|
Valor = "0",
|
|
QtdBytes = 2,
|
|
Escalar = false,
|
|
Enviar = false,
|
|
}
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVDT",
|
|
Descricao = "Temperatura Motor Direito Trás",
|
|
ID_Num = 3,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Offset",
|
|
Valor = "0",
|
|
QtdBytes = 2,
|
|
Escalar = false,
|
|
Enviar = false,
|
|
}
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TMVDF",
|
|
Descricao = "Temperatura Motor Direito Frente",
|
|
ID_Num = 4,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Offset",
|
|
Valor = "0",
|
|
QtdBytes = 2,
|
|
Escalar = false,
|
|
Enviar = false,
|
|
}
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "TBAT",
|
|
Descricao = "Temperatura da bateria",
|
|
ID_Num = 5,
|
|
Componente = S_Code.sTMP,
|
|
Aferir = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
},
|
|
UnidadeMedida = "ºC",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Offset",
|
|
Valor = "0",
|
|
QtdBytes = 2,
|
|
Escalar = false,
|
|
Enviar = false,
|
|
}
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB3V",
|
|
Descricao = "Corrente no Barramento 3V",
|
|
ID_Num = 11,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA219_3A2).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "0",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB5V",
|
|
Descricao = "Corrente no Barramento 5V",
|
|
ID_Num = 12,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA219_3A2).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "1",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB7V2",
|
|
Descricao = "Corrente no Barramento 7,2V",
|
|
ID_Num = 13,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA219_3A2).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "2",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB12V",
|
|
Descricao = "Corrente no Barramento 12V",
|
|
ID_Num = 14,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA228_50A).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "3",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB19V",
|
|
Descricao = "Corrente no Barramento 19V",
|
|
ID_Num = 15,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA228_10A).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "4",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB24V",
|
|
Descricao = "Corrente no Barramento 24V",
|
|
ID_Num = 16,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "mA",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA228_50A).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "5",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "AB36V",
|
|
Descricao = "Corrente no Barramento 36V",
|
|
ID_Num = 17,
|
|
Componente = S_Code.sCOR,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "A",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço I2C",
|
|
Valor = "0x40",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Shunt",
|
|
Valor = ((int)TiposShuntCorrente.INA228_100A).ToString(),
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Canal MUX",
|
|
Valor = "6",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
new SensorModel()
|
|
{
|
|
ID = "GY91",
|
|
Descricao = "Inclinometro",
|
|
ID_Num = 20,
|
|
Componente = S_Code.sIMU,
|
|
Aferir = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.I2C,
|
|
},
|
|
UnidadeMedida = "",
|
|
Parametros = new List<SensorParametroModel>()
|
|
{
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço MPU",
|
|
Valor = "0x68",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
new SensorParametroModel()
|
|
{
|
|
Descricao = "Endereço BMP",
|
|
Valor = "0x76",
|
|
QtdBytes = 1,
|
|
Escalar = false,
|
|
Enviar = true
|
|
},
|
|
}
|
|
},
|
|
|
|
/*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,
|
|
},*/
|
|
},
|
|
Reles = new List<SensorReleModel>()
|
|
{
|
|
new SensorReleModel()
|
|
{
|
|
ID = "RLLRA",
|
|
ID_Num = 31,
|
|
Descricao = "Relé modo operação LoRa",
|
|
Componente = S_Code.sRLE,
|
|
Controlar = true,
|
|
AtuacaoNivelAlto = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.Rele,
|
|
},
|
|
StatusControle = Estado.Desligado,
|
|
ControleExterno = false,
|
|
},
|
|
},
|
|
Servos = new List<SensorServoModel>()
|
|
{
|
|
new SensorServoModel()
|
|
{
|
|
ID = "FRO_ET",
|
|
ID_Num = 41,
|
|
Descricao = "Freio Motor ET",
|
|
Componente = S_Code.sFRO,
|
|
Controlar = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.ServoFreio,
|
|
},
|
|
_AnguloControle = 0,
|
|
},
|
|
new SensorServoModel()
|
|
{
|
|
ID = "FRO_EF",
|
|
ID_Num = 42,
|
|
Descricao = "Freio Motor EF",
|
|
Componente = S_Code.sFRO,
|
|
Controlar = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.ServoFreio,
|
|
},
|
|
_AnguloControle = 0,
|
|
},
|
|
new SensorServoModel()
|
|
{
|
|
ID = "FRO_DT",
|
|
ID_Num = 43,
|
|
Descricao = "Freio Motor DT",
|
|
Componente = S_Code.sFRO,
|
|
Controlar = true,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.ServoFreio,
|
|
},
|
|
_AnguloControle = 180,
|
|
},
|
|
new SensorServoModel()
|
|
{
|
|
ID = "FRO_DF",
|
|
ID_Num = 44,
|
|
Descricao = "Freio Motor DF",
|
|
Componente = S_Code.sFRO,
|
|
Controlar = false,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.ServoFreio,
|
|
},
|
|
_AnguloControle = 180,
|
|
},
|
|
},
|
|
loRaService = new LoRaEspService()
|
|
{
|
|
ID = "LoRa",
|
|
ID_Num = Variaveis.ID_Num_sLRA,
|
|
Componente = S_Code.sLRA,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.M0,
|
|
FuncoesPinout.M1,
|
|
FuncoesPinout.Aux,
|
|
FuncoesPinout.RX,
|
|
FuncoesPinout.TX
|
|
},
|
|
Conectado = false,
|
|
Configurado = false,
|
|
Parametros = new LoRaParametrosModel()
|
|
},
|
|
};
|
|
|
|
for (int i = 0; i < Variaveis.OperacaoEmAndamento.QuantidadeCamerasSolo; i++)
|
|
{
|
|
obj.CameraID_Solo.Add("");
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
public void LimparDados<T>(T _dados)
|
|
{
|
|
var Dados = _dados as SensoriamentoModel;
|
|
Dados.DadosLeitura = new SensoriamentoMensagemJsonModel();
|
|
Dados.DadosLeitura.Momento = DateTime.MinValue;
|
|
Dados.Reles.ForEach(x =>
|
|
{
|
|
x.StatusControle = Estado.Desligado;
|
|
});
|
|
Dados.Servos.ForEach(x =>
|
|
{
|
|
x._AnguloControle = x.Incremental ? 0 : 180;
|
|
});
|
|
loRaService.Conectado = false;
|
|
loRaService.Configurado = false;
|
|
loRaService.Parametros.UltimaLeitura = DateTime.MinValue;
|
|
|
|
ReiniciarLeituras(Dados);
|
|
}
|
|
|
|
public void ReiniciarLeituras(SensoriamentoModel Dados)
|
|
{
|
|
Dados.Reles.ForEach(x =>
|
|
{
|
|
x.Leitura = null;
|
|
});
|
|
Dados.Sinaleiros.ForEach(x =>
|
|
{
|
|
x.Leitura = null;
|
|
});
|
|
Dados.Sensores.ForEach(x =>
|
|
{
|
|
x.Leitura = null;
|
|
});
|
|
Dados.Servos.ForEach(x =>
|
|
{
|
|
x.Leitura = null;
|
|
});
|
|
}
|
|
|
|
public async Task<bool> VerificaPortaSEN(SerialPort porta)
|
|
{
|
|
if (!CanService.IsConnected)
|
|
{
|
|
CanService.DefinirPortaCOM(porta);
|
|
}
|
|
|
|
CanService.RegistrarHandler(_EnderecoCAN_Rx, _SenHandler);
|
|
|
|
if (CanService.IsConnected)
|
|
{
|
|
RequisitarDadosModulo(Variaveis.ID_Num_sMOD, CanMessagePosicaoDados.Status, CanMessagePosicaoDados.Status, true);
|
|
|
|
await Task.Delay(1000);
|
|
|
|
if (Conectado)
|
|
{
|
|
DefinirDispositivo();
|
|
Variaveis.OperacaoEmAndamento.DispSen?.EnviarProtocolosConfiguracao();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!CanService.Iniciado)
|
|
{
|
|
CanService.Fechar();
|
|
}
|
|
|
|
return Conectado;
|
|
}
|
|
|
|
private void DefinirDispositivo()
|
|
{
|
|
if (!SerialService.DispositivosMapeados.Any(x => x.Dispositivo == Dispositivo && x.Mod_ID == Modulo_ID))
|
|
{
|
|
SerialService.DispositivosMapeados.Add(new DispositivoDetalhesModel()
|
|
{
|
|
Endereco = CanService._portName,
|
|
Dispositivo = DadosLeitura.Dispositivo,
|
|
Erro = false,
|
|
Versao = DadosLeitura.Versao.ToString(),
|
|
Mod_ID = Modulo_ID,
|
|
});
|
|
}
|
|
}
|
|
|
|
public void RequisitarDadosModulo(int ID_Num, CanMessagePosicaoDados posicaoTx, CanMessagePosicaoDados posicaoRx, bool aguardaResposta)
|
|
{
|
|
Variaveis.OperacaoEmAndamento.DispSen?.AdicionarMensagemFila(ID_Num, posicaoTx, posicaoRx, null, aguardaResposta);
|
|
}
|
|
|
|
public List<(int, CanMessagePosicaoDados, byte[])> ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
// Inicializar o dicionário
|
|
List<(int, CanMessagePosicaoDados, byte[])> config = new List<(int, CanMessagePosicaoDados, byte[])>();
|
|
|
|
if (!DadosLeitura.Conectado)
|
|
{
|
|
// Adicionar configuração do ProtocoloMOD
|
|
var protocoloMOD = ModConfig(Conectar);
|
|
config.AddRange(protocoloMOD);
|
|
}
|
|
|
|
bool configurarTodos = !DadosLeitura.Conectado && Conectar;
|
|
|
|
// Adicionar configurações dos Sinaleiros
|
|
var sinaleirosConfig = SinaleirosConfig(Conectar, configurarTodos);
|
|
config.AddRange(sinaleirosConfig);
|
|
|
|
// Adicionar configurações dos Servos
|
|
var servosConfig = ServosConfig(Conectar, configurarTodos);
|
|
config.AddRange(servosConfig);
|
|
|
|
// Adicionar configurações dos Reles
|
|
var relesConfig = RelesConfig(Conectar, configurarTodos);
|
|
config.AddRange(relesConfig);
|
|
|
|
// Adicionar configurações dos Sensores que devem ser aferidos
|
|
var sensoresConfig = SensoresConfig(Conectar, configurarTodos);
|
|
config.AddRange(sensoresConfig);
|
|
|
|
// Adicionar configurações do LoRa
|
|
if (!loRaService.Conectado || !loRaService.Configurado || configurarTodos)
|
|
{
|
|
var loraConfig = loRaService.ProtocoloConfiguracao(Pinout, Conectar);
|
|
config.AddRange(loraConfig);
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
private List<(int, CanMessagePosicaoDados, byte[])> ModConfig(bool Conectar)
|
|
{
|
|
var config1 = new List<byte>
|
|
{
|
|
(byte)(Conectar ? 1 : 0),
|
|
(byte)(_TaxaAmostragem / 10),
|
|
(byte)(RequisitarDados ? 1 : 0),
|
|
LoRaParametrosBase.address
|
|
};
|
|
|
|
config1.AddRange(PinoutModel.PinoProtocolo(Pinout, Funcoes, false));
|
|
|
|
return new List<(int, CanMessagePosicaoDados, byte[])>()
|
|
{
|
|
(Variaveis.ID_Num_sMOD, CanMessagePosicaoDados.Config1, config1.ToArray()),
|
|
};
|
|
}
|
|
|
|
public List<(int, CanMessagePosicaoDados, byte[])> SensoresConfig(bool Conectar, bool ConfigurarTodos)
|
|
{
|
|
var config = new List<(int, CanMessagePosicaoDados, byte[])>();
|
|
|
|
foreach (var sensor in Sensores.Where(x => (x.Aferir && (ConfigurarTodos ? true : x.Inicializado != Conectar)) || (!x.Aferir && x.Inicializado)))
|
|
{
|
|
if (!sensor.Aferir && sensor.Inicializado)
|
|
{
|
|
Conectar = false;
|
|
}
|
|
(CanMessagePosicaoDados posicao, byte[] payload) = sensor.ProtocoloConfiguracao(Pinout, Conectar, Dispositivo);
|
|
if (payload != null)
|
|
{
|
|
|
|
config.Add((sensor.ID_Num, posicao, payload));
|
|
}
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
private List<(int, CanMessagePosicaoDados, byte[])> SinaleirosConfig(bool Conectar, bool ConfigurarTodos)
|
|
{
|
|
var config = new List<(int, CanMessagePosicaoDados, byte[])>();
|
|
|
|
foreach (var sinaleiro in Sinaleiros.Where(x => (ConfigurarTodos ? true : x.Inicializado != Conectar)))
|
|
{
|
|
(CanMessagePosicaoDados posicao, var payload) = sinaleiro.ProtocoloConfiguracao(Pinout, Conectar);
|
|
if (payload != null)
|
|
{
|
|
config.Add((sinaleiro.ID_Num, posicao, payload));
|
|
}
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
private List<(int, CanMessagePosicaoDados, byte[])> RelesConfig(bool Conectar, bool ConfigurarTodos)
|
|
{
|
|
var config = new List<(int, CanMessagePosicaoDados, byte[])>();
|
|
|
|
foreach (var rele in Reles.Where(x => (x.Controlar && (ConfigurarTodos ? true : x.Inicializado != Conectar)) || (!x.Controlar && x.Inicializado)))
|
|
{
|
|
if (!rele.Controlar && rele.Inicializado)
|
|
{
|
|
Conectar = false;
|
|
}
|
|
(CanMessagePosicaoDados posicao, var payload) = rele.ProtocoloConfiguracao(Pinout, Conectar);
|
|
if (payload != null)
|
|
{
|
|
config.Add((rele.ID_Num, posicao, payload));
|
|
}
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
private List<(int, CanMessagePosicaoDados, byte[])> ServosConfig(bool Conectar, bool ConfigurarTodos)
|
|
{
|
|
var config = new List<(int, CanMessagePosicaoDados, byte[])>();
|
|
|
|
foreach (var servo in Servos.Where(x => (x.Controlar && (ConfigurarTodos ? true : x.Inicializado != Conectar)) || (!x.Controlar && x.Inicializado)))
|
|
{
|
|
if (!servo.Controlar && servo.Inicializado)
|
|
{
|
|
Conectar = false;
|
|
}
|
|
(CanMessagePosicaoDados posicao, var payload) = servo.ProtocoloConfiguracao(Pinout, Conectar);
|
|
if (payload != null)
|
|
{
|
|
config.Add((servo.ID_Num, posicao, payload));
|
|
}
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
public void IniciarRegistrador()
|
|
{
|
|
if (!(tmrRegistrador?.IsRunning ?? false))
|
|
{
|
|
LimparDados(this);
|
|
|
|
PararRegistrador();
|
|
|
|
tmrRegistrador = new AsyncTaskTimerModel("tmrRegistrador_" + T_Code.Sen.ToString(), tmrRegistrador_Tick, _TaxaAmostragem);
|
|
tmrRegistrador.Start();
|
|
}
|
|
}
|
|
|
|
public void PararRegistrador()
|
|
{
|
|
tmrRegistrador?.Dispose();
|
|
}
|
|
|
|
public async Task tmrRegistrador_Tick()
|
|
{
|
|
if (Variaveis.Fechando)
|
|
{
|
|
return;
|
|
}
|
|
|
|
tmrRegistrador.SetInterval(_TaxaAmostragem);
|
|
|
|
if (SerialService.DispositivosMapeados.Any(x => x.Dispositivo == T_Code.Sen) && (_VezesLeituraReconexao >= _TempoReconexaoComponentes))
|
|
{
|
|
Variaveis.OperacaoEmAndamento.DispSen?.EnviarProtocolosConfiguracao();
|
|
|
|
_VezesLeituraReconexao = 0;
|
|
}
|
|
else
|
|
{
|
|
_VezesLeituraReconexao++;
|
|
}
|
|
|
|
|
|
if (DadosLeitura.Conectado)
|
|
{
|
|
if (RequisitarDados)
|
|
{
|
|
RequisitarDadosModulo(Variaveis.ID_Num_sTOD, CanMessagePosicaoDados.DadosAll, CanMessagePosicaoDados.DadosAll, true);
|
|
}
|
|
|
|
VerificaControleLeitura();
|
|
}
|
|
|
|
AtualizaComponentesIniciados();
|
|
}
|
|
|
|
public (bool, List<string>) StatusModulosSEN()
|
|
{
|
|
var _Sensores = Sensores?.Where(x => x.Aferir).ToList();
|
|
var _SensoresComFalha = _Sensores?.Where(x => !x.Inicializado).ToList();
|
|
bool _sensoresOperantes = (_SensoresComFalha?.Count() ?? 0) < (_Sensores?.Count() ?? 0);
|
|
|
|
var _Sinaleiros = Sinaleiros?.ToList();
|
|
var _SinaleirosComFalha = _Sinaleiros?.Where(x => !x.Inicializado).ToList();
|
|
bool _sinaleirosOperantes = !(_SinaleirosComFalha?.Any() ?? false);
|
|
|
|
var _Reles = Reles?.Where(x => x.Controlar).ToList();
|
|
var _RelesComFalha = _Reles?.Where(x => !x.Inicializado).ToList();
|
|
bool _relesOperantes = !(_RelesComFalha?.Any() ?? false);
|
|
|
|
var _Servos = Servos?.Where(x => x.Controlar).ToList();
|
|
var _ServosComFalha = _Servos?.Where(x => !x.Inicializado).ToList();
|
|
bool _servosOperantes = !(_ServosComFalha?.Any() ?? false);
|
|
|
|
bool _sensoriamentoOperante = _sensoresOperantes && _relesOperantes && _servosOperantes;
|
|
List<string> ModsComFalha =
|
|
(_SensoresComFalha?.Select(x => x.ID) ?? new List<string>())
|
|
.Union(_SinaleirosComFalha?.Select(x => x.ID) ?? new List<string>())
|
|
.Union(_RelesComFalha?.Select(x => x.ID) ?? new List<string>())
|
|
.Union(_ServosComFalha?.Select(x => x.ID) ?? new List<string>())
|
|
.ToList();
|
|
|
|
return (_sensoriamentoOperante, ModsComFalha);
|
|
}
|
|
|
|
private void VerificaControleLeitura()
|
|
{
|
|
if (!frmDiagnosticos.Diagnosticando)
|
|
{
|
|
var ledOP = Sinaleiros.FirstOrDefault(x => x.ID == "LEDOP" && x.Inicializado);
|
|
var ledAT = Sinaleiros.FirstOrDefault(x => x.ID == "LEDAT" && x.Inicializado);
|
|
var ledMN = Sinaleiros.FirstOrDefault(x => x.ID == "LEDMN" && x.Inicializado);
|
|
|
|
var comportamento = VariaveisEquipamento.ComportamentoPorStatus();
|
|
if (ledOP != null && (!ledOP.Comportamento.Equals(comportamento) || (ledOP.LeituraComando != comportamento.statusLED)))
|
|
{
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sLED, ledOP.ID, comportamento);
|
|
}
|
|
|
|
var statusLedAt = Variaveis.OperacaoEmAndamento.Modo == ModoOperacao.MapaGPS ? StatusLED.Aceso : StatusLED.Apagado;
|
|
if (ledAT != null && (!ledAT.Comportamento.statusLED.Equals(statusLedAt) || (ledAT.LeituraComando != statusLedAt)))
|
|
{
|
|
ledAT.Comportamento.statusLED = statusLedAt;
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sLED, ledAT.ID, ledAT.Comportamento);
|
|
}
|
|
|
|
var statusLedMn = Variaveis.OperacaoEmAndamento.Modo == ModoOperacao.Manual ? StatusLED.Aceso : StatusLED.Apagado;
|
|
if (ledMN != null && (!ledMN.Comportamento.statusLED.Equals(statusLedMn) || (ledMN.LeituraComando != statusLedMn)))
|
|
{
|
|
ledMN.Comportamento.statusLED = statusLedMn;
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sLED, ledMN.ID, ledMN.Comportamento);
|
|
}
|
|
|
|
foreach (var rele in Reles.Where(x => x.Controlar && x.Inicializado && !x.ControleExterno))
|
|
{
|
|
if (rele.StatusControle != rele.LeituraEstado)
|
|
{
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sRLE, rele.ID, rele.StatusControle);
|
|
}
|
|
}
|
|
|
|
foreach (var servo in Servos.Where(x => x.Controlar && x.Inicializado))
|
|
{
|
|
if (servo._AnguloControle != servo.LeituraAngulo)
|
|
{
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sFRO, servo.ID, servo._AnguloControle);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AtualizaComponentesIniciados()
|
|
{
|
|
DateTime Agora = DateTime.Now;
|
|
bool modConectado = DadosLeitura.UltimoComandoRespondido.AddMilliseconds(TempoLimiteConexao) > Agora;
|
|
if (!modConectado && DadosLeitura.Conectado)
|
|
{
|
|
DadosLeitura.Conectado = false;
|
|
}
|
|
foreach (var led in Sinaleiros.Where(x => x.Inicializado && x.Leitura.Momento.AddMilliseconds(TempoLimiteConexao) < Agora))
|
|
{
|
|
led.Leitura.Iniciado = false;
|
|
}
|
|
foreach (var rele in Reles.Where(x => x.Inicializado && x.Leitura.Momento.AddMilliseconds(TempoLimiteConexao) < Agora))
|
|
{
|
|
rele.Leitura.Iniciado = false;
|
|
}
|
|
foreach (var servo in Servos.Where(x => x.Inicializado && x.Leitura.Momento.AddMilliseconds(TempoLimiteConexao) < Agora))
|
|
{
|
|
servo.Leitura.Iniciado = false;
|
|
}
|
|
foreach (var sensor in Sensores.Where(x => x.Inicializado && x.Leitura.Momento.AddMilliseconds(TempoLimiteConexao) < Agora))
|
|
{
|
|
sensor.Leitura.Iniciado = false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class SensorModel
|
|
{
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public bool Aferir { get; set; } = false;
|
|
private dynamic _leitura = null;
|
|
public IFirmwareSensorBase Leitura
|
|
{
|
|
get
|
|
{
|
|
switch (Componente)
|
|
{
|
|
case S_Code.sTMP:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.SensoresTemperatura?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
case S_Code.sCOR:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.SensoresCorrente?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
case S_Code.sFLX:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispAtu?.Dados?.DadosLeitura?.SensoresFluxo?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
case S_Code.sPRS:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispAtu?.Dados?.DadosLeitura?.SensoresPressao?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
case S_Code.sMAS:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispAtu?.Dados?.DadosLeitura?.SensoresMassa?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
case S_Code.sIMU:
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.SensoresIMU?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
break;
|
|
default:
|
|
_leitura = null;
|
|
break;
|
|
}
|
|
|
|
return _leitura;
|
|
}
|
|
set
|
|
{
|
|
_leitura = value;
|
|
}
|
|
}
|
|
public bool Inicializado
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Iniciado ?? false;
|
|
}
|
|
}
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public List<SensorParametroModel> Parametros { get; set;} = new List<SensorParametroModel>();
|
|
|
|
public string UnidadeMedida { get; set; }
|
|
public double LimiteMaximo { get; set; }
|
|
public double LimiteMinimo { get; set; }
|
|
|
|
public List<SenSensorimanetoGrafico> LogGrafico = new List<SenSensorimanetoGrafico>();
|
|
|
|
public (CanMessagePosicaoDados, byte[]) ProtocoloConfiguracao(PinoutDataModel Pinout, bool conectar, T_Code Dispositivo)
|
|
{
|
|
List<byte> config = new List<byte>
|
|
{
|
|
(byte)Componente,
|
|
(byte)(conectar ? 1 : 0),
|
|
};
|
|
|
|
var _pinout = Pinout.Clone();
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == ID).ToList();
|
|
if (_pinout.Pinos.Count > 0)
|
|
{
|
|
bool enviaBarramento = Dispositivo == T_Code.Sen;
|
|
|
|
byte[] pinos = PinoutModel.PinoProtocolo(_pinout, Funcoes, enviaBarramento);
|
|
config.AddRange(pinos);
|
|
}
|
|
|
|
foreach (var parametro in Parametros)
|
|
{
|
|
if (parametro.Enviar)
|
|
{
|
|
byte[] paramBytes = parametro.InterpretarParametro();
|
|
config.AddRange(paramBytes);
|
|
}
|
|
}
|
|
|
|
return (CanMessagePosicaoDados.Config1, config.ToArray());
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public class SensorSinaleiroModel
|
|
{
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public SensorSinaleiroComportamentoModel Comportamento { get; set; }
|
|
private FirmwareSinaleiro _leitura = null;
|
|
public FirmwareSinaleiro Leitura
|
|
{
|
|
get
|
|
{
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.Sinaleiros?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
return _leitura;
|
|
}
|
|
set
|
|
{
|
|
_leitura = value;
|
|
}
|
|
}
|
|
public bool Inicializado
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Iniciado ?? false;
|
|
}
|
|
}
|
|
public StatusLED LeituraComando
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Comando ?? StatusLED.Apagado;
|
|
}
|
|
}
|
|
public StatusLED LeituraStatus
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Leitura ?? StatusLED.Apagado;
|
|
}
|
|
}
|
|
|
|
public (CanMessagePosicaoDados, byte[]) ProtocoloConfiguracao(PinoutDataModel pinout, bool conectar)
|
|
{
|
|
var pino = pinout.Pinos.FirstOrDefault(p => p.ComponenteID == ID);
|
|
List<byte> config = new List<byte>
|
|
{
|
|
(byte)Componente,
|
|
(byte)(conectar ? 1 : 0),
|
|
(byte)(pino?.Barramento ?? TiposBarramentos.CONTROLADOR),
|
|
(byte)(pino?.Pino ?? PinoutUtils.PINO_INVALIDO)
|
|
};
|
|
|
|
return (CanMessagePosicaoDados.Config1, config.ToArray());
|
|
}
|
|
|
|
public (int, CanMessagePosicaoDados, byte[]) ProtocoloComando()
|
|
{
|
|
List<byte> frame = new List<byte>
|
|
{
|
|
(byte)Comportamento.statusLED,
|
|
(byte)Comportamento.QtdPiscadas,
|
|
(byte)(Comportamento.TempoMs / 10),
|
|
(byte)(Comportamento.PausaMs / 10)
|
|
};
|
|
|
|
return (ID_Num, CanMessagePosicaoDados.Command1, frame.ToArray());
|
|
}
|
|
|
|
}
|
|
|
|
public class SensorReleModel
|
|
{
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public bool ControleExterno { get; set; }
|
|
public bool Controlar { get; set; }
|
|
public bool AtuacaoNivelAlto { get; set; }
|
|
public Estado StatusControle { get; set; }
|
|
private FirmwareRele _leitura = null;
|
|
public FirmwareRele Leitura
|
|
{
|
|
get
|
|
{
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.Reles?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
return _leitura;
|
|
}
|
|
set
|
|
{
|
|
_leitura = value;
|
|
|
|
if (_leitura != null && ControleExterno)
|
|
{
|
|
StatusControle = _leitura.Leitura;
|
|
}
|
|
}
|
|
}
|
|
public bool Inicializado
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Iniciado ?? false;
|
|
}
|
|
}
|
|
public Estado LeituraEstado
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Leitura ?? Estado.Desligado;
|
|
}
|
|
}
|
|
|
|
public (CanMessagePosicaoDados, byte[]) ProtocoloConfiguracao(PinoutDataModel pinout, bool conectar)
|
|
{
|
|
var pino = pinout.Pinos.FirstOrDefault(p => p.ComponenteID == ID);
|
|
List<byte> config = new List<byte>
|
|
{
|
|
(byte)Componente,
|
|
(byte)(conectar ? 1 : 0),
|
|
(byte)(AtuacaoNivelAlto ? 1 : 0),
|
|
(byte)(pino?.Barramento ?? TiposBarramentos.CONTROLADOR),
|
|
(byte)(pino?.Pino ?? PinoutUtils.PINO_INVALIDO)
|
|
};
|
|
|
|
return (CanMessagePosicaoDados.Config1, config.ToArray());
|
|
}
|
|
|
|
public (int, CanMessagePosicaoDados, byte[]) ProtocoloComando()
|
|
{
|
|
return (
|
|
ID_Num,
|
|
CanMessagePosicaoDados.Command1,
|
|
new byte[]
|
|
{
|
|
(byte)StatusControle
|
|
}
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
public class SensorServoModel
|
|
{
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public string Descricao { get; set; }
|
|
public S_Code Componente { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
public bool Incremental { get; set; }
|
|
|
|
public bool Controlar { get; set; }
|
|
public int _AnguloControle { get; set; }
|
|
private FirmwareServoFreio _leitura = null;
|
|
public FirmwareServoFreio Leitura
|
|
{
|
|
get
|
|
{
|
|
_leitura = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.DadosLeitura?.ServoFreios?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
return _leitura;
|
|
}
|
|
set
|
|
{
|
|
_leitura = value;
|
|
}
|
|
}
|
|
public bool Inicializado
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Iniciado ?? false;
|
|
}
|
|
}
|
|
public double LeituraAngulo
|
|
{
|
|
get
|
|
{
|
|
return Leitura?.Angulo ?? 0;
|
|
}
|
|
}
|
|
|
|
public (CanMessagePosicaoDados, byte[]) ProtocoloConfiguracao(PinoutDataModel pinout, bool conectar)
|
|
{
|
|
var pino = pinout.Pinos.FirstOrDefault(p => p.ComponenteID == ID);
|
|
|
|
List<byte> config = new List<byte>
|
|
{
|
|
(byte)Componente,
|
|
(byte)(conectar ? 1 : 0),
|
|
(byte)(Incremental ? 1 : 0),
|
|
(byte)(pino?.Barramento ?? TiposBarramentos.CONTROLADOR),
|
|
(byte)(pino?.Pino ?? PinoutUtils.PINO_INVALIDO)
|
|
};
|
|
|
|
return (CanMessagePosicaoDados.Config1, config.ToArray());
|
|
}
|
|
|
|
public (int, CanMessagePosicaoDados, byte[]) ProtocoloComando()
|
|
{
|
|
return (
|
|
ID_Num,
|
|
CanMessagePosicaoDados.Command1,
|
|
new byte[]
|
|
{
|
|
(byte)_AnguloControle
|
|
}
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
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 override bool Equals(object obj)
|
|
{
|
|
if (obj == null || GetType() != obj.GetType())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
SensorSinaleiroComportamentoModel other = (SensorSinaleiroComportamentoModel)obj;
|
|
return statusLED == other.statusLED &&
|
|
QtdPiscadas == other.QtdPiscadas &&
|
|
TempoMs == other.TempoMs &&
|
|
PausaMs == other.PausaMs;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
unchecked // Permite overflow de inteiros
|
|
{
|
|
int hash = 17;
|
|
hash = hash * 23 + statusLED.GetHashCode();
|
|
hash = hash * 23 + QtdPiscadas.GetHashCode();
|
|
hash = hash * 23 + TempoMs.GetHashCode();
|
|
hash = hash * 23 + PausaMs.GetHashCode();
|
|
return hash;
|
|
}
|
|
}
|
|
|
|
public static bool operator ==(SensorSinaleiroComportamentoModel left, SensorSinaleiroComportamentoModel right)
|
|
{
|
|
if (ReferenceEquals(left, right))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ((object)left == null || (object)right == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return left.Equals(right);
|
|
}
|
|
|
|
public static bool operator !=(SensorSinaleiroComportamentoModel left, SensorSinaleiroComportamentoModel right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
}
|
|
|
|
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; }
|
|
}
|
|
|
|
public class SensorParametroModel
|
|
{
|
|
public string Descricao { get; set; }
|
|
public string Valor { get; set; }
|
|
public int QtdBytes { get; set; }
|
|
public bool Escalar { get; set; }
|
|
public bool Enviar { get; set; }
|
|
|
|
|
|
public byte[] InterpretarParametro()
|
|
{
|
|
string valor = Valor.Replace(",", ".").Trim();
|
|
|
|
// HEX direto
|
|
if (valor.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
byte parsedHex = Convert.ToByte(valor, 16);
|
|
return QtdBytes == 2 ? new byte[] { 0x00, parsedHex } : new byte[] { parsedHex };
|
|
}
|
|
|
|
// FLOAT com ou sem ponto
|
|
float f = float.Parse(valor, CultureInfo.InvariantCulture);
|
|
ushort final = (ushort)(Escalar ? f * 100 : f);
|
|
|
|
if (QtdBytes == 1)
|
|
return new byte[] { (byte)(final & 0xFF) };
|
|
|
|
return new byte[] {
|
|
(byte)(final >> 8),
|
|
(byte)(final & 0xFF)
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
public class FirmwareSensorTemperatura : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
private double _temperatura;
|
|
public double Temperatura {
|
|
get
|
|
{
|
|
return _temperatura;
|
|
}
|
|
set
|
|
{
|
|
_temperatura = value;
|
|
AtualizarExtremos();
|
|
}
|
|
}
|
|
public double ValorMaximo { get; set; }
|
|
public double ValorMinimo { get; set; }
|
|
public double LimiteMaximo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMaximo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
public double LimiteMinimo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMinimo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private void AtualizarExtremos()
|
|
{
|
|
if (_temperatura > ValorMaximo)
|
|
{
|
|
ValorMaximo = _temperatura;
|
|
}
|
|
if (_temperatura < ValorMinimo || ValorMinimo == 0)
|
|
{
|
|
ValorMinimo = _temperatura;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FirmwareSensorCorrente : IFirmwareSensorBase
|
|
{
|
|
private DateTime _momento;
|
|
public DateTime Momento
|
|
{
|
|
get
|
|
{
|
|
return _momento;
|
|
}
|
|
set
|
|
{
|
|
double dt = (value - _momento).TotalSeconds;
|
|
if (dt > 0)
|
|
{
|
|
_dt = dt;
|
|
}
|
|
_momento = value;
|
|
}
|
|
}
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public double BusVoltage { get; set; }
|
|
public double ShuntVoltage { get; set; }
|
|
public double Current
|
|
{
|
|
get
|
|
{
|
|
return _atual;
|
|
}
|
|
set
|
|
{
|
|
_atual = value;
|
|
|
|
if (ID == "AB36V")
|
|
{
|
|
Variaveis.OperacaoEmAndamento.DispSen?.Dados?.ContadorCarga?.AtualizarConsumo();
|
|
}
|
|
|
|
AtualizarExtremos();
|
|
}
|
|
}
|
|
public double Power { get; set; }
|
|
public double Energy { get; set; }
|
|
public double Temperature { get; set; }
|
|
|
|
public double ValorMaximo { get; set; }
|
|
public double ValorMinimo { get; set; }
|
|
public double LimiteMaximo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMaximo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
public double LimiteMinimo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMinimo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public double _dt { get; set; }
|
|
|
|
private double _atual = 0;
|
|
private void AtualizarExtremos()
|
|
{
|
|
if (_atual > ValorMaximo)
|
|
{
|
|
ValorMaximo = _atual;
|
|
}
|
|
if (_atual < ValorMinimo || ValorMinimo == 0)
|
|
{
|
|
ValorMinimo = _atual;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FirmwareSinaleiro : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public StatusLED Leitura { get; set; }
|
|
public StatusLED Comando { get; set; }
|
|
}
|
|
|
|
public class FirmwareRele : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public Estado Leitura { get; set; }
|
|
}
|
|
|
|
public class FirmwareServoFreio : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public int Angulo { get; set; }
|
|
}
|
|
|
|
public class FirmwareSensorFluxo : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
private double _vazao;
|
|
public double Vazao
|
|
{
|
|
get
|
|
{
|
|
return _vazao;
|
|
}
|
|
set
|
|
{
|
|
_vazao = value;
|
|
AtualizarExtremos();
|
|
}
|
|
}
|
|
public double ValorMaximo { get; set; }
|
|
public double ValorMinimo { get; set; }
|
|
public double LimiteMaximo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispAtu.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMaximo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
public double LimiteMinimo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispAtu.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMinimo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private void AtualizarExtremos()
|
|
{
|
|
if (_vazao > ValorMaximo)
|
|
{
|
|
ValorMaximo = _vazao;
|
|
}
|
|
if (_vazao < ValorMinimo || ValorMinimo == 0)
|
|
{
|
|
ValorMinimo = _vazao;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class FirmwareSensorMassa : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
private double _massa;
|
|
public double Massa
|
|
{
|
|
get
|
|
{
|
|
return _massa;
|
|
}
|
|
set
|
|
{
|
|
_massa = value;
|
|
AtualizarExtremos();
|
|
}
|
|
}
|
|
public double ValorMaximo { get; set; }
|
|
public double ValorMinimo { get; set; }
|
|
public double LimiteMaximo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispAtu.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMaximo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
public double LimiteMinimo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispAtu.Dados.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMinimo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private void AtualizarExtremos()
|
|
{
|
|
if (_massa > ValorMaximo)
|
|
{
|
|
ValorMaximo = _massa;
|
|
}
|
|
if (_massa < ValorMinimo || ValorMinimo == 0)
|
|
{
|
|
ValorMinimo = _massa;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class FirmwareSensorPressao : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public double Pressao { get; set; }
|
|
}
|
|
|
|
public class FirmwareSensorPotenciometro : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public int Leitura { get; set; }
|
|
public double Angulo { get; set; }
|
|
public Sentido Sentido { get; set; }
|
|
public bool LimiteExcedido { get; set; }
|
|
public StatusSensorPotenciometro Status
|
|
{
|
|
get
|
|
{
|
|
// Verifica se há um sensor ativo
|
|
SensorModel Sensor = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.Sensores?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (Sensor == null)
|
|
{
|
|
return StatusSensorPotenciometro.Indefinido;
|
|
}
|
|
|
|
// Verifica se os parâmetros necessários existem
|
|
/*if (Sensor.Parametros.Count <= 3 || Sensor.Parametros[1].Count() <= 1 || Sensor.Parametros[3].Count() <= 1)
|
|
{
|
|
return StatusSensorPotenciometro.Indefinido;
|
|
}*/
|
|
|
|
if (LimiteExcedido && Sentido == Sentido.Horario)
|
|
{
|
|
return StatusSensorPotenciometro.LimiteDireita;
|
|
}
|
|
if (LimiteExcedido && Sentido == Sentido.Antihorario)
|
|
{
|
|
return StatusSensorPotenciometro.LimiteEsquerdo;
|
|
}
|
|
if (Sentido == Sentido.Parado)
|
|
{
|
|
return StatusSensorPotenciometro.Centro;
|
|
}
|
|
if (Sentido == Sentido.Antihorario)
|
|
{
|
|
return StatusSensorPotenciometro.AEsquerda;
|
|
}
|
|
if (Sentido == Sentido.Horario)
|
|
{
|
|
return StatusSensorPotenciometro.ADireita;
|
|
}
|
|
|
|
return StatusSensorPotenciometro.Indefinido;
|
|
}
|
|
}
|
|
|
|
public double LimiteMaximo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.Sensores?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMaximo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
public double LimiteMinimo
|
|
{
|
|
get
|
|
{
|
|
var sensor = Variaveis.OperacaoEmAndamento.DispSen?.Dados?.Sensores?.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
if (sensor != null)
|
|
{
|
|
return sensor.LimiteMinimo;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FirmwareSensorIMU : IFirmwareSensorBase
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ID { get; set; }
|
|
public int ID_Num { get; set; }
|
|
public bool Iniciado { get; set; }
|
|
public double AccX { get; set; }
|
|
public double AccY { get; set; }
|
|
public double AccZ { get; set; }
|
|
public double GyrX { get; set; }
|
|
public double GyrY { get; set; }
|
|
public double GyrZ { get; set; }
|
|
public double MagX { get; set; }
|
|
public double MagY { get; set; }
|
|
public double MagZ { get; set; }
|
|
public double Temperatura { get; set; }
|
|
public double Pressao { get; set; }
|
|
public double Altitude { get; set; }
|
|
public double Roll { get; set; }
|
|
public double Pitch { get; set; }
|
|
public double Yaw { get; set; }
|
|
public double YawFixed
|
|
{
|
|
get
|
|
{
|
|
double angulo = Yaw;
|
|
|
|
// ✅ Aplica o offset armazenado
|
|
return GPSUtils.NormalizarAngulo(GPSUtils.NormalizarAngulo(angulo) + Offset);
|
|
}
|
|
}
|
|
|
|
public double Offset { get; set; } = 0.0;
|
|
private DateTime ultimaAtualizacaoOffset = DateTime.MinValue;
|
|
public void AtualizarOffset(GPSModel UltimaLeitura)
|
|
{
|
|
// Verifica se a qualidade do GPS é boa
|
|
if (UltimaLeitura.QualidadeFix == TiposCorrecaoGPS.RTKFixo || UltimaLeitura.QualidadeFix == TiposCorrecaoGPS.RTKFlutuante)
|
|
{
|
|
// Só atualiza o offset se a última atualização foi há pelo menos 2 segundos (evita variações rápidas)
|
|
if ((DateTime.Now - ultimaAtualizacaoOffset).TotalSeconds > 2)
|
|
{
|
|
// Suaviza a mudança do offset para evitar grandes variações
|
|
double novoOffset = UltimaLeitura.OrientacaoReal - YawFixed;
|
|
Offset = (Offset * 0.8) + (novoOffset * 0.2); // Filtro exponencial
|
|
|
|
// Atualiza o tempo da última atualização
|
|
ultimaAtualizacaoOffset = DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FirmwareSensorIMU Clone()
|
|
{
|
|
return new FirmwareSensorIMU()
|
|
{
|
|
ID_Num = ID_Num,
|
|
Iniciado = Iniciado,
|
|
Pitch = Pitch,
|
|
Roll = Roll,
|
|
Yaw = Yaw,
|
|
Temperatura = Temperatura,
|
|
Altitude = Altitude,
|
|
Pressao = Pressao,
|
|
AccX = AccX,
|
|
AccY = AccY,
|
|
AccZ = AccZ,
|
|
MagX = MagX,
|
|
MagY = MagY,
|
|
MagZ = MagZ,
|
|
GyrX = GyrX,
|
|
GyrY = GyrY,
|
|
GyrZ = GyrZ,
|
|
Offset = Offset,
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
public class SensoriamentoMensagemJsonModel
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public bool Conectado { get; set; }
|
|
public T_Code Dispositivo { get; set; }
|
|
public int Versao { get; set; }
|
|
public string Mod_ID { get; set; }
|
|
public int QualidadeWifi { get; set; }
|
|
public int LatenciaLoop { get; set; }
|
|
public List<FirmwareSensorTemperatura> SensoresTemperatura { get; set; } = new List<FirmwareSensorTemperatura>();
|
|
public List<FirmwareSensorCorrente> SensoresCorrente { get; set; } = new List<FirmwareSensorCorrente>();
|
|
public List<FirmwareSinaleiro> Sinaleiros { get; set; } = new List<FirmwareSinaleiro>();
|
|
public List<FirmwareRele> Reles { get; set; } = new List<FirmwareRele>();
|
|
public List<FirmwareServoFreio> ServoFreios { get; set; } = new List<FirmwareServoFreio>();
|
|
public List<FirmwareSensorIMU> SensoresIMU { get; set; } = new List<FirmwareSensorIMU>();
|
|
public DateTime UltimoComandoRespondido { get; set; }
|
|
}
|
|
|
|
public class SenHandler : ICanMessageHandler
|
|
{
|
|
private double ConverterByteParaDouble(byte[] data, int idx)
|
|
{
|
|
int raw = (data[idx] << 8) | data[idx + 1];
|
|
short _short = (short)raw;
|
|
double _value = _short / 100.0;
|
|
|
|
return _value;
|
|
}
|
|
|
|
private double ConverterValorEscala(byte[] data, int idx, double escala)
|
|
{
|
|
int raw = (data[idx] << 8) | data[idx + 1];
|
|
return raw * escala;
|
|
}
|
|
|
|
private S_Code DetectarComponenteDoID_Num(int ID_Num)
|
|
{
|
|
if (ID_Num == Variaveis.ID_Num_sMOD) return S_Code.sMOD;
|
|
if (ID_Num == Variaveis.ID_Num_sTOD) return S_Code.sTOD;
|
|
if (ID_Num == Variaveis.ID_Num_sLRA) return S_Code.sLRA;
|
|
|
|
var Disp = Variaveis.OperacaoEmAndamento.DispSen.Dados;
|
|
var sensor = Disp.Sensores.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
var rele = Disp.Reles.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
var sinaleiro = Disp.Sinaleiros.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
var servo = Disp.Servos.FirstOrDefault(x => x.ID_Num == ID_Num);
|
|
|
|
return sensor?.Componente ?? rele?.Componente ?? sinaleiro?.Componente ?? servo?.Componente ?? S_Code.sVZO;
|
|
}
|
|
|
|
private void DefinirOuCriar(int CompID_Num, CanMessagePosicaoDados Posicao, S_Code componente, List<dynamic> Parametros)
|
|
{
|
|
var Disp = Variaveis.OperacaoEmAndamento.DispSen;
|
|
var item = Disp.Dados.DadosLeitura;
|
|
|
|
switch (componente)
|
|
{
|
|
case S_Code.sTMP:
|
|
{
|
|
var sensor = Disp.Dados.Sensores.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.SensoresTemperatura.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareSensorTemperatura()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
Temperatura = -1,
|
|
};
|
|
item.SensoresTemperatura.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.Temperatura = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sCOR:
|
|
{
|
|
var sensor = Disp.Dados.Sensores.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.SensoresCorrente.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareSensorCorrente()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
BusVoltage = -1,
|
|
Current = -1,
|
|
Power = -1,
|
|
ShuntVoltage = -1,
|
|
};
|
|
item.SensoresCorrente.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.BusVoltage = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.Current = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.Power = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados2:
|
|
{
|
|
_sensor.Energy = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.Temperature = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sLED:
|
|
{
|
|
var sensor = Disp.Dados.Sinaleiros.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.Sinaleiros.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareSinaleiro()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
Comando = StatusLED.Apagado,
|
|
Leitura = StatusLED.Apagado,
|
|
};
|
|
item.Sinaleiros.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.Leitura = Parametros.Count > 0 ? Parametros[0] : StatusLED.Apagado;
|
|
_sensor.Comando = Parametros.Count > 1 ? Parametros[1] : StatusLED.Apagado;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sRLE:
|
|
{
|
|
var sensor = Disp.Dados.Reles.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.Reles.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareRele()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
Leitura = Estado.Desligado,
|
|
};
|
|
item.Reles.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.Leitura = Parametros.Count > 0 ? Parametros[0] : Estado.Desligado;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sFRO:
|
|
{
|
|
var sensor = Disp.Dados.Servos.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.ServoFreios.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareServoFreio()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
Angulo = -1,
|
|
};
|
|
item.ServoFreios.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.Angulo = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sIMU:
|
|
{
|
|
var sensor = Disp.Dados.Sensores.FirstOrDefault(x => x.ID_Num == CompID_Num);
|
|
var _sensor = item.SensoresIMU.FirstOrDefault(x => x.ID_Num == sensor.ID_Num);
|
|
if (_sensor == null)
|
|
{
|
|
_sensor = new FirmwareSensorIMU()
|
|
{
|
|
ID = sensor.ID,
|
|
ID_Num = CompID_Num,
|
|
Iniciado = false,
|
|
AccX = -1,
|
|
AccY = -1,
|
|
AccZ = -1,
|
|
GyrX = -1,
|
|
GyrY = -1,
|
|
GyrZ = -1,
|
|
MagX = -1,
|
|
MagY = -1,
|
|
MagZ = -1,
|
|
Altitude = -1,
|
|
Temperatura = -1,
|
|
Pressao = -1,
|
|
Pitch = -1,
|
|
Roll = -1,
|
|
Yaw = -1,
|
|
Offset = 0,
|
|
};
|
|
item.SensoresIMU.Add(_sensor);
|
|
}
|
|
if (_sensor != null)
|
|
{
|
|
switch (Posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
_sensor.Iniciado = Parametros.Count > 0 ? Parametros[0] : false;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
_sensor.Roll = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.Pitch = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.Yaw = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados2:
|
|
{
|
|
_sensor.Temperatura = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.Pressao = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.Altitude = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados3:
|
|
{
|
|
_sensor.AccX = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.AccY = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.AccZ = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados4:
|
|
{
|
|
_sensor.GyrX = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.GyrY = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.GyrZ = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados5:
|
|
{
|
|
_sensor.MagX = Parametros.Count > 0 ? Parametros[0] : -1;
|
|
_sensor.MagY = Parametros.Count > 1 ? Parametros[1] : -1;
|
|
_sensor.MagZ = Parametros.Count > 2 ? Parametros[2] : -1;
|
|
break;
|
|
}
|
|
}
|
|
_sensor.Momento = DateTime.Now;
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void ProcessarMensagem(CanMessage mensagem)
|
|
{
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)mensagem.DataRx[0];
|
|
byte ID_Num = mensagem.DataRx[1];
|
|
var data = mensagem.DataRx;
|
|
|
|
var item = Variaveis.OperacaoEmAndamento.DispSen.Dados.DadosLeitura;
|
|
|
|
S_Code componente = DetectarComponenteDoID_Num(ID_Num);
|
|
|
|
Console.WriteLine($"[SEN] Componente={componente.ToString()}, ID_Num={ID_Num}, Posicao={posicao.ToString()}, Data={FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
|
|
switch (posicao)
|
|
{
|
|
case CanMessagePosicaoDados.Status:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sMOD:
|
|
{
|
|
T_Code tipoModulo = (T_Code)data[2];
|
|
bool conectado = data[3] == 1;
|
|
int versao = data[4];
|
|
|
|
Console.WriteLine($"[SEN] Resposta {ID_Num} recebida. Tipo={tipoModulo}, Conectado={conectado}, Versão={versao}");
|
|
|
|
item.Conectado = conectado;
|
|
item.Dispositivo = tipoModulo;
|
|
item.Versao = versao;
|
|
break;
|
|
}
|
|
case S_Code.sLRA:
|
|
{
|
|
bool Conectado = data[2] == 1;
|
|
bool Configurado = data[3] == 1;
|
|
Console.WriteLine($"[LRA] Resposta {ID_Num} recebida. Conectado={Conectado}, Configurado={Configurado}");
|
|
var loraService = Variaveis.OperacaoEmAndamento.DispSen.Dados.loRaService;
|
|
loraService.Conectado = Conectado;
|
|
loraService.Configurado = Configurado;
|
|
loraService.Parametros.UltimaLeitura = DateTime.Now;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
bool Iniciado = data[2] == 1;
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { Iniciado });
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.DadosAll:
|
|
{
|
|
if (componente == S_Code.sTOD)
|
|
{
|
|
int latenciaLoop = data[2];
|
|
Variaveis.OperacaoEmAndamento.DispSen.Dados.DadosLeitura.LatenciaLoop = latenciaLoop;
|
|
|
|
Console.WriteLine($"[SEN] Ciclo concluido. Latencia de loop: {latenciaLoop} ms");
|
|
}
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados1:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sTMP:
|
|
{
|
|
double temperatura = ConverterByteParaDouble(data, 2);
|
|
Console.WriteLine("temperatura ET: " + temperatura);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { temperatura });
|
|
break;
|
|
}
|
|
case S_Code.sCOR:
|
|
{
|
|
double busVoltage = ConverterByteParaDouble(data, 2);
|
|
double current = ConverterByteParaDouble(data, 4) * 10;
|
|
double power = ConverterByteParaDouble(data, 6);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { busVoltage, current, power });
|
|
break;
|
|
}
|
|
case S_Code.sLED:
|
|
{
|
|
StatusLED statusLeitura = (StatusLED)data[2];
|
|
StatusLED statusComando = (StatusLED)data[3];
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { statusLeitura, statusComando });
|
|
break;
|
|
}
|
|
case S_Code.sRLE:
|
|
{
|
|
Estado estado = (Estado)data[2];
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { estado });
|
|
break;
|
|
}
|
|
case S_Code.sFRO:
|
|
{
|
|
int angulo = data[2];
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { angulo });
|
|
break;
|
|
}
|
|
case S_Code.sIMU:
|
|
{
|
|
double roll = ConverterByteParaDouble(data, 2);
|
|
double pitch = ConverterByteParaDouble(data, 4);
|
|
double yaw = ConverterByteParaDouble(data, 6);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { roll, pitch, yaw });
|
|
break;
|
|
}
|
|
case S_Code.sLRA:
|
|
{
|
|
byte address = data[2];
|
|
byte baudAndAirRate = data[3];
|
|
byte baud = (byte)(baudAndAirRate & 0b11100000);
|
|
byte airRate = (byte)((baudAndAirRate & 0b00011100) >> 2);
|
|
byte packetSizeAndPower = data[4];
|
|
byte packetSize = (byte)((packetSizeAndPower & 0b11000000));
|
|
byte power = (byte)(packetSizeAndPower & 0b00000011);
|
|
byte channel = data[5];
|
|
byte tranModeAndWorCycle = data[6];
|
|
byte tranMode = (byte)(tranModeAndWorCycle & 0b01000000);
|
|
byte worCycle = (byte)(tranModeAndWorCycle & 0b00000111);
|
|
var loraService = Variaveis.OperacaoEmAndamento.DispSen.Dados.loRaService;
|
|
Console.WriteLine($"[LRA] Resposta {ID_Num} recebida. Address={address.ToString("X")}, BaudRate={baud.ToString("X")}, AirRate={airRate.ToString("X")}, PacketSize={packetSize.ToString("X")}, Power={power.ToString("X")}, Channel={channel.ToString("X")}, transMode={tranMode.ToString("X")}, WorCycle={worCycle.ToString("X")}");
|
|
loraService.Parametros.address = address;
|
|
loraService.Parametros.baudRate = baud;
|
|
loraService.Parametros.airRate = airRate;
|
|
loraService.Parametros.packetSize = packetSize;
|
|
loraService.Parametros.power = power;
|
|
loraService.Parametros.channel = channel;
|
|
loraService.Parametros.worCycle = worCycle;
|
|
loraService.Parametros.tranMode = tranMode;
|
|
loraService.Parametros.UltimaLeitura = DateTime.Now;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
Console.WriteLine($"[SEN] Resposta de {ID_Num} na posicao {posicao.ToString()} recebida. Parametros nao mapeados: {FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados2:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sCOR:
|
|
{
|
|
double energia = ConverterByteParaDouble(data, 2);
|
|
double temperatura = ConverterByteParaDouble(data, 4);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { energia, temperatura });
|
|
break;
|
|
}
|
|
case S_Code.sIMU:
|
|
{
|
|
double temperatura = ConverterByteParaDouble(data, 2);
|
|
double pressao = ConverterValorEscala(data, 4, 10);
|
|
double altitude = ConverterValorEscala(data, 6, 10);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { temperatura, pressao, altitude });
|
|
break;
|
|
}
|
|
case S_Code.sLRA:
|
|
{
|
|
Console.WriteLine("Dados LORA recebidos da base: " + FuncoesGlobais.ConverterComandoBytesParaTexto(data));
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
Console.WriteLine($"[SEN] Resposta de {ID_Num} na posicao {posicao.ToString()} recebida. Parametros nao mapeados: {FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados3:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sIMU:
|
|
{
|
|
double accX = ConverterByteParaDouble(data, 2);
|
|
double accY = ConverterByteParaDouble(data, 4);
|
|
double accZ = ConverterByteParaDouble(data, 6);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { accX, accY, accZ});
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
Console.WriteLine($"[SEN] Resposta de {ID_Num} na posicao {posicao.ToString()} recebida. Parametros nao mapeados: {FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados4:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sIMU:
|
|
{
|
|
double gyrX = ConverterByteParaDouble(data, 2);
|
|
double gyrY = ConverterByteParaDouble(data, 4);
|
|
double gyrZ = ConverterByteParaDouble(data, 6);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { gyrX, gyrY, gyrZ });
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
Console.WriteLine($"[SEN] Resposta de {ID_Num} na posicao {posicao.ToString()} recebida. Parametros nao mapeados: {FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados.Dados5:
|
|
{
|
|
switch (componente)
|
|
{
|
|
case S_Code.sIMU:
|
|
{
|
|
double magX = ConverterByteParaDouble(data, 2);
|
|
double magY = ConverterByteParaDouble(data, 4);
|
|
double magZ = ConverterByteParaDouble(data, 6);
|
|
DefinirOuCriar(ID_Num, posicao, componente, new List<dynamic>() { magX, magY, magZ });
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
Console.WriteLine($"[SEN] Resposta de {ID_Num} na posicao {posicao.ToString()} recebida. Parametros nao mapeados: {FuncoesGlobais.ConverterComandoBytesParaTexto(data)}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
item.UltimoComandoRespondido = DateTime.Now;
|
|
}
|
|
}
|
|
|
|
}
|