function api_controller()
{
    global $session, $action;
    require "Models/input_model.php";
    require "Models/feed_model.php";
    require "Models/process_model.php";
    // POST arduino posts up to emoncms
    if ($action == 'post' && $session['write']) {
        $node = intval($_GET['node']);
        $json = db_real_escape_string($_GET['json']);
        $csv = db_real_escape_string($_GET['csv']);
    }
    if ($csv) {
        $values = explode(',', $csv);
        $i = 0;
        foreach ($values as $value) {
            $i++;
            if ($node) {
                $key = $i;
            } else {
                $key = "csv" . $i;
            }
            $datapairs[] = $key . ":" . $value;
        }
    }
    if ($json) {
        // preg_replace strips out everything appart from alphanumeric characters, whitespace and -.:,
        $json = preg_replace('/[^\\w\\s-.:,]/', '', $json);
        $datapairs = explode(',', $json);
    }
    if ($json || $csv) {
        $time = time();
        // get the time - data recived time
        if (isset($_GET["time"])) {
            $time = intval($_GET["time"]);
            // - or use sent timestamp if present
        }
        $inputs = register_inputs($session['userid'], $node, $datapairs, $time);
        // register inputs
        process_inputs($session['userid'], $inputs, $time);
        // process inputs to feeds etc
        $output['message'] = "ok";
    }
    return $output;
}
function apply_changes_to_experiment($experiment)
{
    $experiment->name = $_POST['experiment-name'];
    $experiment->description = rtrim($_POST['experiment-description']);
    $experiment->projectID = $_POST['project'];
    //$experiment->applicationId = $_POST['application'];
    $userConfigDataUpdated = $experiment->userConfigurationData;
    $schedulingUpdated = $userConfigDataUpdated->computationalResourceScheduling;
    $schedulingUpdated->resourceHostId = $_POST['compute-resource'];
    $schedulingUpdated->nodeCount = $_POST['node-count'];
    $schedulingUpdated->totalCPUCount = $_POST['cpu-count'];
    //$schedulingUpdated->numberOfThreads = $_POST['threads'];
    $schedulingUpdated->wallTimeLimit = $_POST['wall-time'];
    //$schedulingUpdated->totalPhysicalMemory = $_POST['memory'];
    /*
    switch ($_POST['compute-resource'])
    {
        case 'trestles.sdsc.edu':
            $schedulingUpdated->ComputationalProjectAccount = 'sds128';
            break;
        case 'stampede.tacc.xsede.org':
        case 'lonestar.tacc.utexas.edu':
            $schedulingUpdated->ComputationalProjectAccount = 'TG-STA110014S';
            break;
        default:
            $schedulingUpdated->ComputationalProjectAccount = 'admin';
    }
    */
    $userConfigDataUpdated->computationalResourceScheduling = $schedulingUpdated;
    $experiment->userConfigurationData = $userConfigDataUpdated;
    $applicationInputs = get_application_inputs($experiment->applicationId);
    $experimentInputs = $experiment->experimentInputs;
    // get current inputs
    //var_dump($experimentInputs);
    $experimentInputs = process_inputs($applicationInputs, $experimentInputs);
    // get new inputs
    //var_dump($experimentInputs);
    if ($experimentInputs) {
        $experiment->experimentInputs = $experimentInputs;
        //var_dump($experiment);
        return $experiment;
    }
}
/**
 * Create and configure a new Experiment
 * @return Experiment
 */
function assemble_experiment()
{
    //$experimentAssemblySuccessful = true; // errors will set this to false
    //$experimentPath = EXPERIMENT_DATA_ROOT;
    $experimentInputs = array();
    //$experimentOutputs = array();
    $scheduling = new ComputationalResourceScheduling();
    $scheduling->totalCPUCount = $_POST['cpu-count'];
    $scheduling->nodeCount = $_POST['node-count'];
    //$scheduling->numberOfThreads = $_POST['threads'];
    $scheduling->queueName = 'normal';
    $scheduling->wallTimeLimit = $_POST['wall-time'];
    //$scheduling->totalPhysicalMemory = $_POST['memory'];
    $scheduling->resourceHostId = $_POST['compute-resource'];
    $userConfigData = new UserConfigurationData();
    $userConfigData->computationalResourceScheduling = $scheduling;
    $applicationInputs = get_application_inputs($_POST['application']);
    $experimentInputs = process_inputs($applicationInputs, $experimentInputs);
    //var_dump($experimentInputs);
    global $experimentPath, $pathConstant;
    if ($experimentPath != null) {
        $advHandling = new AdvancedOutputDataHandling();
        //echo($experimentPath);
        $advHandling->outputDataDir = str_replace(EXPERIMENT_DATA_ROOT, $pathConstant, $experimentPath);
        $userConfigData->advanceOutputDataHandling = $advHandling;
    }
    /*
    $applicationOutputs = get_application_outputs($_POST['application']);
    $experimentOutputs = array();
    
    foreach ($applicationOutputs as $applicationOutput)
    {
        $experimentOutput = new DataObjectType();
        $experimentOutput->key = $applicationOutput->name;
        $experimentOutput->type = $applicationOutput->type;
        $experimentOutput->value = '';
    
        $experimentOutputs[] = $experimentOutput;
    }
    */
    $experiment = new Experiment();
    // required
    $experiment->projectID = $_POST['project'];
    $experiment->userName = $_SESSION['username'];
    $experiment->name = $_POST['experiment-name'];
    // optional
    $experiment->description = $_POST['experiment-description'];
    $experiment->applicationId = $_POST['application'];
    $experiment->userConfigurationData = $userConfigData;
    $experiment->experimentInputs = $experimentInputs;
    //$experiment->experimentOutputs = $experimentOutputs;
    if ($experimentInputs) {
        return $experiment;
    }
}