File: scan.pl

package info (click to toggle)
libbiblio-rfid-perl 0.05-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 600 kB
  • sloc: perl: 3,833; makefile: 576; javascript: 309; ansic: 31; sh: 3
file content (71 lines) | stat: -rwxr-xr-x 1,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl

use warnings;
use strict;

use Data::Dump qw(dump);
use Getopt::Long;
use lib 'lib';
use Biblio::RFID::Reader;
use Biblio::RFID::RFID501;

my $loop = 0;
my $reader;
my $debug = 0;
my $log;

GetOptions(
	'loop!'     => \$loop,
	'reader=s', => \$reader,
	'debug+'    => \$debug,
	'log=s'     => \$log,
) || die $!;

my $rfid = Biblio::RFID::Reader->new( $reader );
$Biblio::RFID::debug = $debug;

sub tag {
	my $tag = shift;
	return $tag
		, " AFI: "
		, uc unpack('H2', $rfid->afi($tag))
		, " "
		, dump( $rfid->to_hash( $tag ) )
		, $/
		;
}

my $saved;

sub iso_date {
	my @t = localtime(time);
	return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
}

sub log_tag {
	my $tag = shift;
	return if $saved->{tag} or ! $log;
	my $hash = $rfid->to_hash( $tag );
	open(my $fh, '>>', $log) || die "$log: $!";
	print $fh iso_date,",$tag,", $hash->{content}, "\n";
	close($fh);
}

do {
	my @visible = $rfid->tags(
		enter => sub {
			my $tag = shift;
			print iso_date," reader ", $rfid->from_reader($tag), " enter ", tag($tag);
			log_tag $tag;
		},
		leave => sub {
			my $tag = shift;
			print iso_date," leave ", tag($tag);
		},
	);

	warn iso_date," visible: ",join(' ',@visible),"\n";

	sleep 1;

} while $loop;