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

Updated for version 2.3

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.

Version 2.4 parameter was changed to an ARRAY and expects an ARRAY of publish_objects to be returned. You can split a single payload into multiple payloads if required.

Parameters

$publish_objects– (array of Message objects) See classes in sskaje\mqtt\Message.

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

Example 1

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)
 {
     //using indexed array here
     for ($i = count($publish_objects)-1; $i>=0; $i--) {
     	  $publish_object = $publish_objects[$i];
          $topic = $publish_object->getTopic();
          $topic = $topic.'/somethingelse';
          $publish_object->setTopic($topic);
     }
     return $publish_objects;
 }

Example 2

Split a single message into multiple messages.

 function mqttcogs_msg_in_pre_minew($publish_objects)
 {
     $publish_object_ret = array();
     
     foreach($publish_objects as $key => $publish_object) {       
         $topic = $publish_object->getTopic();

         //filter by topic
         if (!(substr( $topic, 0, 6 ) === "/minew")) {
             array_push($publish_object_ret, $publish_object);
             continue;
         }

         $publish_object_arr = array();

         $mqttmsg = $publish_object->getMessage();
         $payload_arr = json_decode($mqttmsg);

         if (!is_array($payload_arr)) {
            $payload_arr = array($payload_arr);
         }

         // topic is
         // /gw/ac233fc04bba/status
         $topic = explode('/', $topic);
         $gw = $topic[2];

         /*
         {
        "timestamp": "2020-01-11T09:54:26Z",
        "type": "S1",
        "mac": "AC233FA249C9",
        "bleName": "",
        "rssi": -41,
        "battery": 100,
        "temperature": 20.79,
        "humidity": 57.05
      }    
        */
         
         //this is an array of objects
         foreach($payload_arr as $key => $payload) {
             //type filter...
             $pb = clone $publish_object;

             //for each object we want to make sure the utc field is correct
             //and remove the timestamp from the json as it isn't required
             if (property_exists($payload, 'timestamp')) {
                $pb->setDateTime(new DateTime($payload->timestamp));
                unset($payload->timestamp);
             }

             if (property_exists($payload, 'mac')) {
                $pb->setTopic('/minew/'.$payload->mac); 
                unset($payload->mac);
             }

             $payload->gw = $gw;
             $pb->setMessage(json_encode($payload));
             array_push($publish_object_ret, $pb);
         }
     }
     return $publish_object_ret;
 }
add_filter('mqttcogs_msg_in_pre', 'mqttcogs_msg_in_pre_minew', 10, 2);

mqttcogs_topic_pre

Called (by a shortcode) 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 topic, utc and payload.

$topic (string) The topic selector that was used to extract the data. For example, ‘%’

Example

This example uses the mqttcogs_shortcode_pre to intercept data.

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