瀏覽代碼

add :hex_device_id and :dec_device_id tokens for MQTT topic, update README

Chris Mullins 8 年之前
父節點
當前提交
d2fef3418b
共有 3 個文件被更改,包括 30 次插入15 次删除
  1. 5 8
      README.md
  2. 18 7
      lib/MQTT/MqttClient.cpp
  3. 7 0
      lib/MQTT/MqttClient.h

+ 5 - 8
README.md

@@ -180,16 +180,11 @@ This will instruct the ESP to send messages to RGB+CCT bulbs with device ID `0x1
 
 To enable passive listening, make sure that `listen_repeats` is set to something larger than 0 (the default value of 3 is a good choice).
 
-To publish data from intercepted packets to an MQTT topic, configure MQTT server settings, and set the `mqtt_update_topic_pattern` to something of your choice. As with `mqtt_topic_pattern`, the tokens `:device_id`, `:device_type`, and `:group_id` will be substituted with the values from the relevant packet.
+To publish data from intercepted packets to an MQTT topic, configure MQTT server settings, and set the `mqtt_update_topic_pattern` to something of your choice. As with `mqtt_topic_pattern`, the tokens `:device_id`, `:device_type`, and `:group_id` will be substituted with the values from the relevant packet.  `:device_id` will always be substituted with the hexadecimal value of the ID.  You can also use `:hex_device_id`, or `:dec_device_id` if you prefer decimal.
 
-The published message is a JSON blob containing the following keys:
+The published message is a JSON blob containing the state that was changed.
 
-* `device_id`
-* `device_type` (rgb_cct, rgbw, etc.)
-* `group_id`
-* Any number of: `status`, `level`, `hue`, `saturation`, `kelvin`
-
-As an example, if `mqtt_update_topic_pattern` is set to `milight/updates/:device_id/:device_type/:group_id`, and the group 1 on button of a Milight remote is pressed, the following update will be dispatched:
+As an example, if `mqtt_update_topic_pattern` is set to `milight/updates/:hex_device_id/:device_type/:group_id`, and the group 1 on button of a Milight remote is pressed, the following update will be dispatched:
 
 ```ruby
 irb(main):005:0> client.subscribe('milight/updates/+/+/+')
@@ -198,6 +193,8 @@ irb(main):006:0> puts client.get.inspect
 ["lights/updates/0x1C8E/rgb_cct/1", "{\"device_id\":7310,\"group_id\":1,\"device_type\":\"rgb_cct\",\"status\":\"on\"}"]
 ```
 
+**Make sure that `mqtt_topic_pattern` and `matt_update_topic_pattern` are different!**  If they are they same you can put your ESP in a loop where its own updates trigger an infinite command loop.
+
 ## UDP Gateways
 
 You can add an arbitrary number of UDP gateways through the REST API or through the web UI. Each gateway server listens on a port and responds to the standard set of commands supported by the Milight protocol. This should allow you to use one of these with standard Milight integrations (SmartThings, Home Assistant, OpenHAB, etc.).

+ 18 - 7
lib/MQTT/MqttClient.cpp

@@ -121,13 +121,7 @@ void MqttClient::publish(
   }
 
   String topic = _topic;
-
-  String deviceIdStr = String(deviceId, 16);
-  deviceIdStr.toUpperCase();
-
-  topic.replace(":device_id", String("0x") + deviceIdStr);
-  topic.replace(":group_id", String(groupId));
-  topic.replace(":device_type", remoteConfig.name);
+  MqttClient::bindTopicString(topic, remoteConfig, deviceId, groupId);
 
 #ifdef MQTT_DEBUG
   printf_P(PSTR("MqttClient - publishing update to %s: %s\n"), topic.c_str(), update);
@@ -177,3 +171,20 @@ void MqttClient::publishCallback(char* topic, byte* payload, int length) {
   milightClient->prepare(config, deviceId, groupId);
   milightClient->update(obj);
 }
+
+inline void MqttClient::bindTopicString(
+  String& topicPattern,
+  const MiLightRemoteConfig& remoteConfig,
+  const uint16_t deviceId,
+  const uint16_t groupId
+) {
+  String deviceIdHex = String(deviceId, 16);
+  deviceIdHex.toUpperCase();
+  deviceIdHex = String("0x") + deviceIdHex;
+
+  topicPattern.replace(":device_id", deviceIdHex);
+  topicPattern.replace(":hex_device_id", deviceIdHex);
+  topicPattern.replace(":dec_device_id", String(deviceId));
+  topicPattern.replace(":group_id", String(groupId));
+  topicPattern.replace(":device_type", remoteConfig.name);
+}

+ 7 - 0
lib/MQTT/MqttClient.h

@@ -41,6 +41,13 @@ private:
     const char* update,
     const bool retain = false
   );
+
+  inline static void bindTopicString(
+    String& topicPattern,
+    const MiLightRemoteConfig& remoteConfig,
+    const uint16_t deviceId,
+    const uint16_t groupId
+  );
 };
 
 #endif