package main; use strict; use warnings; my $module_name = "NEWSISPM"; my $VERSION = "0.0.1"; sub scanDevices { my $hash = shift; my $raw = readpipe($hash->{PATH} . " -s"); my @lines = split /\n/, $raw; my $deviceID = 0; for my $l (@lines) { # check for a new device if ($l =~ /^Gembird #(\d+)$/) { $deviceID = $1; } if ($l =~ /^serial\snumber:\s+([0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+)/) { $hash->{helper}->{devices}->{$deviceID}->{serial}=$1; $raw = readpipe($hash->{PATH} . " -d ". $deviceID . " -n -g all"); my @lines2 = split /\n/, $raw; for my $l2 (@lines2) { if ($l2 =~ /^Status\sof\soutlet\s(\d+):\s+(\d)/) { $hash->{helper}->{devices}->{$deviceID}->{plugs}->{$1}=$2; } } } } } sub NEWSISPM_GetUpdate { my ($hash) = @_; my $name = $hash->{NAME}; $hash->{STATE} = "running"; scanDevices($hash); my @devices = keys %{$hash->{helper}->{devices}}; $hash->{NR_BUS} = int(@devices); my $nr=0; for my $d (@devices) { my @plugs = keys %{$hash->{helper}->{devices}->{$d}->{plugs}}; $nr += int(@plugs); for my $p (@plugs) { my $msg = "sispmplug " . $hash->{helper}->{devices}->{$d}->{serial} . " " . $p . " " . $hash->{helper}->{devices}->{$d}->{plugs}->{$p}; my %vals; Dispatch($hash, $msg, \%vals); } } $hash->{NR_PLUGS} = $nr; InternalTimer(gettimeofday()+$hash->{Interval}, "NEWSISPM_GetUpdate", $hash); } sub NEWSISPM_Initialize { my ($hash) = @_; $hash->{Clients} = ":SISPM_PLUG:"; my %ml = ( "1:SISPM_PLUG" => '^sispmplug\s[0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+\s\d\s\d', ); $hash->{MatchList} = \%ml; $hash->{DefFn} = 'NEWSISPM_Define'; $hash->{UndefFn} = 'NEWSISPM_Undef'; $hash->{DeleteFn} = "NEWSISPM_Delete"; $hash->{SetFn} = "NEWSISPM_Set"; $hash->{GetFn} = "NEWSISPM_Get"; $hash->{AttrFn} = "NEWSISPM_Attr"; $hash->{WriteFn} = "NEWSISPM_Write"; $hash->{AttrList} = "Interval " . $readingFnAttributes; } sub NEWSISPM_Define { my ($hash, $def) = @_; my @param = split('[\s]', $def); if(int(@param) < 3) { return "too few parameters: define NEWSISPM "; } my $name = $param[0]; my $sispmctl = $param[2]; $hash->{NAME} = $name; $hash->{STATE} = "unknown"; $hash->{PATH} = $sispmctl; $hash->{NR_BUS} = 0; $hash->{NR_PLUGS} = 0; $hash->{Interval} = 30; InternalTimer(gettimeofday()+2, "NEWSISPM_GetUpdate", $hash); return; } sub NEWSISPM_Write { my ( $hash, $cmd, $dev) = @_; my $serial = $dev->{SERIAL}; my $plug = $dev->{PLUG}; if ($cmd eq "on") { readpipe($hash->{PATH} . " -D " . $serial . " -o " . $plug); } elsif ($cmd eq "off") { readpipe($hash->{PATH} . " -D " . $serial . " -f " . $plug); } elsif ($cmd eq "toggle") { if ($dev->{STATE} eq "on") { readpipe($hash->{PATH} . " -D " . $serial . " -o " . $plug); } else { readpipe($hash->{PATH} . " -D " . $serial . " -f " . $plug); } } elsif ($cmd eq "status") { RemoveInternalTimer($hash); InternalTimer(gettimeofday()+1, "NEWSISPM_GetUpdate", $hash); } return; } sub NEWSISPM_Undef { my ( $hash, $name) = @_; RemoveInternalTimer($hash); return; } sub NEWSISPM_Delete { my ( $hash, $name) = @_; return; } sub NEWSISPM_Get { my ( $hash, $name, $opt, @args ) = @_; return; } sub NEWSISPM_Set { my ( $hash, $name, $cmd, @args ) = @_; return undef; } sub NEWSISPM_Attr(@) { my ( $cmd, $name, $attrName, $attrValue ) = @_; return undef; }