2022-03-26 10:19:13 +01:00
|
|
|
package APIsigoclient;
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
|
|
|
|
#
|
|
|
|
#Client for sigo
|
|
|
|
#
|
2022-03-31 07:28:37 +02:00
|
|
|
#380116b5-0522-43da-ab66-477744a731a3
|
|
|
|
#
|
2022-03-26 10:19:13 +01:00
|
|
|
#use lib qw(/var/www/copri-bike/shareeapp-fr01/src);
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use POSIX;
|
|
|
|
use Exporter;
|
|
|
|
our @ISA = qw (Exporter);
|
|
|
|
|
|
|
|
use POSIX;
|
|
|
|
use CGI;
|
|
|
|
use JSON;
|
|
|
|
use LWP::UserAgent;
|
|
|
|
use DateTime;
|
|
|
|
use Time::Piece;
|
|
|
|
use Scalar::Util qw(looks_like_number);
|
|
|
|
use Config::General;
|
|
|
|
|
|
|
|
use Lib::Config;
|
|
|
|
use Mod::DBtank;
|
|
|
|
use Mod::Basework;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
my $cf = new Config;
|
|
|
|
my $dbt = new DBtank;
|
|
|
|
my $bw = new Basework;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self = {};
|
|
|
|
bless($self,$class);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
|
|
|
|
|
|
|
|
my $api_file = "/var/www/copri4/shareeconf/apikeys.cfg";
|
|
|
|
my $aconf = Config::General->new($api_file);
|
|
|
|
my %apikeyconf = $aconf->getall;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new;
|
|
|
|
$ua->agent("sharee sigo-client");
|
|
|
|
my $size = $ua->max_size;
|
2022-03-31 07:28:37 +02:00
|
|
|
my $bytes = 100000;
|
2022-03-26 10:19:13 +01:00
|
|
|
$ua->max_size( $bytes );
|
|
|
|
$ua->default_header( 'x-api-key' => $apikeyconf{sigo}->{api_key} );
|
|
|
|
|
|
|
|
my $json = JSON->new->allow_nonref;
|
|
|
|
my %varenv = $cf->envonline();
|
|
|
|
my $response_in = {};
|
|
|
|
my $dbh = "";
|
|
|
|
my $owner = 169;
|
|
|
|
|
|
|
|
open(FILE,">>$varenv{logdir}/APIsigo_client.log");
|
|
|
|
print FILE "\n*** $now_dt 'sigo client' \n";
|
|
|
|
|
|
|
|
sub get_bikes {
|
|
|
|
my $self = shift;
|
2022-03-31 07:28:37 +02:00
|
|
|
my $uri_operator = shift || "";
|
2022-03-26 10:19:13 +01:00
|
|
|
|
2022-03-30 13:06:39 +02:00
|
|
|
open(FILE,">>$varenv{logdir}/APIsigo_client.log");
|
|
|
|
print FILE "\n*** $now_dt 'sigo client' \n";
|
|
|
|
|
2022-03-26 10:19:13 +01:00
|
|
|
my $endpoint = "https://sigo.dev.sigo.green/api/v1/bikes";
|
|
|
|
print "$endpoint\n";
|
|
|
|
|
2022-03-31 07:28:37 +02:00
|
|
|
my $return = {};
|
|
|
|
my $rest_json = "";
|
|
|
|
my $ret_json = get_sigo("","$endpoint",$rest_json);
|
|
|
|
eval {
|
|
|
|
$response_in = decode_json($ret_json);
|
|
|
|
print FILE "<--- get_bikes response_in:\n" . Dumper($response_in);
|
|
|
|
if(ref($response_in) eq "HASH"){
|
|
|
|
foreach my $resp (@{ $response_in->{items} }) {
|
|
|
|
#print Dumper($resp) . "\n";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{bike} = "$resp->{mobile_bike_id}" || "";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{station} = "$resp->{site}->{address}" || "";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{uri_operator} = "$uri_operator" || "";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{description} = "E-Lastenrad";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{gps}->{latitude} = $resp->{site}->{lat} || "";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{gps}->{longitude} = $resp->{site}->{lon} || "";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{bike_charge} = "$resp->{energy_level}" || "0";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{state} = "";
|
|
|
|
if($resp->{status} eq "ACTIVE"){#?
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{state} = "available";
|
|
|
|
}elsif($resp->{status} eq "MAINTANANCE"){
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{state} = "maintanance";
|
|
|
|
}elsif($resp->{status} eq "OPERATION"){#?
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{state} = "occupied";
|
|
|
|
}
|
|
|
|
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{lock_state} = "unlocked";
|
|
|
|
if($resp->{state}->{locked}){
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{lock_state} = "locked";
|
|
|
|
}
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{system} = "sigo";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{unlock_allowed} = "1";
|
|
|
|
$return->{$resp->{mobile_bike_id}}->{tariff_description} = {};
|
|
|
|
}
|
2022-03-26 10:19:13 +01:00
|
|
|
}
|
2022-03-31 07:28:37 +02:00
|
|
|
};
|
|
|
|
if ($@){
|
|
|
|
print FILE "<--- failure get_bikes raw response_in:\n" . Dumper($ret_json) . "\n";
|
|
|
|
warn $@;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
2022-03-26 10:19:13 +01:00
|
|
|
|
2022-03-31 07:28:37 +02:00
|
|
|
return $return;
|
2022-03-26 10:19:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#main GET
|
|
|
|
sub get_sigo {
|
|
|
|
my $self = shift;
|
|
|
|
my $endpoint = shift || "";
|
|
|
|
my $rest_json = shift || "";
|
|
|
|
my $sigo_request = "$endpoint";
|
|
|
|
|
|
|
|
print FILE "===> GET sigo >> " . $sigo_request . "\n" . $rest_json . "\n";
|
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => "$sigo_request");
|
|
|
|
$req->content_type('application/json');
|
|
|
|
|
|
|
|
$req->content($rest_json);
|
|
|
|
|
|
|
|
my $res = $ua->request($req);
|
|
|
|
if ($res->is_success) {
|
|
|
|
#print $res->content;
|
|
|
|
return $res->content;
|
|
|
|
print $res->status_line, "\n";
|
|
|
|
}else {
|
|
|
|
print $res->status_line, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub post_sigo {
|
|
|
|
my $self = shift;
|
|
|
|
my $endpoint = shift || "";
|
|
|
|
my $rest_json = shift || "";
|
|
|
|
my $sigo_request = "$endpoint";
|
|
|
|
|
|
|
|
print FILE "===> POST sigo >> " . $sigo_request . "\n" . $rest_json . "\n";
|
|
|
|
|
|
|
|
my $req = HTTP::Request->new(POST => "$sigo_request");
|
|
|
|
$req->content_type('application/json');
|
|
|
|
|
|
|
|
$req->content($rest_json);
|
|
|
|
|
|
|
|
my $res = $ua->request($req);
|
|
|
|
if ($res->is_success) {
|
|
|
|
#print $res->content;
|
|
|
|
return $res->content;
|
|
|
|
print $res->status_line, "\n";
|
|
|
|
}else {
|
|
|
|
print $res->status_line, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
|
|
|
|
|
|
|
1;
|