IPCamera-Reolink

 view release on metacpan or  search on metacpan

lib/IPCamera/Reolink.pm  view on Meta::CPAN

        $self->{camera_certificate_authority_file} = $camera_certificate_authority_file;
        $self->{_is_hash_ref} = 0;
    } # if
    $self->{_camera_rest_client} = undef; # defer REST connection to camera until first Login()
    $self->{_camera_login_token} = undef;  # pass to other API functions
    $self->{_camera_login_lease_time} = 0; # time in seconds that the login token is valid, force initial Login()
    $self->{_camera_login_lease_start_time} = time(); # time that the login token was acquired, force initial Login()
    my $camera_url = $self->{camera_url};
    if($camera_url =~ m/^https/i){
        $self->{_is_https} = 1;
    }else{
        $self->{_is_https} = 0;
    } # if
    return $self;
} # new()

# _sendCameraCommand() - send command to camera and return response
sub _sendCameraCommand($$$$){
    my($camera_rest_client, $camera_command, $request_r, $token) = @_;

    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): enter: camera_command '$camera_command' request_r '" . Dumper($request_r) . "'\n" if($DEBUG > 2);

    my $t1 = Time::HiRes::time() if($DEBUG > 1);
    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t1 . ": call JSON::encode_json()\n" if($DEBUG > 2);
    my $encoded_request = JSON::encode_json($request_r);

    my $t2 = Time::HiRes::time() if($DEBUG > 2);

    if(defined($token)){
        if($camera_command eq 'Snap'){
            # Snap command returns JPG data not JSON, rs is a random string used to prevent browser caching of the image data.
            my $url = 'api.cgi?cmd=' . $camera_command . '&token=' . $token . '&rs=' . @$request_r[0]->{param}->{rs};
            print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t2 . ": call POST($url)\n" if($DEBUG > 2);
            $camera_rest_client->POST($url, $encoded_request);
        }else{
            # Returns JSON data.
            my $url = 'api.cgi?cmd=' . $camera_command . '&token=' . $token;
            print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t2 . ": call POST($url)\n" if($DEBUG > 2);
            $camera_rest_client->POST($url, $encoded_request);
        } # if
    }else{
        # $token undefined for Login
        my $url = 'api.cgi?cmd=' . $camera_command;
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t2 . ": call POST($url)\n" if($DEBUG > 2);
        $camera_rest_client->POST($url, $encoded_request);
    } # if

    my $t3 = Time::HiRes::time() if($DEBUG > 2);
    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t3 . ": call responseCode()\n" if($DEBUG > 2);
    my $response_code = $camera_rest_client->responseCode();

    my $t4 = Time::HiRes::time() if($DEBUG > 2);
    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t4 . ": call responseContent()\n" if($DEBUG > 2);
    my $response_content = $camera_rest_client->responseContent(); # JSON

    if($camera_command eq 'Snap'){
        # responseContent is not JSON but the actual JPG image data
        #
        # Content-Type: image/jpeg
        # Content-Length: 171648
        # Connection: keep-alive
        # X-Frame-Options: SAMEORIGIN
        # X-XSS-Protection: 1; mode=block
        # X-Content-Type-Options: nosniff
        # .............................(JPG data)
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t4 . ": jpg responseContent() length " . length($response_content) . "\n" if($DEBUG > 2);
        my $t6 = Time::HiRes::time() if($DEBUG > 1);
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . ($t2 - $t1) . " POST " . ($t3 - $t2) . " responseCode " . ($t4 - $t3) . " responseContent " . ($t6 - $t4) . " TOTAL " . ($t6 - $t1) . "\n...
        if($DEBUG > 1){
            if($DEBUG > 3){
                my @headers = $camera_rest_client->responseHeaders();
                foreach my $header (@headers){
                    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): header '" . $header . "' '" . $camera_rest_client->responseHeader($header) . "' \n";
                } # for
            } # if
            my $save_linewidth = $Data::Dump::LINEWIDTH;
            $Data::Dump::LINEWIDTH = 500; # no linebreak
            print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): command '" . $camera_command . "' token '" . (defined($token) ? $token : 'undef') . "' request '" . Dumper($request_r) . "' response leng...
            $Data::Dump::LINEWIDTH = $save_linewidth; 
        } # if
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): exit OK\n" if($DEBUG > 2);
        return $response_content;
    }else{
        my $t5 = Time::HiRes::time() if($DEBUG > 2);
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): " . $t5 . ": call JSON::decode_json()\n" if($DEBUG > 2);

        my $response_r;
        eval{
            $response_r = JSON::decode_json($response_content);
        };
        if($@){
            print STDERR scalar(localtime()) . ": error: IPCamera::Reolink::_sendCameraCommand($camera_command): JSON::decode_json() failed - '$@' - responseContent '$response_content'\n";
            return undef;
        } # if

        my $t6 = Time::HiRes::time() if($DEBUG > 1);
        print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): JSON encode " . ($t2 - $t1) . " POST " . ($t3 - $t2) . " responseCode " . ($t4 - $t3) . " responseContent " . ($t5 - $t4) . " JSON decode " ....
        if($DEBUG > 1){
            if($DEBUG > 3){
                my @headers = $camera_rest_client->responseHeaders();
                foreach my $header (@headers){
                    print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): header '" . $header . "' '" . $camera_rest_client->responseHeader($header) . "' \n";
                } # for
            } # if
            my $save_linewidth = $Data::Dump::LINEWIDTH;
            $Data::Dump::LINEWIDTH = 500; # no linebreak
            print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): command '" . $camera_command . "' token '" . (defined($token) ? $token : 'undef') . "' request '" . Dumper($request_r) . "' response '" ....
            $Data::Dump::LINEWIDTH = $save_linewidth; 
        } # if

        my $code = @$response_r[0]->{code};
        if($response_code ne '200' || $code != 0){
            print STDERR scalar(localtime()) . ": error: IPCamera::Reolink::_sendCameraCommand($camera_command): exit ERROR: response_code '$response_code ' code '$code'\n" if($DEBUG > 2);
            return undef;
        }else{
            print STDERR scalar(localtime()) . ": debug: IPCamera::Reolink::_sendCameraCommand($camera_command): exit OK response_code '$response_code ' code '$code'\n" if($DEBUG > 2);
            return $response_r;
        } # if
    } # if
} # _sendCameraCommand()



( run in 2.896 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )