Skip to content
Snippets Groups Projects
put_batch.php 2.36 KiB
Newer Older
root's avatar
root committed
<?php

namespace SmartData;

require_once('../bin/smartdata/SmartAPI.php');
root's avatar
root committed
require_once('../bin/smartdata/Logger.php');

http_response_code(HttpStatusCode::BAD_REQUEST);

$compl = array();
if(isset($_GET['version']))    $compl = array_merge($compl, array('version' => $_GET['version']));
if(isset($_GET['unit']))       $compl = array_merge($compl, array('unit' => $_GET['unit']));
//if(isset($_GET['value']))      $compl = array_merge($compl, array('value' => $_GET['value']));
if(isset($_GET['error']))      $compl = array_merge($compl, array('error' => $_GET['error']));
if(isset($_GET['confidence'])) $compl = array_merge($compl, array('confidence' => $_GET['confidence']));
if(isset($_GET['x']))          $compl = array_merge($compl, array('x' => $_GET['x']));
if(isset($_GET['y']))          $compl = array_merge($compl, array('y' => $_GET['y']));
if(isset($_GET['z']))          $compl = array_merge($compl, array('z' => $_GET['z']));
//if(isset($_GET['t']))          $compl = array_merge($compl, array('t' => $_GET['t']));
if(isset($_GET['dev']))        $compl = array_merge($compl, array('dev' => $_GET['dev']));


//Logger::print_r($compl, true);

try {
    $content = file_get_contents('php://input');

    if(is_array(json_decode($content, true))) {
        // Is JSON
        throw new Exception\NotImplementedException("JSON-based import in batch has not been implemented yet");
    } else {
        // Is Binary
        if(!REQUEST_CERT){
            throw new Exception\BadRequestException("!CERT > For batch import, the user must use a valid certificate");
        }
        $series = Series::unpack($content);
        Logger::debug($series, true);

        $smartdata_array = array();
        while($content) {
            $data = SmartData::unpack($content, $compl);
            Logger::debug($data, true);
root's avatar
root committed
            if($data instanceof SmartData)
                array_push($smartdata_array, $data);
        }

        $db = new Backend();
        if(!$db->insertBatch($series, ...$smartdata_array)) {
            throw new Exception\InsertionFailedException("Insertion Failed");
        }
    }
}catch(Exception\CustomException $e){
    http_response_code($e->getHTTPCodeError());
    Logger::exception($e);
    return false;
}catch(\Exception $e){
    http_response_code(HttpStatusCode::BAD_REQUEST);
    Logger::exception($e);
    return false;
}

http_response_code(HttpStatusCode::NO_CONTENT);