sharee.bike/copri4/main/src/Mod/APIjsonserver.pm

996 lines
45 KiB
Perl
Raw Normal View History

2021-12-30 12:05:56 +01:00
package Mod::APIjsonserver;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#Server for sharee json api
#
##In DB context $q->escapeHTML must always done by API
#
use warnings;
use strict;
use Exporter;
our @ISA = qw (Exporter);
use POSIX;
use CGI;
use Apache2::Const -compile => qw(OK );
use JSON;
use Scalar::Util qw(looks_like_number);
use Config::General;
use Lib::Config;
use Mod::DBtank;
use Mod::Basework;
use Mod::Shareework;
use Mod::APIfunc;
use Mod::APIjsonclient;
2022-05-11 08:05:35 +02:00
use Mod::APIsigclient;
2021-12-30 12:05:56 +01:00
use Data::Dumper;
sub handler {
my ($r) = @_;
my $q = new CGI;
2022-01-05 11:08:12 +01:00
$q->import_names('R');
2021-12-30 12:05:56 +01:00
my $json = JSON->new->allow_nonref;
my $cf = new Config;
my $dbt = new DBtank;
my $bw = new Basework;
my $tk = new Shareework;
my $apif = new APIfunc;
my $jsc = new APIjsonclient;
2022-05-11 08:05:35 +02:00
my $si = new APIsigclient;
2021-12-30 12:05:56 +01:00
2022-04-07 21:07:59 +02:00
my $dbh = "";
2021-12-30 12:05:56 +01:00
my %varenv = $cf->envonline();
my $oprefix = $dbt->{operator}->{$varenv{dbname}}->{oprefix};
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $lang="de";
my @keywords = $q->param;
my $debug=1;
my $user_agent = $q->user_agent();
2022-06-02 10:34:03 +02:00
#my %headers = map { $_ => $q->http($_) } $q->http();
#$bw->log("headers:",\%headers,"");
$bw->log("APIjsonserver request:\n--> user-agent '$user_agent' ",$q,"");
2021-12-30 12:05:56 +01:00
print $q->header(-type => "application/json", -charset => "utf-8", -'Access-Control-Allow-Origin' => "*");
2022-06-02 10:34:03 +02:00
#for my $header ( keys %headers ) {
# print "$header: $headers{$header}\n";
#}
2021-12-30 12:05:56 +01:00
my $respreq = $q->param('request') || "";
my $apiserver = $q->url(-base=>1) || "";
2022-01-05 11:08:12 +01:00
my $response = {
apiserver => "$apiserver",
response => "$respreq",
uri_primary => "$dbt->{primary}->{sharee_primary}->{primaryApp}",
2022-06-07 06:53:15 +02:00
copri_version => "4.1.12.5",
2022-01-05 11:08:12 +01:00
user_id => "",
authcookie => "",
new_authcoo => "0",
clearing_cache => "0",
agb_checked => "0",
user_group => [],
user_tour => [],
response_state => "OK, nothing todo",
2022-06-07 06:53:15 +02:00
agb_html => "site/agb_sharee_1.html",
2022-01-16 12:17:11 +01:00
privacy_html => "site/privacy_2.html",
2022-01-14 10:49:45 +01:00
impress_html => "site/impress_1.html",
2022-05-20 19:37:57 +02:00
tariff_info_html => "site/tariff_info.html",
2022-06-07 06:53:15 +02:00
bike_info_html => "site/bike_info_sharee_1.html",
2022-01-05 11:08:12 +01:00
lang => "DE",
last_used_operator => {
operator_name => "sharee.bike | TeilRad GmbH",
operator_color => "#009699",
operator_email => "hotline\@sharee.bike",
operator_phone => "+49 761 45370097",
operator_hours => "Bürozeiten: Montag, Mittwoch, Freitag 9-12 Uhr",
},
init_map => {
center => { latitude => "", longitude => "" },
radius => ""
}
};
my $aowner = 0;
my $coo = $q->param('authcookie') || $q->param('sessionid') || "";
if(!$coo && !$q->param('merchant_id')){
2021-12-30 12:05:56 +01:00
$response->{response_state} = "Failure 9900: no authcookie or merchant_id defined";
$response->{response_text} = "Authentifizierung fehlgeschlagen.";
2022-01-05 11:08:12 +01:00
$bw->log("Failure 9900: NO authcookie or merchant_id defined",$q,"");
2021-12-30 12:05:56 +01:00
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
return Apache2::Const::OK;
exit 0;
2022-01-15 11:17:07 +01:00
}else{
2022-05-19 16:00:41 +02:00
my $return_merchant = { project_id => "" };
($aowner, $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
#if($aowner && ($aowner == 195 || $aowner == 185 || $aowner == 176)){
if($return_merchant->{project_id} eq "Konstanz"){
2022-01-15 11:17:07 +01:00
$response->{agb_html} = "site/agb_konrad_1.html";
2022-01-18 07:04:04 +01:00
$response->{bike_info_html} = "site/bike_info_konrad_1.html";
2022-01-15 11:17:07 +01:00
}
2022-05-19 16:00:41 +02:00
if($return_merchant->{project_id} eq "Bayern"){
2022-05-20 19:37:57 +02:00
$response->{agb_html} = "site/agb_lastenradbayern.html";
$response->{bike_info_html} = "site/bike_info_bayern.html";
2022-05-24 10:46:05 +02:00
$response->{privacy_html} = "site/privacy_lastenradbayern.html";
2022-05-19 16:00:41 +02:00
}
2021-12-30 12:05:56 +01:00
}
#If param>40 || value > 400 then exit
2021-12-30 12:05:56 +01:00
foreach(@keywords){
if(length($_) > 40 || length($q->param($_)) > 400){
$response->{response_state} = "Failure 9000: amount of characters in $_ exceeds";
2022-01-05 11:08:12 +01:00
$bw->log("Failure 9000: amount of characters in $_ exceeds",$q,"");
2021-12-30 12:05:56 +01:00
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
return Apache2::Const::OK;
exit 0;
}
}
#RESTful bikeAPP ------------------------------------------------------------------
if($q->param('user_device_manufaturer') || $q->param('user_device_model') || $q->param('user_device_platform') || $q->param('user_device_version') || $q->param('user_device_id')){
my $user_device = $q->param('user_device_manufaturer') . ";" . $q->param('user_device_model') . ";" . $q->param('user_device_platform') . ";" . $q->param('user_device_version') . ";" . $q->param('user_device_id');
$q->param(-name=>'user_device',-value=>"$user_device");
$bw->log("user_device",$q->param('user_device'),"");
}
#just auth_verify
if($q->param('request') eq "auth_verify"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
}else{
2022-02-14 16:06:02 +01:00
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
}
#authout
elsif($q->param('request') eq "authout"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->authout($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response->{authcookie} = "$auth->{authcookie}";
$response->{response_state} = "OK, logout";
$response->{response_text} = "Auf Wiedersehen.";
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
2022-02-14 16:06:02 +01:00
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
}
#authorization
elsif($q->param('request') eq "authorization"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->authorization($q,"","",$aowner);
2021-12-30 12:05:56 +01:00
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
$response->{response_text} = "Herzlich willkommen im Fahrradmietsystem";
}else{
$response->{response_state} = "Failure: cannot generate authcookie";
$response->{response_text} = "Entschuldigung, die Anmeldung schlug fehl";
}
}
#booking request
elsif($q->param('request') eq "booking_request"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
2022-05-11 08:05:35 +02:00
my $rows = 0;
2022-01-05 11:08:12 +01:00
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
2022-04-07 21:07:59 +02:00
my $bike = $q->escapeHTML($q->param('bike')) || "";
if($bike){
my $bike_id = $bike;
$bike_id =~ s/S[1-9]X/SX/;
$bike_id = $1 if($bike_id =~ /(\d+)/);
2022-04-07 21:07:59 +02:00
#check count of occcupied/requested bikes
2022-06-10 20:49:26 +02:00
#"station_lock_state" disables a OR condition to view available as requested/occupied
my $record = $apif->user_bikes_occupied($q,$authraw,"station_lock_state");
2022-04-07 21:07:59 +02:00
my $count=0;
my $still_requested = 0;
foreach my $id (keys(%$record)){
2021-12-30 12:05:56 +01:00
$count++;
2022-04-07 21:07:59 +02:00
if($bike_id && $bike_id == $record->{$id}->{barcode}){
$still_requested = 1;
$response->{response_state} = "OK, bike " . $bike . " already requested or occupied";
$response->{response_text} = "Fahrrad Nr. " . $bike . " ist bereits reserviert";
}
}
if(!$still_requested){
2022-01-10 12:33:49 +01:00
#only if not debug
if(!$authraw->{int11} && $count >= 3){
2021-12-30 12:05:56 +01:00
$response->{response_state} = "Failure: booking_request declined. max count of 3 occupied bikes has been reached";
$response->{response_text} = "Die maximale Anzahl von 3 Reservierungen wurde erreicht";
}else{
my $gps = "";
my $latitude = "";
my $longitude = "";
#old
if($q->param('gps')){
my $gps_input = $q->param('gps');
$gps_input =~ s/\s//g if($gps_input);
$latitude = $q->escapeHTML($1) if($gps_input =~ /^(\d+\.\d+),\d+/);
$longitude = $q->escapeHTML($1) if($gps_input =~ /\d+,(\d+\.\d+)$/);
$gps = "$latitude,$longitude" if($latitude && $longitude);
}
#new
if($q->param('latitude') && $q->param('longitude')){
my $latitude_in = $q->param('latitude');
my $longitude_in = $q->param('longitude');
$latitude = $1 if($latitude_in =~ /(\d+\.\d+)/);
$longitude = $1 if($longitude_in =~ /(\d+\.\d+)/);
$gps = "$latitude,$longitude" if($latitude && $longitude);
}
2022-04-07 21:07:59 +02:00
my $response_book = {};
my $ct_bike = {};
my $ct_tariff = {};
($ct_bike,$ct_tariff) = $apif->fetch_bike_tariff(\%varenv,$authraw,$bike,$aowner);
2022-06-02 10:34:03 +02:00
#sig booking_request
my $sig_book = {
2022-06-07 06:53:15 +02:00
bikeId => "",
rentalId => "",
reservationId => ""
};
2022-04-07 21:07:59 +02:00
if($ct_bike->{barcode} && $ct_tariff->{barcode}){
2022-06-21 15:30:22 +02:00
#sig reservation. not mandatory, disabled because sig seems to get confused
if(1==2 && $ct_bike->{int11} == 3){
2022-06-02 10:34:03 +02:00
$sig_book = $si->sig_booking(\%varenv,"reserve",$authraw,$ct_bike,"");
2022-05-27 17:11:35 +02:00
$sig_book->{bikeId} = "$ct_bike->{txt22}";
2022-06-10 06:47:18 +02:00
#we only need rentalId because of reserve/end uses rentalId
#$sig_book->{reservationId} = "$R::reservationId" if($R::reservationId);#only for cmd tests
$sig_book->{rentalId} = "$R::rentalId" if($R::rentalId);#only for cmd tests
2022-04-07 21:07:59 +02:00
}
2022-05-19 16:00:41 +02:00
$response_book = $apif->booking_request($q,\%varenv,$authraw,$bike,$ct_bike,$ct_tariff,$aowner,$gps,$sig_book);
2022-04-07 21:07:59 +02:00
}elsif(!$ct_tariff->{barcode}){
$response->{response_state} = "Failure 2089: booking bike $bike fails, no user tariff available";
$response->{response_text} = "Reservierungsfehler Fahrrad Nr. $bike. Es konnte kein Mietrad Tarif gefunden werden.";
}elsif(!$ct_bike->{barcode}){
$response->{response_state} = "Failure 2001: booking bike $bike fails, bike not available";
$response->{response_text} = "Fahrrad Nr. $bike ist leider nicht verfügbar. Ist die Mietrad Flotte für sie freigeschaltet? Bitte überprüfen Sie Ihre Profildaten auf Vollständigkeit";
}
2021-12-30 12:05:56 +01:00
#just in time booking
if(ref($response_book) eq "HASH" && $response_book->{response_state} =~ /OK/ && $q->param('state') && $q->param('state') =~ /occupied/){
2022-05-11 08:05:35 +02:00
my $booking_values = {};
2022-04-07 21:07:59 +02:00
my $ctpos = {};
my $booking_pos = {
table => "contenttranspos",
fetch => "one",
barcode => "$bike_id",
2022-04-12 11:21:19 +02:00
int10 => "2",
2022-04-07 21:07:59 +02:00
ca_id => "$authraw->{c_id}",
};
my $dbh = "";
$ctpos = $dbt->fetch_tablerecord($dbh,$booking_pos);
#sig booking
2022-04-07 21:07:59 +02:00
if($ctpos->{int11} == 3){
#usecase with reservationId (in ctpos) is in sig not defined, thats because using ct_bike.txt22
2022-06-02 10:34:03 +02:00
#$sig_book = $si->sig_booking(\%varenv,"rental",$authraw,$ct_bike,$ctpos);
2022-06-21 15:30:22 +02:00
#we have to use $ct_bike->{txt22} because we doesn't using sig reserve in this case
system("$varenv{basedir}/src/scripts/sig_client.pl $varenv{syshost} 'rental' $authraw->{c_id} $ct_bike->{txt22} '' &");
2022-06-07 06:53:15 +02:00
$sig_book->{rentalId} = "$R::rentalId" if($R::rentalId);#only for cmd tests
2022-05-11 08:05:35 +02:00
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
$response = {%$response, %$booking_values};
}else{
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
$response = {%$response, %$booking_values};
2022-04-07 21:07:59 +02:00
}
2021-12-30 12:05:56 +01:00
}else{
2022-05-11 08:05:35 +02:00
$response = {%$response, %$response_book};
2021-12-30 12:05:56 +01:00
}
}
#return list of occupied/requested bikes
2022-06-10 20:49:26 +02:00
#"station_lock_state" disables a OR condition to view available as requested/occupied
$record = $apif->user_bikes_occupied($q,$authraw,"station_lock_state");
2021-12-30 12:05:56 +01:00
$response->{bikes_occupied} = $apif->rentals($record,$authraw,"1");#returns JSON rental values
2022-04-07 21:07:59 +02:00
}#end still_requested
2021-12-30 12:05:56 +01:00
}else{
$response->{response_state} = "Failure: no bike defined";
$response->{response_text} = "Abbruch, es wurde kein Fahrrad ausgewählt";
}
}else{
2022-02-14 16:06:02 +01:00
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
2022-06-08 20:18:11 +02:00
}#end booking request
2021-12-30 12:05:56 +01:00
#booking cancel/update
elsif($q->param('request') eq "booking_cancel" || $q->param('request') eq "booking_update"){
if($q->param('request') eq "booking_cancel"){
$q->param(-name=>'request',-value=>"booking_update");
$q->param(-name=>'state',-value=>"canceled");
}
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
my $rows = 0;
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
2022-04-07 21:07:59 +02:00
my $bike = $q->escapeHTML($q->param('bike')) || "";
if($bike){
my $bike_id = $bike;
$bike_id =~ s/S[1-9]X/SX/;
$bike_id = $1 if($bike_id =~ /(\d+)/);
2022-04-07 21:07:59 +02:00
my $ctpos = {};
my $booking_pos = {
table => "contenttranspos",
fetch => "one",
barcode => "$bike_id",
int10 => "IN::('2','3')",
ca_id => "$authraw->{c_id}",
};
$ctpos = $dbt->fetch_tablerecord($dbh,$booking_pos);
my $sig_book = {
2022-05-11 08:05:35 +02:00
bikeId => $ctpos->{txt22},
rentalId => $ctpos->{txt11},
reservationId => ""
};
2021-12-30 12:05:56 +01:00
if($q->param('request') eq "booking_update" && $q->param('state') && $q->param('state') =~ /canceled/){
my $booking_values = {};
2022-04-07 21:07:59 +02:00
2022-04-12 11:21:19 +02:00
if($ctpos->{int11} == 3){
2022-06-02 10:34:03 +02:00
$sig_book = $si->sig_booking(\%varenv,"reserve_end",$authraw,"",$ctpos);
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
}else{
2022-05-27 17:11:35 +02:00
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,"");
2022-04-12 11:21:19 +02:00
}
2021-12-30 12:05:56 +01:00
$response = {%$response, %$booking_values};
2022-04-05 13:19:00 +02:00
}elsif($q->param('request') eq "booking_update" && (($q->param('state') && $q->param('state') =~ /occupied|available/) || ($q->param('lock_state') && $q->param('lock_state') =~ /locking|locked|unlocking|unlocked/))){
2022-05-11 08:05:35 +02:00
my $booking_values = {};
2022-04-07 21:07:59 +02:00
2022-06-07 06:53:15 +02:00
#update on sig
2022-04-07 21:07:59 +02:00
if($ctpos->{int11} == 3){
2022-04-12 11:21:19 +02:00
if($q->param('state') eq "occupied"){
2022-06-02 10:34:03 +02:00
system("$varenv{basedir}/src/scripts/sig_client.pl $varenv{syshost} 'rental' $authraw->{c_id} '' $ctpos->{c_id} &");
2022-04-12 11:21:19 +02:00
}
2022-06-08 20:18:11 +02:00
#rental/end will be done by sig push-notification
#if($q->param('state') eq "available"){
2022-06-07 06:53:15 +02:00
#system("$varenv{basedir}/src/scripts/sig_client.pl $varenv{syshost} 'rental_end' $authraw->{c_id} '' $ctpos->{c_id} &");
2022-06-08 20:18:11 +02:00
#}
2022-04-07 21:07:59 +02:00
2022-06-07 06:53:15 +02:00
#TODO, check if booking_values in both cases: push-notify and/or app trigger works
$sig_book->{rentalId} = "$R::rentalId" if($R::rentalId);#only for cmd tests
2022-05-11 08:05:35 +02:00
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
$response = {%$response, %$booking_values};
2022-06-07 06:53:15 +02:00
#update on Ilockit
2022-05-11 08:05:35 +02:00
}else{
($rows, $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner,$sig_book);
$response = {%$response, %$booking_values};
}
2021-12-30 12:05:56 +01:00
#keep in mind, it works on operator dependency
2022-06-08 20:18:11 +02:00
#only for project=Bayern and developer enabled
#redundant code in APIfunc sub rentals for sig user_miniquery
if($booking_values->{state} eq "available" && (($dbt->{operator}->{$varenv{dbname}}->{project} eq "Bayern" && $authraw->{int23} < 4) || ($authraw->{c_id} == 1842 || $authraw->{c_id} == 5781 || $authraw->{c_id} == 11765 || $authraw->{c_id} == 1843 || $authraw->{c_id} == 38883))){
2021-12-30 12:05:56 +01:00
2022-06-07 06:53:15 +02:00
$bw->log("user_miniquery via $varenv{dbname} user ID $authraw->{c_id} exist count:",$authraw->{int23},"");
2022-06-08 20:18:11 +02:00
$response->{user_miniquery} = $dbt->evaluationsfragen($dbh);
2021-12-30 12:05:56 +01:00
2022-06-07 06:53:15 +02:00
#user_miniquest_count
my $user_miniquest_count = $authraw->{int23} || 0;
if($user_miniquest_count <= 4){
$user_miniquest_count++;
my $update_op = {
2021-12-30 12:05:56 +01:00
table => "contentadr",
int23 => $user_miniquest_count,
atime => "now()",
};
2022-06-07 06:53:15 +02:00
my $rows = $dbt->update_record($dbh,$update_op,$authraw);
}
}#end mini_quest
}#end occupied|available
2021-12-30 12:05:56 +01:00
my $record = $apif->user_bikes_occupied($q,$authraw);
$response->{bikes_occupied} = $apif->rentals($record,$authraw,"1");
}else{
$response->{response_state} = "Failure: no bike defined";
$response->{response_text} = "Abbruch, es wurde keine Fahrrad Nummer angegeben";
2021-12-30 12:05:56 +01:00
}
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
2022-02-14 16:06:02 +01:00
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
2022-06-08 20:18:11 +02:00
}#end booking cancel/update
2021-12-30 12:05:56 +01:00
#user_bikes_occupied
elsif($q->param('request') eq "user_bikes_occupied"){
if($varenv{syshost} eq "shareeapp-primary"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
2022-01-31 14:55:05 +01:00
($response->{bikes_occupied},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
2021-12-30 12:05:56 +01:00
}else{
2022-02-14 16:06:02 +01:00
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
}else{
my ($auth,$authraw) = $apif->auth_verify($q);
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
my $record = $apif->user_bikes_occupied($q,$authraw);
$response->{bikes_occupied} = $apif->rentals($record,$authraw,"1");
}else{
2022-02-14 16:06:02 +01:00
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Sitzung wurde unterbrochen";
2021-12-30 12:05:56 +01:00
}
}
}
#bikes_available
elsif($q->param('request') eq "bikes_available"){
if($varenv{syshost} eq "shareeapp-primary"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
2022-01-31 14:55:05 +01:00
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
2021-12-30 12:05:56 +01:00
#$bw->log("X bikes_available $varenv{syshost} $stamp: $response->{user_group}",$response,"");
}else{
2022-01-11 10:53:21 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
2022-02-11 20:29:10 +01:00
#on operator loop select, operator adr must be select to get user_group
my ($auth,$authraw) = $apif->auth_verify($q);
2021-12-30 12:05:56 +01:00
$response = { %$response, %$auth };
$bw->log("Y bikes_available by c_id $authraw->{c_id}, Tarif:",$authraw->{txt30},"");
2022-03-31 21:53:53 +02:00
if($varenv{syshost} eq "shareeapp-sx"){
2022-05-11 08:05:35 +02:00
($response->{bikes},my $return2copri->{bikes}) = $si->sig_available($q,\%varenv,$authraw);
$tk->sigbike_cupdate($return2copri->{bikes});
2022-03-31 21:53:53 +02:00
}else{
$response->{bikes} = $apif->bikes_available($q,\%varenv,$authraw);
}
2021-12-30 12:05:56 +01:00
}
if(ref($response->{bikes}) ne "HASH"){
$response->{response_state} = "Failure 5003: cannot find any user defined bike tariff";
$response->{response_text} = "Abbruch, es konnte kein gültiger Tarif gefunden werden";
}
}
#bikes_all with service_state calculater
#cronjob for maintanance update runs at ~ 7:00
elsif($q->param('request') eq "bikes_all"){
if($varenv{syshost} eq "shareeapp-primary"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
2022-01-31 14:55:05 +01:00
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
2021-12-30 12:05:56 +01:00
}else{
2022-01-09 18:31:20 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
$response->{response_text} = "Vorsicht, das ist die Liste aller Leihräder unabhängig von der Verfügbarkeit";
2022-01-31 14:55:05 +01:00
#on shareetool only stations on user_tour
2021-12-30 12:05:56 +01:00
my $stations_allraw = {};
(my $stations_not_used,$stations_allraw) = $apif->stations_all($q,\%varenv,"",$authraw) if($aowner && $aowner eq "187");#shareetool
2021-12-30 12:05:56 +01:00
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,$stations_allraw);
2021-12-30 12:05:56 +01:00
my $bike = "all";
my $interval = $q->param('interval') || "31";
my $service_state_debug = "\n";
my $pos_record = {};
my $response_work = {};
2022-01-02 14:57:19 +01:00
#shareetool
if($aowner && $aowner eq "187"){
2021-12-30 12:05:56 +01:00
(my $xresponse, $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$interval);
($response_work, my $node) = $apif->service_work($pos_record,$bikes_allraw,"",$node_template);
#Pseudocode -- Calculate service_state:
#if (state == "defekt") -> service_state=1-rot
#else if (1 harte Wartung fällig und ( mind. 1 weiche Wartung fällig -oder- Aufgabe Text eingetragen )) -> service_state=2-Blau
#else if ( mind. 1 harte Wartung fällig ) -> service_state=3-grün
#else service_state=4-grau
}
my $i=0;
foreach my $bid (sort { $bikes_allraw->{$a}->{mtime} cmp $bikes_allraw->{$b}->{mtime} } keys (%$bikes_allraw)){
my $biselect = $bikes_allraw->{$bid}->{barcode};
$i++;
#service_state Calculator
$bikes_all->{$oprefix . $bid}->{todo_info} = "0";
$bikes_all->{$oprefix . $bid}->{service_state} = "0";
$bikes_allraw->{$bid}->{service_state_blue} = 0;
$bikes_allraw->{$bid}->{service_state_green} = 0;
$bikes_allraw->{$bid}->{service_state_exist} = 0;
#print "bid:$bid|$bikes_allraw->{$bid}->{mtime}\n";
2022-01-02 14:57:19 +01:00
#shareetool disabled, needs also to much cpu-time
#if(ref($response_work->{$biselect}) ne "HASH" || $response_work->{$biselect}->{int01}->{c_id} !~ /\d/ && $aowner && $aowner eq "187"){
2021-12-30 12:05:56 +01:00
# my $pos_record_bi = $apif->service_work_select($biselect,"","");
# (my $response_work_bi, my $node) = $apif->service_work($pos_record_bi,$bikes_allraw);
# $response_work->{$biselect} = $response_work_bi->{$biselect};
#print "Rad:$biselect " . Dumper($response_work->{$biselect});
#}
#2019-02-14, fixed
if(ref($response_work->{$biselect}) eq "HASH" && $aowner && $aowner eq "187"){#shareetool
2021-12-30 12:05:56 +01:00
#print "$biselect: $response_work->{$biselect}->{mtime}\n";
$bikes_allraw->{$bid}->{service_state_exist} = 1;
foreach my $id (keys(%{$response_work->{$biselect}})){
#$bw->log("response_work:$biselect","|$id|$response_work->{$biselect}->{$id}->{mtime}|$response_work->{$biselect}->{$id}->{service_type}|$response_work->{$biselect}->{$id}->{time_over}|$response_work->{$biselect}->{$id}->{work_val}","");
2021-12-30 12:05:56 +01:00
#time_over && (service_type || Aufgaben)
if(($response_work->{$biselect}->{$id}->{time_over} == 1 && $response_work->{$biselect}->{$id}->{service_type} >= 1) || ($id eq "txt01" && $response_work->{$biselect}->{$id}->{work_val})){
#print $id . ":" . $response_work->{$biselect}->{$id}->{work_val} . "\n";
if($id eq "txt01" && $response_work->{$biselect}->{$id}->{work_val} && $response_work->{$biselect}->{$id}->{work_val} ne "NaN" && $response_work->{$biselect}->{$id}->{work_val} !~ /::erledigt::/){
$bikes_all->{$oprefix . $bid}->{todo_info} = "1";
}
if(($response_work->{$biselect}->{$id}->{time_over} == 1 && $response_work->{$biselect}->{$id}->{service_type} == 1) || ($id eq "txt01" && $response_work->{$biselect}->{$id}->{work_val} && $response_work->{$biselect}->{$id}->{work_val} ne "NaN" && $response_work->{$biselect}->{$id}->{work_val} !~ /::erledigt::/)){
$bikes_allraw->{$bid}->{service_state_blue}++;
}
if($response_work->{$biselect}->{$id}->{time_over} == 1 && $response_work->{$biselect}->{$id}->{service_type} == 2){
$bikes_allraw->{$bid}->{service_state_green}++;
}
}
}#end response_work service_state calc
if($bikes_allraw->{$bid}->{service_state_exist} == 1 && $bikes_all->{$oprefix . $bid}->{state} eq "defect"){
$bikes_all->{$oprefix . $bid}->{service_state} = "1";
$service_state_debug .= "$bid: service_state 1\n";
#$bw->log("defect service_state bike: $bid:",$bikes_all->{$oprefix . $bid}->{service_state},"");
}
elsif($bikes_allraw->{$bid}->{service_state_blue} >= 1 && $bikes_allraw->{$bid}->{service_state_green} >= 1){
#print "$bikes_allraw->{$bid}->{service_state_blue}|$bikes_allraw->{$bid}->{service_state_green}" if($bid eq "5");
#$bikes_all->{$oprefix . $bid}->{service_state} = "$bikes_allraw->{$bid}->{service_state_blue}";
$bikes_all->{$oprefix . $bid}->{service_state} = "2";
$bikes_all->{$oprefix . $bid}->{state} = "maintanance";
#if($bikes_allraw->{$bid}->{txt10} && $bikes_allraw->{$bid}->{txt10} !~ /defect|maintanance|requested|occupied/)
if($bikes_allraw->{$bid}->{int10} && ($bikes_allraw->{$bid}->{int10} == 1 || $bikes_allraw->{$bid}->{int10} == 6)){
$service_state_debug .= "$bid: service_state 2\n";
#$bw->log("maintanance service_state bike: $bid:",$bikes_all->{$oprefix . $bid}->{service_state},"");
#4 = "maintanance"
$apif->bikestate_update($authraw,$bikes_allraw->{$bid}->{c_id},"4");
}
}
elsif($bikes_allraw->{$bid}->{service_state_green} >= 1){
#$bikes_all->{$oprefix . $bid}->{service_state} = "$bikes_allraw->{$bid}->{service_state_green}";
$bikes_all->{$oprefix . $bid}->{service_state} = "3";
$bikes_all->{$oprefix . $bid}->{state} = "maintanance";
#if($bikes_allraw->{$bid}->{txt10} && $bikes_allraw->{$bid}->{txt10} !~ /defect|maintanance|requested|occupied/){
if($bikes_allraw->{$bid}->{int10} && ($bikes_allraw->{$bid}->{int10} == 1 || $bikes_allraw->{$bid}->{int10} == 6)){
$service_state_debug .= "$bid: service_state 3\n";
#$bw->log("maintanance service_state bike: $bid:",$bikes_all->{$oprefix . $bid}->{service_state},"");
#4 = "maintanance"
$apif->bikestate_update($authraw,$bikes_allraw->{$bid}->{c_id},"4");
}
}elsif($bikes_allraw->{$bid}->{service_state_exist} == 1){
#if($bikes_allraw->{$bid}->{txt10} && $bikes_allraw->{$bid}->{txt10} =~ /maintanance/)
if($bikes_allraw->{$bid}->{int10} && $bikes_allraw->{$bid}->{int10} == 4){
$service_state_debug .= "$bid: 0\n";
#$bw->log("maintanance TO available service_state bike: $bid:",$bikes_all->{$oprefix . $bid}->{service_state},"");
#1 = "available"
$apif->bikestate_update($authraw,$bikes_allraw->{$bid}->{c_id},"1");
}
}
#workaround to get todo_info on defect
#else NOT if(ref($response_work->{$biselect}) eq "HASH" && $response_work->{$biselect}->{int01}->{c_id}) ---> because 31 day select
}
if($bikes_all->{$oprefix . $bid}->{state} eq "defect"){
$bikes_all->{$oprefix . $bid}->{service_state} = "1";
my $search = { key => "txt01",
val => "%",
};
my $pos_record_bi = $apif->service_work_search($biselect,"","",$search);
if($pos_record_bi->{txt01} && $pos_record_bi->{txt01} ne "NaN" && $pos_record_bi->{txt01} !~ /::erledigt::/){
#$bikes_all->{$oprefix . $bid}->{todo_info} = "$pos_record_bi->{txt01}";
$bikes_all->{$oprefix . $bid}->{todo_info} = "1";
}
$service_state_debug .= "$bid: service_state 1\n";
}
}
$bw->log("service_state_debug",$service_state_debug,"");
$response->{bikes} = $bikes_all;
}
}
#stations_all
elsif($q->param('request') eq "stations_all"){
if($varenv{syshost} eq "shareeapp-primary"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
2022-01-31 14:55:05 +01:00
($response->{stations},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
2021-12-30 12:05:56 +01:00
}else{
2022-01-09 18:31:20 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
($response->{stations},my $stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
2021-12-30 12:05:56 +01:00
}
}
#stations_available
elsif($q->param('request') eq "stations_available"){
if($varenv{syshost} eq "shareeapp-primary"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
#Mein konrad App
2022-02-14 16:06:02 +01:00
#if($dbt->{merchant_ids}->{$varenv{merchant_id}}->{id} eq "176")
#if($aowner && $aowner eq "176")
2022-01-19 19:11:41 +01:00
#$response->{merchant_message} = "Herzlich Willkommen bei der neuen konrad App! Die App ist zwar schon installierbar, bis zur vollständigen Umstellung des Systems sind aber noch keine Räder ausleihbar. Das ist erst voraussichtlich Ende Januar der Fall und wird den Nutzern noch mitgeteilt. Danke für Ihr Verständnis! Ihr konrad-Team";
#}
2021-12-30 12:05:56 +01:00
$response = { %$response, %$auth };
2022-01-31 14:55:05 +01:00
($response->{stations},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
2021-12-30 12:05:56 +01:00
}else{
2022-01-11 10:53:21 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
2022-03-31 21:53:53 +02:00
if($varenv{syshost} eq "shareeapp-sx"){
2022-05-11 08:05:35 +02:00
($response->{stations},my $return2copri->{stations}) = $si->sig_available($q,\%varenv,$authraw);
$tk->sigstation_cupdate($return2copri->{stations});
2022-03-31 21:53:53 +02:00
}else{
2022-05-17 06:13:53 +02:00
($response->{stations}, my $response_raw) = $apif->stations_available($q,\%varenv,$authraw,"");
2022-03-31 21:53:53 +02:00
}
2021-12-30 12:05:56 +01:00
}
}
#user_feedback / user_minianswer of user_miniquery
elsif($q->param('request') eq "user_feedback" || $q->param('request') eq "user_minianswer"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
$response->{uri_operator} = "$varenv{wwwhost}";
if(ref($auth) eq "HASH" && $auth->{authcookie}){
my $customer = $auth->{c_id};
#print Dumper($auth);
(my $xresponse->{$customer}, my $responseraw, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"","1");
$bw->log("c_id: $crecord->{c_id} |user_feedback OR user_minianswer node_template",$node_template,"");
my $back_id = "";
my $rows=0;
#if(!$back_id){#disabled because of every feedback have to be saved
if(1==1){
#INSERT just dadaset
$back_id = $apif->service_insert($q,$authraw,$node_template,$crecord,$aowner);
2021-12-30 12:05:56 +01:00
$rows = $apif->service_update($q,$authraw,$node_template,$back_id);
if($rows && $rows > 0){
$response->{response_state} = "OK, feedback insert and update";
$response->{response_text} = "Danke für die Nachricht.";
}else{
$response->{response_state} = "Failure 3606, feedback_update";
$response->{response_text} = "Die Nachricht konnte leider nicht gespeichert werden.";
}
}
#($xresponse->{$customer}, $responseraw, $node_template, $crecord) = $apif->service_select($q,$authraw,$back_id,"") if($back_id);
#my $response_work = {};
#$response_work->{feedback} = $apif->feedback_response($responseraw,$node_template);
#$response = { %$response, %$response_work };
}
}
#service_done
#insert and/or update
elsif($q->param('request') eq "service_done"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
my $station_id = $1 if($q->param('station') =~ /(\d+)/);
#my $bike_id = $1 if($q->param('bike') =~ /(\d+)/);
my $bike = $q->escapeHTML($q->param('bike')) || "";
my $bike_id = $bike;
$bike_id =~ s/S[1-9]X/SX/;
$bike_id = $1 if($bike_id =~ /(\d+)/);
2021-12-30 12:05:56 +01:00
$response->{uri_operator} = "$varenv{wwwhost}";
if(ref($auth) eq "HASH" && $auth->{authcookie}){
if(looks_like_number($bike_id) || looks_like_number($station_id)){
my $article = looks_like_number($bike_id) || looks_like_number($station_id);
#select services with max work_duration of 1 day and service_worker alias contentadr.c_id = contenttranspo.owner match
(my $xresponse->{$article}, my $responseraw, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"","1");
$bw->log("service_done OOO: node",$node_template,"");
if(ref($xresponse->{$article}) eq "HASH"){
$bw->log("service_done xresponse",$xresponse->{$article},"");
my $service_id = "";
#select last service_id with work_duration < 1 day
foreach my $id (sort { $xresponse->{$article}->{$a}->{mtime} cmp $xresponse->{$article}->{$b}->{mtime} } keys (%{$xresponse->{$article}})){
$service_id = $id if($id > 1);
}
my $rows=0;
$service_id = $1 if($q->param('service_id') =~ /(\d+)/);
if(!$service_id){
#INSERT just dadaset (without work values)
($response->{service_id}) = $apif->service_insert($q,$authraw,$node_template,$crecord);
$bw->log("service insert ",$response,"");
#once again to get node_record template
($xresponse->{$article}, $responseraw, $node_template, $crecord) = $apif->service_select($q,$authraw,"","1");
#UPDATE
$service_id = $response->{service_id};
$rows = $apif->service_update($q,$authraw,$node_template,$response->{service_id});
$response->{response_state} = "OK" if($rows > 0);
$response->{response_text} = "OK, service_insert and update" if($rows > 0);
}else{
#UPDATE
$rows = $apif->service_update($q,$authraw,$node_template,$service_id);
$response->{response_state} = "OK" if($rows > 0);
$response->{response_text} = "OK, service_update" if($rows > 0);
}
#UPDATE bike content state
if($q->param('work_id') eq "state" && looks_like_number($bike_id) && $q->param('work_val') =~ /available|maintanance|defect/){
#once again to get node_record template
($xresponse->{$article}, $responseraw, $node_template, $crecord) = $apif->service_select($q,$authraw,"","1");
while (my ($key, $value) = each %{ $dbt->{copri_conf}->{bike_state} }) {
if($q->param('work_val') eq $value){
$rows = $apif->bikestate_update($authraw,$responseraw->{$service_id}->{cc_id},$key);
$response->{response_state} = "OK";
$response->{response_text} = "OK, bikestate_update to state=$value";
}
}
}
if(($q->param('work_id') eq "int04" || $q->param('work_id') eq "station") && looks_like_number($bike_id) && ($q->param('work_val') || looks_like_number($q->param('work_val')))){
$q->param(-name=>'work_id',-value=>"int04") if($q->param('work_id') eq "station");#station db-field is int04
my $to_station_id = $1 if($q->param('work_val') =~ /(\d+)/);
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
2021-12-30 12:05:56 +01:00
#add-on to log redistribute#TODO dedicated db-fieled
my $action = "txt10=" . $oprefix . $bikes_allraw->{$bike_id}->{int04} . " - " . $q->param('work_val');
$apif->service_update($q,$authraw,$node_template,$service_id,$action);
2022-01-09 18:31:20 +01:00
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
2021-12-30 12:05:56 +01:00
if(looks_like_number($stations_allraw->{$to_station_id}->{int04})){
($xresponse->{$article}, $responseraw, $node_template, $crecord) = $apif->service_select($q,$authraw,"","1");
my $update_hash = { int04 => "$to_station_id" };
$rows = $apif->bikestate_update($authraw,$responseraw->{$service_id}->{cc_id},"",$update_hash);
$response->{response_state} = "OK" if($rows > 0);
$response->{response_text} = "OK, bikestate_update to_station_id $to_station_id" if($rows > 0);
}elsif($to_station_id == 0){#werkstatt
($xresponse->{$article}, $responseraw, $node_template, $crecord) = $apif->service_select($q,$authraw,"","1");
my $update_hash = { int04 => "$to_station_id" };
#5 = "defect"
$rows = $apif->bikestate_update($authraw,$responseraw->{$service_id}->{cc_id},"5",$update_hash);
$response->{response_state} = "OK" if($rows > 0);
$response->{response_text} = "OK, bikestate_update to Werkstatt $to_station_id" if($rows > 0);
}else{
$bw->log("service_update fails to_station_id: $to_station_id",$stations_allraw->{$to_station_id}->{int04},"");
$response->{response_state} = "Failure 3003: service_update fails";
}
}
if($rows != 1){
$response->{response_state} = "Failure 3004: service_update fails";
}
$response->{service_id_done} = $service_id;
}else{
$response->{response_state} = "Failure 3009: service_update fails because of can not find bike or station";
$response->{response_text} = "Fehler, angefragte Artikel konnte nicht gefunden werden!";
}
my $response_work = {};
my $node = {};
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
2021-12-30 12:05:56 +01:00
if(looks_like_number($bike_id)){
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"","100");#check interval
($response_work, $node) = $apif->service_work($pos_record,$bikes_allraw,"",$node_template);
}
elsif(looks_like_number($station_id)){
2022-01-09 18:31:20 +01:00
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
2021-12-30 12:05:56 +01:00
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"","100");#check interval
($response_work, $node) = $apif->service_work($pos_record,$stations_allraw,"",$node_template);
}
#inject oprefix
my $op_response_work = {};
foreach my $key (keys %$response_work){
$op_response_work->{$oprefix . $key} = $response_work->{$key};
}
$response = { %$response, %$op_response_work, %$auth };
my $node_template_id = 0;
$node_template_id = $node_template->{template_id} if(ref($node_template) eq "HASH" && $node_template->{template_id});
$response->{service_template} = "$node_template_id";
}else{
$response->{response_state} = "Failure 3002: no bike OR station ID defined";
}
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
}
}#end service_done
#service_work
#service_work. select last service by bike-id
elsif($q->param('request') eq "service_work"){
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
my ($auth,$authraw) = $apif->auth_verify($q);
my $station_id = "";
$station_id = $1 if($q->param('station') =~ /(\d+)/);
#$bike_id = $1 if($q->param('bike') =~ /(\d+)/);
my $bike = $q->escapeHTML($q->param('bike')) || "";
my $bike_id = $bike;
$bike_id =~ s/S[1-9]X/SX/;
$bike_id = $1 if($bike_id =~ /(\d+)/);
2021-12-30 12:05:56 +01:00
$response->{uri_operator} = "$varenv{wwwhost}";
my $node = {};
2022-01-26 15:52:25 +01:00
my $response_work = {};
2021-12-30 12:05:56 +01:00
my $history = 0;
$history = $q->param('history') if(looks_like_number($q->param('history')));
if(ref($auth) eq "HASH" && $auth->{authcookie}){
if(looks_like_number($bike_id)){
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
2021-12-30 12:05:56 +01:00
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
#$bw->log("service_work bike_id $bike_id pos_record",$pos_record,"");
2022-01-26 15:52:25 +01:00
($response_work, $node) = $apif->service_work($pos_record,$bikes_allraw,$history,$node_template);
2021-12-30 12:05:56 +01:00
$bw->log("service_work bike_id $bike_id response_work",$response_work,"");
if(ref($response_work) ne "HASH"){#if fails
$response->{response_state} = "Failure 4010: no service found";
}
my $op_response_work = {};
foreach my $key (keys %$response_work){
$op_response_work->{$oprefix . $key} = $response_work->{$key};
}
$response = { %$response, %$op_response_work, %$auth };
}elsif($q->param('bike') && $q->param('bike') eq "all"){
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
2021-12-30 12:05:56 +01:00
my $bike = $q->param('bike');
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
2022-01-26 15:52:25 +01:00
($response_work, $node) = $apif->service_work($pos_record,$bikes_allraw,$history,$node_template);
2021-12-30 12:05:56 +01:00
if(ref($response_work) ne "HASH"){#if fails
$response->{response_state} = "Failure 4011: no service found";
}
my $op_response_work = {};
foreach my $key (keys %$response_work){
$op_response_work->{$oprefix . $key} = $response_work->{$key};
}
$response = { %$response, %$op_response_work, %$auth };
}elsif(looks_like_number($station_id)){
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
2021-12-30 12:05:56 +01:00
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
2022-01-26 15:52:25 +01:00
($response_work, $node) = $apif->service_work($pos_record,$stations_allraw,$history,$node_template);
2021-12-30 12:05:56 +01:00
if(ref($response_work) ne "HASH"){#if fails
$response->{response_state} = "Failure 4013: no service found";
}
my $op_response_work = {};
foreach my $key (keys %$response_work){
$op_response_work->{$oprefix . $key} = $response_work->{$key};
}
$response = { %$response, %$op_response_work, %$auth };
}elsif($q->param('station') && $q->param('station') eq "all"){
2022-01-09 18:31:20 +01:00
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
2021-12-30 12:05:56 +01:00
my $station = $q->param('station');
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
2022-01-26 15:52:25 +01:00
($response_work, $node) = $apif->service_work($pos_record,$stations_allraw,$history,$node_template);
2021-12-30 12:05:56 +01:00
if(ref($response_work) ne "HASH"){#if fails
$response->{response_state} = "Failure 4014: no service found";
}
my $op_response_work = {};
foreach my $key (keys %$response_work){
$op_response_work->{$oprefix . $key} = $response_work->{$key};
}
2022-01-26 15:52:25 +01:00
$response = { %$response, %$op_response_work, %$auth };
2021-12-30 12:05:56 +01:00
}else{
$response->{response_state} = "Failure 3002: no bike OR station ID defined";
}
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
}
$response->{service_template} = "$node->{template_id}";
}#end service_work
#last if request not defined
else{
2022-01-05 11:08:12 +01:00
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
2021-12-30 12:05:56 +01:00
$response->{'response_state'} = "Failure: request not defined";
}
#end RESTful ------------------------------------------------------------
#FINAL JSON response OUTPUT ----------------------------------------------------------
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
$bw->log("APIjsonserver response by $user_agent mapped aowner:$aowner",$jrout,"");
2021-12-30 12:05:56 +01:00
#end JSON ----------------------------------------------------------------------------
return Apache2::Const::OK;
}#end handler
1;