publish gpio role

This commit is contained in:
technyon
2023-06-03 22:59:03 +02:00
parent 8d324bfc20
commit 356b34b293
4 changed files with 39 additions and 33 deletions

2
Gpio.h
View File

@@ -85,6 +85,8 @@ private:
PinRole::OutputHighRtoActive, PinRole::OutputHighRtoActive,
PinRole::OutputHighCmActive, PinRole::OutputHighCmActive,
PinRole::OutputHighRtoOrCmActive, PinRole::OutputHighRtoOrCmActive,
PinRole::GeneralInput,
PinRole::GeneralOutput
}; };
std::vector<PinEntry> _pinConfiguration; std::vector<PinEntry> _pinConfiguration;

View File

@@ -62,4 +62,5 @@
#define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState" #define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState"
#define mqtt_topic_network_device "/maintenance/networkDevice" #define mqtt_topic_network_device "/maintenance/networkDevice"
#define mqtt_topic_gpio_prefix "/gpio/" #define mqtt_topic_gpio_prefix "/gpio"
#define mqtt_topic_gpio_input "/input_"

View File

@@ -36,7 +36,8 @@ Network::Network(Preferences *preferences, Gpio* gpio, const String& maintenance
_maintenancePathPrefix[i] = maintenancePathPrefix.charAt(i); _maintenancePathPrefix[i] = maintenancePathPrefix.charAt(i);
} }
String connectionStateTopic = _preferences->getString(preference_mqtt_lock_path) + mqtt_topic_mqtt_connection_state; _lockPath = _preferences->getString(preference_mqtt_lock_path);
String connectionStateTopic = _lockPath + mqtt_topic_mqtt_connection_state;
memset(_mqttConnectionStateTopic, 0, sizeof(_mqttConnectionStateTopic)); memset(_mqttConnectionStateTopic, 0, sizeof(_mqttConnectionStateTopic));
len = connectionStateTopic.length(); len = connectionStateTopic.length();
@@ -144,9 +145,6 @@ void Network::setupDevice()
void Network::initialize() void Network::initialize()
{ {
_networkGpio->initialize();
registerMqttReceiver(_networkGpio);
_restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000; _rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000;
@@ -215,16 +213,21 @@ void Network::initialize()
_publishDebugInfo = _preferences->getBool(preference_publish_debug_info); _publishDebugInfo = _preferences->getBool(preference_publish_debug_info);
char gpioPath[200];
for(const auto& pinEntry : _gpio->pinConfiguration()) for(const auto& pinEntry : _gpio->pinConfiguration())
{ {
switch(pinEntry.role) switch(pinEntry.role)
{ {
case PinRole::GeneralInput: case PinRole::GeneralInput:
String inp = "input_"; memset(gpioPath, 0, sizeof(gpioPath));
inp.concat(pinEntry.pin); buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_input + std::to_string(pinEntry.pin)).c_str(), "role" });
initTopic(mqtt_topic_gpio_prefix, inp.c_str(), ) publishString(_lockPath.c_str(), gpioPath, "input");
break; break;
case PinRole::GeneralOutput: case PinRole::GeneralOutput:
memset(gpioPath, 0, sizeof(gpioPath));
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_input + std::to_string(pinEntry.pin)).c_str(), "role" });
publishString(_lockPath.c_str(), gpioPath, "output");
break; break;
} }
} }
@@ -327,8 +330,6 @@ bool Network::update()
_lastMaintenanceTs = ts; _lastMaintenanceTs = ts;
} }
_networkGpio->update();
return true; return true;
} }
@@ -464,36 +465,38 @@ bool Network::reconnect()
void Network::subscribe(const char* prefix, const char *path) void Network::subscribe(const char* prefix, const char *path)
{ {
char prefixedPath[500]; char prefixedPath[500];
buildMqttPath(prefix, path, prefixedPath); buildMqttPath(prefixedPath, { prefix, path });
_subscribedTopics.push_back(prefixedPath); _subscribedTopics.push_back(prefixedPath);
} }
void Network::initTopic(const char *prefix, const char *path, const char *value) void Network::initTopic(const char *prefix, const char *path, const char *value)
{ {
char prefixedPath[500]; char prefixedPath[500];
buildMqttPath(prefix, path, prefixedPath); buildMqttPath(prefixedPath, { prefix, path });
String pathStr = prefixedPath; String pathStr = prefixedPath;
String valueStr = value; String valueStr = value;
_initTopics[pathStr] = valueStr; _initTopics[pathStr] = valueStr;
} }
void Network::buildMqttPath(const char* prefix, const char* path, char* outPath) void Network::buildMqttPath(char* outPath, std::initializer_list<const char*> paths)
{ {
int offset = 0; int offset = 0;
int i=0;
while(prefix[i] != 0x00) for(const char* path : paths)
{ {
outPath[offset] = prefix[i]; if(path[0] != '/')
{
outPath[offset] = '/';
++offset;
}
int i = 0;
while(path[i] != 0)
{
outPath[offset] = path[i];
++offset; ++offset;
++i; ++i;
} }
i=0;
while(path[i] != 0x00)
{
outPath[offset] = path[i];
++i;
++offset;
} }
outPath[offset] = 0x00; outPath[offset] = 0x00;
@@ -567,7 +570,7 @@ void Network::publishFloat(const char* prefix, const char* topic, const float va
char str[30]; char str[30];
dtostrf(value, 0, precision, str); dtostrf(value, 0, precision, str);
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str); _device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
} }
@@ -576,7 +579,7 @@ void Network::publishInt(const char* prefix, const char *topic, const int value)
char str[30]; char str[30];
itoa(value, str, 10); itoa(value, str, 10);
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str); _device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
} }
@@ -585,7 +588,7 @@ void Network::publishUInt(const char* prefix, const char *topic, const unsigned
char str[30]; char str[30];
utoa(value, str, 10); utoa(value, str, 10);
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str); _device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
} }
@@ -594,7 +597,7 @@ void Network::publishULong(const char* prefix, const char *topic, const unsigned
char str[30]; char str[30];
utoa(value, str, 10); utoa(value, str, 10);
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str); _device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
} }
@@ -603,14 +606,14 @@ void Network::publishBool(const char* prefix, const char *topic, const bool valu
char str[2] = {0}; char str[2] = {0};
str[0] = value ? '1' : '0'; str[0] = value ? '1' : '0';
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str); _device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
} }
bool Network::publishString(const char* prefix, const char *topic, const char *value) bool Network::publishString(const char* prefix, const char *topic, const char *value)
{ {
char path[200] = {0}; char path[200] = {0};
buildMqttPath(prefix, topic, path); buildMqttPath(path, { prefix, topic });
return _device->mqttPublish(path, MQTT_QOS_LEVEL, true, value) > 0; return _device->mqttPublish(path, MQTT_QOS_LEVEL, true, value) > 0;
} }

View File

@@ -8,7 +8,6 @@
#include "networkDevices/IPConfiguration.h" #include "networkDevices/IPConfiguration.h"
#include "MqttTopics.h" #include "MqttTopics.h"
#include "Gpio.h" #include "Gpio.h"
#include "NetworkGpio.h"
enum class NetworkDeviceType enum class NetworkDeviceType
{ {
@@ -101,12 +100,13 @@ private:
void onMqttConnect(const bool& sessionPresent); void onMqttConnect(const bool& sessionPresent);
void onMqttDisconnect(const espMqttClientTypes::DisconnectReason& reason); void onMqttDisconnect(const espMqttClientTypes::DisconnectReason& reason);
void buildMqttPath(const char* prefix, const char* path, char* outPath); void buildMqttPath(char* outPath, std::initializer_list<const char*> paths);
static Network* _inst; static Network* _inst;
const char* _lastWillPayload = "offline"; const char* _lastWillPayload = "offline";
char _mqttConnectionStateTopic[211] = {0}; char _mqttConnectionStateTopic[211] = {0};
String _lockPath;
Preferences* _preferences; Preferences* _preferences;
Gpio* _gpio; Gpio* _gpio;