package POEDaemon::WheelRun::GPIOWS2801; use strict; use warnings FATAL => 'all'; no warnings 'redefine'; use POE; use POEDaemon; sub states { return $_[0], [qw( gpiows2801_start gpiows2801_stop gpiows2801_init gpiows2801_restart gpiows2801_childexit gpiows2801_stderr_line gpiows2801 )]; } sub gpiows2801_start { my $kernel = $_[KERNEL]; $kernel->yield('gpiows2801_init'); } sub gpiows2801_stop { my $kernel = $_[KERNEL]; $kernel->yield(wheelrun_kill => { wheel_name => 'gpiows2801' }); } sub gpiows2801_init { my ($kernel, $heap) = @_[KERNEL, HEAP]; log_enabled && logline 'event fired'; my @pins; if ($heap->{tcpclient_dynamic_gpio_pins}->{out_ws2801} && ref $heap->{tcpclient_dynamic_gpio_pins}->{out_ws2801} eq 'ARRAY') { @pins = @{$heap->{tcpclient_dynamic_gpio_pins}->{out_ws2801}}; } elsif (cfg->{ws2801_gpio_pins} && ref cfg->{ws2801_gpio_pins} eq 'ARRAY') { @pins = @{cfg->{ws2801_gpio_pins}}; } return unless $#pins == 1; log_enabled && logline 'wheelrun_exec'; $kernel->yield(wheelrun_exec => { prog => [ 'util/gpio.pl', 'ws2801', join(',', @pins) ], name => 'gpiows2801', stderr_event => 'gpiows2801_stderr_line', }); } sub gpiows2801_restart { my ($kernel, $heap) = @_[KERNEL, HEAP]; log_enabled && logline 'event fired'; if ($heap->{wheel_run}->{children_by_name}->{gpiows2801}) { $heap->{wheelrun_gpiows2801_restartmode} = 1; $kernel->yield('gpiows2801_stop'); } else { $kernel->yield('gpiows2801_init'); } } sub gpiows2801_childexit { my ($kernel, $heap) = @_[KERNEL, HEAP]; if ($heap->{wheelrun_gpiows2801_restartmode}) { delete $heap->{wheelrun_gpiows2801_restartmode}; $kernel->yield('gpiows2801_init'); } else { unless ($heap->{shutdown}) { $kernel->yield(shutdown => 'gpiows2801 child abnormally terminated'); } } } sub gpiows2801_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 "GPIOWS2801 %s", $line; $output_per_target->{tcpclient} = sprintf "%s gpiows2801 %s", POECLIENT_CMD_PREFIX, $line; #my $output = sprintf "%s gpiows2801 %s", # POECLIENT_CMD_PREFIX, # lc $line; # # #foreach my $wheel_id (keys %{$heap->{tcpclient}->{connections}}) #{ # my $client = $heap->{tcpclient}->{connections}->{$wheel_id}; # # # next unless $client; # # # next unless $client->{wheel}; # # # $kernel->yield(tcpclient_output => # { # wheel_id => $wheel_id, # output => $output, # }); #} $kernel->call($session => wheelrun_send_network_reply => { output_per_target => $output_per_target, }); } sub gpiows2801 { my ($heap, $data) = @_[HEAP, ARG0]; unless (defined $data) { log_enabled && logline 'error, data is not defined'; return; } my $wheel = $heap->{wheel_run}->{children_by_name}->{gpiows2801}->{wheel}; return unless $wheel; #log_enabled && logline "put '%s'", $data; $wheel->put($data); } 1;