120 lines
3.8 KiB
C#
120 lines
3.8 KiB
C#
using AgroBase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AgroBase.Services
|
|
{
|
|
public class CoulombCounterService
|
|
{
|
|
public CoulombCounterService(double _max_Amp)
|
|
{
|
|
double coulombsMax = _max_Amp * 3600;
|
|
CargaMaxima = coulombsMax;
|
|
//double consumo = (Variaveis.OperacaoEmAndamento.DispSen?.Dados?.BateriaCargaConsumida ?? 0);
|
|
//Consumido = consumo < CargaMaxima ? consumo : 0;
|
|
Consumido = 0;
|
|
CargaAtual = CargaMaxima - Consumido;
|
|
//double consumoW = (Variaveis.OperacaoEmAndamento.DispSen?.Dados?.BateriaCargaConsumidaWatts ?? 0);
|
|
//ConsumidoWatts = consumoW;
|
|
}
|
|
|
|
public double TempoEstimadoMinutos { get; private set; }
|
|
private const int BufferSize = 30; // últimos 3 segundos se chamada a cada 100ms
|
|
private readonly Queue<double> _historicoCorrente = new Queue<double>();
|
|
|
|
private double CargaMaxima { get; set; }
|
|
private double CargaAtual { get; set; }
|
|
public double CargaAtualAmp
|
|
{
|
|
get
|
|
{
|
|
return CargaAtual / 3600.0;
|
|
}
|
|
}
|
|
private double Consumido { get; set; }
|
|
public double _Consumido
|
|
{
|
|
get
|
|
{
|
|
return Consumido;
|
|
}
|
|
}
|
|
public double ConsumidoAmp
|
|
{
|
|
get
|
|
{
|
|
return Consumido / 3600.0;
|
|
}
|
|
}
|
|
public double PercentualAtual
|
|
{
|
|
get
|
|
{
|
|
return (CargaAtual / CargaMaxima) * 100;
|
|
}
|
|
}
|
|
public double PercentualConsumido
|
|
{
|
|
get
|
|
{
|
|
return (Consumido / CargaMaxima) * 100;
|
|
}
|
|
}
|
|
public double PercentualBateria { get; private set; }
|
|
public double ConsumidoWatts { get; set; }
|
|
|
|
public void AtualizarConsumo()
|
|
{
|
|
var Bateria = Variaveis.OperacaoEmAndamento.Parametros.DadosLeitura.Bateria;
|
|
|
|
if (!Bateria.Iniciado)
|
|
{
|
|
return;
|
|
}
|
|
|
|
PercentualBateria = Math.Round(FuncoesMatematicas.Map(Bateria.TensaoInstantanea, VariaveisEquipamento.TensaoMinimaBateria, VariaveisEquipamento.TensaoMaximaBateria, 0.0, 100.0), 2);
|
|
|
|
// Verifique se as datas são válidas (não MinValue, MaxValue ou valores anômalos)
|
|
if (Bateria._dt == 0 || Bateria._dt > 10)
|
|
{
|
|
Console.WriteLine("Erro: Leitura inválida detectada.");
|
|
return;
|
|
}
|
|
|
|
// Calcule os coulombs e atualize os valores
|
|
double coulombsLeitura = Bateria.CorrenteInstantanea * Bateria._dt;
|
|
|
|
Consumido += coulombsLeitura;
|
|
CargaAtual -= coulombsLeitura;
|
|
|
|
ConsumidoWatts += Bateria.PotenciaAcumulada - ConsumidoWatts;
|
|
|
|
// Armazena a corrente instantânea
|
|
_historicoCorrente.Enqueue(Bateria.CorrenteInstantanea);
|
|
|
|
if (_historicoCorrente.Count > BufferSize)
|
|
_historicoCorrente.Dequeue();
|
|
|
|
// Calcular corrente média (em A)
|
|
double correnteMedia = _historicoCorrente.Average();
|
|
|
|
// Verifica se a tensão está acima da mínima
|
|
if (Bateria.TensaoInstantanea > VariaveisEquipamento.TensaoMinimaBateria && correnteMedia > 0)
|
|
{
|
|
TempoEstimadoMinutos = (CargaAtualAmp / correnteMedia) * 60;
|
|
}
|
|
else
|
|
{
|
|
TempoEstimadoMinutos = 0;
|
|
}
|
|
}
|
|
|
|
public void AtualizarValorConsumido(double _consumo_Amp)
|
|
{
|
|
double coulombsConsumidos = _consumo_Amp * 3600;
|
|
Consumido = coulombsConsumidos;
|
|
}
|
|
}
|
|
}
|