17_EGPM2LAN.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. ##############################################
  2. # $Id: 17_EGPM2LAN.pm 12092 2016-08-30 10:39:50Z alexus2033 $
  3. #
  4. # based / modified Version 98_EGPMS2LAN from ericl
  5. #
  6. # (c) 2013, 2014 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} = "loglevel:0,1,2,3,4,5,6 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" if($a[1] eq "?");
  70. my $name = shift @a;
  71. my $setcommand = shift @a;
  72. my $params = join(" ", @a);
  73. my $logLevel = GetLogLevel($name,4);
  74. Log $logLevel, "EGPM2LAN set $name (". $hash->{IP}. ") $setcommand $params";
  75. EGPM2LAN_Login($hash, $logLevel);
  76. if($setcommand eq "on" || $setcommand eq "off")
  77. {
  78. if($params eq "all")
  79. { #switch all Sockets; thanks to eric!
  80. for (my $count = 1; $count <= 4; $count++)
  81. {
  82. EGPM2LAN_Switch($hash, $setcommand, $count, $logLevel);
  83. }
  84. }
  85. else
  86. { #switch single Socket
  87. EGPM2LAN_Switch($hash, $setcommand, $params, $logLevel);
  88. }
  89. EGPM2LAN_Statusrequest($hash, $logLevel, 1);
  90. }
  91. elsif($setcommand eq "toggle")
  92. {
  93. my $currentstate = EGPM2LAN_Statusrequest($hash, $logLevel, 1);
  94. if(defined($currentstate))
  95. {
  96. my @powerstates = split(",", $currentstate);
  97. my $newcommand="off";
  98. if($powerstates[$params-1] eq "0")
  99. {
  100. $newcommand="on";
  101. }
  102. EGPM2LAN_Switch($hash, $newcommand, $params, $logLevel);
  103. EGPM2LAN_Statusrequest($hash, $logLevel, 0);
  104. }
  105. }
  106. elsif($setcommand eq "statusrequest")
  107. {
  108. EGPM2LAN_Statusrequest($hash, $logLevel, 1);
  109. }
  110. elsif($setcommand eq "clearreadings")
  111. {
  112. delete $hash->{READINGS};
  113. }
  114. else
  115. {
  116. return "unknown argument $setcommand, choose one of on, off, toggle, statusrequest, clearreadings";
  117. }
  118. EGPM2LAN_Logoff($hash, $logLevel);
  119. $hash->{CHANGED}[0] = $setcommand;
  120. $hash->{READINGS}{lastcommand}{TIME} = TimeNow();
  121. $hash->{READINGS}{lastcommand}{VAL} = $setcommand." ".$params;
  122. return undef;
  123. }
  124. ################################
  125. sub EGPM2LAN_Switch($$$$) {
  126. my ($hash, $state, $port, $logLevel) = @_;
  127. $state = ($state eq "on" ? "1" : "0");
  128. my $fritz = 0; #may be important for FritzBox-users
  129. my $data = "cte1=" . ($port == "1" ? $state : "") . "&cte2=" . ($port == "2" ? $state : "") . "&cte3=" . ($port == "3" ? $state : "") . "&cte4=". ($port == "4" ? $state : "");
  130. Log $logLevel, "EGPM2LAN $data";
  131. eval {
  132. # Parameter: $url, $timeout, $data, $noshutdown, $loglevel
  133. GetFileFromURL("http://".$hash->{IP}."/", 5,$data ,$fritz ,$logLevel);
  134. };
  135. if ($@){
  136. ### catch block
  137. Log $logLevel, "EGPM2LAN error: $@";
  138. };
  139. return 1;
  140. }
  141. ################################
  142. sub EGPM2LAN_Login($$) {
  143. my ($hash, $logLevel) = @_;
  144. Log $logLevel,"EGPM2LAN try to Login @".$hash->{IP};
  145. eval{
  146. GetFileFromURLQuiet("http://".$hash->{IP}."/login.html", 5,"pw=" . (defined($hash->{PASSWORD}) ? $hash->{PASSWORD} : ""),0 ,$logLevel);
  147. };
  148. if ($@){
  149. ### catch block
  150. Log 1, "EGPM2LAN Login error: $@";
  151. return 0;
  152. };
  153. Log $logLevel,"EGPM2LAN Login successful!";
  154. return 1;
  155. }
  156. ################################
  157. sub EGPM2LAN_GetDeviceInfo($$) {
  158. my ($hash, $input) = @_;
  159. my $logLevel = GetLogLevel($hash->{NAME},4);
  160. #try to read Device Name
  161. my ($devicename) = $input =~ m/<h2>(.+)<\/h2><\/div>/si;
  162. $hash->{DEVICENAME} = trim($devicename);
  163. #try to read Socket Names
  164. my @socketlist;
  165. while ($input =~ m/<h2 class=\"ener\">(.+?)<\/h2>/gi)
  166. {
  167. my $socketname = trim($1);
  168. $socketname =~ s/ /_/g; #remove spaces
  169. push(@socketlist, $socketname);
  170. }
  171. #check 4 dublicate Names
  172. my %seen;
  173. foreach my $entry (@socketlist)
  174. {
  175. next unless $seen{$entry}++;
  176. Log $logLevel, "EGPM2LAN Sorry! Can't use devicenames. ".trim($entry)." is duplicated.";
  177. @socketlist = qw(Socket_1 Socket_2 Socket_3 Socket_4);
  178. }
  179. if(int(@socketlist) < 4)
  180. {
  181. @socketlist = qw(Socket_1 Socket_2 Socket_3 Socket_4);
  182. }
  183. return @socketlist;
  184. }
  185. ################################
  186. sub EGPM2LAN_Statusrequest($$$) {
  187. my ($hash, $logLevel, $autoCr) = @_;
  188. my $name = $hash->{NAME};
  189. my $response = GetFileFromURL("http://".$hash->{IP}."/", 5,"" , 0 ,$logLevel);
  190. #Log 1,$response;
  191. if(defined($response) && $response =~ /.,.,.,./)
  192. {
  193. my $powerstatestring = $&;
  194. Log $logLevel, "EGPM2LAN Powerstate: " . $powerstatestring;
  195. my @powerstates = split(",", $powerstatestring);
  196. if(int(@powerstates) == 4)
  197. {
  198. my $index;
  199. my $newstatestring;
  200. my @socketlist = EGPM2LAN_GetDeviceInfo($hash,$response);
  201. readingsBeginUpdate($hash);
  202. foreach my $powerstate (@powerstates)
  203. {
  204. $index++;
  205. if(length(trim($socketlist[$index-1]))==0)
  206. {
  207. $socketlist[$index-1]="Socket_".$index;
  208. }
  209. if(AttrVal($name, "stateDisplay", "sockNumber") eq "sockName") {
  210. $newstatestring .= $socketlist[$index-1].": ".($powerstates[$index-1] ? "on" : "off")." ";
  211. } else {
  212. $newstatestring .= $index.": ".($powerstates[$index-1] ? "on" : "off")." ";
  213. }
  214. #Create Socket-Object if not available
  215. my $defptr = $modules{EGPM}{defptr}{$name.$index};
  216. if($autoCr && AttrVal($name, "autocreate", "on") eq "on" && not defined($defptr))
  217. {
  218. if(Value("autocreate") eq "active")
  219. {
  220. Log $logLevel, "EGPM2LAN: Autocreate EGPM for Socket $index";
  221. CommandDefine(undef, $name."_".$socketlist[$index-1]." EGPM $name $index");
  222. }
  223. else
  224. {
  225. Log 2, "EGPM2LAN: Autocreate disabled in globals section";
  226. $attr{$name}{autocreate} = "off";
  227. }
  228. }
  229. #Write state 2 related Socket-Object
  230. if (defined($defptr))
  231. {
  232. if (ReadingsVal($defptr->{NAME},"state","") ne ($powerstates[$index-1] ? "on" : "off"))
  233. { #check for chages and update -> trigger event
  234. Log $logLevel, "Update State of ".$defptr->{NAME};
  235. readingsSingleUpdate($defptr, "state", ($powerstates[$index-1] ? "on" : "off") ,1);
  236. }
  237. $defptr->{DEVICENAME} = $hash->{DEVICENAME};
  238. $defptr->{SOCKETNAME} = $socketlist[$index-1];
  239. }
  240. readingsBulkUpdate($hash, $index."_".$socketlist[$index-1], ($powerstates[$index-1] ? "on" : "off"));
  241. }
  242. readingsBulkUpdate($hash, "state", $newstatestring);
  243. readingsEndUpdate($hash, 0);
  244. #everything is fine
  245. return $powerstatestring;
  246. }
  247. else
  248. {
  249. Log $logLevel, "EGPM2LAN: Failed to parse powerstate";
  250. }
  251. }
  252. else
  253. {
  254. $hash->{STATE} = "Login failed";
  255. Log $logLevel, "EGPM2LAN: Login failed";
  256. }
  257. #something went wrong :-(
  258. return undef;
  259. }
  260. sub EGPM2LAN_Logoff($$) {
  261. my ($hash, $logLevel) = @_;
  262. GetFileFromURL("http://".$hash->{IP}."/login.html", 5,"" ,0 ,$logLevel);
  263. return 1;
  264. }
  265. sub
  266. EGPM2LAN_Define($$)
  267. {
  268. my ($hash, $def) = @_;
  269. my @a = split("[ \t][ \t]*", $def);
  270. my $u = "wrong syntax: define <name> EGPM2LAN IP Password";
  271. return $u if(int(@a) < 2);
  272. $hash->{IP} = $a[2];
  273. if(int(@a) == 4)
  274. {
  275. $hash->{PASSWORD} = $a[3];
  276. }
  277. else
  278. {
  279. $hash->{PASSWORD} = "";
  280. }
  281. my $result = EGPM2LAN_Login($hash, 3);
  282. if($result == 1)
  283. {
  284. $hash->{STATE} = "initialized";
  285. EGPM2LAN_Statusrequest($hash, 4, 0);
  286. EGPM2LAN_Logoff($hash, 4);
  287. }
  288. return undef;
  289. }
  290. 1;
  291. =pod
  292. =item device
  293. =item summary controls a LAN-Socket device from Gembird
  294. =item summary_DE steuert eine LAN-Steckdosenleiste von Gembird
  295. =begin html
  296. <a name="EGPM2LAN"></a>
  297. <h3>EGPM2LAN</h3>
  298. <ul>
  299. <br>
  300. <a name="EGPM2LANdefine"></a>
  301. <b>Define</b>
  302. <ul>
  303. <code>define &lt;name&gt; EGPM2LAN &lt;IP-Address&gt; [&lt;Password&gt;]</code><br>
  304. <br>
  305. 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.
  306. If you have more than one device, it is helpful to connect and set names for your sockets over the web-interface first.
  307. 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>
  308. <b>EG-PMS2-LAN with surge protector feature was not tested until now.</b>
  309. </ul><br>
  310. <a name="EGPM2LANset"></a>
  311. <b>Set</b>
  312. <ul>
  313. <code>set &lt;name&gt; &lt;[on|off|toggle]&gt &lt;socketnr.&gt;</code><br>
  314. Switch the socket on or off.<br>
  315. <br>
  316. <code>set &lt;name&gt; &lt;[on|off]&gt &lt;all&gt;</code><br>
  317. Switch all available sockets on or off.<br>
  318. <br>
  319. <code>set &lt;name&gt; &lt;staterequest&gt;</code><br>
  320. Update the device information and the state of all sockets.<br>
  321. If <a href="#autocreate">autocreate</a> is enabled, an <a href="#EGPM">EGPM</a> device will be created for each socket.<br>
  322. <br>
  323. <code>set &lt;name&gt; &lt;clearreadings&gt;</code><br>
  324. Removes all readings from the list to get rid of old socketnames.
  325. </ul>
  326. <br>
  327. <a name="EGPM2LANget"></a>
  328. <b>Get</b> <ul>N/A</ul><br>
  329. <a name="EGPM2LANattr"></a>
  330. <b>Attributes</b>
  331. <ul>
  332. <li>stateDisplay</li>
  333. 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.
  334. <li>autocreate</li>
  335. Default: <b>on</b> <a href="#EGPM">EGPM</a>-devices will be created automatically with a <b>set</b>-command.
  336. Change this attribute to value <b>off</b> to avoid that mechanism.
  337. <li><a href="#loglevel">loglevel</a></li>
  338. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  339. </ul>
  340. <br>
  341. <br>
  342. <br>
  343. Example:
  344. <ul>
  345. <code>define mainswitch EGPM2LAN 10.192.192.20 SecretGarden</code><br>
  346. <code>set mainswitch on 1</code><br>
  347. </ul>
  348. </ul>
  349. =end html
  350. =begin html_DE
  351. <a name="EGPM2LAN"></a>
  352. <h3>EGPM2LAN</h3>
  353. <ul>
  354. <br>
  355. <a name="EGPM2LANdefine"></a>
  356. <b>Define</b>
  357. <ul>
  358. <code>define &lt;name&gt; EGPM2LAN &lt;IP-Address&gt; [&lt;Password&gt;]</code><br>
  359. <br>
  360. 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..
  361. 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
  362. 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.
  363. </ul><br>
  364. <a name="EGPM2LANset"></a>
  365. <b>Set</b>
  366. <ul>
  367. <code>set &lt;name&gt; &lt;[on|off|toggle]&gt &lt;socketnr.&gt;</code><br>
  368. Schaltet die gew&auml;hlte Steckdose ein oder aus.<br>
  369. <br>
  370. <code>set &lt;name&gt; &lt;[on|off]&gt &lt;all&gt;</code><br>
  371. Schaltet alle Steckdosen gleichzeitig ein oder aus.<br>
  372. <br>
  373. <code>set &lt;name&gt; &lt;staterequest&gt;</code><br>
  374. Aktualisiert die Statusinformation der Steckdosenleiste.<br>
  375. 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>
  376. <br>
  377. <code>set &lt;name&gt; &lt;clearreadings&gt;</code><br>
  378. L&ouml;scht alle ung&uuml;ltigen Eintr&auml;ge im Abschnitt &lt;readings&gt;.
  379. </ul>
  380. <br>
  381. <a name="EGPM2LANget"></a>
  382. <b>Get</b> <ul>N/A</ul><br>
  383. <a name="EGPM2LANattr"></a>
  384. <b>Attribute</b>
  385. <ul>
  386. <li>stateDisplay</li>
  387. 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.
  388. <li>autocreate</li>
  389. Default: <b>on</b> <a href="#EGPM">EGPM</a>-Eintr&auml;ge werden automatisch mit dem <b>set</b>-command erstellt.
  390. <li><a href="#loglevel">loglevel</a></li>
  391. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  392. </ul>
  393. <br>
  394. <br>
  395. <br>
  396. Beispiel:
  397. <ul>
  398. <code>define sleiste EGPM2LAN 10.192.192.20 SecretGarden</code><br>
  399. <code>set sleiste on 1</code><br>
  400. </ul>
  401. </ul>
  402. =end html_DE
  403. =cut