| 1 | package Net::MQTT::Simple::One_Shot_Loader; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | require Net::MQTT::Simple; #skip import | |||||||||||
| 5 | |||||||||||||
| 6 | 2 | our $VERSION = '0.02'; | |||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | Net::MQTT::Simple::One_Shot_Loader - Perl package to add one_shot method to Net::MQTT::Simple | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | require Net::MQTT::Simple::One_Shot_Loader; | ||||||||||||
| 15 | use Net::MQTT::Simple; #or Net::MQTT::Simple::SSL | ||||||||||||
| 16 | my $mqtt = Net::MQTT::Simple->new($host); | ||||||||||||
| 17 | my $obj = $mqtt->one_shot($topic_sub, $topic_pub, $message_pub, $timeout_seconds); #isa Net::MQTT::Simple::One_Shot_Loader::Response | ||||||||||||
| 18 | my $value = $obj->message; | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | This package loads the C<one_shot> method into the L<Net::MQTT::Simple> name space to provide a well tested remote procedure call (RPC) via MQTT. Many IoT devices only support MQTT as a protocol so, in order to query state or settings these properties need to be requested by sending a message on one queue and receiving a response on another queue. | ||||||||||||
| 23 | |||||||||||||
| 24 | Due to the way L<Net::MQTT::Simple::SSL> was implemented as a super class of L<Net::MQTT::Simple> and since the author of L<Net::MQTT::Simple> did not want to implement this method in his package (ref L<GitHub|https://github.com/Juerd/Net-MQTT-Simple/pull/22#pullrequestreview-1340685240>), we implemented this method in a method loader package. | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 METHODS | ||||||||||||
| 27 | |||||||||||||
| 28 | =head2 one_shot | ||||||||||||
| 29 | |||||||||||||
| 30 | Returns an object representing the first message that matches the subscription topic after publishing the message on the message topic. Returns an object with the error set to a true value on error like timeout. | ||||||||||||
| 31 | |||||||||||||
| 32 | my $response = $mqtt->one_shot($topic_sub, $topic_pub, $message_pub, $timeout_seconds); | ||||||||||||
| 33 | |||||||||||||
| 34 | if (not $response->error) { | ||||||||||||
| 35 | my $message = $response->message; | ||||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | =cut | ||||||||||||
| 39 | |||||||||||||
| 40 | { | ||||||||||||
| 41 | package Net::MQTT::Simple::One_Shot_Loader::Response; | ||||||||||||
| 42 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 0 | ▶ | sub error {shift->{'error'}}; | ||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
| |||||||||||||
| 45 | 0 | ▶ | sub topic {shift->{'topic'}}; | ||||||||||
|
Subroutine
topic: not called CC=1 SCAR=6.9
| |||||||||||||
| 46 | 0 | ▶ | sub message {shift->{'message'}}; | ||||||||||
|
Subroutine
message: not called CC=1 SCAR=6.9
| |||||||||||||
| 47 | 0 | ▶ | sub time {shift->{'time'}}; | ||||||||||
|
Subroutine
time: not called CC=1 SCAR=6.9
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | { | ||||||||||||
| 51 | 2 | package Net::MQTT::Simple; | |||||||||||
| 52 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 2 | ▶ | use Time::HiRes qw{}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | |||||||||||||
| 56 | sub one_shot { | ||||||||||||
| 57 | 0 | ▶ | my $self = shift; #isa Net::MQTT::Simple or Net::MQTT::Simple::SSL | ||||||||||
|
Subroutine
one_shot: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||
| 58 | 0 | ▶ | my $topic_sub = shift or die('Error: subscribe topic is required'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | ▶ | my $topic_pub = shift or die('Error: publish topic is required'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | my $message = shift; | |||||||||||
| 61 | 0 | ▶ | $message = '' unless defined $message; #default '', allow 0 and support perl 5.8 | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | ▶ | my $timeout = shift || 1.5; #seconds | ||||||||||
|
Condition0%0/2
shift() || 1.5
MC/DC0%0/1
shift() || 1.5
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 63 | |||||||||||||
| 64 | 0 | my $found = 0; #anonymous sub updates these variables | |||||||||||
| 65 | 0 | my $topic_out = $topic_sub; | |||||||||||
| 66 | 0 | my $message_out = ''; | |||||||||||
| 67 | |||||||||||||
| 68 | $self->subscribe($topic_sub => sub { | ||||||||||||
| 69 | 0 | ▶ | unless ($found) { #stop after first found but we get multiple calls per tick | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 70 | 0 | $found = 1; | |||||||||||
| 71 | 0 | $topic_out = shift; | |||||||||||
| 72 | 0 | $message_out = shift; | |||||||||||
| 73 | } | ||||||||||||
| 74 | } | ||||||||||||
| 75 | 0 | ); | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | my $timer = Time::HiRes::time(); | |||||||||||
| 78 | 0 | $self->publish($topic_pub => $message); | |||||||||||
| 79 | |||||||||||||
| 80 | 0 | my $future = Time::HiRes::time() + $timeout; | |||||||||||
| 81 | 0 | ▶ | while (Time::HiRes::time() < $future) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | $self->tick($timeout); #it takes a few ticks to clear out LWT | |||||||||||
| 83 | 0 | ▶ | last if $found; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | 0 | $timer = Time::HiRes::time() - $timer; | |||||||||||
| 86 | 0 | ▶ | my $error = $found ? '' : sprintf('subscribe timeout (%0.1f s)', $timer); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | |||||||||||||
| 88 | 0 | $self->unsubscribe($topic_sub); #must unsubscribe to do one_shot back to back | |||||||||||
| 89 | 0 | return bless { | |||||||||||
| 90 | error => $error, | ||||||||||||
| 91 | topic => $topic_out, | ||||||||||||
| 92 | message => $message_out, | ||||||||||||
| 93 | time => $timer, | ||||||||||||
| 94 | }, 'Net::MQTT::Simple::One_Shot_Loader::Response'; | ||||||||||||
| 95 | } | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 SEE ALSO | ||||||||||||
| 99 | |||||||||||||
| 100 | L<Net::MQTT::Simple> | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||
| 103 | |||||||||||||
| 104 | Michael R. Davis | ||||||||||||
| 105 | |||||||||||||
| 106 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 107 | |||||||||||||
| 108 | MIT License | ||||||||||||
| 109 | |||||||||||||
| 110 | Copyright (c) 2023 Michael R. Davis | ||||||||||||
| 111 | |||||||||||||
| 112 | =cut | ||||||||||||
| 113 | |||||||||||||
| 114 | 2 | 1; | |||||||||||