Parcourir la source

resolve conflicts

Christopher Mullins il y a 6 ans
Parent
commit
77328d9ab1

Fichier diff supprimé car celui-ci est trop grand
+ 2 - 2
dist/index.html.gz.h


+ 9 - 0
lib/MQTT/MqttClient.cpp

@@ -6,6 +6,7 @@
 #include <ArduinoJson.h>
 #include <WiFiClient.h>
 #include <MiLightRadioConfig.h>
+#include <AboutStringHelper.h>
 
 MqttClient::MqttClient(Settings& settings, MiLightClient*& milightClient)
   : milightClient(milightClient),
@@ -80,6 +81,13 @@ bool MqttClient::connect() {
   }
 }
 
+void MqttClient::sendBirthMessage() {
+  if (settings.mqttBirthTopic.length() > 0) {
+    String aboutStr = AboutStringHelper::generateAboutString(true);
+    mqttClient->publish(settings.mqttBirthTopic.c_str(), aboutStr.c_str());
+  }
+}
+
 void MqttClient::reconnect() {
   if (lastConnectAttempt > 0 && (millis() - lastConnectAttempt) < MQTT_CONNECTION_ATTEMPT_FREQUENCY) {
     return;
@@ -88,6 +96,7 @@ void MqttClient::reconnect() {
   if (! mqttClient->connected()) {
     if (connect()) {
       subscribe();
+      sendBirthMessage();
 
 #ifdef MQTT_DEBUG
       Serial.println(F("MqttClient - Successfully connected to MQTT server"));

+ 1 - 0
lib/MQTT/MqttClient.h

@@ -30,6 +30,7 @@ private:
   char* domain;
   unsigned long lastConnectAttempt;
 
+  void sendBirthMessage();
   bool connect();
   void subscribe();
   void publishCallback(char* topic, byte* payload, int length);

+ 25 - 0
lib/Settings/AboutStringHelper.cpp

@@ -0,0 +1,25 @@
+#include <AboutStringHelper.h>
+#include <ArduinoJson.h>
+#include <Settings.h>
+#include <ESP8266WiFi.h>
+
+String AboutStringHelper::generateAboutString(bool abbreviated) {
+  DynamicJsonBuffer buffer;
+  JsonObject& response = buffer.createObject();
+
+  response["firmware"] = QUOTE(FIRMWARE_NAME);
+  response["version"] = QUOTE(MILIGHT_HUB_VERSION);
+  response["ip_address"] = WiFi.localIP().toString();
+  response["reset_reason"] = ESP.getResetReason();
+
+  if (! abbreviated) {
+    response["variant"] = QUOTE(FIRMWARE_VARIANT);
+    response["free_heap"] = ESP.getFreeHeap();
+    response["arduino_version"] = ESP.getCoreVersion();
+  }
+
+  String body;
+  response.printTo(body);
+
+  return body;
+}

+ 11 - 0
lib/Settings/AboutStringHelper.h

@@ -0,0 +1,11 @@
+#include <Arduino.h>
+
+#ifndef _ABOUT_STRING_HELPER_H
+#define _ABOUT_STRING_HELPER_H
+
+class AboutStringHelper {
+public:
+  static String generateAboutString(bool abbreviated = false);
+};
+
+#endif

+ 2 - 0
lib/Settings/Settings.cpp

@@ -100,6 +100,7 @@ void Settings::patch(JsonObject& parsedSettings) {
     this->setIfPresent(parsedSettings, "mqtt_state_topic_pattern", mqttStateTopicPattern);
     this->setIfPresent(parsedSettings, "mqtt_lwt_topic", mqttLwtTopic);
     this->setIfPresent(parsedSettings, "mqtt_lwt_message", mqttLwtMessage);
+    this->setIfPresent(parsedSettings, "mqtt_birth_topic", mqttBirthTopic);
     this->setIfPresent(parsedSettings, "discovery_port", discoveryPort);
     this->setIfPresent(parsedSettings, "listen_repeats", listenRepeats);
     this->setIfPresent(parsedSettings, "state_flush_interval", stateFlushInterval);
@@ -211,6 +212,7 @@ void Settings::serialize(Stream& stream, const bool prettyPrint) {
   root["mqtt_state_topic_pattern"] = this->mqttStateTopicPattern;
   root["mqtt_lwt_topic"] = this->mqttLwtTopic;
   root["mqtt_lwt_message"] = this->mqttLwtMessage;
+  root["mqtt_birth_topic"] = this->mqttBirthTopic;
   root["discovery_port"] = this->discoveryPort;
   root["listen_repeats"] = this->listenRepeats;
   root["state_flush_interval"] = this->stateFlushInterval;

+ 5 - 0
lib/Settings/Settings.h

@@ -14,6 +14,10 @@
 #define XQUOTE(x) #x
 #define QUOTE(x) XQUOTE(x)
 
+#ifndef FIRMWARE_NAME
+#define FIRMWARE_NAME unknown
+#endif
+
 #ifndef FIRMWARE_VARIANT
 #define FIRMWARE_VARIANT unknown
 #endif
@@ -162,6 +166,7 @@ public:
   String mqttStateTopicPattern;
   String mqttLwtTopic;
   String mqttLwtMessage;
+  String mqttBirthTopic;
   GroupStateField *groupStateFields;
   size_t numGroupStateFields;
   uint16_t discoveryPort;

+ 11 - 10
lib/WebServer/MiLightHttpServer.cpp

@@ -6,6 +6,7 @@
 #include <MiLightRadioConfig.h>
 #include <string.h>
 #include <TokenIterator.h>
+#include <AboutStringHelper.h>
 #include <index.html.gz.h>
 
 void MiLightHttpServer::begin() {
@@ -114,19 +115,19 @@ void MiLightHttpServer::onSettingsSaved(SettingsSavedHandler handler) {
 }
 
 void MiLightHttpServer::handleAbout() {
-  DynamicJsonBuffer buffer;
-  JsonObject& response = buffer.createObject();
+  // DynamicJsonBuffer buffer;
+  // JsonObject& response = buffer.createObject();
 
-  response["version"] = QUOTE(MILIGHT_HUB_VERSION);
-  response["variant"] = QUOTE(FIRMWARE_VARIANT);
-  response["free_heap"] = ESP.getFreeHeap();
-  response["arduino_version"] = ESP.getCoreVersion();
-  response["reset_reason"] = ESP.getResetReason();
+  // response["version"] = QUOTE(MILIGHT_HUB_VERSION);
+  // response["variant"] = QUOTE(FIRMWARE_VARIANT);
+  // response["free_heap"] = ESP.getFreeHeap();
+  // response["arduino_version"] = ESP.getCoreVersion();
+  // response["reset_reason"] = ESP.getResetReason();
 
-  String body;
-  response.printTo(body);
+  // String body;
+  // response.printTo(body);
 
-  server.send(200, APPLICATION_JSON, body);
+  server.send(200, APPLICATION_JSON, AboutStringHelper::generateAboutString());
 }
 
 void MiLightHttpServer::handleGetRadioConfigs() {

+ 1 - 1
platformio.ini

@@ -24,7 +24,7 @@ lib_deps_external =
   CircularBuffer@~1.2.0
 extra_scripts =
   pre:.build_web.py
-build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -DHTTP_UPLOAD_BUFLEN=128 -Idist -Ilib/DataStructures
+build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -DHTTP_UPLOAD_BUFLEN=128 -D FIRMWARE_NAME=milight-hub -Idist -Ilib/DataStructures
 # -D DEBUG_PRINTF
 # -D MQTT_DEBUG
 # -D MILIGHT_UDP_DEBUG

+ 6 - 0
web/src/js/script.js

@@ -159,6 +159,12 @@ var UI_FIELDS = [ {
     type: "string",
     tab: "tab-mqtt"
   }, {
+    tag:   "mqtt_birth_topic", 
+    friendly: "MQTT Birth Topic",
+    help: "Birth Topic - JSON blob with system details will be sent to this topic upon connection",
+    type: "string",
+    tab: "tab-mqtt"
+  }, {
     tag:   "radio_interface_type", 
     friendly: "Radio interface type",
     help: "2.4 GHz radio model. Only change this if you know you're not using an NRF24L01!",