Skip to content

Commit

Permalink
Implement #62
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Roger committed Apr 14, 2016
1 parent 6fe93a5 commit 2bc6c8d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,14 @@ void BootNormal::_mqttCallback(char* topic, char* payload) {
}
}

if (homieNodePropertyIndex == -1) {
if (!homieNode->getSubscribeToAll() && homieNodePropertyIndex == -1) {
Logger.log(F("Node "));
Logger.log(node);
Logger.log(F(" not subscribed to "));
Logger.logln(property);
return;
}

Subscription homieNodeSubscription = homieNode->getSubscriptions()[homieNodePropertyIndex];

Logger.logln(F("Calling global input handler..."));
bool handled = this->_interface->globalInputHandler(node, property, message);
if (handled) return;
Expand All @@ -270,8 +268,12 @@ void BootNormal::_mqttCallback(char* topic, char* payload) {
handled = homieNode->getInputHandler()(property, message);

This comment has been minimized.

Copy link
@rossdargan

rossdargan Apr 14, 2016

Does this line not make the mods below unnecessary? I think the issue I had was the original code would return if the property index == -1. Now that won't happen this call will return true in which case the new code will not be called, or it returns false in which case the handler gets called twice (and will return false twice).

Maybe I'm misreading the code :)

This comment has been minimized.

Copy link
@marvinroger

marvinroger Apr 14, 2016

Member

This is not the same handler: above, we call the node handler (tied to a specific node), below we call the node property handler (tied to a specific node and a specific property).

if (handled) return;

Logger.logln(F("Calling property input handler..."));
handled = homieNodeSubscription.inputHandler(message);
if (homieNodePropertyIndex != -1) { // might not if subscribed to all only
Subscription homieNodeSubscription = homieNode->getSubscriptions()[homieNodePropertyIndex];
Logger.logln(F("Calling property input handler..."));
handled = homieNodeSubscription.inputHandler(message);
}

if (!handled){
Logger.logln(F("No handlers handled the following packet:"));
Logger.log(F(" • Node ID: "));
Expand Down
11 changes: 9 additions & 2 deletions src/HomieNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

using namespace HomieInternals;

HomieNode::HomieNode(const char* id, const char* type, NodeInputHandler inputHandler)
HomieNode::HomieNode(const char* id, const char* type, NodeInputHandler inputHandler, bool subscribeToAll)
: _inputHandler(inputHandler)
, _subscriptionsCount(0) {
, _subscriptionsCount(0)
, _subscribeToAll(subscribeToAll) {
if (strlen(id) + 1 > MAX_NODE_ID_LENGTH || strlen(type) + 1 > MAX_NODE_TYPE_LENGTH) {
Serial.println(F("✖ HomieNode(): either the id or type string is too long"));
abort();
Expand All @@ -15,6 +16,8 @@ HomieNode::HomieNode(const char* id, const char* type, NodeInputHandler inputHan
}

void HomieNode::subscribe(const char* property, PropertyInputHandler inputHandler) {
if (this->_subscribeToAll) return;

if (strlen(property) + 1 > MAX_NODE_PROPERTY_LENGTH) {
Serial.println(F("✖ subscribe(): the property string is too long"));
abort();
Expand Down Expand Up @@ -47,6 +50,10 @@ unsigned char HomieNode::getSubscriptionsCount() const {
return this->_subscriptionsCount;
}

bool HomieNode::getSubscribeToAll() const {
return this->_subscribeToAll;
}

NodeInputHandler HomieNode::getInputHandler() const {
return this->_inputHandler;
}
4 changes: 3 additions & 1 deletion src/HomieNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HomieNode {
friend HomieInternals::BootNormal;
friend HomieInternals::BootConfig;
public:
HomieNode(const char* id, const char* type, HomieInternals::NodeInputHandler nodeInputHandler = [](String property, String value) { return false; });
HomieNode(const char* id, const char* type, HomieInternals::NodeInputHandler nodeInputHandler = [](String property, String value) { return false; }, bool subscribeToAll = false);

void subscribe(const char* property, HomieInternals::PropertyInputHandler inputHandler = [](String value) { return false; });

Expand All @@ -26,12 +26,14 @@ class HomieNode {
const char* getType() const;
const HomieInternals::Subscription* getSubscriptions() const;
unsigned char getSubscriptionsCount() const;
bool getSubscribeToAll() const;
HomieInternals::NodeInputHandler getInputHandler() const;

const char* _id;
const char* _type;
HomieInternals::Subscription _subscriptions[HomieInternals::MAX_SUBSCRIPTIONS_COUNT_PER_NODE];
unsigned char _subscriptionsCount;
bool _subscribeToAll;
HomieInternals::NodeInputHandler _inputHandler;
};

Expand Down

0 comments on commit 2bc6c8d

Please sign in to comment.