mirror of
https://github.com/anklimov/lighthub
synced 2025-12-08 04:39:49 +03:00
channels abstraction changed
This commit is contained in:
41
lighthub/abstractch.cpp
Normal file
41
lighthub/abstractch.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
#include "abstractch.h"
|
||||
#include <PubSubClient.h>
|
||||
#include "utils.h"
|
||||
#include <aJSON.h>
|
||||
#include "main.h"
|
||||
|
||||
extern lan_status lanStatus;
|
||||
extern PubSubClient mqttClient;
|
||||
|
||||
int abstractCh::publish(char* topic, long value, char* subtopic)
|
||||
{
|
||||
char valstr[16];
|
||||
printUlongValueToStr(valstr, value);
|
||||
return publish(topic, valstr,subtopic);
|
||||
};
|
||||
|
||||
int abstractCh::publish(char* topic, float value, char* subtopic)
|
||||
{
|
||||
char valstr[16];
|
||||
printFloatValueToStr(value, valstr);
|
||||
return publish(topic, valstr,subtopic);
|
||||
};
|
||||
|
||||
int abstractCh::publish(char* topic, char * value, char* subtopic)
|
||||
{
|
||||
char addrstr[MQTT_TOPIC_LENGTH];
|
||||
|
||||
if (topic)
|
||||
{
|
||||
strncpy(addrstr,topic,sizeof(addrstr));
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,topic);
|
||||
strncat(addrstr,subtopic,sizeof(addrstr));
|
||||
if (mqttClient.connected() && lanStatus == OPERATION)
|
||||
{
|
||||
mqttClient.publish(addrstr, value, true);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
16
lighthub/abstractch.h
Normal file
16
lighthub/abstractch.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
|
||||
class abstractCh {
|
||||
public:
|
||||
abstractCh(){};
|
||||
// virtual int Setup(int addr) = 0;
|
||||
virtual int Poll() = 0;
|
||||
|
||||
protected:
|
||||
// Input * in;
|
||||
int publish(char* topic, long value, char* subtopic = NULL);
|
||||
int publish(char* topic, float value, char* subtopic = NULL );
|
||||
int publish(char* topic, char * value, char* subtopic = NULL);
|
||||
//friend Input;
|
||||
};
|
||||
@@ -31,14 +31,7 @@ int abstractIn::publish(char * value, char* subtopic)
|
||||
aJsonObject *emit = aJson.getObjectItem(in->inputObj, "emit");
|
||||
if (emit)
|
||||
{
|
||||
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
strncat(addrstr,subtopic,sizeof(addrstr));
|
||||
if (mqttClient.connected() && lanStatus == OPERATION)
|
||||
{
|
||||
mqttClient.publish(addrstr, value, true);
|
||||
return 1;
|
||||
}
|
||||
return publish(emit->valuestring,value,subtopic);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
#include "abstractch.h"
|
||||
|
||||
class Input;
|
||||
class abstractIn {
|
||||
class abstractIn : public abstractCh{
|
||||
public:
|
||||
abstractIn(Input * _in){in=_in;};
|
||||
abstractIn(Input * _in):abstractCh(){in=_in;};
|
||||
virtual int Setup(int addr) = 0;
|
||||
virtual int Poll() = 0;
|
||||
|
||||
|
||||
12
lighthub/abstractout.h
Normal file
12
lighthub/abstractout.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
|
||||
class Input;
|
||||
class abstractOut {
|
||||
public:
|
||||
abstractOut(Input * _in){in=_in;};
|
||||
virtual int Setup(int addr) = 0;
|
||||
virtual int Poll() = 0;
|
||||
protected:
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user