cotonic.mqtt Cotonic
MQTT
The mqtt module provides functions to work with mqtt topics. The broker uses this module as the basis to provide its functionality. This module also has some utility functions to easily extract information from topics.
matches cotonic.mqtt.matches(pattern, topic)
Returns true when the pattern matches the topic, false otherwise.
cotonic.mqtt.matches("truck/+/speed", "truck/01/speed");
=> true
cotonic.mqtt.matches("boat/+/speed", "truck/01/speed");
=> false
cotonic.mqtt.matches("+/+/speed", "plane/01/speed");
=> true
cotonic.mqtt.matches("+/+/speed", "plane/01/height");
=> false
fill cotonic.mqtt.fill(pattern, params)
Fill can use a pattern topic, and use the param object to create an mqtt topic. Returns a string with the created topic.
cotonic.mqtt.fill("truck/+truck_id/speed", {truck_id: 100});
=> "truck/100/speed"
extract cotonic.mqtt.extract(pattern, topic)
Extract values from topic into an object. The pattern +<key> matches a single level of the topic path. It places an attribute key with as value the found element in the path in the returned object. The pattern #<key> matches a multi level path. When it is matched it places that part of the path into a list. Returns an object with the found elements.
cotonic.mqtt.extract("truck/+truck_id/speed", "truck/01/speed");
=> {truck_id: "01"}
cotonic.mqtt.extract("truck/+truck_id/#params", "truck/01/speed");
=> {truck_id: "01", params: ["speed"]}
exec cotonic.mqtt.exec(pattern, topic)
When the pattern matches the topic, extract the values from the topic. Returns the extracted values when the pattern matches, null otherwise.
cotonic.mqtt.exec("truck/+truck_id/speed", "truck/01/speed");
=> {truck_id: "01"}
cotonic.mqtt.exec("boat/+truck_id/speed", "truck/01/speed");
=> null
remove_named_wildcards cotonic.mqtt.remove_named_wildcards(pattern)
Remove the special, and non mqtt compliant, wildcards from the pattern and return a compliant topic.
cotonic.mqtt.remove_named_wildcards("truck/+truck_id/speed");
=> "truck/+/speed"
cotonic.mqtt.remove_named_wildcards("truck/#truck_info");
=> "truck/#"