183 lines
4.7 KiB
C++
183 lines
4.7 KiB
C++
#include "SerialService.h"
|
|
#include "Pinout.h"
|
|
#include "Utils.h"
|
|
#include "IComponenteCAN.h"
|
|
|
|
#ifndef ReleModel
|
|
#define ReleModel
|
|
|
|
class Rele : public ComponenteCAN {
|
|
public:
|
|
|
|
Rele(String _modID, String _id) {
|
|
Mod_ID = _modID;
|
|
_ID = _id;
|
|
}
|
|
|
|
String Mod_ID;
|
|
String _ID;
|
|
int ID_Num;
|
|
PinoModel _pino;
|
|
bool Iniciado = false;
|
|
|
|
bool AtuacaoNivelAlto = true;
|
|
|
|
Estado _Status = Desligado;
|
|
|
|
void Inicializar() {
|
|
if (Iniciado) {
|
|
MostrarLog("Rele ja inicializado");
|
|
return;
|
|
}
|
|
|
|
_pino.Conectar(!AtuacaoNivelAlto);
|
|
|
|
Iniciado = _pino.Definido();
|
|
|
|
if (Iniciado) {
|
|
MostrarLog("Rele iniciado");
|
|
}
|
|
else
|
|
{
|
|
MostrarLog("Rele nao iniciado");
|
|
}
|
|
|
|
}
|
|
|
|
void Desligar() {
|
|
if (!Iniciado) {
|
|
MostrarLog("Rele nao esta inicializado");
|
|
return;
|
|
}
|
|
|
|
// Redefinir as configurações para os valores iniciais
|
|
_pino.Desconectar();
|
|
|
|
MostrarLog("Rele Desligado");
|
|
|
|
Iniciado = false;
|
|
}
|
|
|
|
void AtualizarStatus(Estado novoEstado) {
|
|
if (Iniciado) {
|
|
bool status = novoEstado == Ligado;
|
|
if (!AtuacaoNivelAlto) {
|
|
status = !status;
|
|
}
|
|
|
|
MostrarLog("Atualizando status do rele de " + String(_Status) + " para " + String(novoEstado));
|
|
|
|
_pino.set(status);
|
|
_Status = (Estado)_pino.get();
|
|
}
|
|
}
|
|
|
|
void AtualizarLeitura() {
|
|
if (Iniciado) {
|
|
_Status = (Estado)_pino.get();
|
|
}
|
|
}
|
|
|
|
void RequisitarDados() {
|
|
AtualizarLeitura();
|
|
}
|
|
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
|
std::vector<uint8_t> data;
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
|
data.push_back(ID_Num);
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Status: {
|
|
data.push_back(Iniciado ? 1 : 0);
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados::Dados1: {
|
|
data.push_back(static_cast<uint8_t>(_Status));
|
|
break;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
static std::vector<uint8_t> ConfigurarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data, const String& Mod_ID) {
|
|
std::vector<uint8_t> status;
|
|
if (data.size() < 2) return status;
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0];
|
|
uint8_t idNum = data[1];
|
|
auto it = std::find_if(lista.begin(), lista.end(), [idNum](Rele* s) { return s->ID_Num == idNum; });
|
|
bool jaExiste = it != lista.end();
|
|
Rele* sensor;
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Config1: {
|
|
if (data.size() < 7) return status;
|
|
bool conectar = data[3] == 1;
|
|
bool atuacaoNivelAlto = data[4] == 1;
|
|
TiposBarramentos tipoBarramento = (TiposBarramentos)data[5];
|
|
int pino = data[6];
|
|
if (conectar) {
|
|
if (!jaExiste) {
|
|
sensor = new Rele(Mod_ID, "sRLE_" + String(idNum));
|
|
}
|
|
else {
|
|
sensor = *it;
|
|
}
|
|
if (!sensor->Iniciado) {
|
|
sensor->ID_Num = idNum;
|
|
sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT);
|
|
sensor->AtuacaoNivelAlto = atuacaoNivelAlto;
|
|
sensor->Inicializar();
|
|
if (!jaExiste) {
|
|
lista.push_back(sensor);
|
|
sensor->MostrarLog("Rele adicionado via CAN: sRLE_" + String(idNum));
|
|
}
|
|
}
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
} else {
|
|
if (jaExiste) {
|
|
sensor = *it;
|
|
sensor->Desligar();
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
sensor->MostrarLog("Rele removido via CAN: sRLE_" + String(idNum));
|
|
delete sensor;
|
|
lista.erase(it);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
|
|
static void ComandarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data) {
|
|
if (data.size() < 3) return;
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0];
|
|
uint8_t idNum = data[1];
|
|
for (auto* _rele : lista) {
|
|
if (_rele->ID_Num == idNum) {
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Command1: {
|
|
Estado novoEstado = (Estado)data[2];
|
|
_rele->AtualizarStatus(novoEstado);
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private:
|
|
bool DebugMode = false;
|
|
|
|
void MostrarLog(String mensagem) {
|
|
if (DebugMode) {
|
|
PrintTela("[RLE " + _ID + "] " + mensagem);
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif |