Adding a new bluetooth device.
This commit is contained in:
parent
97c55c6198
commit
34747d1960
72
lib/Exd/DeviceToBluetooth.pm
Normal file
72
lib/Exd/DeviceToBluetooth.pm
Normal file
@ -0,0 +1,72 @@
|
||||
package Exd::DeviceToBluetooth;
|
||||
|
||||
use v5.40.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Moo;
|
||||
|
||||
use GD::Image;
|
||||
use Path::Tiny;
|
||||
use Printer::ESCPOS;
|
||||
use Exd::DeviceToRawFile;
|
||||
use Net::Bluetooth;
|
||||
|
||||
has address => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has port => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has _guts_device => ( is => 'rw' );
|
||||
|
||||
has _tempdir => ( is => 'rw', );
|
||||
|
||||
sub _device($self) {
|
||||
if ( !defined $self->_guts_device ) {
|
||||
$self->_tempdir( Path::Tiny->tempdir );
|
||||
$self->_guts_device(
|
||||
Exd::DeviceToRawFile->new(
|
||||
output_file => $self->_tempfile,
|
||||
)
|
||||
);
|
||||
}
|
||||
return $self->_guts_device;
|
||||
}
|
||||
|
||||
sub _tempfile($self) {
|
||||
my $tempdir = $self->_tempdir;
|
||||
return $tempdir->child('tmp.escpos');
|
||||
}
|
||||
|
||||
sub _build__guts_device($self) {
|
||||
my $device = Printer::ESCPOS->new(
|
||||
driverType => 'File',
|
||||
deviceFilePath => $self->output_file,
|
||||
);
|
||||
}
|
||||
|
||||
sub image( $self, $image ) {
|
||||
$self->_device->image($image);
|
||||
}
|
||||
|
||||
sub lf($self) {
|
||||
$self->_device->lf;
|
||||
}
|
||||
|
||||
sub print($self) {
|
||||
$self->_device->print;
|
||||
$self->_guts_device(undef);
|
||||
my $obj = Net::Bluetooth->newsocket('RFCOMM');
|
||||
if ( 0 != $obj->connect( $self->address, $self->port ) ) {
|
||||
die 'Unable to connect to bluetooth';
|
||||
}
|
||||
my $fh = $obj->perlfh;
|
||||
print $fh path( $self->_tempfile )->slurp_raw;
|
||||
}
|
||||
1;
|
@ -8,10 +8,11 @@ use warnings;
|
||||
use Exd::Printer;
|
||||
use Exd::DeviceToImage;
|
||||
use Exd::DeviceToRawFile;
|
||||
use Exd::DeviceToBluetooth;
|
||||
use Exd::Utils;
|
||||
|
||||
{
|
||||
my $device = Exd::DeviceToImage->new( output_file => 'a.png' );
|
||||
my $device = Exd::DeviceToBluetooth->new( address => '5A:4A:AE:8C:E9:D2', port => 1 );
|
||||
my $printer = Exd::Printer->new( device => $device );
|
||||
|
||||
my $hoz = Exd::Utils::get_gd_image('scripts/hoz.jpg');
|
||||
|
Loading…
Reference in New Issue
Block a user