agrobot_base/AgroBase/AgroBase/Models/OIDModel.cs

294 lines
9.4 KiB
C#

using AgroBase.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using static AgroBase.Models.Enums;
namespace AgroBase.Models
{
public class OIDModel
{
public byte Endereco { get; set; }
public bool Iniciado
{
get
{
// Houve comandos recebidos do driver dentro dos ultimos 10 segundos
bool iniciado = UltimoComandoRecebido.AddSeconds(10) > DateTime.Now;
if (!iniciado && Configurado)
{
Configurado = false;
}
return iniciado;
}
}
public bool Configurado { get; set; }
public DateTime UltimoComandoRecebido { get; set; }
public OIDModoControle ModoControle
{
get
{
return (OIDModoControle)ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "ModoControle").Registro));
}
}
public int ControleCorrente
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "ControleCorrente").Registro));
}
}
public int ControleVelocidade
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "ControleVelocidade").Registro));
}
}
public int ControleCicloTrabalho
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "ControleCicloTrabalho").Registro));
}
}
public int ControleCorrenteFreio
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "ControleCorrenteFreio").Registro));
}
}
public int RampaSubidaVelocidade
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "RampaSubidaVelocidade").Registro));
}
}
public int RampaDescidaVelocidade
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "RampaDescidaVelocidade").Registro));
}
}
public int CodigoAlarme
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "CodErro").Registro));
}
}
public int Heartbeat
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "Heartbeat").Registro));
}
}
public double Torque_Max
{
get
{
return ConvertByteArrayToFloat(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "CorrenteTorqueMaximo").Registro));
}
}
public int ERPM
{
get
{
return ConvertByteArrayToInt(GetParametroValue(OIDService.Parametros.First(x => x.Chave == "VelocidadeAngular").Registro));
}
}
public double CorrenteMotor
{
get
{
var P_A = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "CorrenteMotor").Registro);
if (P_A == null)
{
return -1;
}
double corrente = ConvertByteArrayToFloat(P_A.Valor) / P_A.Escala;
return Math.Abs(corrente);
}
}
public double CorrenteBarramento
{
get
{
var P_A = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "CorrenteBarramento").Registro);
if (P_A == null)
{
return -1;
}
double corrente = ConvertByteArrayToFloat(P_A.Valor) / P_A.Escala;
return Math.Abs(corrente);
}
}
public Sentido Direcao
{
get
{
return
ERPM > 0 ? Sentido.Horario :
ERPM < 0 ? Sentido.Antihorario :
Sentido.Parado;
}
}
public double RPM
{
get
{
var P_V = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "VelocidadeAngular").Registro);
if (P_V == null)
{
return 0;
}
double rpm = Math.Abs((double)ERPM / (double)VariaveisEquipamento.NumeroPolosMotor);
return rpm;
}
}
public double Tensao
{
get
{
var P_V = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "Tensao").Registro);
if (P_V == null)
{
return -1;
}
double tensao = ConvertByteArrayToFloat(P_V.Valor) / P_V.Escala;
return tensao;
}
}
public double Temperatura
{
get
{
var P_T = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "Temperatura").Registro);
if (P_T == null)
{
return -1;
}
double temperatura = ConvertByteArrayToFloat(P_T.Valor) / P_T.Escala;
return temperatura;
}
}
public double Potencia
{
get
{
var P_W = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "Potencia").Registro);
if (P_W == null)
{
return -1;
}
double potencia = ConvertByteArrayToFloat(P_W.Valor) / P_W.Escala;
return Math.Abs(potencia);
}
}
public double CicloTrabalho
{
get
{
var P_C = parametros.FirstOrDefault(x => x.Registro == OIDService.Parametros.First(y => y.Chave == "CicloTrabalho").Registro);
if (P_C == null)
{
return -1;
}
double cicloTrabalho = ConvertByteArrayToFloat(P_C.Valor) / P_C.Escala;
return Math.Abs(cicloTrabalho);
}
}
public bool Freado
{
get
{
return ModoControle == OIDModoControle.Freio || ModoControle == OIDModoControle.FreioDeMao;
}
}
public List<OIDParametrosRequisitar> parametros { get; set; }
int ConvertByteArrayToInt(byte[] bytes)
{
if (bytes == null) return 0;
if (bytes.Length == 2)
{
return BitConverter.ToInt16(bytes.Reverse().ToArray(), 0);
}
else if (bytes.Length == 4)
{
return BitConverter.ToInt32(bytes.Reverse().ToArray(), 0);
}
return 0;
}
private static float ConvertByteArrayToFloat(byte[] bytes)
{
if (bytes == null || bytes.Length != 4) return 0;
// Separar os dois registradores (parte inteira e decimal)
short parteInteira = BitConverter.ToInt16(bytes.Take(2).Reverse().ToArray(), 0);
short parteDecimal = BitConverter.ToInt16(bytes.Skip(2).Reverse().ToArray(), 0);
// Construir o valor real com precisão de 4 casas decimais (igual ao Python)
float valor = parteInteira + (parteDecimal / 10000.0f);
return valor;
}
byte[] GetParametroValue(ushort registro)
{
var p = parametros.FirstOrDefault(x => x.Registro == registro);
return p != null ? p.Valor : null;
}
}
public class OIDParametrosRequisitar
{
public ushort Registro { get; set; }
public byte[] Valor { get; set; }
public double Escala { get; set; }
}
public class OIDComandoModel
{
public DateTime Momento { get; set; } = DateTime.Now;
public bool Enviado { get; set; } = false;
public bool ErroEnvio { get; set; } = false;
public bool Respondido { get; set; } = false;
public byte Endereco { get; set; }
public byte[] Comando { get; set; }
public byte[] Resposta { get; set; }
public ModbusParametrosModel Parametro { get; set; }
public float Valor { get; set; }
public bool Get { get; set; }
public bool AguardaResposta { get; set; } = false;
public int TimeoutResposta { get; set; } = 1000;
public int TamanhoEsperado { get; set; } = 8;
}
public enum OIDModoControle
{
Corrente = 0,
Velocidade = 1,
CicloTrabalho = 2,
PosicaoAbsoluta = 3,
PosicaoRelativa = 4,
PosicaoCorrente = 5,
Freio = 6,
FreioDeMao = 7,
RetornoZero = 8,
RetornoZeroParada = 9,
Rampa = 10
}
}