migrate to OSM

This commit is contained in:
ragu 2022-05-27 17:11:35 +02:00
parent cc4dfcd70f
commit 46f86f6c05
13 changed files with 476 additions and 59 deletions

View file

@ -1985,6 +1985,7 @@ sub stations_available(){
my $op_return = {};
foreach my $id (sort { $record->{$a}->{barcode} <=> $record->{$b}->{barcode} } keys (%$record)){
$return->{$id}->{authed} = "$authed";
$return->{$id}->{capacity} = $record->{$id}->{int05} || 1;
$return->{$id}->{station} = "$dbt->{operator}->{$varenv->{dbname}}->{oprefix}$record->{$id}->{int04}";
$return->{$id}->{uri_operator} = "$varenv->{wwwhost}";

View file

@ -250,7 +250,7 @@ elsif($q->param('request') eq "booking_request"){
($ct_bike,$ct_tariff) = $apif->fetch_bike_tariff(\%varenv,$authraw,$bike,$aowner);
my $sig_book = {
bikeId => "$ct_bike->{txt22}",
bikeId => "$ct_bike->{txt22}",
rentalId => "",
reservationId => ""
};
@ -259,6 +259,7 @@ elsif($q->param('request') eq "booking_request"){
#sig reservation. not mandatory
if($ct_bike->{int11} == 3){
$sig_book = $si->sig_booking(\%varenv,$authraw,$ct_bike,"","reserve");
$sig_book->{bikeId} = "$ct_bike->{txt22}";
}
$response_book = $apif->booking_request($q,\%varenv,$authraw,$bike,$ct_bike,$ct_tariff,$aowner,$gps,$sig_book);
}elsif(!$ct_tariff->{barcode}){
@ -360,7 +361,7 @@ elsif($q->param('request') eq "booking_cancel" || $q->param('request') eq "booki
$sig_book = $si->sig_booking(\%varenv,$authraw,"",$ctpos,"reserve_end");
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
}else{
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,"");
}
$response = {%$response, %$booking_values};

View file

@ -7,7 +7,7 @@ package APIsigclient;
#
#380116b5-0522-43da-ab66-477744a731a3
#
use lib qw(/var/www/copri-bike/shareeapp-sx/src);
#use lib qw(/var/www/copri-bike/shareeapp-sx/src);
use warnings;
use strict;
use POSIX;
@ -27,6 +27,7 @@ use Lib::Config;
use Mod::DBtank;
use Mod::Basework;
use Mod::APIfunc;
use Mod::Shareework;
use Data::Dumper;
my $q = new CGI;
@ -34,6 +35,7 @@ my $json = JSON->new->allow_nonref;
my $cf = new Config;
my $dbt = new DBtank;
my $apif = new APIfunc;
my $tk = new Shareework;
my $bw = new Basework;
sub new {
@ -69,7 +71,7 @@ sub sig_available {
my $authed = 0;
my $tariff_content = {};
$authed = 1 if(ref($ctadr) eq "HASH" && $ctadr->{c_id} && $ctadr->{c_id} > 0);
(my $bike_group,my $user_group,$tariff_content,my $user_tour) = $apif->fetch_tariff($varenv->{dbname},$ctadr,$q->param('ctadrcookie'));
(my $bike_group,my $user_group,$tariff_content,my $user_tour) = $apif->fetch_tariff($varenv->{dbname},$ctadr,$q->param('authcookie'));
my $hotline_hash = {
table => "contentuser",
@ -103,7 +105,7 @@ sub sig_available {
#station (mainly using sigojson site object)
my $station = "SX$resp->{site}->{id}";
if($station && looks_like_number($resp->{site}->{id}) && $resp->{site}->{status} eq "ACTIVE" && $resp->{site}->{lat} && $resp->{site}->{lon}){
if($station && looks_like_number($resp->{site}->{id})){
print FILE "Station: $station\n";
$response_out->{$station}->{station} = "$station";
$response_out->{$station}->{authed} = "$authed";
@ -113,14 +115,15 @@ sub sig_available {
$response_out->{$station}->{gps}->{latitude} = "$resp->{site}->{lat}" || "";
$response_out->{$station}->{gps}->{longitude} = "$resp->{site}->{lon}" || "";
$response_out->{$station}->{gps_radius} = "75";
#Other than Active status, should not be used to display information to a customer
$response_out->{$station}->{state} = "defect";
if($resp->{site}->{status} eq "ACTIVE"){#?
if(uc($resp->{site}->{status}) eq "ACTIVE"){
$response_out->{$station}->{state} = "available";
}elsif($resp->{site}->{status} eq "MAINTANANCE"){
}elsif(uc($resp->{site}->{status}) eq "MAINTANANCE"){
$response_out->{$station}->{state} = "maintanance";
}elsif($resp->{site}->{status} eq "OFFLINE"){
$response_out->{$station}->{state} = "defect";
}
$response_out->{$station}->{operator_data} = {
"operator_name" => "",
"operator_hours" => "",
@ -149,6 +152,7 @@ sub sig_available {
}
}
delete $response_out->{$station} if(uc($resp->{site}->{status}) ne "ACTIVE" || !$resp->{site}->{lat} || !$resp->{site}->{lon});
}
}#end stations_available
@ -172,18 +176,19 @@ sub sig_available {
$response_out->{$bike}->{gps}->{longitude} = "$resp->{state}->{lon}" || "";
$response_out->{$bike}->{bike_charge} = "$resp->{energy_level}" || "0";
$response_out->{$bike}->{state} = "defect";
if($resp->{reservation_state} eq "RESERVED"){
if(uc($resp->{reservation_state}) eq "RESERVED"){
$response_out->{$bike}->{state} = "requested";
}elsif($resp->{reservation_state} eq "ACTIVE-RENTAL"){
}elsif(uc($resp->{reservation_state}) eq "ACTIVE-RENTAL"){
$response_out->{$bike}->{state} = "occupied";
}elsif(!$resp->{reservation_state}){
$response_out->{$bike}->{state} = "available";
}
#$response_out->{$bike}->{lock_state} = "undefined";#?
#$response_out->{$bike}->{lock_state} = "locked";# I think default should be unlocked
#1. change can take up to 2 mins to reflect
#2. When a customer will start a reservation, it will always be unlock,
#Sigo software automatically handle this condition
$response_out->{$bike}->{lock_state} = "unlocked";
if($resp->{state}->{locked}){
if($resp->{lock}->{status} eq "locked"){
$response_out->{$bike}->{lock_state} = "locked";
}
$response_out->{$bike}->{system} = "sigo";
@ -249,7 +254,8 @@ sub sig_available {
$return2copri->{$bike}->{int20} = $key;
}
}
delete $response_out->{$bike} if($resp->{status} ne "ACTIVE");#don't view not active bikes
#delete if $resp->{status} ne "ACTIVE" || reservation_state ne Reserved|Active-Rental
delete $response_out->{$bike} if(uc($resp->{status}) ne "ACTIVE" || $resp->{reservation_state});#don't view not active bikes
}
}#end bikes_available
@ -296,6 +302,7 @@ sub sig_booking {
);
}
#reservation end, this sig request is still not defined. maybe end by rentalId
#int10 state will be set on booking_request
elsif($todo eq "reserve_end"){
$endpoint .= "reserve/end";
%json = (
@ -304,6 +311,7 @@ sub sig_booking {
);
}
#rental start
#int10 state will be set on main booking_update
elsif($todo eq "rental"){
$endpoint .= "rental";
my $sig_bikeId = $ctpos->{txt22} || $ct_bike->{txt22};
@ -357,12 +365,27 @@ sub sig_booking {
if($ctpos->{c_id}){
my $rows = 0;
my $update_pos = {
table => "contenttranspos",
mtime => "now()",
owner => "169",
};
my $rows = 0;
#TODO try it with user_bikes_available
#on sig api states maybe not just in time available
#check and save sig bike states
#$q->param(-name=>'request',-value=>"bikes_available");
#(my $response->{bikes},my $return2copri) = $self->sig_available($q,$varenv,$ctadr);
#my $ctrows = 0;
#$ctrows = $tk->sigbike_cupdate($return2copri);
#print FILE "<--- ctrows:$ctrows, update content state with:" . Dumper($return2copri) . "\n";
#foreach my $sid (keys (%{$return2copri})){
# $update_pos->{int10} = "$return2copri->{$sid}->{int10}" if($return2copri->{$sid}->{int10});
# $update_pos->{int20} = "$return2copri->{$sid}->{int20}" if($return2copri->{$sid}->{int20});
#}
$update_pos->{txt10} = "$sig_book->{reservationId}" if($sig_book->{reservationId});
$update_pos->{txt11} = "$sig_book->{rentalId}" if($sig_book->{rentalid});
$rows = $dbt->update_record($dbh,$update_pos,$ctpos);

208
copri4/main/src/Mod/GBFSout.pm Executable file
View file

@ -0,0 +1,208 @@
package Mod::GBFSout;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#without login only public Stations have to be viewable:
#https://shareeapp-primary.copri-bike.de/GBFSout
#
#with login and valid autcookie:
#https://shareeapp-primary.copri-bike.de/GBFSout?sessionid=a49aba2b5440be72816db2_rafo87znqx
#
#
use warnings;
use strict;
use POSIX;
use Exporter;
our @ISA = qw (Exporter);
use CGI::Cookie ();
use CGI ':standard';
use Apache2::Const -compile => qw(OK );
use Scalar::Util qw(looks_like_number);
use JSON;
use LWP::UserAgent;
use Lib::Config;
use Mod::DBtank;
use Mod::APIfunc;
use Mod::Basework;
use Data::Dumper;
my $bw = new Basework;
sub handler {
my $r = shift;
my $q = new CGI;
$q->import_names('R');
my $dbt = new DBtank;
my $apif = new APIfunc;
my $cf = new Config;
my %varenv = $cf->envonline();
my $coo = $q->cookie('domcookie') || $R::sessionid || "";
my $users_sharee = { c_id => 0 };
my $api_return = { authcookie => '' };
($api_return,$users_sharee) = $apif->auth_verify($q,$coo,"");
if($dbt->{website}->{$varenv{syshost}}->{merchant_id}){
$api_return->{authcookie} = $dbt->{website}->{$varenv{syshost}}->{merchant_id} if(!$api_return->{authcookie});
}elsif($dbt->{operator}->{$varenv{dbname}}->{merchant_id}){
$api_return->{authcookie} = $dbt->{operator}->{$varenv{dbname}}->{merchant_id} if(!$api_return->{authcookie});
}elsif($dbt->{primary}->{$varenv{dbname}}->{merchant_id}){
$api_return->{authcookie} = $dbt->{primary}->{$varenv{dbname}}->{merchant_id} if(!$api_return->{authcookie});
}
$users_sharee->{c_id} = "0" if(!$users_sharee->{c_id});
$bw->log("GBFSout handler with api_return: ",$api_return,"");
print $q->header(-type => "application/json", -charset => "utf-8", -'Access-Control-Allow-Origin' => "*");
#main
my $project = "all";
$project = $dbt->{website}->{$varenv{syshost}}->{project} if($dbt->{website}->{$varenv{syshost}}->{project});
$project = $dbt->{operator}->{$varenv{dbname}}->{project} if($dbt->{operator}->{$varenv{dbname}}->{project});
#request primary will jsonclient loop_sharees
my $uri_request = $dbt->{primary}->{sharee_primary}->{primaryApp};
#detect DMS, because this will do it directly without loop_sharees
$uri_request = $dbt->{operator}->{$varenv{dbname}}->{operatorApp} if($varenv{dbname} ne "sharee_primary" && $varenv{syshost} =~ /shareedms-/);
if($R::request eq "stations_available"){
station_information($api_return,\%varenv,$users_sharee,$uri_request,$project);
}
if($R::request eq "bikes_available"){
vehicle_status($api_return,\%varenv,$users_sharee,$uri_request,$project);
}
return Apache2::Const::OK;
}
sub station_information {
my $authcookie = shift || { authcookie => '' };
my $varenv = shift;
my $users_sharee = shift || { c_id => 0 };
my $uri_request = shift || "";
my $project = shift || "all";
my $json = JSON->new->allow_nonref;
my $rest_stations = "request=stations_available&project=$project&authcookie=$authcookie->{authcookie}";
my $gbfs_resp = {};
my $stations_json = fetch_primary_json("",$uri_request,$rest_stations);
#decode json to hash
my $response_stations = decode_json($stations_json);
my @gbfs_stations = ();
foreach my $station (keys (%{ $response_stations->{shareejson}->{stations} })) {
#print Dumper($response_stations->{shareejson}->{stations}->{$station});
my %gbfs_station = ();
$gbfs_station{$station}{station_id} = $response_stations->{shareejson}->{stations}->{$station}->{station};
$gbfs_station{$station}{name} = Encode::encode('utf-8', Encode::decode('iso-8859-1',$response_stations->{shareejson}->{stations}->{$station}->{description}));
$gbfs_station{$station}{capacity} = $response_stations->{shareejson}->{stations}->{$station}->{capacity};
$gbfs_station{$station}{lat} = $response_stations->{shareejson}->{stations}->{$station}->{gps}->{latitude};
$gbfs_station{$station}{lon} = $response_stations->{shareejson}->{stations}->{$station}->{gps}->{longitude};
$gbfs_station{$station}{is_charging_station} = 1 if(grep(/300102/, @{$response_stations->{shareejson}->{stations}->{$station}->{station_group}}));#E-L
push (@gbfs_stations, $gbfs_station{$station});
}
$gbfs_resp = {
"last_updated" => 1653031183,
"ttl" => 10,
"version" => "2.2",
"data" => {
"stations" => [ @gbfs_stations ]
}
};
my $jrout = $json->pretty->encode($gbfs_resp);
print $jrout;
return;
}#end station_information
sub vehicle_status {
my $authcookie = shift || { authcookie => '' };
my $varenv = shift;
my $users_sharee = shift || { c_id => 0 };
my $uri_request = shift || "";
my $project = shift || "all";
my $json = JSON->new->allow_nonref;
my $rest_bikes = "request=bikes_available&project=$project&authcookie=$authcookie->{authcookie}";
my $gbfs_resp = {};
my $bikes_json = fetch_primary_json("",$uri_request,$rest_bikes);
#decode json to hash
my $response_bikes = decode_json($bikes_json);
my @gbfs_bikes = ();
foreach my $bike (keys (%{ $response_bikes->{shareejson}->{bikes} })) {
#if($response_bikes->{shareejson}->{bikes}->{$bike}->{station} eq "FR105"){
my %gbfs_bike = ();
$gbfs_bike{$bike}{station_id} = $response_bikes->{shareejson}->{bikes}->{$bike}->{station};
$gbfs_bike{$bike}{vehicle_id} = $response_bikes->{shareejson}->{bikes}->{$bike}->{bike};
$gbfs_bike{$bike}{vehicle_name} = Encode::encode('utf-8', Encode::decode('iso-8859-1',$response_bikes->{shareejson}->{bikes}->{$bike}->{description}));#not part of gbfs
$gbfs_bike{$bike}{vehicle_type_id} = $response_bikes->{shareejson}->{bikes}->{$bike}->{bike_group}[0];
#$gbfs_bike{$bike}{is_reserved} = 0;
#$gbfs_bike{$bike}{is_disabled} = 0;
push (@gbfs_bikes, $gbfs_bike{$bike});
#}
}
#{
#"vehicle_id":"987fd100-b822-4347-86a4-b3eef8ca8b53",
#"last_reported":1609866204,
#"is_reserved":false,
#"is_disabled":false,
#"vehicle_type_id":"def456",
#"current_range_meters":6543.0,
#"station_id":"86",
#"pricing_plan_id":"plan3"
#}
$gbfs_resp = {
"last_updated" => 1653031183,
"ttl" => 0,
"version" => "3.0",
"data" => {
"vehicles" => [ @gbfs_bikes ]
}
};
my $jrout = $json->pretty->encode($gbfs_resp);
print $jrout;
return;
}#end vehicle_status
#requestor
sub fetch_primary_json {
my $self = shift;
my $primary_server = shift || "";
my $rest = shift || "";
my $primary_request = "$primary_server/APIjsonserver?$rest";
$bw->log("GBFS primary_request: ",$primary_request,"");
my $ua = LWP::UserAgent->new;
$ua->agent("sharee GBFSout");
my $req = HTTP::Request->new(GET => "$primary_request");
$req->content_type('application/x-www-form-urlencoded');
$req->content($rest);
#Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
#print $res->content;
return $res->content;
#print $res->status_line, "\n";
}else {
return "";
#print $res->status_line, "\n";
}
}
1;

View file

@ -72,18 +72,14 @@ sub handler {
return Apache2::Const::OK;
}
sub kmlGenerator {
my $authcookie = shift || { authcookie => '' };
my $varenv = shift;
my $users_sharee = shift || { c_id => 0 };
$users_sharee->{c_id} = "0" if(!$users_sharee->{c_id});
my $q = new CGI;
my $dbt = new DBtank;
my $json = JSON->new->allow_nonref;
my $cf = new Config;
my $lang="de";
my $dbh = "";
my $project = "all";
$project = $dbt->{website}->{$varenv->{syshost}}->{project} if($dbt->{website}->{$varenv->{syshost}}->{project});
@ -93,9 +89,12 @@ sub kmlGenerator {
my $icon_red = "Open_Red.png";
my $icon_blue = "Open_Blue.png";
if($project eq "Bayern"){
$icon_green = "LastenradBay-custom-marker-gruen.png";
$icon_red = "LastenradBay-custom-marker-rot.png";
$icon_blue = "LastenradBay-custom-marker-bayblau.png";
$icon_green = "LastenradBay_Standortpfeil_gruen_03a.png";
$icon_red = "LastenradBay_Standortpfeil_rot_03a.png";
$icon_blue = "LastenradBay_Standortpfeil_blau_03a.png";
#$icon_green = "LastenradBay-custom-marker-gruen.png";
#$icon_red = "LastenradBay-custom-marker-rot.png";
#$icon_blue = "LastenradBay-custom-marker-bayblau.png";
}
my $timestamp = strftime "%Y%m%d%H%M%S", localtime;
@ -225,31 +224,9 @@ sub kmlGenerator {
print XML "</Document>\n";
print XML "</kml>\n";
close(XML);
chmod 0666, "$varenv->{basedir}/xml/$kmlfile";
#my $update_kml = {};
#if($users_sharee->{c_id} > 0){
# $update_kml = {
# table => "contentadr",
# atime => "now()",
# c_id => $users_sharee->{c_id},
# };
#}else{
# $update_kml = {
# table => "content",
# mtime => "now()",
# c_id => "3",
# };
#}
#my $dbh_primary = $dbt->dbconnect_extern("sharee_primary");
#$dbt->update_one($dbh_primary,$update_kml,"txt20='$kmlfile'");
#print "station_hash ALL:" . Dumper($station_hash);
#print "bike_hash ALL:" . Dumper($bike_hash);
#
return $kmlfile;
}#end kmlGenerator
#requestor