DeyeCloud-Client

 view release on metacpan or  search on metacpan

lib/DeyeCloud/Client.pm  view on Meta::CPAN

        'appSecret' => $options{'appSecret'},
        'email'     => $options{'email'},
        'password'  => lc sha256_hex $options{'password'}
    }));
    my $response = $self->ua->request($request);
    if ($response->is_success) {
        my $content = $response->decoded_content;
        my $payload = {};
        $payload = $json->decode($content);
        unless (defined $payload) {
            $self->seterror(DeyeCloud::Client::Common::E_DECFAILED);
            return undef;
        }
        return $payload;
    } else {
        $self->seterror(
            DeyeCloud::Client::Common::E_REQFAILED,
            $response->code,
            ($response->message || status_message $response->code)
        );
        return undef;
    }
} #}}}

sub call :prototype($$%) ($self, $command, %content) {
    #{{{

=pod

=over 4

=item B<baseurl(STRING)>

This method gets or sets Deye Cloud API server base URL:

    my $baseurl = $deye->baseurl;
    $deye->baseurl('https://eu1-developer.deyecloud.com/v1.0');

=item B<method(STRING)>

Get or set HTTP method used to send query data to Deye Cloud API
servers. Endpoints and appropriate methods are described in Deye
Cloud developer guide.

    my $method = $deye->method;
    $deye->method('GET');

=item B<call(STRING, HASH)>

This is universal method to conversate with Deye Cloud API servers
as follows:

    # Setting API server base URL and method is not required in
    # every case. However, pay an attention that device data
    # retrieval CAN require setting those variables explicitly!
    #
    $deye->baseurl('https://eu1-developer.deyecloud.com/v1.0');
    $deye->method('POST');
    my $data = $deye->call('station/latest', 'stationId' => INTEGER);

Available options list depends on endpoint. Most endpoints are described
in Deye Cloud developer guide.

=back

=cut

    unless ($self->ua->default_header('Authorization')) {
        $self->seterror(DeyeCloud::Client::Common::E_NOTOKEN);
        return undef;
    }
    $self->seterror();
    my $json = JSON->new->pretty(0);
    my $uri = URI->new($self->baseurl);
    my $path = join '/', $uri->path, $command;
    $uri->path($path);
    $uri->query_form(%content) if $self->method eq 'GET';
    my $request = HTTP::Request->new($self->method, $uri);
    $request->content($json->encode({ %content })) if $self->method ne 'GET';
    my $response = $self->ua->request($request);
    if ($response->is_success) {
        my $content = $response->decoded_content;
        my $payload = {};
        $payload = $json->decode($content);
        unless (defined $payload) {
            $self->seterror(DeyeCloud::Client::Common::E_DECFAILED);
            return undef;
        }
        return $payload;
    } else {
        $self->seterror(
            DeyeCloud::Client::Common::E_REQFAILED,
            $response->code,
            ($response->message || HTTP::Status::status_message $response->code)
        );
        return undef;
    }
} #}}}

sub status :prototype($$%) ($self, %options) {
    #{{{

=pod

=over 4

=item B<status(HASH)>

Wrapper method to get latest station or device status. Implicitly sets
the right method and baseurl, both are restored on return. Main difference
between B<call()> and B<status()> methods is that B<call()> returns reference
to a hash and B<status()> returns a blessed reference to a B<DeyeCloud::Client::Device>
or B<DeyeCloud::Client::Station> class object. You can use either a variable name
or a getter method in order to refer to a specific variable:

    my $status = $deye->status('deviceId' => INTEGER);
    
    printf "Grid voltage, L1: %.2f\n", $status->G_V_L1;
    
    # or, the same in another way:
    



( run in 1.064 second using v1.01-cache-2.11-cpan-524268b4103 )