File Coverage

blib/lib/App/HiveHub.pm
Criterion Covered Total %
statement 40 76 52.6
branch 0 8 0.0
condition 0 3 0.0
subroutine 14 18 77.7
pod 0 1 0.0
total 54 106 50.9


line stmt bran cond sub pod time code
1             package App::HiveHub;
2             BEGIN {
3 1     1   848 $App::HiveHub::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Implementation of Hive Hub for gathering from the hive drones [IN DEVELOPMENT]
6             $App::HiveHub::VERSION = '0.001';
7             our $VERSION ||= '0.000';
8              
9 1         6 use MooX qw(
10             Options
11 1     1   817 );
  1         21820  
12              
13 1     1   501847 use Path::Tiny;
  1         24007  
  1         188  
14 1     1   1277 use PocketIO;
  1         190063  
  1         35  
15 1     1   1030 use Plack::Builder;
  1         24882  
  1         105  
16 1     1   13401 use Twiggy::Server;
  1         78163  
  1         53  
17 1     1   11 use AnyEvent;
  1         2  
  1         23  
18 1     1   847 use AnyEvent::SerialPort;
  1         37835  
  1         40  
19 1     1   1154 use AnyEvent::HiveJSO;
  1         53839  
  1         35  
20 1     1   1218 use AnyEvent::HTTP;
  1         30566  
  1         160  
21 1     1   8125 use File::ShareDir::ProjectDistDir;
  1         43115  
  1         10  
22 1     1   577 use JSON::MaybeXS;
  1         2  
  1         71  
23 1     1   1823 use DateTime;
  1         252703  
  1         45  
24 1     1   1854 use DDP;
  1         57191  
  1         12  
25              
26             option 'port' => (
27             is => 'ro',
28             format => 'i',
29             default => '8888',
30             doc => 'port for the webserver',
31             );
32              
33             option 'hive' => (
34             is => 'ro',
35             format => 's',
36             predicate => 1,
37             doc => 'target url for dispatching received packages',
38             );
39              
40             option 'homehive' => (
41             is => 'ro',
42             format => 's',
43             predicate => 1,
44             doc => 'target url of homehive cloud server (do not use)',
45             );
46              
47             option 'serial' => (
48             is => 'ro',
49             format => 's',
50             default => '/dev/ttyAMA0',
51             doc => 'serial port for the HiveJSO stream',
52             );
53              
54             option 'baud' => (
55             is => 'ro',
56             format => 's',
57             default => '9600',
58             doc => 'baud rate for the serial port',
59             );
60              
61             sub run {
62 0     0 0   my ( $self ) = @_;
63              
64 0           my $root = path(dist_dir('App-HiveHub'))->absolute->realpath->stringify;
65 0           my $server = Twiggy::Server->new(
66             port => $self->port,
67             );
68 0           my $last_socket;
69 0     0     my $pocketio = PocketIO->new( handler => sub {} );
  0            
70              
71 0           my $uart = AnyEvent::SerialPort->new(
72             serial_port => [
73             $self->serial,
74             [ baudrate => $self->baud ],
75             ],
76             read_size => 1,
77             );
78              
79             $uart->on_read(sub {
80 0     0     my ( $uart ) = @_;
81             $uart->push_read(hivejso => sub {
82 0           my ( $uart, $data ) = @_;
83 0 0         if (ref $data eq 'HiveJSO::Error') {
84 0           p($data->error); p($data->garbage);
  0            
85 0           return;
86             }
87 0           print $data->original_json."\n";
88 0           my $hivehub_data = $data->add(
89             timestamp => DateTime->now->epoch,
90             );
91 0 0         if ($self->has_homehive) {
92             http_post($self->homehive, '{"orig":'.$data->original_json.',"hivehub":'.$hivehub_data->hivejso.'}',
93             headers => {
94             'user-agent' => 'HiveHub/'.$VERSION,
95             'content-type' => 'application/json',
96 0           }, sub {},
97 0           );
98             }
99 0 0         if ($self->has_hive) {
100             http_post($self->hive, $hivehub_data->hivejso,
101             headers => {
102             'user-agent' => 'HiveHub/'.$VERSION,
103             'content-type' => 'application/json',
104 0           }, sub {},
105 0           );
106             }
107 0 0 0       if ($pocketio->pool->{connections} && %{$pocketio->pool->{connections}}) {
  0            
108 0           my @keys = keys %{$pocketio->pool->{connections}};
  0            
109 0           $pocketio->pool->{connections}->{$keys[0]}->sockets->emit('data',decode_json($hivehub_data->hivejso));
110             }
111 0           });
112 0           });
113              
114             $server->register_service(builder {
115 0     0     mount '/socket.io' => $pocketio;
116             mount '/' => builder {
117 0           enable 'Rewrite', rules => sub { s{^/$}{/index.html}; };
  0            
118 0           enable "Plack::Middleware::Static", path => qr{^/}, root => $root;
119 0           };
120 0           });
121              
122 0           print "\n Starting HiveHub (port ".$self->port.")...\n\n";
123              
124 0           AE::cv->recv;
125             }
126              
127             1;
128              
129             __END__