############################################## # $Id$ # # based / modified Version 98_EGPMS2LAN from ericl # # (c) 2013 - 2017 Copyright: Alex Storny (moselking at arcor dot de) # All rights reserved # # This script free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # any later version. # # The GNU General Public License can be found at # http://www.gnu.org/copyleft/gpl.html. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # ################################################################ # -> Module 70_EGPM.pm (for a single Socket) needed. ################################################################ package main; use strict; use warnings; use HttpUtils; sub EGPM2LAN_Initialize($) { my ($hash) = @_; $hash->{Clients} = ":EGPM:"; $hash->{GetFn} = "EGPM2LAN_Get"; $hash->{SetFn} = "EGPM2LAN_Set"; $hash->{DefFn} = "EGPM2LAN_Define"; $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6 stateDisplay:sockNumber,sockName autocreate:on,off"; } ################################### sub EGPM2LAN_Get($@) { my ($hash, @a) = @_; my $getcommand; return "argument is missing" if(int(@a) != 2); $getcommand = $a[1]; if($getcommand eq "state") { if(defined($hash->{STATE})) { return $hash->{STATE}; } } elsif($getcommand eq "lastcommand") { if(defined($hash->{READINGS}{lastcommand}{VAL})) { return $hash->{READINGS}{lastcommand}{VAL}; } } else { return "Unknown argument $getcommand, choose one of state:noArg lastcommand:noArg".(exists($hash->{READINGS}{output})?" output:noArg":""); } return ""; } ################################### sub EGPM2LAN_Set($@) { my ($hash, @a) = @_; return "no set value specified" if(int(@a) < 2); return "Unknown argument $a[1], choose one of on:1,2,3,4,all off:1,2,3,4,all toggle:1,2,3,4 clearreadings:noArg statusrequest:noArg password" if($a[1] eq "?"); my $name = shift @a; my $setcommand = shift @a; my $params = join(" ", @a); my $logLevel = GetLogLevel($name,4); Log $logLevel, "EGPM2LAN set $name (". $hash->{IP}. ") $setcommand $params"; EGPM2LAN_Login($hash, $logLevel); if($setcommand eq "on" || $setcommand eq "off") { if($params eq "all") { #switch all Sockets; thanks to eric! for (my $count = 1; $count <= 4; $count++) { EGPM2LAN_Switch($hash, $setcommand, $count, $logLevel); } } else { #switch single Socket EGPM2LAN_Switch($hash, $setcommand, $params, $logLevel); } EGPM2LAN_Statusrequest($hash, $logLevel, 1); } elsif($setcommand eq "toggle") { my $currentstate = EGPM2LAN_Statusrequest($hash, $logLevel, 1); if(defined($currentstate)) { my @powerstates = split(",", $currentstate); my $newcommand="off"; if($powerstates[$params-1] eq "0") { $newcommand="on"; } EGPM2LAN_Switch($hash, $newcommand, $params, $logLevel); EGPM2LAN_Statusrequest($hash, $logLevel, 0); } } elsif($setcommand eq "statusrequest") { EGPM2LAN_Statusrequest($hash, $logLevel, 1); } elsif($setcommand eq "password") { delete $hash->{PASSWORD} if($params eq "" && defined($hash->{PASSWORD})); EGPM2LAN_StorePassword($hash, $params); } elsif($setcommand eq "clearreadings") { delete $hash->{READINGS}; } else { return "unknown argument $setcommand, choose one of on, off, toggle, statusrequest, clearreadings"; } EGPM2LAN_Logoff($hash, $logLevel); $hash->{CHANGED}[0] = $setcommand; $hash->{READINGS}{lastcommand}{TIME} = TimeNow(); $hash->{READINGS}{lastcommand}{VAL} = $setcommand." ".$params; return undef; } ################################ sub EGPM2LAN_StorePassword($$) { my ($hash, $password) = @_; my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd"; my $key = getUniqueId().$index; my $enc_pwd = ""; if(eval "use Digest::MD5;1") { $key = Digest::MD5::md5_hex(unpack "H*", $key); $key .= Digest::MD5::md5_hex($key); } for my $char (split //, $password) { my $encode=chop($key); $enc_pwd.=sprintf("%.2x",ord($char)^ord($encode)); $key=$encode.$key; } Log 3, "EGPM2LAN write password to file uniqueID"; my $err = setKeyValue($index, $enc_pwd); if(defined($err)){ #Fallback, if file is not available $hash->{PASSWORD}=$password; return "EGPM2LAN write Password failed!"; } $hash->{PASSWORD}="***"; return "Password saved."; } ################################ sub EGPM2LAN_ReadPassword($) { my ($hash) = @_; #password available? return undef if (!defined($hash->{PASSWORD})); #for old installations/fallback if($hash->{PASSWORD} ne "***"){ return $hash->{PASSWORD}; } my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd"; my $key = getUniqueId().$index; my ($password, $err); Log 3, "EGPM2LAN Read password from file uniqueID"; ($err, $password) = getKeyValue($index); if ( defined($err) ) { Log 1, "EGPM2LAN unable to read password from file: $err"; return undef; } if (defined($password) ) { if ( eval "use Digest::MD5;1" ) { $key = Digest::MD5::md5_hex(unpack "H*", $key); $key .= Digest::MD5::md5_hex($key); } my $dec_pwd = ''; for my $char (map { pack('C', hex($_)) } ($password =~ /(..)/g)) { my $decode=chop($key); $dec_pwd.=chr(ord($char)^ord($decode)); $key=$decode.$key; } return $dec_pwd; } else { Log 1, "EGPM2LAN No password in file"; return undef; } } ################################ sub EGPM2LAN_Switch($$$$) { my ($hash, $state, $port, $logLevel) = @_; $state = ($state eq "on" ? "1" : "0"); my $fritz = 0; #may be important for FritzBox-users my $data = "cte1=" . ($port == "1" ? $state : "") . "&cte2=" . ($port == "2" ? $state : "") . "&cte3=" . ($port == "3" ? $state : "") . "&cte4=". ($port == "4" ? $state : ""); Log $logLevel, "EGPM2LAN $data"; eval { # Parameter: $url, $timeout, $data, $noshutdown, $loglevel GetFileFromURL("http://".$hash->{IP}."/", 5,$data ,$fritz ,$logLevel); }; if ($@){ ### catch block Log $logLevel, "EGPM2LAN error: $@"; }; return 1; } ################################ sub EGPM2LAN_Login($$) { my ($hash, $logLevel) = @_; Log $logLevel,"EGPM2LAN try to Login @".$hash->{IP}; my $passwd = EGPM2LAN_ReadPassword($hash); eval{ GetFileFromURLQuiet("http://".$hash->{IP}."/login.html", 5,"pw=" .(defined($passwd) ? $passwd : ""),0 ,$logLevel); }; if ($@){ ### catch block Log 1, "EGPM2LAN Login error: $@"; return 0; }; Log $logLevel,"EGPM2LAN Login successful!"; return 1; } ################################ sub EGPM2LAN_GetDeviceInfo($$) { my ($hash, $input) = @_; my $logLevel = GetLogLevel($hash->{NAME},4); #try to read Device Name my ($devicename) = $input =~ m/
define <name> EGPM2LAN <IP-Address>
set <name> password [<one-word>]
set <name> <[on|off|toggle]> <socketnr.>
set <name> <[on|off]> <all>
set <name> <staterequest>
set <name> <clearreadings>
get <name> state
define mainswitch EGPM2LAN 10.192.192.20
set mainswitch password SecretGarden
set mainswitch on 1
define <name> EGPM2LAN <IP-Address>
set <name> <[on|off|toggle]> <socketnr.>
set <name> <[on|off]> <all>
set <name> password [<mein-passwort>]
set <name> <staterequest>
set <name> <clearreadings>
get <name> state
define sleiste EGPM2LAN 10.192.192.20
set sleiste password SecretGarden
set sleiste on 1