DomoEsp_v2.0/DomoEspSensor/SensorTimer.h

95 lines
2.1 KiB
C++

#ifndef SensorTimerDef
#define SensorTimerDef 1
#include "defines.h"
//sensor virtual
class SensorTimer: public DomoEspSensorReceiver
{
bool val;
bool valDef;
Ctimer t;
public:
SensorTimer()
{
val=0;
topic[0]=0;
}
void set(const char* topic_id, bool _valdef, int segTime)
{
t.set(segTime);
valDef=_valdef;
val=valDef;
strcpy(topic, topic_id);
}
virtual float getVal()
{
return (float)val;
}
virtual void procesa(IMqttManager * man, int tiempo)
{
if(val!=valDef)
{
if(t.onTimer())
{
val=valDef;
tiempo=2;
}
}
if(tiempo==2)
{
#ifdef DEBUG_PS
Serial.print("LogTm ");
Serial.println(val);
#endif
//loguea------------
//char buffer_t[MAXTOPICVAR];
//char buffer_p[MAXTOPICVAR];
sprintf(buffer_p, "%d", (int)val);
sprintf(buffer_t, "%s/get",topic);
man->MqttSend(buffer_t, buffer_p);
}
}
virtual void SubscribeMqtt(IMqttManager* man){
//char buffer_t[MAXTOPICVAR];
sprintf(buffer_t, "%s/set",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((tipo!=Topic::PUL && tipo!=Topic::SET) || strcmp(_topic, topic))
return;
float valOld=val;
if(tipo==Topic::PUL)
{
if(valDef==0)
val=1;
else
val=0;
t.inicia();
//MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens);
}
else if(tipo==Topic::SET)
{
if(payload[0]=='X')
val=!(val>0);
else
val=atof(payload);
if(val!=valDef)
t.inicia();
//MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens);
}
if(valOld!=val)
{
sprintf(buffer_p, "%d", (int)val);
sprintf(buffer_t, "%s/get",topic);
man->MqttSend(buffer_t, buffer_p);
}
}
};
#endif