Skip to content

Commit

Permalink
Add type and properties node properties
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Aug 19, 2016
1 parent f347f92 commit 586ad7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ bool lightOnHandler(String value) {
}

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, LOW);

Homie_setFirmware("awesome-relay", "1.0.0");
lightNode.subscribe("on", lightOnHandler);

lightNode.advertise("on")->settable(lightOnHandler);

Homie.setup();
}

Expand Down
57 changes: 40 additions & 17 deletions src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ BootNormal::BootNormal()
BootNormal::~BootNormal() {
}

char* BootNormal::_prefixMqttTopic(PGM_P topic) {
_mqttTopic = std::unique_ptr<char[]>(new char[strlen(_interface->config->get().mqtt.baseTopic) + strlen(_interface->config->get().deviceId) + strlen_P(topic) + 1]);
void BootNormal::_prefixMqttTopic() {
strcpy(_mqttTopic.get(), _interface->config->get().mqtt.baseTopic);
strcat(_mqttTopic.get(), _interface->config->get().deviceId);
}

char* BootNormal::_prefixMqttTopic(PGM_P topic) {
_prefixMqttTopic();
strcat_P(_mqttTopic.get(), topic);

return _mqttTopic.get();
Expand Down Expand Up @@ -78,25 +81,27 @@ void BootNormal::_onMqttConnected() {

_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$homie")), 1, true, HOMIE_VERSION);
_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$implementation")), 1, true, "esp8266");
_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$online")), 1, true, "true");

char nodes[HomieNode::nodes.size() * (MAX_NODE_ID_LENGTH + 1 + MAX_NODE_ID_LENGTH + 1) - 1];
char *begin = nodes;
char *ptr = nodes;

for (HomieNode* iNode : HomieNode::nodes) {
if (ptr != begin) *ptr++ = ',';
auto len = strlen(iNode->getId());
memcpy(ptr, iNode->getId(), len);
ptr += len;
*ptr++ = ':';
len = strlen(iNode->getType());
memcpy(ptr, iNode->getType(), len);
ptr += len;
std::unique_ptr<char[]> subtopic = std::unique_ptr<char[]>(new char[1 + strlen(iNode->getId()) + 12 + 1]); // /id/$properties
strcpy_P(subtopic.get(), PSTR("/"));
strcat(subtopic.get(), iNode->getId());
strcat_P(subtopic.get(), PSTR("/$type"));
_interface->mqttClient->publish(_prefixMqttTopic(subtopic.get()), 1, true, iNode->getType());

strcpy_P(subtopic.get(), PSTR("/"));
strcat(subtopic.get(), iNode->getId());
strcat_P(subtopic.get(), PSTR("/$properties"));
String properties;
for (Property* iProperty : iNode->getProperties()) {
properties.concat(iProperty->getProperty());
if (iProperty->isSettable()) properties.concat(":settable");
properties.concat(",");
}
if (iNode->getProperties().size() >= 1) properties.remove(properties.length() - 1);
_interface->mqttClient->publish(_prefixMqttTopic(subtopic.get()), 1, true, properties.c_str());
}

*ptr = '\0';
_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$nodes")), 1, true, nodes);
_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$name")), 1, true, _interface->config->get().name);

IPAddress localIp = WiFi.localIP();
Expand Down Expand Up @@ -138,6 +143,8 @@ void BootNormal::_onMqttConnected() {
_interface->mqttClient->subscribe(_prefixMqttTopic(PSTR("/$ota")), 2);
}

_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$online")), 1, true, "true");

_interface->readyToOperate = true;
if (_interface->led.enabled) _interface->blinker->stop();

Expand Down Expand Up @@ -326,6 +333,22 @@ void BootNormal::setup() {

if (_interface->led.enabled) _interface->blinker->start(LED_WIFI_DELAY);

// Generate topic buffer
size_t baseTopicLength = strlen(_interface->config->get().mqtt.baseTopic) + strlen(_interface->config->get().deviceId);
size_t longestSubtopicLength = 28 + 1; // /$implementation/ota/enabled
for (HomieNode* iNode : HomieNode::nodes) {
size_t nodeMaxTopicLength = 1 + strlen(iNode->getId()) + 12 + 1; // /id/$properties
if (nodeMaxTopicLength > longestSubtopicLength) longestSubtopicLength = nodeMaxTopicLength;

for (Property* iProperty : iNode->getProperties()) {
size_t propertyMaxTopicLength = 1 + strlen(iNode->getId()) + 1 + strlen(iProperty->getProperty()) + 1;
if (iProperty->isSettable()) propertyMaxTopicLength += 4; // /set

if (propertyMaxTopicLength > longestSubtopicLength) longestSubtopicLength = propertyMaxTopicLength;
}
}
_mqttTopic = std::unique_ptr<char[]>(new char[baseTopicLength + longestSubtopicLength]);

_wifiGotIpHandler = WiFi.onStationModeGotIP(std::bind(&BootNormal::_onWifiGotIp, this, std::placeholders::_1));
_wifiDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&BootNormal::_onWifiDisconnected, this, std::placeholders::_1));

Expand Down
1 change: 1 addition & 0 deletions src/Homie/Boot/BootNormal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BootNormal : public Boot {
void _onMqttConnected();
void _onMqttDisconnected(AsyncMqttClientDisconnectReason reason);
void _onMqttMessage(char* topic, char* payload, uint8_t qos, size_t len, size_t index, size_t total);
void _prefixMqttTopic();
char* _prefixMqttTopic(PGM_P topic);
};
} // namespace HomieInternals

0 comments on commit 586ad7a

Please sign in to comment.