From 517ceea15cb508dee31a76946a10892f594069c4 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Wed, 8 May 2019 15:55:06 +0200 Subject: [PATCH] Sming 3.8.0 update --- spejsiot/SpejsNode.cpp | 30 +++++++++++++++++++++--------- spejsiot/SpejsNode.h | 3 +-- spejsiot/endpoints/DHTEndpoint.cpp | 2 +- spejsiot/endpoints/DHTEndpoint.h | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/spejsiot/SpejsNode.cpp b/spejsiot/SpejsNode.cpp index 6b867c4..61cf223 100644 --- a/spejsiot/SpejsNode.cpp +++ b/spejsiot/SpejsNode.cpp @@ -33,7 +33,7 @@ void SpejsNode::systemReady() { registerEndpoint("$implementation", new ImplementationEndpoint()); // Keepalive Timer initialization - keepaliveTimer.initializeMs(10000, TimerDelegate(&SpejsNode::keepAliveHandler, this)).start(); + keepaliveTimer.initializeMs(10000, [=]() { keepAliveHandler(); }).start(); statusLED.high(); } @@ -147,18 +147,30 @@ void SpejsNode::onConnected() { debugf("Connection successful"); + mqtt.setMessageHandler([=](MqttClient& client, mqtt_message_t* message) -> int { + if (message == nullptr) return -1; + + String topic = String((const char*)message->publish.topic_name.data, message->publish.topic_name.length); + String content; + if(message->publish.content.data) { + content.concat((const char*)message->publish.content.data, message->publish.content.length); + } + + mqttCallback(topic, content); + return 0; + }); + // MQTT initialization mqtt.setWill(DEV_TOPIC("$online"), "false", 1, true); #ifdef ENABLE_SSL const uint8_t sha1Fingerprint[] = SSL_FINGERPRINT; - mqtt.connect("iot-" + deviceID, "", "", true); mqtt.addSslOptions(SSL_SERVER_VERIFY_LATER); mqtt.setSslFingerprint(sha1Fingerprint, 20); -#else - mqtt.connect("iot-" + deviceID); #endif + mqtt.connect(brokerURL, "iot-" + deviceID); + for(unsigned int i = 0 ; i < endpoints.count() ; i++) { endpoints.valueAt(i)->onConnected(); } @@ -177,9 +189,9 @@ void SpejsNode::onConnected() { // HTTP initialization http.listen(80); - http.addPath("/", HttpPathDelegate(&SpejsNode::httpIndex, this)); - http.addPath("/config.json", HttpPathDelegate(&SpejsNode::httpConfig, this)); - http.setDefaultHandler(HttpPathDelegate(&SpejsNode::httpFile, this)); + http.paths.set("/", HttpPathDelegate(&SpejsNode::httpIndex, this)); + http.paths.set("/config.json", HttpPathDelegate(&SpejsNode::httpConfig, this)); + http.paths.setDefault(HttpPathDelegate(&SpejsNode::httpFile, this)); http.setBodyParser("application/json", bodyToStringParser); } @@ -198,7 +210,7 @@ void SpejsNode::httpConfig(HttpRequest &request, HttpResponse &response) void SpejsNode::httpFile(HttpRequest &request, HttpResponse &response) { - String file = request.getPath(); + String file = request.uri.Path; if (file[0] == '/') file = file.substring(1); @@ -215,7 +227,7 @@ void SpejsNode::httpFile(HttpRequest &request, HttpResponse &response) JsonObjectStream* stream = new JsonObjectStream(); JsonObject& json = stream->getRoot(); json["status"] = result.status; - response.sendJsonObject(stream); + response.sendDataStream(stream, MIME_JSON); } } } diff --git a/spejsiot/SpejsNode.h b/spejsiot/SpejsNode.h index dfc2380..17123fa 100644 --- a/spejsiot/SpejsNode.h +++ b/spejsiot/SpejsNode.h @@ -42,7 +42,7 @@ public: highState = highState_; pinMode(pin, OUTPUT); - animateTimer.initializeMs(50, TimerDelegate(&LED::animate, this)).start(); + animateTimer.initializeMs(50, [=]() { animate(); }).start(); } void idle() { @@ -89,7 +89,6 @@ public: uint8_t currentSlot; SpejsNode(String _deviceType) : - mqtt(MQTT_BROKER, MQTT_PORT, MqttStringSubscriptionCallback(&SpejsNode::mqttCallback, this)), deviceType(_deviceType) {}; void onConnected(); diff --git a/spejsiot/endpoints/DHTEndpoint.cpp b/spejsiot/endpoints/DHTEndpoint.cpp index 319eedc..b5745b9 100644 --- a/spejsiot/endpoints/DHTEndpoint.cpp +++ b/spejsiot/endpoints/DHTEndpoint.cpp @@ -4,7 +4,7 @@ void DHTEndpoint::bind(String _name, SpejsNode* _parent) { Endpoint::bind(_name, _parent); sensor.setup(pin, sensor_type); - samplingTimer.initializeMs(samplingRate, TimerDelegate(&DHTEndpoint::sample, this)).start(); + samplingTimer.initializeMs(samplingRate, [=]() { sample(); }).start(); } void DHTEndpoint::sample() { diff --git a/spejsiot/endpoints/DHTEndpoint.h b/spejsiot/endpoints/DHTEndpoint.h index d37b6b4..7bc5329 100644 --- a/spejsiot/endpoints/DHTEndpoint.h +++ b/spejsiot/endpoints/DHTEndpoint.h @@ -18,7 +18,7 @@ protected: public: DHTEndpoint(int _pin, int _samplingRate = 10000, DHTesp::DHT_MODEL_t _sensor_type=DHTesp::DHT11) : - ValueEndpoint("dht"), pin(_pin), sensor_type(_sensor_type), + ValueEndpoint("dht"), sensor_type(_sensor_type), pin(_pin), samplingRate(_samplingRate) {} void bind(String name, SpejsNode* _parent);