160 lines
3.4 KiB
Perl
160 lines
3.4 KiB
Perl
package main;
|
|
use strict;
|
|
use warnings;
|
|
use Data::Dumper;
|
|
|
|
my $module_name = "SISPM_PLUG";
|
|
my $VERSION = "0.0.1";
|
|
|
|
sub SISPM_PLUG_Initialize
|
|
{
|
|
my ($hash) = @_;
|
|
|
|
$hash->{Match} = '^sispmplug\s[0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+:[0-9a-z]+\s\d\s\d';
|
|
|
|
$hash->{DefFn} = 'SISPM_PLUG_Define';
|
|
$hash->{UndefFn} = 'SISPM_PLUG_Undef';
|
|
$hash->{SetFn} = "SISPM_PLUG_Set";
|
|
$hash->{GetFn} = "SISPM_PLUG_Get";
|
|
$hash->{ParseFn} = "SISPM_PLUG_Parse";
|
|
}
|
|
|
|
sub SISPM_PLUG_Define
|
|
{
|
|
my ($hash, $def) = @_;
|
|
my @param = split('[\s]', $def);
|
|
|
|
if(int(@param) < 4)
|
|
{
|
|
return "too few parameters: define <name> SISPM_PLUG <serial> <plug>";
|
|
}
|
|
|
|
|
|
my $name = $param[0];
|
|
my $serial = $param[2];
|
|
my $plug = $param[3];
|
|
|
|
$hash->{NAME} = $name;
|
|
$hash->{PREV}->{STATE} = "undefined";
|
|
$hash->{SERIAL} = $serial;
|
|
$hash->{PLUG} = $plug;
|
|
|
|
|
|
my $devSerial = $serial;
|
|
$devSerial =~s/://g;
|
|
$devSerial .= $plug;
|
|
|
|
$modules{SISPM_PLUG}{defptr}{"SISPM_PLUG_".$devSerial} = $hash;
|
|
|
|
AssignIoPort($hash);
|
|
|
|
return;
|
|
}
|
|
|
|
sub SISPM_PLUG_Undef
|
|
{
|
|
my ( $hash, $name) = @_;
|
|
return;
|
|
}
|
|
|
|
sub SISPM_PLUG_Parse
|
|
{
|
|
my ($hash, $msg) = @_;
|
|
my ($dummy, $serial, $plug, $state) = split(' ', $msg);
|
|
my $devSerial = $serial;
|
|
$devSerial =~s/://g;
|
|
$devSerial .= $plug;
|
|
|
|
# in the message state is 0/1 but we want strings
|
|
$state = $state == 1 ? "on" : "off";
|
|
|
|
# fetch us the device hash for the specific plug
|
|
my $device = $modules{SISPM_PLUG}{defptr}{"SISPM_PLUG_".$devSerial};
|
|
|
|
#if the device already exist just update values
|
|
if ($device)
|
|
{
|
|
|
|
if($device->{STATE} ne $state) {
|
|
$device->{READINGS}{PREVSTATE}{TIME} = TimeNow();
|
|
$device->{READINGS}{PREVSTATE}{VAL} = $device->{STATE};
|
|
$device->{PREV}{STATE} = $device->{STATE};
|
|
$device->{CHANGED}[0] = $state;
|
|
DoTrigger($device->{NAME}, undef);
|
|
}
|
|
|
|
$device->{STATE} = $state;
|
|
$device->{READINGS}{state}{TIME} = TimeNow();
|
|
$device->{READINGS}{state}{VAL} = $state;
|
|
|
|
return $device->{NAME};
|
|
}
|
|
# if the device does not exist create it with autocreate
|
|
else
|
|
{
|
|
return "UNDEFINED SISPM_PLUG_$devSerial SISPM_PLUG $serial $plug";
|
|
}
|
|
|
|
}
|
|
|
|
sub SISPM_PLUG_Get
|
|
{
|
|
my ( $hash, $name, $opt, @args ) = @_;
|
|
|
|
return;
|
|
}
|
|
|
|
sub SISPM_PLUG_Set
|
|
{
|
|
my ( $hash, $name, $cmd, @args ) = @_;
|
|
my $processed = 0;
|
|
my $newstate = $hash->{STATE};
|
|
my $msg="";
|
|
|
|
# if no command is given
|
|
return "\"set $name\" needs at least one argument" unless(defined($cmd));
|
|
|
|
# set state and message according to command
|
|
if ($cmd eq "on")
|
|
{
|
|
$newstate = "on";
|
|
$processed = 1;
|
|
}
|
|
elsif($cmd eq "off")
|
|
{
|
|
$newstate = "off";
|
|
$processed = 1;
|
|
}
|
|
elsif($cmd eq "toggle")
|
|
{
|
|
$newstate = $hash->{STATE} eq "on" ? "off" : "on";
|
|
$processed = 1;
|
|
}
|
|
elsif($cmd eq "status")
|
|
{
|
|
$processed = 1;
|
|
}
|
|
|
|
if ($processed)
|
|
{
|
|
# update the state according to command because status update interval may be too slow
|
|
$hash->{READINGS}{PREVSTATE}{TIME} = TimeNow();
|
|
$hash->{READINGS}{PREVSTATE}{VAL} = $hash->{STATE};
|
|
$hash->{PREV}{STATE} = $hash->{STATE};
|
|
$hash->{CHANGED}[0] = $newstate;
|
|
$hash->{STATE} = $newstate;
|
|
$hash->{READINGS}{state}{TIME} = TimeNow();
|
|
$hash->{READINGS}{state}{VAL} = $newstate;
|
|
DoTrigger($hash->{NAME}, undef);
|
|
|
|
# send the message
|
|
IOWrite($hash, $cmd, $hash);
|
|
|
|
return;
|
|
}
|
|
|
|
return "Unknown argument $cmd, choose one of on off toggle status";
|
|
}
|
|
|
|
1;
|