Cleanup, move topic subscription to Endpoint::onConnected method
parent
c4098e6163
commit
41455c0e5a
|
@ -11,6 +11,11 @@ void Endpoint::notify(String property, String value) {
|
||||||
parent->notify(name + "/" + property, value);
|
parent->notify(name + "/" + property, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Endpoint::onConnected() {
|
||||||
|
parent->subscribe(name + "/+/set");
|
||||||
|
parent->notify(name + "/$type", type);
|
||||||
|
}
|
||||||
|
|
||||||
EndpointResult OutputEndpoint::onValue(String property, String value) {
|
EndpointResult OutputEndpoint::onValue(String property, String value) {
|
||||||
if (value == "1" or value == "on" or value == "true") {
|
if (value == "1" or value == "on" or value == "true") {
|
||||||
currentValue = 1;
|
currentValue = 1;
|
||||||
|
|
|
@ -31,6 +31,8 @@ public:
|
||||||
virtual EndpointResult onValue(String property, String value) {
|
virtual EndpointResult onValue(String property, String value) {
|
||||||
return 400;
|
return 400;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onConnected();
|
||||||
};
|
};
|
||||||
|
|
||||||
class OutputEndpoint : public Endpoint {
|
class OutputEndpoint : public Endpoint {
|
||||||
|
|
|
@ -64,11 +64,14 @@ void SpejsNode::httpIndex(HttpRequest &request, HttpResponse &response)
|
||||||
"\nMAC: " + WifiStation.getMAC());
|
"\nMAC: " + WifiStation.getMAC());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Successful network connection handler
|
||||||
|
*/
|
||||||
void SpejsNode::onConnected() {
|
void SpejsNode::onConnected() {
|
||||||
Serial.println("Connection successful");
|
Serial.println("Connection successful");
|
||||||
|
|
||||||
// MQTT initialization
|
// MQTT initialization
|
||||||
mqtt.setWill(TOPIC_PREFIX + deviceID + "/$online", "false", 1, true);
|
mqtt.setWill(DEV_TOPIC("$online"), "false", 1, true);
|
||||||
|
|
||||||
#ifdef ENABLE_SSL
|
#ifdef ENABLE_SSL
|
||||||
const uint8_t sha1Fingerprint[] = SSL_FINGERPRINT;
|
const uint8_t sha1Fingerprint[] = SSL_FINGERPRINT;
|
||||||
|
@ -80,22 +83,19 @@ void SpejsNode::onConnected() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for(unsigned int i = 0 ; i < endpoints.count() ; i++) {
|
for(unsigned int i = 0 ; i < endpoints.count() ; i++) {
|
||||||
mqtt.subscribe(TOPIC_PREFIX + deviceID + "/" + endpoints.keyAt(i) + "/+/set");
|
endpoints.valueAt(i)->onConnected();
|
||||||
}
|
}
|
||||||
mqtt.subscribe(TOPIC_PREFIX + deviceID + "/$implementation/+");
|
|
||||||
|
subscribe("$implementation/+");
|
||||||
|
|
||||||
// Say hello
|
// Say hello
|
||||||
mqtt.publish(DEV_TOPIC("$online"), "true", true);
|
notify("$online", "true");
|
||||||
mqtt.publish(DEV_TOPIC("$homie"), "2", true);
|
notify("$homie", "2");
|
||||||
mqtt.publish(DEV_TOPIC("$name"), deviceType, true);
|
notify("$name", deviceType);
|
||||||
mqtt.publish(DEV_TOPIC("$localip"), WifiStation.getIP().toString(), true);
|
notify("$localip", WifiStation.getIP().toString());
|
||||||
mqtt.publish(DEV_TOPIC("$mac"), WifiStation.getMAC(), true);
|
notify("$mac", WifiStation.getMAC());
|
||||||
mqtt.publish(DEV_TOPIC("$fw/name"), "spejsiot", true);
|
notify("$fw/name", "spejsiot");
|
||||||
mqtt.publish(DEV_TOPIC("$fw/version"), BUILD_ID, true);
|
notify("$fw/version", BUILD_ID);
|
||||||
|
|
||||||
for(unsigned int i = 0; i < endpoints.count(); i++) {
|
|
||||||
mqtt.publish(DEV_TOPIC(endpoints.keyAt(i) + "/$type"), endpoints.valueAt(i)->type, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// HTTP initialization
|
// HTTP initialization
|
||||||
http.listen(80);
|
http.listen(80);
|
||||||
|
@ -155,32 +155,40 @@ void SpejsNode::initializeMDNS() {
|
||||||
espconn_mdns_init(info);
|
espconn_mdns_init(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Publish on device-specific topic
|
||||||
|
*/
|
||||||
bool SpejsNode::notify(String key, String value) {
|
bool SpejsNode::notify(String key, String value) {
|
||||||
if(mqtt.getConnectionState() == eTCS_Connected) {
|
mqtt.publish(DEV_TOPIC(key), value, true);
|
||||||
mqtt.publish(TOPIC_PREFIX + deviceID + "/" + key, value, true);
|
return mqtt.getConnectionState() == eTCS_Connected;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
Serial.println("MQTT Not Connected!!!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Subsribe to device-specific topic
|
||||||
|
*/
|
||||||
|
bool SpejsNode::subscribe(String topic) {
|
||||||
|
mqtt.subscribe(DEV_TOPIC(topic));
|
||||||
|
return mqtt.getConnectionState() == eTCS_Connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Register new endpoint
|
||||||
|
*/
|
||||||
void SpejsNode::registerEndpoint(String key, Endpoint* endpoint) {
|
void SpejsNode::registerEndpoint(String key, Endpoint* endpoint) {
|
||||||
endpoints[key] = endpoint;
|
endpoints[key] = endpoint;
|
||||||
endpoint->bind(key, this);
|
endpoint->bind(key, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpejsNode::mqttCallback(String origtopic, String value) {
|
void SpejsNode::mqttCallback(String origtopic, String value) {
|
||||||
String devicePrefix = TOPIC_PREFIX + deviceID;
|
String devicePrefix = DEV_TOPIC("");
|
||||||
|
|
||||||
if(!origtopic.startsWith(devicePrefix)) {
|
if(!origtopic.startsWith(devicePrefix)) {
|
||||||
Serial.println("ignoring");
|
Serial.println("ignoring");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int propPos = origtopic.indexOf("/", devicePrefix.length() + 1);
|
int propPos = origtopic.indexOf("/", devicePrefix.length());
|
||||||
String endpoint = origtopic.substring(devicePrefix.length() + 1, propPos);
|
String endpoint = origtopic.substring(devicePrefix.length(), propPos);
|
||||||
String property = origtopic.substring(propPos+1, origtopic.indexOf("/", propPos+1));
|
String property = origtopic.substring(propPos+1, origtopic.indexOf("/", propPos+1));
|
||||||
|
|
||||||
if(endpoints.contains(endpoint)) {
|
if(endpoints.contains(endpoint)) {
|
||||||
|
|
|
@ -31,6 +31,11 @@ protected:
|
||||||
|
|
||||||
void buildMetadata(JsonObjectStream* stream);
|
void buildMetadata(JsonObjectStream* stream);
|
||||||
|
|
||||||
|
void mqttCallback(String, String);
|
||||||
|
void otaUpdateCallback(bool result);
|
||||||
|
void httpFile(HttpRequest &request, HttpResponse &response);
|
||||||
|
void httpIndex(HttpRequest &request, HttpResponse &response);
|
||||||
|
void httpMetadata(HttpRequest &request, HttpResponse &response);
|
||||||
public:
|
public:
|
||||||
String deviceID;
|
String deviceID;
|
||||||
String deviceType;
|
String deviceType;
|
||||||
|
@ -44,12 +49,10 @@ public:
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
bool notify(String key, String value);
|
bool notify(String key, String value);
|
||||||
|
bool subscribe(String topic);
|
||||||
|
|
||||||
void registerEndpoint(String key, Endpoint* cb);
|
void registerEndpoint(String key, Endpoint* cb);
|
||||||
void mqttCallback(String, String);
|
|
||||||
void otaUpdateCallback(bool result);
|
|
||||||
void httpFile(HttpRequest &request, HttpResponse &response);
|
|
||||||
void httpIndex(HttpRequest &request, HttpResponse &response);
|
|
||||||
void httpMetadata(HttpRequest &request, HttpResponse &response);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue