PHP Scripting

PHP Scheduled

Scheduled Headless scripts are typically used to generate new timeseries data from existing timeseries data.

Scheduled Interval

This Script uses the start_timestamp and end_timestamp of the std_input of the Context object to process data.  

Code

<?php

//use Model Node of the PHP API
use \TiqUtilities\Model\Node;

//this is required for the Context object
require_once 'thinkiq_context.php';

//get Script Context Object
$context = new Context();

//get the start and end time of the interval to process from the std_inputs
$start = $context->std_inputs->start_timestamp;
$end = $context->std_inputs->end_timestamp;

//check to see if the end time is NOT in the future 
if (strtotime($end->format('Y-m-d H:i:s')) < time()){

    //get the instance of this script
    $script_parent = new Node($context->std_inputs->node_id);

    //get attributes from $script_parent
    //required to populate the attributes propety of the Node object
    $script_parent->getAttributes();

    //get the setpoint_publish Attribute object from $script_parent
    $setpoint_publish = $script_parent->attributes['setpoint_publish'];

    //get value stream from $setpoint_publish given $start and $end
    $setpoint_publish_vs = $setpoint_publish->getTimeSeries($start, $end);

    //number of data changes found in the value stream
    $data_changes_found = count($setpoint_publish_vs);

    //get the data_change_count Attribute object from $script_parent
    $data_change_count = $script_parent->attributes['data_change_count'];

    //insertTimeSeries is expecting arrays; the following couple of lines only insert a single value/timestamp
    $value = array($data_changes_found);
    $timestamp = array($end);

    //insert the single element arrays for both value and timestamp
    $data_change_count->insertTimeSeries($value, $timestamp);

    //$context->return_data() must be used 
    $context->return_data();
}
//else, the end time is in the future 
else{
    //return start timestamp to keep the interval from advancing and cliping the end_timestamp
    $return_data_array = array('start_timestamp' => $start->format(DateTimeInterface::RFC3339_EXTENDED));

    //$context->return_data with start_timestamp
    $context->return_data($return_data_array);
}
 

Output

This Script writes the number of data changes counted on the setpoint_publish Attribute to the data_change_count Attribute.  It does so for the Interval defined by the start_timestamp and end_timestamp of the Context object.

Contact Us

ThinkIQ

65 Enterprise
Aliso Viejo, CA  92656
United States

phone: 844-THINKIQ (844)844-6547
email: support@thinkiq.com
websitewww.thinkiq.com