Skip to content
Snippets Groups Projects
md_put.php 1.82 KiB
<?php

namespace SmartData;

require_once('../bin/smartdata/SmartAPI.php');
require_once('../bin/smartdata/Logger.php');
require_once('../bin/smartdata/MultiSmartData.php');

http_response_code(HttpStatusCode::BAD_REQUEST);

try {
    $content = file_get_contents('php://input');
    //if(!REQUEST_CERT){
    //    throw new Exception\BadRequestException("!CERT > For batch import, the user must use a valid certificate");
    //}

    $values = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
    if (json_last_error() === JSON_ERROR_NONE) {
        if (property_exists( $values, "credentials") ) {
            $credentials = Credentials::fromJson( $values->credentials );
            $backend = new Backend($credentials);
        } else {
            $backend = new Backend();
        }
        $data = MultiSmartData::multi_devices($values);
        if ($data[0]->version == 17) {
            if (!$backend->insert_multiple(...$data)) {
                throw new Exception\InsertionFailedException("MultiValue SmartData Insertion Failed");
            }
        } else {
           if (!$backend->track_multiple(...$data)) {
                throw new Exception\InsertionFailedException("MultiValue SmartData Insertion Failed");
            }
        }
    } else {
        // Is Binary
        $data = MultiSmartData::unpackMultiDevice($content);
        $db = new Backend();
        if(!$db->insert_multiple(...$data)) {
            throw new Exception\InsertionFailedException("Binary MultiDevice SmartData 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);