minor accounting and journaling fix

This commit is contained in:
ragu 2022-11-14 21:16:22 +01:00
parent 0ac2e784ca
commit 2e2c824dbe
17 changed files with 223 additions and 300 deletions

View file

@ -4,7 +4,9 @@
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#
#sudo su www-data -c "./src/scripts/Ilockit_cloud.pl shareedms-fr01 get_events 95"
##sudo su www-data -c "./src/scripts/Ilockit_cloud.pl shareedms-fr01 get_events 95"
#
#sudo su www-data -c "./src/scripts/Ilockit_cloud.pl shareedms-fr01 get_positions"
#
#Ilockit GPS cloud
#
@ -86,11 +88,12 @@ while (my ($key, $value) = each %{ $dbt->{operator} }) {
&get_devices($dbh) if($todo eq "get_devices");
#sudo su www-data -c "./src/scripts/Ilockit_cloud.pl shareedms-fr01 get_events 95"
#in cronjob disabled
&get_events($dbh,$response_in) if($todo eq "get_events");
if($todo eq "get_events"){
&get_events($dbh,$response_in,$value->{oprefix});
&get_positions($dbh,$value->{oprefix});
}
#sudo su www-data -c "./src/scripts/Ilockit_cloud.pl shareedms-fr01 get_positions 6572"
&get_positions($dbh) if($todo eq "get_positions");
#&get_positions($dbh,$value->{oprefix}) if($todo eq "get_positions");
}
}
@ -190,12 +193,16 @@ sub get_devicesONcontenttheftpos {
my $dbh = shift;
my $key = shift;
my $id = shift;
my $end_time = shift || "";
my $pref = {
table => "contenttheftpos",
fetch => "one",
#mtime => ">::(now() - interval '1 day')",
$key => "$id",
};
$pref->{end_time} = ">=::$end_time" if($end_time);
my $record = $dbt->fetch_tablerecord($dbh,$pref);
return $record;
}
@ -211,12 +218,6 @@ sub get_devices {
$response_in = decode_json($ret_json);
print FILE "ilockit get_devices response_in:" . Dumper($response_in);
#foreach my $result (@{ $response_in }) {
# if($result->{id}){
# print $result->{id} . "\n";
# print $result->{name} . "\n";
# }
#}
my $pref = {
table => "content",
@ -229,7 +230,7 @@ sub get_devices {
my $rows = 0;
if(1==1 && ref($response_in) eq "ARRAY"){
foreach my $id (sort { $record->{$a}->{barcode} <=> $record->{$b}->{barcode} } keys (%$record)){
foreach my $resp (@{ $response_in }) {
foreach my $resp (sort { $a->{id} <=> $b->{id} } (@{ $response_in })) {
#print "if($resp->{name} eq $record->{$id}->{txt18} && ($resp->{id} && $resp->{id} ne $record->{$id}->{int13}))\n";
if($resp->{name} eq $record->{$id}->{txt18} && ($resp->{id} && $resp->{id} ne $record->{$id}->{int13})){
my $update = {
@ -251,7 +252,8 @@ sub get_devices {
#get_events
sub get_events {
my $dbh = shift;
$response_in = shift;
my $response_in = shift;
my $oprefix = shift || "";
#1. select all devices on content
print FILE "ilockit get_events get_devicesONcontent_all\n";
@ -275,17 +277,25 @@ sub get_events {
#$response_in = decode_json($ret_json);
#print FILE "ilockit get_events response_in:" . Dumper($response_in);
foreach my $resp (@{ $response_in }) {
foreach my $resp (sort { $a->{id} <=> $b->{id} } (@{ $response_in })) {
#if($record_cc->{$id}->{int13} eq $resp->{deviceId} && $resp->{type} eq "deviceOnline"){
if($record_cc->{$id}->{int13} && $record_cc->{$id}->{int13} eq $resp->{deviceId} && ref($resp->{attributes}) eq "HASH" && $resp->{attributes}->{statusCode} && $resp->{attributes}->{statusCode} eq "alarm"){
if($record_cc->{$id}->{int13} && $record_cc->{$id}->{int13} eq $resp->{deviceId} && $resp->{type} eq "lockStatus" && ref($resp->{attributes}) eq "HASH" && $resp->{attributes}->{statusCode}){
my $theft_record = { c_id => 0 };
$theft_record = get_devicesONcontenttheftpos($dbh,"int01",$resp->{id});
$theft_record = get_devicesONcontenttheftpos($dbh,"int01",$resp->{id},"");#Alarm detected
print FILE "shareeTime: $today\n";
print FILE "bike: $oprefix$record_cc->{$id}->{barcode}\n";
print FILE "id: $resp->{id}\n";
print FILE "deviceId: $resp->{deviceId}\n";
print FILE "type: $resp->{type}\n";
print FILE "statusCode: $resp->{attributes}->{statusCode}\n";
print FILE "serverTime: $resp->{serverTime}\n\n";
my $int10 = 0;
my $txt10 = $resp->{attributes}->{statusCode};
if($resp->{attributes}->{statusCode} eq "alarm"){
$int10 = 7;
}
#insert theft alarm if no theft alarm with theft id
if(!$theft_record->{c_id}){
my $serverTime = localizedtime($resp->{serverTime});
@ -295,12 +305,14 @@ sub get_events {
barcode => "$record_cc->{$id}->{barcode}",
int04 => "$record_cc->{$id}->{int04}",#end station
txt06 => "$record_cc->{$id}->{txt06}",#end gps
txt10 => "$resp->{attributes}->{statusCode}",
txt18 => "$record_cc->{$id}->{txt18}",
owner => $owner,
mtime => "now()",
int10 => "7",#theft alarm
int10 => "$int10",#theft alarm
int01 => "$resp->{id}",#keeps id for event_type
int13 => "$resp->{deviceId}",
int20 => "$record_cc->{$id}->{int20}",
start_time => "$serverTime",
end_time => "$serverTime",
};
@ -316,51 +328,81 @@ sub get_events {
sub get_positions {
my $dbh = shift;
my $oprefix = shift || "";
my $record_cc = get_devicesONcontent_all($dbh,"C2-04");
my $endpoint = "https://tracking.ilockit.bike/api/positions";
my $today = DateTime->now( time_zone => "Europe/Berlin" );
$today .= "Z";
#my $from_datetime = DateTime->now( time_zone => "Europe/Berlin" );
#$from_datetime->subtract( days => 1 );
#$from_datetime .= "Z";
#my $rest = "from=2021-06-11T07:44:10Z\&to=2021-06-11T12:44:10Z\&deviceId=$deviceId" if($deviceId);
#my $ret_json = fetch_ilockit_cloud("","$endpoint",$rest);
#$response_in = decode_json($ret_json);
#print Dumper($response_in);
my $interval = 3;
my $from_datetime = DateTime->now( time_zone => "Europe/Berlin" );
$from_datetime->subtract( minutes => $interval );
$from_datetime = utctime($from_datetime,"0");
$from_datetime .= "Z";
#2. loope cloud
foreach my $id (sort { $record_cc->{$a}->{barcode} <=> $record_cc->{$b}->{barcode} } keys (%$record_cc)){
my $ctpos = { c_id => 0 };
$ctpos = get_devicesONcontenttranspos($dbh,$record_cc->{$id}->{int13});
if($ctpos->{int20} == 1){#if locked then get position in range of end_time to now
print FILE "get_position deviceId: $ctpos->{int13} --> lock_state:$ctpos->{int20}| last rental end_time:$ctpos->{end_time}\n";
$ctpos = get_devicesONcontenttranspos($dbh,$record_cc->{$id}->{int13});#get last rental
#get only positions until smartlock end_time is locked
if($ctpos->{int13} && $ctpos->{end_time} =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/){
$ctpos->{end_time} =~ s/\..*$//;
my $end_time = $ctpos->{end_time};
$end_time =~ s/\s/T/;
my $from_datetime = utctime($end_time,"0");
$from_datetime .= "Z";
#checke also Ilockit lockStatus
my $theft_record_closed = { c_id => 0 };
my $pref_th = {
table => "contenttheftpos",
fetch => "one",
txt10 => "ilike::closed%",
int13 => $record_cc->{$id}->{int13},
end_time => ">::$ctpos->{end_time}",
};
$theft_record_closed = $dbt->fetch_tablerecord($dbh,$pref_th) if($ctpos->{end_time});
#For Closed locks
#if copri.locked OR Ilockit.closed, then get position in range of end_time to now
if($record_cc->{$id}->{int13} && (($ctpos->{int20} && $ctpos->{int20} == 1) || $theft_record_closed->{c_id})){
print FILE "get_position bike $ctpos->{ct_name} $ctpos->{int13} --> copri lock_state:$ctpos->{int20} end_time:$ctpos->{end_time} | Ilockit.statusCode: $theft_record_closed->{txt10}\n";
#3 minutes until now last Alarm detected with deviceId
my $theft_record_detect = { c_id => 0 };
$pref_th = {
table => "contenttheftpos",
fetch => "one",
int10 => 7,
int13 => $record_cc->{$id}->{int13},
end_time => ">=::(now() - interval '$interval min')",
};
#end_time => ">::$ctpos->{end_time}",
$theft_record_detect = $dbt->fetch_tablerecord($dbh,$pref_th);
print FILE "theft_record_detect bike: $theft_record_detect->{barcode} | $theft_record_detect->{c_id} | last end_time:$theft_record_detect->{end_time}\n";
#get only positions if locked and last alarm before 3 minutes
if($theft_record_detect->{c_id} && $ctpos->{int13} && $ctpos->{end_time} =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/){
#$ctpos->{end_time} =~ s/\..*$//;
#my $end_time = $ctpos->{end_time};
#$end_time =~ s/\s/T/;
#my $from_datetime = utctime($end_time,"0");
#$from_datetime .= "Z";
#keep in mind, api-from maybe deviceTime (end_time-1) and position timestamp is serverTime+2
my $rest = "from=$from_datetime\&to=$today\&deviceId=$record_cc->{$id}->{int13}";
my $ret_json = fetch_ilockit_cloud("","$endpoint",$rest);
my $response_in = {};
$response_in = decode_json($ret_json);
print FILE "ilockit get_positions response_in:" . Dumper($response_in);
foreach my $resp (@{ $response_in }) {
my $theftmove_count = 0;
foreach my $resp (sort { $a->{id} <=> $b->{id} } (@{ $response_in })) {
print FILE "$resp->{id} shareeTime: $today | serverTime: $resp->{serverTime} | ilockit get_positions movement: $theft_record_detect->{barcode} | $record_cc->{$id}->{int13} eq $resp->{deviceId} && $resp->{speed} > 0 && $resp->{attributes}->{distance})\n";
#if($record_cc->{$id}->{int13} eq $resp->{deviceId} && $resp->{speed} > 0 && $resp->{attributes}->{distance} >= 200){
if($record_cc->{$id}->{int13} eq $resp->{deviceId}){
my $theft_record = get_devicesONcontenttheftpos($dbh,"int02",$resp->{id});
$theftmove_count++;#count if move holds on for min 2 times
my $theft_record = get_devicesONcontenttheftpos($dbh,"int02",$resp->{id},"");#Alarm movement
print FILE "shareeTime: $today\n";
print FILE "bike: $oprefix$record_cc->{$id}->{barcode}\n";
print FILE "id: $resp->{id}\n";
print FILE "deviceId: $resp->{deviceId}\n";
print FILE "serverTime: $resp->{serverTime}\n\n";
#if(!$theft_record->{c_id} && $theftmove_count >= 2){
if(!$theft_record->{c_id}){
my $serverTime = localizedtime($resp->{serverTime});
my $insert = {
@ -370,12 +412,14 @@ sub get_positions {
txt06 => "$resp->{latitude}, $resp->{longitude}",
owner => $owner,
mtime => "now()",
int10 => "8",#gps position
int02 => "$resp->{id}",#keeps id for event_type
int03 => "$theftmove_count",
int07 => "$resp->{speed}",
int08 => "$resp->{attributes}->{distance}",
int09 => "$resp->{attributes}->{totalDistance}",
int10 => "8",#gps position marker
int13 => "$resp->{deviceId}",
int20 => "$record_cc->{$id}->{int20}",
start_time => "$serverTime",
end_time => "$serverTime",
};

View file

@ -126,7 +126,8 @@ if($todo eq "locking_progress" && $phone && $pos_id){
#SMS message fraud to me
if($todo eq "fraud_rental" && $pos_id){
my $sms_to = "$dbt->{copri_conf}->{sms_to}";
#my $sms_to = "$dbt->{copri_conf}->{sms_to}";
my $sms_to = "01759776061";
my $booking_pos = {
table => "contenttranspos",