spejsiot/spejsiot/Endpoint.cpp

36 lines
963 B
C++
Raw Normal View History

2016-09-18 08:30:34 +00:00
#include <SpejsNode.h>
#include <Endpoint.h>
void Endpoint::bind(String _name, SpejsNode* _parent) {
2016-09-18 08:30:34 +00:00
parent = _parent;
name = _name;
2016-09-18 08:30:34 +00:00
}
void Endpoint::notify(String property, String value) {
2016-12-04 22:45:25 +00:00
if(parent)
parent->notify(name + "/" + property, value);
2016-09-18 08:30:34 +00:00
}
void Endpoint::onConnected() {
parent->subscribe(name + "/+/set");
2019-05-08 16:03:29 +00:00
if (type.length()) {
parent->notify(name + "/$type", type);
}
}
2017-03-06 16:56:37 +00:00
void Endpoint::onMessage(String topic, String payload) {
String devicePrefix = parent->DEV_TOPIC("");
2017-03-06 16:56:37 +00:00
if(!topic.startsWith(devicePrefix)) {
return;
2016-09-18 08:30:34 +00:00
}
2016-12-04 22:45:25 +00:00
2017-03-06 16:56:37 +00:00
int propPos = topic.indexOf("/", devicePrefix.length());
String endpoint = topic.substring(devicePrefix.length(), propPos);
String property = topic.substring(propPos+1, topic.indexOf("/", propPos+1));
2016-09-18 08:30:34 +00:00
2017-03-06 16:56:37 +00:00
if(name.equals(endpoint)) {
debugf("%s - %s response: %d\n", endpoint.c_str(), property.c_str(), onValue(property, payload).status);
2016-10-04 19:06:45 +00:00
}
2016-09-19 23:03:11 +00:00
}