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