2024-11-27 17:38:56 +00:00
|
|
|
#include "SerialService.h"
|
|
|
|
|
#include "Pinout.h"
|
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
|
|
#ifndef ReleModel
|
|
|
|
|
#define ReleModel
|
|
|
|
|
|
|
|
|
|
class Rele {
|
|
|
|
|
public:
|
|
|
|
|
|
2025-04-14 12:52:19 +00:00
|
|
|
static std::vector<uint8_t> ConfigurarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data, const String& Mod_ID);
|
2025-04-11 20:13:15 +00:00
|
|
|
static void ComandarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data, uint8_t& id);
|
2024-11-29 20:28:25 +00:00
|
|
|
|
|
|
|
|
Rele(String _modID, String _id) {
|
2024-11-27 17:38:56 +00:00
|
|
|
Mod_ID = _modID;
|
|
|
|
|
_ID = _id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Mod_ID;
|
|
|
|
|
String _ID;
|
2025-04-09 21:11:40 +00:00
|
|
|
int ID_Num;
|
2024-11-27 17:38:56 +00:00
|
|
|
PinoModel _pino;
|
|
|
|
|
bool Iniciado = false;
|
|
|
|
|
|
2024-11-29 20:28:25 +00:00
|
|
|
bool AtuacaoNivelAlto = true;
|
|
|
|
|
|
2024-11-27 17:38:56 +00:00
|
|
|
Estado _Status = Desligado;
|
|
|
|
|
|
|
|
|
|
void Inicializar() {
|
|
|
|
|
if (Iniciado) {
|
2024-11-29 20:28:25 +00:00
|
|
|
PrintTela(_ID + " ja inicializado");
|
2024-11-27 17:38:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 21:11:40 +00:00
|
|
|
_pino.Conectar(!AtuacaoNivelAlto);
|
2024-11-27 17:38:56 +00:00
|
|
|
|
2025-04-09 21:11:40 +00:00
|
|
|
Iniciado = _pino.Definido();
|
|
|
|
|
|
|
|
|
|
if (Iniciado) {
|
2024-11-29 20:28:25 +00:00
|
|
|
PrintTela(_ID + " iniciado");
|
|
|
|
|
}
|
2025-04-09 21:11:40 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PrintTela(_ID + " nao iniciado");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 17:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Desligar() {
|
|
|
|
|
if (!Iniciado) {
|
2024-11-29 20:28:25 +00:00
|
|
|
PrintTela(_ID + " nao esta inicializado");
|
2024-11-27 17:38:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redefinir as configurações para os valores iniciais
|
|
|
|
|
_pino.Desconectar();
|
|
|
|
|
|
2024-11-29 20:28:25 +00:00
|
|
|
PrintTela(_ID + " Desligado");
|
2024-11-27 17:38:56 +00:00
|
|
|
|
|
|
|
|
Iniciado = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AtualizarStatus(Estado novoEstado) {
|
2025-04-09 21:11:40 +00:00
|
|
|
if (Iniciado) {
|
|
|
|
|
bool status = novoEstado == Ligado;
|
|
|
|
|
if (!AtuacaoNivelAlto) {
|
|
|
|
|
status = !status;
|
|
|
|
|
}
|
2024-11-29 20:28:25 +00:00
|
|
|
|
2025-04-09 21:11:40 +00:00
|
|
|
PrintTela("Atualizando status do rele de ", false);
|
|
|
|
|
PrintTela(String(_Status), false);
|
|
|
|
|
PrintTela(" para ", false);
|
|
|
|
|
PrintTela(String(novoEstado));
|
|
|
|
|
_pino.set(status);
|
|
|
|
|
_Status = (Estado)_pino.get();
|
|
|
|
|
}
|
2024-11-27 17:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AtualizarLeitura() {
|
2025-04-09 21:11:40 +00:00
|
|
|
if (Iniciado) {
|
|
|
|
|
_Status = (Estado)_pino.get();
|
|
|
|
|
}
|
2024-11-27 17:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RequisitarDados() {
|
|
|
|
|
AtualizarLeitura();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 20:13:15 +00:00
|
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
2025-04-09 21:11:40 +00:00
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
data.push_back(ID_Num);
|
2025-04-11 20:13:15 +00:00
|
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
|
|
|
|
switch (posicao) {
|
|
|
|
|
case CanMessagePosicaoDados::Status: {
|
|
|
|
|
data.push_back(Iniciado ? 1 : 0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CanMessagePosicaoDados::Dados1: {
|
|
|
|
|
data.push_back(_Status);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-09 21:11:40 +00:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 17:38:56 +00:00
|
|
|
};
|
|
|
|
|
|
2024-11-29 20:28:25 +00:00
|
|
|
|
2025-04-14 12:52:19 +00:00
|
|
|
std::vector<uint8_t> Rele::ConfigurarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data, const String& Mod_ID) {
|
|
|
|
|
std::vector<uint8_t> status;
|
|
|
|
|
if (data.size() < 3) return status;
|
2025-04-11 20:13:15 +00:00
|
|
|
uint8_t idNum = data[1];
|
|
|
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[2];
|
|
|
|
|
switch (posicao) {
|
|
|
|
|
case CanMessagePosicaoDados::Config1: {
|
2025-04-14 12:52:19 +00:00
|
|
|
if (data.size() < 8) return status;
|
2025-04-11 20:13:15 +00:00
|
|
|
bool conectar = data[4] == 1;
|
|
|
|
|
bool atuacaoNivelAlto = data[5] == 1;
|
|
|
|
|
TiposBarramentos tipoBarramento = (TiposBarramentos)data[6];
|
|
|
|
|
int pino = data[7];
|
|
|
|
|
auto it = std::find_if(lista.begin(), lista.end(), [idNum](Rele* s) { return s->ID_Num == idNum; });
|
|
|
|
|
if (conectar) {
|
|
|
|
|
if (it == lista.end()) {
|
|
|
|
|
Rele* sensor = new Rele(Mod_ID, "sRLE_" + String(idNum));
|
|
|
|
|
sensor->ID_Num = idNum;
|
|
|
|
|
sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT);
|
|
|
|
|
sensor->AtuacaoNivelAlto = atuacaoNivelAlto;
|
2024-11-29 20:28:25 +00:00
|
|
|
sensor->Inicializar();
|
|
|
|
|
lista.push_back(sensor);
|
2025-04-11 20:13:15 +00:00
|
|
|
PrintTela("Rele adicionado via CAN: sRLE_" + String(idNum));
|
2025-04-14 12:52:19 +00:00
|
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
2024-11-29 20:28:25 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-04-11 20:13:15 +00:00
|
|
|
if (it != lista.end()) {
|
|
|
|
|
Rele* sensor = *it;
|
|
|
|
|
sensor->Desligar();
|
2025-04-14 12:52:19 +00:00
|
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
2025-04-11 20:13:15 +00:00
|
|
|
delete sensor;
|
|
|
|
|
lista.erase(it);
|
|
|
|
|
PrintTela("Rele removido via CAN: sRLE_" + String(idNum));
|
|
|
|
|
}
|
2024-11-29 20:28:25 +00:00
|
|
|
}
|
2025-04-11 20:13:15 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-14 12:52:19 +00:00
|
|
|
return status;
|
2025-04-11 20:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Rele::ComandarRele(std::vector<Rele*>& lista, std::vector<uint8_t>& data, uint8_t& id) {
|
|
|
|
|
if (data.size() < 4) return;
|
|
|
|
|
for (auto* _rele : lista) {
|
|
|
|
|
if (_rele->ID_Num == id) {
|
|
|
|
|
_rele->AtualizarStatus((Estado)data[3]);
|
|
|
|
|
return;
|
2024-11-29 20:28:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 17:38:56 +00:00
|
|
|
#endif
|