Hooks


Action Hooks

Action hooks are used for doing ‘things’ when data comes in from the MQTT broker.

after_mqttcogs_msg_in

Called after MqttCogs has received data from the Mqtt broker and persisted data to the database. If you want to intercept data before it is persisted see Filter Hooks below.

Parameters

$utc – (int) utc time in milliseconds (since epoch) that the message arrived

$topic – (string) the mqtt topic

$payload – (string) the mqtt payload

Example

Send an email when a particular threshold is crossed.

add_action('after_mqttcogs_msg_in', 'mqttcogs_msg_in', 10, 3);

function mqttcogs_msg_in($utc, $topic, $payload)
{
	switch($topic) {
		case 'mysensors_out/93/0/1/0/37':
			if (((int) $payload)<90) {
				wp_mail( 'test@test.com', 'Sensor Moisture LOW', $payload ); 
			}
		break;
	}
}

Filter Hooks

WordPress filter hooks are normally used to manipulate data before and after it is persisted or displayed. In the case of MqttCogs these filter hooks are useful for manipulating Mqtt messages before they are persisted to the database table. You might, for example, want to expand some JSON data or add some other custom data to the payload.

mqttcogs_msg_in_pre

Called after an Mqtt message is received but before the Mqtt message is persisted to the wordpress database.

Parameters

$publish_object– (object) See classes in sskaje\mqtt\Message.

$datetime(datetime) when the message arrived. Use setTimestamp to alter this if required.

Example

Fiddle with the topic before the mqtt message is saved to the database.

add_filter('mqttcogs_msg_in_pre', 'mqttcogs_msg_in_pre', 10, 3);
 function mqttcogs_msg_in_pre($publish_object, $utc)
 {
     $topic = $publish_object.getTopic();
     $topic = $topic.'/somethingelse';
     $publish_object.setTopic($topic);
     return $publish_object;
 }

mqttcogs_topic_pre

Called on *each* topic just before it is used to extract data from the database. The filter is run once for *each* topic that results from the user topic selector. The topic selector can result in multiple topics as it can be delimited with a comma (easy..) or if it contains a wildcard %. For example, if a user passed mysensors_out/{user}/1/1/0/0, the filter may be used to replace the {user} part with the wordpress user id.

Parameters

$topic (string) The Mqtt topic

Example

This example uses the mqttcogs_topic_pre to change and {user} text in the topic to BigBrother.

add_filter('mqttcogs_topic_pre', 'filter_mqttcogs_topic_pre', 10, 3);
 function filter_mqttcogs_topic_pre($topic)
 {
     $topic = str_replace("{user}","BigBrother",$topic);
     return $topic;
 }

mqttcogs_shortcode_pre

Called after Mqtt data is extracted from the database but before any Mqtt shortcodes are run.

Parameters

$therows– (ARRAY_A) Associative array of extracted data rows. Fields include utc and payload.

$topic (string) The Mqtt topic

Example

This example uses the mqttcogs_shortcode_pre to intercept data. todo..

add_filter('mqttcogs_shortcode_pre', 'mqttcogs_shortcode_pre', 10, 3);
 function mqttcogs_shortcode_pre($therows, $topic)
 {
     foreach($therows as $key => $row) {
           //decode the payload into a json object
           $payload =json_decode($row['payload'], true);
           //fiddle with the object, perhaps just ignore it!
           unset($therows[$key]);
     }
     return $therows;
 }

About | Examples | Shortcodes | Hooks | Download