From 0dd4f35728c1e33825d5bb2f86104a483168c24d Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Fri, 6 Aug 2010 21:00:40 +0200 Subject: [PATCH] split RFID::Bibio::Reader::Serial as base class --- lib/RFID/Biblio.pm | 88 +------------------------------- lib/RFID/Biblio/Reader/3M810.pm | 2 +- lib/RFID/Biblio/Reader/CPRM02.pm | 2 +- lib/RFID/Biblio/Reader/Serial.pm | 62 ++++++++++++++++++++++ t/09-Serial.t | 10 ++++ t/10-3M-810.t | 3 -- t/20-CPR-M02.t | 3 -- 7 files changed, 75 insertions(+), 95 deletions(-) create mode 100644 lib/RFID/Biblio/Reader/Serial.pm create mode 100755 t/09-Serial.t diff --git a/lib/RFID/Biblio.pm b/lib/RFID/Biblio.pm index 458b2c6..1139a0c 100644 --- a/lib/RFID/Biblio.pm +++ b/lib/RFID/Biblio.pm @@ -6,7 +6,6 @@ use strict; use base 'Exporter'; our @EXPORT = qw( hex2bytes as_hex hex_tag ); -use Device::SerialPort qw(:STAT); use Data::Dump qw(dump); =head1 NAME @@ -25,92 +24,7 @@ my $debug = 0; This module tries to support USB serial RFID readers wsing simple API which is sutable for direct mapping to REST JSONP service. -Perhaps a little code snippet. - - use RFID::Biblio; - - my $rfid = RFID::Biblio->new( - device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE - ); - - # invetory tags in reader range and read data from them - my $visible = $rfid->scan; - -=head1 METHODS - -=head2 new - -Open serial port (if needed) and init reader - -=cut - -sub new { - my $class = shift; - my $self = {@_}; - bless $self, $class; - - $self->port; - - $self->init && return $self; -} - -=head2 port - - my $serial_obj = $self->port; - -=cut - -sub port { - my $self = shift; - - return $self->{port} if defined $self->{port}; - - my $settings = $self->serial_settings; - my $device = $settings->{device} ||= $ENV{RFID_DEVICE}; - warn "# settings ",dump $settings; - - if ( ! $device ) { - warn "# no device, serial port not opened\n"; - return; - } - - $self->{port} = Device::SerialPort->new( $settings->{device} ) - || die "can't open serial port: $!\n"; - - $self->{port}->$_( $settings->{$_} ) - foreach ( qw/handshake baudrate databits parity stopbits/ ); - -} - -=head2 scan - - my $visible = $rfid->scan; - -Returns hash with keys which match tag UID and values with blocks - -=cut - -sub scan { - my $self = shift; - - warn "# scan tags in reader range\n"; - my @tags = $self->inventory; - - my $visible; - # FIXME this is naive implementation which just discards other tags - foreach my $tag ( @tags ) { - my $blocks = $self->read_blocks( $tag ); - if ( ! $blocks ) { - warn "ERROR: can't read tag $tag\n"; - delete $visible->{$tag}; - } else { - $visible->{$tag} = $blocks->{$tag}; - } - } - - return $visible; -} - +For implementing application take a look at L =head1 READER IMPLEMENTATION diff --git a/lib/RFID/Biblio/Reader/3M810.pm b/lib/RFID/Biblio/Reader/3M810.pm index f26e9ab..d959e57 100644 --- a/lib/RFID/Biblio/Reader/3M810.pm +++ b/lib/RFID/Biblio/Reader/3M810.pm @@ -24,7 +24,7 @@ L use warnings; use strict; -use base 'RFID::Biblio'; +use base 'RFID::Biblio::Reader::Serial'; use RFID::Biblio; use Time::HiRes; diff --git a/lib/RFID/Biblio/Reader/Serial.pm b/lib/RFID/Biblio/Reader/Serial.pm new file mode 100644 index 0000000..85dbf2b --- /dev/null +++ b/lib/RFID/Biblio/Reader/Serial.pm @@ -0,0 +1,62 @@ +package RFID::Biblio::Reader::Serial; + +use warnings; +use strict; + +use Device::SerialPort qw(:STAT); +use Data::Dump qw(dump); + +=head1 NAME + +RFID::Biblio::Reader::Serial - helper to provide serial port + +=cut + +=head1 METHODS + +=head2 new + +Open serial port (if needed) and init reader + +=cut + +sub new { + my $class = shift; + my $self = {@_}; + bless $self, $class; + + $self->port; + + $self->init && return $self; +} + + +=head2 port + + my $serial_obj = $self->port; + +=cut + +sub port { + my $self = shift; + + return $self->{port} if defined $self->{port}; + + my $settings = $self->serial_settings; + my $device = $settings->{device} ||= $ENV{RFID_DEVICE}; + warn "# settings ",dump $settings; + + if ( ! $device ) { + warn "# no device, serial port not opened\n"; + return; + } + + $self->{port} = Device::SerialPort->new( $settings->{device} ) + || die "can't open serial port: $!\n"; + + $self->{port}->$_( $settings->{$_} ) + foreach ( qw/handshake baudrate databits parity stopbits/ ); + +} + +1 diff --git a/t/09-Serial.t b/t/09-Serial.t new file mode 100755 index 0000000..e6e9eac --- /dev/null +++ b/t/09-Serial.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl + +use Test::More tests => 1; + +use lib 'lib'; + +BEGIN { + use_ok( 'RFID::Biblio::Reader::Serial' ); +} + diff --git a/t/10-3M-810.t b/t/10-3M-810.t index 1099a21..1118c4d 100755 --- a/t/10-3M-810.t +++ b/t/10-3M-810.t @@ -34,7 +34,4 @@ foreach my $tag ( @tags ) { } -ok( my $visible = $o->scan, 'scan' ); -diag dump $visible; - done_testing(); diff --git a/t/20-CPR-M02.t b/t/20-CPR-M02.t index 7ba476d..1fbf05c 100755 --- a/t/20-CPR-M02.t +++ b/t/20-CPR-M02.t @@ -27,7 +27,4 @@ foreach my $tag ( @tags ) { } -ok( my $visible = $o->scan, 'scan' ); -diag dump $visible; - done_testing; -- 2.20.1