143 lines
3.3 KiB
C++
143 lines
3.3 KiB
C++
#ifndef ActuadorDef
|
|
#define ActuadorDef 1
|
|
#include "defines.h"
|
|
//sensor activador envia a topicOut el valor val_send si los activadores cumplen umbral
|
|
class Cactivador
|
|
{
|
|
public:
|
|
Cactivador(){}
|
|
DomoEspSensorReceiver *sen;
|
|
float umbral;
|
|
char operacion;//< > = val oper umbral
|
|
bool onchange;
|
|
};
|
|
|
|
|
|
class Actuador: public DomoEspSensorReceiver
|
|
{
|
|
float val;
|
|
char topicOut[MAXTOPICVAR];
|
|
char valSend[3];
|
|
Cactivador activador[MAX_ACTIVADORES];
|
|
|
|
public:
|
|
char id[10];
|
|
Actuador()
|
|
{
|
|
strcpy(id, "Meca");
|
|
topic[0]=0;
|
|
for(int i=0; i<MAX_ACTIVADORES; i++)
|
|
{
|
|
activador[i].sen=0;
|
|
}
|
|
}
|
|
void set(char* topic_id, char* topic_id_out, int valdef)
|
|
{
|
|
val=valdef;
|
|
strcpy(topic, topic_id);
|
|
strcpy(topicOut, topic_id_out);
|
|
}
|
|
void AddActivador(DomoEspSensorReceiver *sen, char oper, float umbral, bool onchange=false)
|
|
{
|
|
int i=0;
|
|
for(; i<MAX_ACTIVADORES; i++)
|
|
{
|
|
if(!activador[i].sen)
|
|
{
|
|
activador[i].sen=sen;
|
|
activador[i].operacion=oper;
|
|
activador[i].umbral=umbral;
|
|
activador[i].onchange = onchange;
|
|
break;
|
|
}
|
|
}
|
|
if(i>=MAX_ACTIVADORES)
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.print(id);
|
|
Serial.println("----------------------ERROR NO ENTRAN MAS ACTUADORES---------------- ");
|
|
#endif
|
|
}
|
|
}
|
|
void setValSend(char* _valSend)
|
|
{
|
|
strcpy(valSend, _valSend);
|
|
}
|
|
virtual float getVal()
|
|
{
|
|
return val;
|
|
}
|
|
virtual void SubscribeMqtt(IMqttManager* man){
|
|
//char buffer_t[MAXTOPICVAR];
|
|
sprintf(buffer_t, "%s/get",topic);
|
|
man->MqttSubs(buffer_t);
|
|
sprintf(buffer_t, "%s/pul",topic);
|
|
man->MqttSubs(buffer_t);
|
|
}
|
|
virtual void OnMqtt(IMqttManager * man, char* _topic, char* payload, int tipo)
|
|
{
|
|
if(!validaTopic(_topic))
|
|
return;
|
|
if(tipo==Topic::GET)
|
|
{
|
|
val=atof(payload);
|
|
ejecuta(man);
|
|
}
|
|
else if(tipo==Topic::PUL)
|
|
ejecuta(man);
|
|
}
|
|
private:
|
|
bool validaTopic(char* _topic)
|
|
{
|
|
if (!strcmp(_topic, topic))
|
|
return true;
|
|
for (int i = 0; i < MAX_ACTIVADORES; i++)
|
|
{
|
|
if (activador[i].sen && activador[i].onchange)
|
|
{
|
|
if (!strcmp(activador[i].sen->topic, _topic))
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
|
|
}
|
|
void ejecuta(IMqttManager * man)
|
|
{
|
|
if(valida())
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.print("Ejecuta id: ");
|
|
Serial.println(id);
|
|
|
|
#endif
|
|
strcpy(buffer_p, valSend);
|
|
sprintf(buffer_t, "%s/set",topicOut);
|
|
man->MqttSend(buffer_t, buffer_p);
|
|
}
|
|
}
|
|
bool valida()
|
|
{
|
|
|
|
for(int i=0; i<MAX_ACTIVADORES; i++)
|
|
{
|
|
if(activador[i].sen && !opera(i))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
bool opera( int i)
|
|
{
|
|
if(activador[i].operacion=='>')
|
|
return activador[i].sen->getVal()>activador[i].umbral;
|
|
if(activador[i].operacion=='<')
|
|
return activador[i].sen->getVal()<activador[i].umbral;
|
|
if(activador[i].operacion=='=')
|
|
return activador[i].sen->getVal()==activador[i].umbral;
|
|
return false;
|
|
}
|
|
};
|
|
|
|
#endif |