sharee.bike/copri4/main/src/Mod/APIjsonserver.pm
2022-01-31 14:55:05 +01:00

924 lines
42 KiB
Perl
Executable file

package Mod::APIjsonserver;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#Server for sharee json api
#
#use lib qw(/var/www/copri4/tinkdms/src);
#
##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;
use Data::Dumper;
use Sys::Hostname;
my $hostname = hostname;
sub handler {
my ($r) = @_;
my $q = new CGI;
my $netloc = $q->url(-base=>1);
$q->import_names('R');
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;
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();
$bw->log("APIjsonserver request:\n--> user-agent '$user_agent'",$q,"");
print $q->header(-type => "application/json", -charset => "utf-8", -'Access-Control-Allow-Origin' => "*");
my $respreq = $q->param('request') || "";
my $apiserver = $q->url(-base=>1) || "";
my $response = {
apiserver => "$apiserver",
response => "$respreq",
uri_primary => "$dbt->{primary}->{sharee_primary}->{primaryApp}",
copri_version => "4.1.10.2",
user_id => "",
authcookie => "",
new_authcoo => "0",
clearing_cache => "0",
agb_checked => "0",
user_group => [],
user_tour => [],
response_state => "OK, nothing todo",
privacy_html => "site/privacy_2.html",
agb_html => "site/agb.html",
impress_html => "site/impress_1.html",
tariff_info_html => "site/tariff_info_1.html",
bike_info_html => "site/bike_info.html",
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')){
$response->{response_state} = "Failure 9900: no authcookie or merchant_id defined";
$response->{response_text} = "Authentifizierung fehlgeschlagen.";
$bw->log("Failure 9900: NO authcookie or merchant_id defined",$q,"");
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
return Apache2::Const::OK;
exit 0;
}else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
if($aowner && ($aowner == 195 || $aowner == 185 || $aowner == 176)){
$response->{agb_html} = "site/agb_konrad_1.html";
$response->{bike_info_html} = "site/bike_info_konrad_1.html";
}
}
#If param>40 || value > 400 then exit
foreach(@keywords){
if(length($_) > 40 || length($q->param($_)) > 400){
$response->{response_state} = "Failure 9000: amount of characters in $_ exceeds";
$bw->log("Failure 9000: amount of characters in $_ exceeds",$q,"");
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"){
($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->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
}else{
$response->{response_state} = "Failure: cannote match authcookie";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
#authout
elsif($q->param('request') eq "authout"){
($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->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";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
#authorization
elsif($q->param('request') eq "authorization"){
($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);
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"){
($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->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
if($q->param('bike')){
#check count of occcupied/requested bikes
my $record = $apif->user_bikes_occupied($q,$authraw);
my $bikes_occupied = $apif->rentals($record,$authraw,"1");
my $count=0;
foreach my $id (keys(%$bikes_occupied)){
$count++;
}
#only if not debug
if(!$authraw->{int11} && $count >= 3){
$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);
}
my $response_book = $tk->net_booking($authraw,$q->param('bike'),$aowner,$gps);
#just in time booking
if(ref($response_book) eq "HASH" && $response_book->{response_state} =~ /OK/ && $q->param('state') && $q->param('state') =~ /occupied/){
(my $rows, my $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner);
$response = {%$response, %$booking_values};
}else{
$response = {%$response, %$response_book};
}
}
$record = $apif->user_bikes_occupied($q,$authraw);
$bikes_occupied = $apif->rentals($record,$authraw,"1");
foreach my $id (keys(%$bikes_occupied)){
if($bikes_occupied->{$id}->{bike} eq $q->param('bike')){
if($bikes_occupied->{$id}->{int10} == 2){
$response->{response_state} = "OK, bike " . $q->param('bike') . " requested";
$response->{response_text} = "Fahrrad Nr. " . $q->param('bike') . " ist reserviert";
}elsif($bikes_occupied->{$id}->{int10} == 3){
$response->{response_state} = "OK, bike " . $q->param('bike') . " requested and occupied";
$response->{response_text} = "Fahrrad Nr. " . $q->param('bike') . " ist gemietet";
}
}
}
#return list of occupied/requested bikes
$record = $apif->user_bikes_occupied($q,$authraw);
$response->{bikes_occupied} = $apif->rentals($record,$authraw,"1");#returns JSON rental values
}else{
$response->{response_state} = "Failure: no bike defined";
$response->{response_text} = "Abbruch, es wurde kein Fahrrad ausgewählt";
$response->{timeCode} = 0;#if fails
}
}else{
$response->{response_state} = "Failure 1002: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
#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");
}
($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->auth_verify($q);
my $rows = 0;
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
if($q->param('bike')){
if($q->param('request') eq "booking_update" && $q->param('state') && $q->param('state') =~ /canceled/){
($rows, my $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner);
$response = {%$response, %$booking_values};
}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|unlocked/))){
($rows, my $booking_values) = $apif->booking_update($q,\%varenv,$authraw,$aowner);
$response = {%$response, %$booking_values};
#keep in mind, it works on operator dependency
$bw->log("user_miniquery via $varenv{dbname} exist count:",$authraw->{int23},"");
#Nur für project=Bayern und für Entwickler aktiviert
if($booking_values->{state} eq "available" && (($dbt->{operator}->{$varenv{dbname}}->{project} eq "Bayern" && $authraw->{int23} >= 1 && $authraw->{int23} < 4) || ($authraw->{c_id} == 1842 || $authraw->{c_id} == 5781 || $authraw->{c_id} == 11765 || $authraw->{c_id} == 1843))){
#TODO $ user_miniquery have to be in db table on primary
$bw->log("user_miniquery communicated to user ID",$authraw->{c_id},"");
my $user_miniquery = {
title => "Bitte unterstützen Sie unsere Begleitforschung",
subtitle => "Ihre drei Antworten werden anonym gespeichert.",
footer => "Herzlichen Dank und viel Spaß bei der nächsten Fahrt!",
questions => {
q1 => {
type => "check_one",
quest_text => "1. Was war der Hauptzweck dieser Ausleihe?",
query => {
opt1 => "a. Einkauf",
opt2 => "b. Kinderbeförderung",
opt3 => "c. Lastentransport",
opt4 => "d. Freizeit",
opt5 => "e. Ausprobieren",
opt6 => "f. Sonstiges"
}
},
q2 => {
type => "check_one",
quest_text => "2. Welches Verkehrsmittel hätten Sie ansonsten benutzt?",
query => {
opt1 => "a. Auto",
opt2 => "b. Motorrad oder Motorroller",
opt3 => "c. Bus oder Bahn",
opt4 => "d. Eigenes Fahrrad",
opt5 => "e. Zu Fuß",
opt6 => "f. Keines (ich hätte die Fahrt sonst nicht gemacht)",
opt7 => "g. Sonstige"
}
},
q3 => {
type => "text",
quest_text => "3. Haben Sie Anmerkungen oder Anregungen?",
query => {
opt1 => ""
}
}
}
};
$response->{user_miniquery} = $user_miniquery;
}#end mini_quest
#user_miniquest_count
my $user_miniquest_count = $authraw->{int23} || 0;
if($q->param('request') && $q->param('request') eq "booking_update" && $q->param('state') eq "available" && $user_miniquest_count <= 4){
$user_miniquest_count++;
my $update_op = {
table => "contentadr",
int23 => $user_miniquest_count,
atime => "now()",
owner => "198",
};
my $dbh = "";
my $rows = $dbt->update_record($dbh,$update_op,$authraw);
}
}
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";
$response->{timeCode} = 0;#if fails
}
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
#user_rental_history
elsif($q->param('request') eq "user_rentals_history"){
($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->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
my ($record,$operator_hash) = $apif->user_rentals_history($q,$authraw);
$response->{rentals} = $apif->rentals($record,$authraw,"0");
}else{
$response->{response_state} = "Failure 1001: authcookie not defined";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
#user_bikes_occupied
elsif($q->param('request') eq "user_bikes_occupied"){
if($varenv{syshost} eq "shareeapp-primary"){
($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->auth_verify($q);
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
($response->{bikes_occupied},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
}else{
$response->{response_state} = "Failure 1001: authcookie on primary not defined";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}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{
$response->{response_state} = "Failure 1001: authcookie on operator not defined";
$response->{response_text} = "Entschuldigung, die Session wurde unterbrochen";
}
}
}
#bikes_available
elsif($q->param('request') eq "bikes_available"){
#use Time::HiRes qw/gettimeofday/;
if($varenv{syshost} eq "shareeapp-primary"){
($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->auth_verify($q);
$response = { %$response, %$auth };
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
#my $stamp = gettimeofday;
#$bw->log("X bikes_available $varenv{syshost} $stamp: $response->{user_group}",$response,"");
}else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
my ($auth,$authraw) = $apif->auth_verify($q);#on operator loop select, operator adr must be select to get user_group
$response = { %$response, %$auth };
$bw->log("Y bikes_available by c_id $authraw->{c_id}, Tarif:",$authraw->{txt30},"");
$response->{bikes} = $apif->bikes_available($q,\%varenv,$authraw);
}
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"){
($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->auth_verify($q);
$response = { %$response, %$auth };
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
}else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
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";
#on shareetool only stations on user_tour
my $stations_allraw = {};
(my $stations_not_used,$stations_allraw) = $apif->stations_all($q,\%varenv,"",$authraw) if($aowner && $aowner eq "187");#shareetool
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,$stations_allraw);
my $bike = "all";
my $interval = $q->param('interval') || "31";
my $service_state_debug = "\n";
my $pos_record = {};
my $response_work = {};
#shareetool
if($aowner && $aowner eq "187"){
(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";
#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"){
# 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
#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}","");
#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"){
($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->auth_verify($q);
$response = { %$response, %$auth };
($response->{stations},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
}else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
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);
}
}
#stations_available
elsif($q->param('request') eq "stations_available"){
if($varenv{syshost} eq "shareeapp-primary"){
($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->auth_verify($q);
#Mein konrad App
#if($dbt->{merchant_ids}->{$varenv{merchant_id}}->{id} eq "176"){
#if($aowner && $aowner eq "176"){
#$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";
#}
$response = { %$response, %$auth };
($response->{stations},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$authraw,$return_merchant);
}else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
($response->{stations}, my $response_raw) = $apif->stations_available($q,\%varenv,$authraw);
}
}
#user_feedback / user_minianswer of user_miniquery
elsif($q->param('request') eq "user_feedback" || $q->param('request') eq "user_minianswer"){
($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->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);
$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"){
($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->auth_verify($q);
my $station_id = $1 if($q->param('station') =~ /(\d+)/);
my $bike_id = $1 if($q->param('bike') =~ /(\d+)/);
$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+)/);
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
#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);
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
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 = {};
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
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)){
my ($stations_all,$stations_allraw) = $apif->stations_all($q,\%varenv,$bikes_on_station,$authraw);
(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"){
($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->auth_verify($q);
my $station_id = "";
my $bike_id = "";
$station_id = $1 if($q->param('station') =~ /(\d+)/);
$bike_id = $1 if($q->param('bike') =~ /(\d+)/);
$response->{uri_operator} = "$varenv{wwwhost}";
my $node = {};
my $response_work = {};
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)){
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
(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,"");
($response_work, $node) = $apif->service_work($pos_record,$bikes_allraw,$history,$node_template);
$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"){
my ($bikes_all,$bikes_allraw,$bikes_on_station) = $apif->bikes_all($q,\%varenv,$authraw,"");
my $bike = $q->param('bike');
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
($response_work, $node) = $apif->service_work($pos_record,$bikes_allraw,$history,$node_template);
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)){
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);
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
($response_work, $node) = $apif->service_work($pos_record,$stations_allraw,$history,$node_template);
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"){
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);
my $station = $q->param('station');
(my $xresponse, my $pos_record, my $node_template, my $crecord) = $apif->service_select($q,$authraw,"",$history);
($response_work, $node) = $apif->service_work($pos_record,$stations_allraw,$history,$node_template);
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};
}
$response = { %$response, %$op_response_work, %$auth };
}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{
($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 };
$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,"");
#end JSON ----------------------------------------------------------------------------
return Apache2::Const::OK;
}#end handler
1;