Skip to content

Instantly share code, notes, and snippets.

@keshavsaharia
Last active November 27, 2021 17:55
Show Gist options
  • Save keshavsaharia/5894067 to your computer and use it in GitHub Desktop.
Save keshavsaharia/5894067 to your computer and use it in GitHub Desktop.
PERL script for communicating with the Digital Loggers Web Power Switch that uses a simple decomposition-based natural language parser.
my $cmd = "";
my $base = "/path/to/dlogger.pl"; # Change this to the path where the provided PERL utility is kept
my $auth = " admin:1234"; # Change this to the authentication you use to log in to your switch
my $status = "";
my $buffer = "";
my $temp = "";
my @outlets;
my @outletstatus;
my %toggle = ("on" => "off", "off" => "on");
push(@cmd, $_) foreach (@ARGV);
foreach (@cmd) {
chop if (substr($_, -1, 1) eq "s"); # remove plurality of words (careful with words ending with s)
next if ($_ =~ m/^(to|the|my|and|switch|turn)$/);
if ($_ =~ m/^(on|off)$/i) {
$status = $_;
}
elsif ($_ =~ m/^\d+$/i) {
if (int($_) >= 1 and int($_) <= 8) {
$cmd1 = $cmd1." ".$_.$status;
}
if (int($_) > 8 and int($_) <= 16) {
$cmd2 = $cmd2." ".(int($_)-8).$status;
}
}
else {
$buffer = $buffer.$_;
if (exists $commands{$buffer}) {
@outlets = split(/ /,$commands{$buffer});
push(@cmd, $status);
push(@cmd, $_) foreach (@outlets);
$buffer = "";
}
}
}
print "$buffer\n" if ($buffer ne "");
system("perl $base 192.168.0.100 $auth $cmd1") if ($cmd1 ne "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment