sharee.bike/copri4/main/src/scripts/Ilockit_CSV2DB.pl

144 lines
4.6 KiB
Perl
Raw Normal View History

2021-12-30 12:05:56 +01:00
#!/usr/bin/perl
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#For pdf pw files
#"pdftotext -upw FksjxxxxxxfmC1h Teilrad_ISI_PLUS_BB01207.pdf"
#:1,$ s/^\n//g
#:1,$ s/\n/;/g
#:1,$ s/;C2-/\rC2-/g
#:1,$ s/;QR-/\rQR-/g
#
#
#check if UTF-8
#file Teilrad_ISI_PLUS_BB01207.txt
#iconv -t utf-8 -f iso-8859-1 Ilockitkeys_110520.csv -o Ilockitkeys_110520_utf8.cs
#
2022-03-18 14:51:25 +01:00
#sudo su www-data -c "./src/scripts/Ilockit_CSV2DB.pl shareedms-kn ../shareeconf/smartlock-keys/Ilockit-16-BB01742_1v2.csv"
2021-12-30 12:05:56 +01:00
#
#do it twice to set hex-key and bike-nr!
#
use vars qw($syshost);
BEGIN {
$syshost = $ARGV[0] || exit 1;
}
2022-03-18 14:51:25 +01:00
use lib "/var/www/copri-bike/$syshost/src";
2021-12-30 12:05:56 +01:00
my $csv_file = $ARGV[1] || "";
use strict;
use warnings;
use POSIX;
use DBI;
use CGI ':standard';
use Lib::Config;
use Mod::DBtank;
use Scalar::Util qw(looks_like_number);
use Data::Dumper;
my $q = new CGI;
my $cf = new Config;
my %varenv = $cf->envonline("$syshost");
print "$varenv{wwwhost}\n";
my $dbt = new DBtank;
my $lang = "de";
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $dbh = "";
2022-03-30 07:29:24 +02:00
my $main_id = 300103;#TODO Flotten ID
2021-12-30 12:05:56 +01:00
my $template_id = 205;
###
#Start loop payone log and update userid ##################################
####
#reading CSV
my $dbh_csv = DBI->connect("DBI:CSV:");
$dbh_csv->{'csv_tables'}->{'ilockitcsv'} = {
'eol' => "\n",
'sep_char' => ";",
'quote_char' => undef,
#'quote_char' => "\"",
'escape_char' => undef,
'file' => $csv_file,
'col_names' => ["serialnr","ilockit_id","ilockit_key","c1","c2","c3","c4","c5","c6"]
};
my $sth = $dbh_csv->prepare("SELECT * FROM ilockitcsv where serialnr like 'C2-%'");
my $rc = $sth->execute();
my $csv = $sth->fetchall_hashref("serialnr");
my $i=0;
foreach my $id (sort { $csv->{$a}->{serialnr} cmp $csv->{$b}->{serialnr} } keys (%$csv)){
$i++;
if($csv->{$id}->{serialnr}){
$csv->{$id}->{ilockit_id} =~ s/\+/\-/;
$csv->{$id}->{c1} =~ s/Rot/red/;$csv->{$id}->{c1} =~ s/Blau/blue/;$csv->{$id}->{c1} =~ s/Gr.+n/green/;
$csv->{$id}->{c2} =~ s/Rot/red/;$csv->{$id}->{c2} =~ s/Blau/blue/;$csv->{$id}->{c2} =~ s/Gr.+n/green/;
$csv->{$id}->{c3} =~ s/Rot/red/;$csv->{$id}->{c3} =~ s/Blau/blue/;$csv->{$id}->{c3} =~ s/Gr.+n/green/;
$csv->{$id}->{c4} =~ s/Rot/red/;$csv->{$id}->{c4} =~ s/Blau/blue/;$csv->{$id}->{c4} =~ s/Gr.+n/green/;
$csv->{$id}->{c5} =~ s/Rot/red/;$csv->{$id}->{c5} =~ s/Blau/blue/;$csv->{$id}->{c5} =~ s/Gr.+n/green/;
$csv->{$id}->{c6} =~ s/Rot/red/;$csv->{$id}->{c6} =~ s/Blau/blue/;$csv->{$id}->{c6} =~ s/Gr.+n/green/;
print "$i) $csv->{$id}->{serialnr}|\n";
my $ct = &select_content($csv->{$id});
my $data = {
table => "content",
main_id => "$main_id",
template_id => "$template_id",
txt22 => "$csv->{$id}->{serialnr}",
txt18 => "$csv->{$id}->{ilockit_id}",
byte01 => "\\x$csv->{$id}->{ilockit_key}",
int04 => "0",#station Werkstatt
int10 => "5",#defect
int11 => "2",#Ilockit
2022-03-18 14:51:25 +01:00
int20 => "2",#unlocked
2022-03-30 07:29:24 +02:00
txt01 => "Mietrad neu", #Bezeichnung #TODO
txt04 => "", #Besonderheiten
2021-12-30 12:05:56 +01:00
txt15 => "", #Firmware
txt17 => "", #GUID from APP
txt23 => "$csv->{$id}->{c1} $csv->{$id}->{c2} $csv->{$id}->{c3} $csv->{$id}->{c4} $csv->{$id}->{c5} $csv->{$id}->{c6}",
mtime => "now()",
owner => "1842",
};
if(ref($ct) eq "HASH" && $ct->{c_id} && ref($data) eq "HASH" && $csv->{$id}->{serialnr}){
print "UPDATE $csv->{$id}->{serialnr} (c_id:$ct->{c_id})\n";
$data->{barcode} = $ct->{c_id} if(!$ct->{barcode});
$dbt->update_record($dbh,$data, { c_id => $ct->{c_id} });
print Dumper($data) . "\n";
}elsif(ref($data) eq "HASH" && $csv->{$id}->{serialnr}){
print "INSERT $csv->{$id}->{serialnr}\n";
$dbt->insert_contentoid($dbh,$data);
print Dumper($data) . "\n";
}else{
print "nothing todo\n";
}
}
}
#content check if still exists
sub select_content{
my $ct_hash = shift;
my $fetch = {
table => "content",
main_id => "$main_id",
template_id => "$template_id",
fetch => "one",
};
if(ref($ct_hash) eq "HASH" && $ct_hash->{serialnr}){
$fetch = { %$fetch , txt22 => "=::$ct_hash->{serialnr}" };
#$fetch = { %$fetch , barcode => "<::100" };
#$fetch = { %$fetch , int10 => "!=::3" };
#$fetch = { %$fetch , int11 => "!=::2" };
}
my $ct = $dbt->fetch_record($dbh,$fetch);
return $ct;
}