package POEDaemon::WheelRun::GPIOPoll; use strict; use warnings FATAL => 'all'; no warnings 'redefine'; use POSIX qw(strftime); use POE; use POEDaemon; sub states { return $_[0], [qw( gpiopoll_start gpiopoll_stop gpiopoll_init gpiopoll_restart gpiopoll_childexit gpiopoll_stdout_line gpiopoll_stderr_line )]; } sub gpiopoll_start { my ($kernel, $heap) = @_[KERNEL, HEAP]; #if (cfg->{poesession_mode} eq 'client') #{ # log_enabled && logline "ignored: delayed, poeserver-configured start is used in poesession mode '%s'", cfg->{poesession_mode}; # # return; #} #$heap->{gpiopoll_start_executed} = 1; $kernel->yield('gpiopoll_init'); } sub gpiopoll_stop { my $kernel = $_[KERNEL]; $kernel->yield(wheelrun_kill => { wheel_name => 'gpiopoll' }); } sub gpiopoll_init { my ($kernel, $heap, $args) = @_[KERNEL, HEAP, ARG0]; log_enabled && logline 'event fired'; my @pins = (); if (cfg->{poll_gpio_pins} && ref cfg->{poll_gpio_pins} eq 'ARRAY') { @pins = @{cfg->{poll_gpio_pins}}; } if ($args->{pins} && ref $args->{pins} eq 'ARRAY') { @pins = @{$args->{pins}}; } if (cfg->{poll_gpio_pins_extra} && ref cfg->{poll_gpio_pins_extra} eq 'ARRAY') { @pins = (@pins, @{cfg->{poll_gpio_pins_extra}}); } my $prog = [ 'util/gpio.pl', 'poll', join(',', sort {$a <=> $b} @pins), (cfg->{gpio_poll_delay} || 1) * 1000000, ]; if (@pins) { log_enabled && logline 'wheelrun_exec'; $kernel->yield(wheelrun_exec => { prog => $prog, name => 'gpiopoll', stdout_event => 'gpiopoll_stdout_line', stderr_event => 'gpiopoll_stderr_line', }); } } sub gpiopoll_restart { my ($kernel, $heap) = @_[KERNEL, HEAP]; log_enabled && logline "event fired, gpiopoll_start_executed = '%s'", $heap->{gpiopoll_start_executed} || 'n/a'; if ($heap->{wheel_run}->{children_by_name}->{gpiopoll}) { $heap->{wheelrun_gpiopoll_restartmode} = 1; $kernel->yield('gpiopoll_stop'); } else { $kernel->yield(gpiopoll_init => { pins => [ keys %{$heap->{tcpclient_dynamic_gpio_pins}->{in}} ], }); } } sub gpiopoll_childexit { my ($kernel, $heap) = @_[KERNEL, HEAP]; if ($heap->{wheelrun_gpiopoll_restartmode}) { delete $heap->{wheelrun_gpiopoll_restartmode}; $kernel->yield(gpiopoll_init => { pins => [ keys %{$heap->{tcpclient_dynamic_gpio_pins}->{in}} ], }); } else { unless ($heap->{shutdown}) { $kernel->yield(shutdown => 'gpiopoll child abnormally terminated'); } } } sub gpiopoll_stdout_line { my ($kernel, $session, $heap, $line) = @_[KERNEL, SESSION, HEAP, ARG0]; my ($pin, $value); if ($line =~ /^(\d{1,3})=([01])$/) { $pin = $1; $value = $2; } else { log_enabled && logline "invalid line = '%s'", $line; return; } $heap->{gpiopoll}->{pinstatus}->{$pin} = $value; $heap->{gpiopoll}->{pintimes}->{$pin} = time_hires; my $output_per_target; $output_per_target->{tcpserver} = sprintf "GPIO %s=%s", $pin, $value; $output_per_target->{tcpclient} = sprintf "%s gpio %s=%s", POECLIENT_CMD_PREFIX, $pin, $value; #foreach my $wheel_id (keys %{$heap->{tcpserver}->{connections}}) #{ # next unless $heap->{tcpserver}->{connections}->{$wheel_id}->{gpio_watch}; # # # next unless $heap->{tcpserver}->{connections}->{$wheel_id}->{wheel}; # # # my $output = sprintf "WATCH %s=%s", # $pin, # $value; # # # $kernel->call($session => tcpserver_output => # { # wheel_id => $wheel_id, # output => $output, # use_flush => 1, # }); #} # # #foreach my $wheel_id (keys %{$heap->{tcpclient}->{connections}}) #{ # next unless $heap->{tcpclient}->{connections}->{$wheel_id}->{wheel}; # # # my $output = sprintf "%s gpio %s", # POECLIENT_CMD_PREFIX, # $line; # # # $kernel->call($session => tcpclient_output => # { # wheel_id => $wheel_id, # output => $output, # use_flush => 1, # }); #} $kernel->call($session => wheelrun_send_network_reply => { output_per_target => $output_per_target, }); #log_enabled && logline "poll change gpio %s=%s", # $pin, # $value; log_enabled && logline "gpio %s=%s", $pin, $value; } sub gpiopoll_stderr_line { my ($kernel, $session, $heap, $line) = @_[KERNEL, SESSION, HEAP, ARG0]; log_enabled && logline "line = '%s'", $line; return unless $line =~ /^ready$/i; $line = lc $line; my $output_per_target; $output_per_target->{tcpserver} = sprintf "GPIOPOLL %s", $line; $output_per_target->{tcpclient} = sprintf "%s gpiopoll %s", POECLIENT_CMD_PREFIX, $line; #foreach my $wheel_id (keys %{$heap->{tcpclient}->{connections}}) #{ # next unless $heap->{tcpclient}->{connections}->{$wheel_id}; # # # next unless $heap->{tcpclient}->{connections}->{$wheel_id}->{wheel}; # # # my $output = sprintf "%s gpiopoll %s", # POECLIENT_CMD_PREFIX, # lc $line; # # # $kernel->yield(tcpclient_output => # { # wheel_id => $wheel_id, # output => $output, # }); #} $kernel->call($session => wheelrun_send_network_reply => { output_per_target => $output_per_target, }); } 1;