| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | #============================================================================== | ||
| 4 | # Ham::Fldigi::Client | ||
| 5 | # v0.002 | ||
| 6 | # (c) 2012 Andy Smith, M0VKG | ||
| 7 | #============================================================================== | ||
| 8 | # DESCRIPTION | ||
| 9 | # This module handles communications with a running fldigi instance | ||
| 10 | #============================================================================== | ||
| 11 | # SYNOPSIS | ||
| 12 | # use Ham::Fldigi; | ||
| 13 | # my $f = new Ham::Fldigi('LogLevel' => 4, | ||
| 14 | # 'LogFile' => './debug.log', | ||
| 15 | # 'LogPrint' => 1, | ||
| 16 | # 'LogWrite' => 1); | ||
| 17 | # my $client = $f->client('Hostname' => 'localhost', | ||
| 18 | # 'Port' => '7362', | ||
| 19 | # 'Name' => 'default'); | ||
| 20 | # $client->modem("BPSK125"); | ||
| 21 | # $client->send("CQ CQ CQ DE M0VKG M0VKG M0VKG KN"); | ||
| 22 | #============================================================================== | ||
| 23 | |||
| 24 | # Perl documentation is provided inline in pod format. | ||
| 25 | # To view, run:- | ||
| 26 | # perldoc Client.pm | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Ham::Fldigi::Client - Perl extension for communicating with Fldigi. | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use Ham::Fldigi; | ||
| 35 | my $f = new Ham::Fldigi; | ||
| 36 | my $c = $f->client('Hostname' => 'localhost', | ||
| 37 | 'Port' => '7362', | ||
| 38 | 'Name' => 'example'); | ||
| 39 | |||
| 40 | $c->command('fldigi.version'); | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This module is for communicating with individual Fldigi instances. | ||
| 45 | |||
| 46 | It uses Fldigi's XMLRPC service, which usually runs on localhost:7362, providing support for it has been compiled in. | ||
| 47 | |||
| 48 | =head2 EXPORT | ||
| 49 | |||
| 50 | None by default. | ||
| 51 | =cut | ||
| 52 | |||
| 53 | package Ham::Fldigi::Client; | ||
| 54 | |||
| 55 | 1 | ▶ | use 5.012004; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 58 | |||
| 59 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 60 | 1 | ▶ | use Data::GUID; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 61 | 1 | ▶ | use RPC::XML::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 62 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 63 | 1 | ▶ | use Time::HiRes qw( usleep ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 64 | 0 | ▶ | use POE qw( Wheel::Run ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 65 | use base qw(Ham::Fldigi::Debug); | ||
| 66 | |||
| 67 | has 'hostname' => (is => 'rw'); | ||
| 68 | has 'port' => (is => 'rw'); | ||
| 69 | has 'name' => (is => 'rw'); | ||
| 70 | has 'url' => (is => 'ro'); | ||
| 71 | has 'id' => (is => 'ro'); | ||
| 72 | has '_xmlrpc' => (is => 'ro'); | ||
| 73 | has '_session' => (is => 'ro'); | ||
| 74 | has '_buffer_tx' => (is => 'ro'); | ||
| 75 | has '_buffer_rx' => (is => 'ro'); | ||
| 76 | |||
| 77 | our $VERSION = '0.002'; | ||
| 78 | |||
| 79 | =head1 CONSTRUCTORS | ||
| 80 | |||
| 81 | =head2 Client->new('Hostname' => I<hostname>, 'Port' => I<port>, 'Name' => I<name>) | ||
| 82 | |||
| 83 | Creates a new B<Ham::Fldigi::Client> object with the specified arguments. By default, 'localhost' and '7362' are assumed for I<Hostname> and I<Port> respectively. I<Name> is for use with C<Ham::Fldigi::Shell>, and can be safely left blank. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub new { | ||
| 88 | |||
| 89 | # Get our name, and set an ID | ||
| 90 | my $class = shift; | ||
| 91 | my $g = Data::GUID->new; | ||
| 92 | |||
| 93 | # Fill in the class ID and version | ||
| 94 | my $self = { | ||
| 95 | 'version' => $VERSION, | ||
| 96 | 'id' => $g->as_string, | ||
| 97 | }; | ||
| 98 | |||
| 99 | # Bless self | ||
| 100 | bless $self, $class; | ||
| 101 | |||
| 102 | $self->debug("Constructor called. Version ".$VERSION.", with ID ".$self->id."."); | ||
| 103 | |||
| 104 | # Grab the passed client details. | ||
| 105 | # Assume localhost, 7362 and the GUID for the name if no options passed. | ||
| 106 | my (%params) = @_; | ||
| 107 | if(defined($params{'Hostname'})) { | ||
| 108 | $self->hostname($params{'Hostname'}); | ||
| 109 | } else { | ||
| 110 | $self->hostname("localhost"); | ||
| 111 | } | ||
| 112 | if(defined($params{'Port'})) { | ||
| 113 | $self->port($params{'Port'}); | ||
| 114 | } else { | ||
| 115 | $self->port(7362); | ||
| 116 | } | ||
| 117 | if(defined($params{'Name'})) { | ||
| 118 | $self->name($params{'Name'}); | ||
| 119 | } else { | ||
| 120 | $self->name($self->id); | ||
| 121 | } | ||
| 122 | |||
| 123 | # Generate the XML-RPC URL from the parameters | ||
| 124 | $self->{url} = 'http://'.$self->hostname.':'.$self->port.'/RPC2'; | ||
| 125 | $self->debug("Hostname is ".$self->hostname.", port is ".$self->port." and name is ".$self->name."."); | ||
| 126 | $self->debug("XML-RPC URL is ".$self->url."."); | ||
| 127 | |||
| 128 | # Initialise the RPC::XML::Client object | ||
| 129 | $self->{_xmlrpc} = RPC::XML::Client->new($self->url); | ||
| 130 | |||
| 131 | # Check connectivity with the fldigi client by checking the version | ||
| 132 | $self->debug("Checking connectivity with fldigi client at ".$self->url."..."); | ||
| 133 | my $fldigi_version = $self->version; | ||
| 134 | |||
| 135 | # If we get a version back, everything's fine. | ||
| 136 | if(defined($fldigi_version)) { | ||
| 137 | $self->debug("Version is ".$fldigi_version."."); | ||
| 138 | } else { | ||
| 139 | return undef; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Return... | ||
| 143 | $self->debug("Returning..."); | ||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head1 METHODS | ||
| 148 | |||
| 149 | =head2 Client->command(I<command>, [I<arguments>]) | ||
| 150 | |||
| 151 | Send command I<command>, with optional arguments I<arguments>. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub command { | ||
| 156 | |||
| 157 | my ($self, $cmd, $args) = @_; | ||
| 158 | |||
| 159 | # If $args is unset, set it with an empty value | ||
| 160 | if(!defined($args)) { $args = "" }; | ||
| 161 | |||
| 162 | $self->debug("Making XMLRPC call '".$cmd."' (args: ".$args.") to http://".$self->hostname.":".$self->port."/RPC2..."); | ||
| 163 | my $r = $self->_xmlrpc->simple_request($cmd, $args); | ||
| 164 | |||
| 165 | # Check for undef response, which means there's been an error | ||
| 166 | if($RPC::XML::ERROR ne "") { | ||
| 167 | $self->error("Error talking to ".$self->url."!"); | ||
| 168 | $self->error("RPC::XML::Client reports: ".$RPC::XML::ERROR); | ||
| 169 | return undef; | ||
| 170 | } | ||
| 171 | |||
| 172 | # If there's no response from XMLRPC, set it to '(null)' | ||
| 173 | if(!defined($r)) { $r = "(null)"; }; | ||
| 174 | |||
| 175 | $self->debug("Response from XMLRPC request is: ".$r); | ||
| 176 | return $r; | ||
| 177 | } | ||
| 178 | |||
| 179 | =head2 Client->version | ||
| 180 | |||
| 181 | Query for the version of Fldigi. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub version { | ||
| 186 | |||
| 187 | my ($self) = @_; | ||
| 188 | my $r = $self->command('fldigi.version'); | ||
| 189 | |||
| 190 | return $r; | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | =head2 Client->send("I<text>") | ||
| 195 | |||
| 196 | Clears the TX window of any existing text, adds the text passed as I<text>, and then transmits. It automatically adds a '^r' at the end to tell Fldigi to switch back to RX afterwards. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub send { | ||
| 201 | |||
| 202 | my ($self, $text) = @_; | ||
| 203 | |||
| 204 | # Clear the TX window of any existing text, add our text and then switch to TX. | ||
| 205 | # We add a '^r' on the end to tell fldigi to stop once it's transmitted all | ||
| 206 | # the waiting text. | ||
| 207 | $self->command("text.clear_tx"); | ||
| 208 | $self->command("text.add_tx", $text."^r"); | ||
| 209 | $self->command("main.tx"); | ||
| 210 | |||
| 211 | } | ||
| 212 | |||
| 213 | 1; | ||
| 214 | __END__ | ||