17_EGPM2LAN.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. ##############################################
  2. # $Id: 17_EGPM2LAN.pm 14071 2017-04-22 12:13:43Z alexus $
  3. #
  4. # based / modified Version 98_EGPMS2LAN from ericl
  5. #
  6. # (c) 2013 - 2017 Copyright: Alex Storny (moselking at arcor dot de)
  7. # All rights reserved
  8. #
  9. # This script free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # any later version.
  13. #
  14. # The GNU General Public License can be found at
  15. # http://www.gnu.org/copyleft/gpl.html.
  16. #
  17. # This script is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. ################################################################
  23. # -> Module 70_EGPM.pm (for a single Socket) needed.
  24. ################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use HttpUtils;
  29. sub
  30. EGPM2LAN_Initialize($)
  31. {
  32. my ($hash) = @_;
  33. $hash->{Clients} = ":EGPM:";
  34. $hash->{GetFn} = "EGPM2LAN_Get";
  35. $hash->{SetFn} = "EGPM2LAN_Set";
  36. $hash->{DefFn} = "EGPM2LAN_Define";
  37. $hash->{AttrList} = "stateDisplay:sockNumber,sockName autocreate:on,off";
  38. }
  39. ###################################
  40. sub
  41. EGPM2LAN_Get($@)
  42. {
  43. my ($hash, @a) = @_;
  44. my $getcommand;
  45. return "argument is missing" if(int(@a) != 2);
  46. $getcommand = $a[1];
  47. if($getcommand eq "state")
  48. {
  49. if(defined($hash->{STATE})) {
  50. return $hash->{STATE}; }
  51. }
  52. elsif($getcommand eq "lastcommand")
  53. {
  54. if(defined($hash->{READINGS}{lastcommand}{VAL})) {
  55. return $hash->{READINGS}{lastcommand}{VAL}; }
  56. }
  57. else
  58. {
  59. return "Unknown argument $getcommand, choose one of state:noArg lastcommand:noArg".(exists($hash->{READINGS}{output})?" output:noArg":"");
  60. }
  61. return "";
  62. }
  63. ###################################
  64. sub
  65. EGPM2LAN_Set($@)
  66. {
  67. my ($hash, @a) = @_;
  68. return "no set value specified" if(int(@a) < 2);
  69. 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 "?");
  70. my $name = shift @a;
  71. my $setcommand = shift @a;
  72. my $params = join(" ", @a);
  73. Log3 "EGPM2LAN", 4, "set $name (". $hash->{IP}. ") $setcommand $params";
  74. EGPM2LAN_Login($hash);
  75. if($setcommand eq "on" || $setcommand eq "off")
  76. {
  77. if($params eq "all")
  78. { #switch all Sockets; thanks to eric!
  79. for (my $count = 1; $count <= 4; $count++)
  80. {
  81. EGPM2LAN_Switch($hash, $setcommand, $count);
  82. }
  83. }
  84. else
  85. { #switch single Socket
  86. EGPM2LAN_Switch($hash, $setcommand, $params);
  87. }
  88. EGPM2LAN_Statusrequest($hash, 1);
  89. }
  90. elsif($setcommand eq "toggle")
  91. {
  92. my $currentstate = EGPM2LAN_Statusrequest($hash, 1);
  93. if(defined($currentstate))
  94. {
  95. my @powerstates = split(",", $currentstate);
  96. my $newcommand="off";
  97. if($powerstates[$params-1] eq "0")
  98. {
  99. $newcommand="on";
  100. }
  101. EGPM2LAN_Switch($hash, $newcommand, $params);
  102. EGPM2LAN_Statusrequest($hash, 0);
  103. }
  104. }
  105. elsif($setcommand eq "statusrequest")
  106. {
  107. EGPM2LAN_Statusrequest($hash, 1);
  108. }
  109. elsif($setcommand eq "password")
  110. {
  111. my $result = EGPM2LAN_StorePassword($hash, $params);
  112. Log3 "EGPM2LAN", 1,$result;
  113. if($params eq ""){
  114. delete $hash->{PASSWORD} if(defined($hash->{PASSWORD}));
  115. } else {
  116. $params="***";
  117. }
  118. }
  119. elsif($setcommand eq "clearreadings")
  120. {
  121. delete $hash->{READINGS};
  122. }
  123. else
  124. {
  125. return "unknown argument $setcommand, choose one of on, off, toggle, statusrequest, clearreadings";
  126. }
  127. EGPM2LAN_Logoff($hash);
  128. $hash->{CHANGED}[0] = $setcommand;
  129. $hash->{READINGS}{lastcommand}{TIME} = TimeNow();
  130. $hash->{READINGS}{lastcommand}{VAL} = $setcommand." ".$params;
  131. return undef;
  132. }
  133. ################################
  134. sub EGPM2LAN_StorePassword($$)
  135. {
  136. my ($hash, $password) = @_;
  137. my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd";
  138. my $key = getUniqueId().$index;
  139. my $enc_pwd = "";
  140. if(eval "use Digest::MD5;1")
  141. {
  142. $key = Digest::MD5::md5_hex(unpack "H*", $key);
  143. $key .= Digest::MD5::md5_hex($key);
  144. }
  145. for my $char (split //, $password)
  146. {
  147. my $encode=chop($key);
  148. $enc_pwd.=sprintf("%.2x",ord($char)^ord($encode));
  149. $key=$encode.$key;
  150. }
  151. Log3 "EGPM2LAN", 4, "write password to file uniqueID";
  152. my $err = setKeyValue($index, $enc_pwd);
  153. if(defined($err)){
  154. #Fallback, if file is not available
  155. $hash->{PASSWORD}=$password;
  156. return "EGPM2LAN: Write Password failed!";
  157. }
  158. $hash->{PASSWORD}="***" if($password ne "");
  159. return "EGPM2LAN: Password saved.";
  160. }
  161. ################################
  162. sub EGPM2LAN_ReadPassword($)
  163. {
  164. my ($hash) = @_;
  165. #for old installations/fallback to clear-text PWD
  166. if(defined($hash->{PASSWORD}) && $hash->{PASSWORD} ne "***"){
  167. return $hash->{PASSWORD};
  168. }
  169. my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd";
  170. my $key = getUniqueId().$index;
  171. my ($password, $err);
  172. Log3 "EGPM2LAN", 3, "Read password from file uniqueID";
  173. ($err, $password) = getKeyValue($index);
  174. if ( defined($err) ) {
  175. Log3 "EGPM2LAN",0, "unable to read password from file: $err";
  176. return undef;
  177. }
  178. if (defined($password) ) {
  179. if ( eval "use Digest::MD5;1" ) {
  180. $key = Digest::MD5::md5_hex(unpack "H*", $key);
  181. $key .= Digest::MD5::md5_hex($key);
  182. }
  183. my $dec_pwd = '';
  184. for my $char (map { pack('C', hex($_)) } ($password =~ /(..)/g)) {
  185. my $decode=chop($key);
  186. $dec_pwd.=chr(ord($char)^ord($decode));
  187. $key=$decode.$key;
  188. }
  189. $hash->{PASSWORD}="***";
  190. return $dec_pwd;
  191. }
  192. else {
  193. Log3 "EGPM2LAN",4 ,"No password in file";
  194. return "";
  195. }
  196. }
  197. ################################
  198. sub EGPM2LAN_Switch($$$) {
  199. my ($hash, $state, $port) = @_;
  200. $state = ($state eq "on" ? "1" : "0");
  201. my $fritz = 0; #may be important for FritzBox-users
  202. my $data = "cte1=" . ($port == "1" ? $state : "") . "&cte2=" . ($port == "2" ? $state : "") . "&cte3=" . ($port == "3" ? $state : "") . "&cte4=". ($port == "4" ? $state : "");
  203. Log3 "EGPM2LAN",5 , $data;
  204. eval {
  205. # Parameter: $url, $timeout, $data, $noshutdown, $loglevel
  206. GetFileFromURL("http://".$hash->{IP}."/", 5,$data ,$fritz);
  207. };
  208. if ($@){
  209. ### catch block
  210. Log3 "EGPM2LAN", 1 ,"error: $@";
  211. };
  212. return 1;
  213. }
  214. ################################
  215. sub EGPM2LAN_Login($) {
  216. my ($hash) = @_;
  217. my $name = $hash->{NAME};
  218. my $passwd = EGPM2LAN_ReadPassword($hash);
  219. Log3 $name,4 , "EGPM2LAN: try to connect ".$hash->{IP};
  220. eval{
  221. GetFileFromURLQuiet("http://".$hash->{IP}."/login.html", 5,"pw=" .(defined($passwd) ? $passwd : ""),0 );
  222. };
  223. if ($@){
  224. ### catch block
  225. Log3 $name, 0, "EGPM2LAN Login error: $@";
  226. return 0;
  227. };
  228. return 1;
  229. }
  230. ################################
  231. sub EGPM2LAN_GetDeviceInfo($$) {
  232. my ($hash, $input) = @_;
  233. #try to read Device Name
  234. my ($devicename) = $input =~ m/<h2>(.+)<\/h2><\/div>/si;
  235. $hash->{DEVICENAME} = trim($devicename);
  236. #try to read Socket Names
  237. my @socketlist;
  238. while ($input =~ m/<h2 class=\"ener\">(.+?)<\/h2>/gi)
  239. {
  240. my $socketname = trim($1);
  241. $socketname =~ s/ /_/g; #remove spaces
  242. push(@socketlist, $socketname);
  243. }
  244. #check 4 dublicate Names
  245. my %seen;
  246. foreach my $entry (@socketlist)
  247. {
  248. next unless $seen{$entry}++;
  249. Log3 "EGPM2LAN", 1, "Sorry! Can't use devicenames. ".trim($entry)." is duplicated.";
  250. @socketlist = qw(Socket_1 Socket_2 Socket_3 Socket_4);
  251. }
  252. if(int(@socketlist) < 4)
  253. {
  254. @socketlist = qw(Socket_1 Socket_2 Socket_3 Socket_4);
  255. }
  256. return @socketlist;
  257. }
  258. ################################
  259. sub EGPM2LAN_Statusrequest($$) {
  260. my ($hash, $autoCr) = @_;
  261. my $name = $hash->{NAME};
  262. my $response = GetFileFromURL("http://".$hash->{IP}."/", 5,"" , 0);
  263. if(not defined($response)){
  264. Log3 $name, 0, "EGPM2LAN: Cant connect to ".$hash->{IP};
  265. $hash->{STATE} = "Connection failed";
  266. return 0
  267. }
  268. Log3 $name, 5, "EGPM2LAN: $response";
  269. if($response =~ /.,.,.,./)
  270. {
  271. my $powerstatestring = $&;
  272. Log3 $name, 2, "EGPM2LAN Powerstate: " . $powerstatestring;
  273. my @powerstates = split(",", $powerstatestring);
  274. if(int(@powerstates) == 4)
  275. {
  276. my $index;
  277. my $newstatestring;
  278. my @socketlist = EGPM2LAN_GetDeviceInfo($hash,$response);
  279. readingsBeginUpdate($hash);
  280. foreach my $powerstate (@powerstates)
  281. {
  282. $index++;
  283. if(length(trim($socketlist[$index-1]))==0)
  284. {
  285. $socketlist[$index-1]="Socket_".$index;
  286. }
  287. if(AttrVal($name, "stateDisplay", "sockNumber") eq "sockName") {
  288. $newstatestring .= $socketlist[$index-1].": ".($powerstates[$index-1] ? "on" : "off")." ";
  289. } else {
  290. $newstatestring .= $index.": ".($powerstates[$index-1] ? "on" : "off")." ";
  291. }
  292. #Create Socket-Object if not available
  293. my $defptr = $modules{EGPM}{defptr}{$name.$index};
  294. if($autoCr && AttrVal($name, "autocreate", "on") eq "on" && not defined($defptr))
  295. {
  296. if(Value("autocreate") eq "active")
  297. {
  298. Log3 $name, 1, "EGPM2LAN: Autocreate EGPM for Socket $index";
  299. CommandDefine(undef, $name."_".$socketlist[$index-1]." EGPM $name $index");
  300. }
  301. else
  302. {
  303. Log3 $name, 2, "EGPM2LAN: Autocreate disabled in globals section";
  304. $attr{$name}{autocreate} = "off";
  305. }
  306. }
  307. #Write state 2 related Socket-Object
  308. if (defined($defptr))
  309. {
  310. if (ReadingsVal($defptr->{NAME},"state","") ne ($powerstates[$index-1] ? "on" : "off"))
  311. { #check for chages and update -> trigger event
  312. Log3 $name, 3, "EGPM2LAN: Update State of ".$defptr->{NAME};
  313. readingsSingleUpdate($defptr, "state", ($powerstates[$index-1] ? "on" : "off") ,1);
  314. }
  315. $defptr->{DEVICENAME} = $hash->{DEVICENAME};
  316. $defptr->{SOCKETNAME} = $socketlist[$index-1];
  317. }
  318. readingsBulkUpdate($hash, $index."_".$socketlist[$index-1], ($powerstates[$index-1] ? "on" : "off"));
  319. }
  320. readingsBulkUpdate($hash, "state", $newstatestring);
  321. readingsEndUpdate($hash, 0);
  322. #everything is fine
  323. return $powerstatestring;
  324. }
  325. else
  326. {
  327. Log3 $name, 0,"EGPM2LAN: Failed to parse powerstate";
  328. }
  329. }
  330. else
  331. {
  332. $hash->{STATE} = "Login failed";
  333. Log3 $name, 0,"EGPM2LAN: Login failed";
  334. }
  335. #something went wrong :-(
  336. return undef;
  337. }
  338. ################################
  339. sub EGPM2LAN_Logoff($) {
  340. my ($hash) = @_;
  341. GetFileFromURL("http://".$hash->{IP}."/login.html", 5,"" ,0 ,3);
  342. return 1;
  343. }
  344. ################################
  345. sub EGPM2LAN_Define($$)
  346. {
  347. my ($hash, $def) = @_;
  348. my @a = split("[ \t][ \t]*", $def);
  349. my $u = "wrong syntax: define <name> EGPM2LAN IP [Password]";
  350. return $u if(int(@a) < 2);
  351. $hash->{IP} = $a[2];
  352. if(int(@a) == 4)
  353. {
  354. EGPM2LAN_StorePassword($hash, $a[3]);
  355. $hash->{DEF} = $a[2]; ## remove password
  356. }
  357. my $result = EGPM2LAN_Login($hash);
  358. if($result == 1)
  359. {
  360. $hash->{STATE} = "initialized";
  361. EGPM2LAN_Statusrequest($hash,0);
  362. EGPM2LAN_Logoff($hash);
  363. }
  364. return undef;
  365. }
  366. 1;
  367. =pod
  368. =item device
  369. =item summary controls a LAN-Socket device from Gembird
  370. =item summary_DE steuert eine LAN-Steckdosenleiste von Gembird
  371. =begin html
  372. <a name="EGPM2LAN"></a>
  373. <h3>EGPM2LAN</h3>
  374. <ul>
  375. <br>
  376. <a name="EGPM2LANdefine"></a>
  377. <b>Define</b>
  378. <ul>
  379. <code>define &lt;name&gt; EGPM2LAN &lt;IP-Address&gt;</code><br>
  380. <br>
  381. Creates a Gembird &reg; <a href="http://energenie.com/item.aspx?id=7557" >Energenie EG-PM2-LAN</a> device to switch up to 4 sockets over the network.
  382. If you have more than one device, it is helpful to connect and set names for your sockets over the web-interface first.
  383. The name settings will be adopted to FHEM and helps you to identify the sockets. Please make sure that you&acute;re logged off from the Energenie web-interface otherwise you can&acute;t control it with FHEM at the same time.<br>
  384. Create a <a href="#EGPM">EGPM-Module</a> to control a single socket with additional features.<br>
  385. <b>EG-PMS2-LAN with surge protector feature was not tested until now.</b>
  386. </ul><br>
  387. <a name="EGPM2LANset"></a>
  388. <b>Set</b>
  389. <ul>
  390. <code>set &lt;name&gt; password [&lt;one-word&gt;]</code><br>
  391. Encrypt and store device-password in FHEM. Leave empty to remove the password.<br>
  392. Before 04/2017, the password was stored in clear-text using the DEFINE-Command, but it should not be stored in the config-file.<br>
  393. <br>
  394. <code>set &lt;name&gt; &lt;[on|off|toggle]&gt &lt;socketnr.&gt;</code><br>
  395. Switch the socket on or off.<br>
  396. <br>
  397. <code>set &lt;name&gt; &lt;[on|off]&gt &lt;all&gt;</code><br>
  398. Switch all available sockets on or off.<br>
  399. <br>
  400. <code>set &lt;name&gt; &lt;staterequest&gt;</code><br>
  401. Update the device information and the state of all sockets.<br>
  402. If <a href="#autocreate">autocreate</a> is enabled, an <a href="#EGPM">EGPM</a> device will be created for each socket.<br>
  403. <br>
  404. <code>set &lt;name&gt; &lt;clearreadings&gt;</code><br>
  405. Removes all readings from the list to get rid of old socketnames.
  406. </ul>
  407. <br>
  408. <a name="EGPM2LANget"></a>
  409. <b>Get</b>
  410. <ul><code>get &lt;name&gt; state</code><br>
  411. Returns a text like this: "1: off 2: on 3: off 4: off" or the last error-message if something went wrong.<br>
  412. </ul><br>
  413. <a name="EGPM2LANattr"></a>
  414. <b>Attributes</b>
  415. <ul>
  416. <li>stateDisplay</li>
  417. Default: <b>socketNumer</b> changes between <b>socketNumer</b> and <b>socketName</b> in front of the current state. Call <b>set statusrequest</b> to update all states.
  418. <li>autocreate</li>
  419. Default: <b>on</b> <a href="#EGPM">EGPM</a>-devices will be created automatically with a <b>set</b>-command.
  420. Change this attribute to value <b>off</b> to avoid that mechanism.
  421. <li><a href="#loglevel">loglevel</a></li>
  422. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  423. </ul>
  424. <br>
  425. <br>
  426. <br>
  427. Example:
  428. <ul>
  429. <code>define mainswitch EGPM2LAN 10.192.192.20</code><br>
  430. <code>set mainswitch password SecretGarden</code><br>
  431. <code>set mainswitch on 1</code><br>
  432. </ul>
  433. </ul>
  434. =end html
  435. =begin html_DE
  436. <a name="EGPM2LAN"></a>
  437. <h3>EGPM2LAN</h3>
  438. <ul>
  439. <br>
  440. <a name="EGPM2LANdefine"></a>
  441. <b>Define</b>
  442. <ul>
  443. <code>define &lt;name&gt; EGPM2LAN &lt;IP-Address&gt;</code><br>
  444. <br>
  445. Das Modul erstellt eine Verbindung zu einer Gembird &reg; <a href="http://energenie.com/item.aspx?id=7557" >Energenie EG-PM2-LAN</a> Steckdosenleiste und steuert 4 angeschlossene Ger&auml;te..
  446. Falls mehrere Steckdosenleisten &uuml;ber das Netzwerk gesteuert werden, ist es ratsam, diese zuerst &uuml;ber die Web-Oberfl&auml;che zu konfigurieren und die einzelnen Steckdosen zu benennen. Die Namen werden dann automatisch in die
  447. Oberfl&auml;che von FHEM &uuml;bernommen. Bitte darauf achten, die Weboberfl&auml;che mit <i>Logoff</i> wieder zu verlassen, da der Zugriff sonst blockiert wird.
  448. </ul><br>
  449. <a name="EGPM2LANset"></a>
  450. <b>Set</b>
  451. <ul>
  452. <code>set &lt;name&gt; &lt;[on|off|toggle]&gt &lt;socketnr.&gt;</code><br>
  453. Schaltet die gew&auml;hlte Steckdose ein oder aus.<br>
  454. <br>
  455. <code>set &lt;name&gt; &lt;[on|off]&gt &lt;all&gt;</code><br>
  456. Schaltet alle Steckdosen gleichzeitig ein oder aus.<br>
  457. <br>
  458. <code>set &lt;name&gt; password [&lt;mein-passwort&gt;]</code><br>
  459. Speichert das Passwort verschl&uuml;sselt in FHEM ab. Zum Entfernen eines vorhandenen Passworts den Befehl ohne Parameter aufrufen.<br>
  460. Vor 04/2017 wurde das Passwort im Klartext gespeichert und mit dem DEFINE-Command &uuml;bergeben.<br>
  461. <br>
  462. <code>set &lt;name&gt; &lt;staterequest&gt;</code><br>
  463. Aktualisiert die Statusinformation der Steckdosenleiste.<br>
  464. Wenn das globale Attribut <a href="#autocreate">autocreate</a> aktiviert ist, wird f&uuml;r jede Steckdose ein <a href="#EGPM">EGPM</a>-Eintrag erstellt.<br>
  465. <br>
  466. <code>set &lt;name&gt; &lt;clearreadings&gt;</code><br>
  467. L&ouml;scht alle ung&uuml;ltigen Eintr&auml;ge im Abschnitt &lt;readings&gt;.
  468. </ul>
  469. <br>
  470. <a name="EGPM2LANget"></a>
  471. <b>Get</b>
  472. <ul><code>get &lt;name&gt; state</code><br>
  473. Gibt einen Text in diesem Format aus: "1: off 2: on 3: off 4: off" oder enth&auml;lt die letzte Fehlermeldung.<br>
  474. </ul><br>
  475. <a name="EGPM2LANattr"></a>
  476. <b>Attribute</b>
  477. <ul>
  478. <li>stateDisplay</li>
  479. Default: <b>socketNumer</b> wechselt zwischen <b>socketNumer</b> and <b>socketName</b> f&uuml;r jeden Statuseintrag. Verwende <b>set statusrequest</b>, um die Anzeige zu aktualisieren.
  480. <li>autocreate</li>
  481. Default: <b>on</b> <a href="#EGPM">EGPM</a>-Eintr&auml;ge werden automatisch mit dem <b>set</b>-command erstellt.
  482. <li><a href="#loglevel">loglevel</a></li>
  483. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  484. </ul>
  485. <br>
  486. <br>
  487. <br>
  488. Beispiel:
  489. <ul>
  490. <code>define sleiste EGPM2LAN 10.192.192.20</code><br>
  491. <code>set sleiste password SecretGarden</code><br>
  492. <code>set sleiste on 1</code><br>
  493. </ul>
  494. </ul>
  495. =end html_DE
  496. =cut