120 lines
2.7 KiB
C++
120 lines
2.7 KiB
C++
#ifndef MecanismoPulsoDef
|
|
#define MecanismoPulsoDef 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
|
|
};
|
|
#define MAX_ACTIVADORES 5
|
|
|
|
class MecanismoPulso: public DomoEspSensorReceiver
|
|
{
|
|
float val;
|
|
char topicOut[MAXTOPICVAR];
|
|
char valSend[3];
|
|
Cactivador activador[MAX_ACTIVADORES];
|
|
|
|
public:
|
|
char id[10];
|
|
MecanismoPulso()
|
|
{
|
|
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)
|
|
{
|
|
for(int i=0; i<MAX_ACTIVADORES; i++)
|
|
{
|
|
if(!activador[i].sen)
|
|
{
|
|
activador[i].sen=sen;
|
|
activador[i].operacion=oper;
|
|
activador[i].umbral=umbral;
|
|
}
|
|
}
|
|
}
|
|
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(strcmp(_topic, topic))
|
|
return;
|
|
if(tipo==Topic::GET)
|
|
{
|
|
float val_act=atof(payload);
|
|
if (val_act!=val)
|
|
{
|
|
val=val_act;
|
|
ejecuta(man);
|
|
}
|
|
}
|
|
else if(tipo==Topic::PUL)
|
|
{
|
|
ejecuta(man);
|
|
}
|
|
}
|
|
private:
|
|
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 |