00_FBAHAHTTP.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ##############################################
  2. # $Id: 00_FBAHAHTTP.pm 16344 2018-03-06 21:06:34Z rudolfkoenig $
  3. package main;
  4. # Documentation: AHA-HTTP-Interface.pdf, AVM_Technical_Note_-_Session_ID.pdf
  5. use strict;
  6. use warnings;
  7. use Time::HiRes qw(gettimeofday);
  8. use FritzBoxUtils;
  9. sub
  10. FBAHAHTTP_Initialize($)
  11. {
  12. my ($hash) = @_;
  13. $hash->{WriteFn} = "FBAHAHTTP_Write";
  14. $hash->{DefFn} = "FBAHAHTTP_Define";
  15. $hash->{SetFn} = "FBAHAHTTP_Set";
  16. $hash->{AttrFn} = "FBAHAHTTP_Attr";
  17. $hash->{ReadyFn} = "FBAHAHTTP_Ready";
  18. $hash->{RenameFn} = "FBAHAHTTP_RenameFn";
  19. $hash->{DeleteFn} = "FBAHAHTTP_Delete";
  20. $hash->{AttrList} = "dummy:1,0 fritzbox-user polltime async_delay ".
  21. "disable:0,1 disabledForIntervals fbTimeout";
  22. }
  23. #####################################
  24. sub
  25. FBAHAHTTP_Define($$)
  26. {
  27. my ($hash, $def) = @_;
  28. my @a = split("[ \t][ \t]*", $def);
  29. return "wrong syntax: define <name> FBAHAHTTP hostname"
  30. if(@a != 3);
  31. $hash->{Clients} = ":FBDECT:";
  32. my %matchList = ( "1:FBDECT" => ".*" );
  33. $hash->{MatchList} = \%matchList;
  34. for my $d (devspec2array("TYPE=FBDECT")) {
  35. if($defs{$d}{IODev} && $defs{$d}{IODev}{TYPE} eq "FBAHA") {
  36. my $n = $defs{$d}{IODev}{NAME};
  37. CommandAttr(undef, "$d IODev $hash->{NAME}");
  38. CommandDelete(undef, $n) if($defs{$n});
  39. }
  40. $defs{$d}{IODev} = $hash
  41. }
  42. $hash->{CmdStack} = ();
  43. return undef if($hash->{DEF} eq "none"); # DEBUGGING
  44. InternalTimer(1, "FBAHAHTTP_Poll", $hash);
  45. $hash->{STATE} = "defined";
  46. return undef;
  47. }
  48. #####################################
  49. sub
  50. FBAHAHTTP_Delete($)
  51. {
  52. my ($hash) = @_;
  53. my $name = $hash->{NAME};
  54. my ($err, $fb_pw) = setKeyValue("FBAHAHTTP_PASSWORD_$name", undef);
  55. }
  56. sub
  57. FBAHAHTTP_connect($)
  58. {
  59. my ($hash) = @_;
  60. my $name = $hash->{NAME};
  61. my $dev = $hash->{DEF};
  62. my $dr = sub {
  63. $hash->{STATE} = $_[0];
  64. Log 2, $hash->{STATE};
  65. $hash->{CmdStack} = ();
  66. return $hash->{STATE};
  67. };
  68. my $fb_user = AttrVal($name, "fritzbox-user", '');
  69. return $dr->("MISSING: attr $name fritzbox-user") if(!$fb_user);
  70. my ($err, $fb_pw) = getKeyValue("FBAHAHTTP_PASSWORD_$name");
  71. return $dr->("ERROR: $err") if($err);
  72. return $dr->("MISSING: set $name password") if(!$fb_pw);
  73. my $sid = FB_doCheckPW($hash->{DEF}, $fb_user, $fb_pw);
  74. if(!$sid) {
  75. $hash->{NEXT_OPEN} = time()+60;
  76. $readyfnlist{"$name.$dev"} = $hash;
  77. return $dr->("$name error: cannot get SID, ".
  78. "check connection/hostname/fritzbox-user/password")
  79. }
  80. delete($hash->{RetriedCmd});
  81. delete($readyfnlist{"$name.$dev"});
  82. $hash->{".SID"} = $sid;
  83. $hash->{STATE} = "connected";
  84. Log3 $name, 4, "FBAHAHTTP_connect $name: got SID $sid";
  85. return undef;
  86. }
  87. sub
  88. FBAHAHTTP_RenameFn($$)
  89. {
  90. my ($new, $old) = @_;
  91. for my $d (devspec2array("TYPE=FBDECT")) {
  92. my $hash = $defs{$d};
  93. next if(!$hash);
  94. $hash->{DEF} =~ s/^$old:/$new:/;
  95. $attr{$d}{IODev} = $new if(AttrVal($d,"IODev","") eq $old);
  96. }
  97. FBDECT_renameIoDev($new, $old);
  98. }
  99. #####################################
  100. sub
  101. FBAHAHTTP_Poll($)
  102. {
  103. my ($hash) = @_;
  104. my $name = $hash->{NAME};
  105. return if(IsDisabled($name));
  106. if(!$hash->{".SID"}) {
  107. my $ret = FBAHAHTTP_connect($hash);
  108. return $ret if($ret);
  109. }
  110. my $sid = $hash->{".SID"};
  111. my $host = ($hash->{DEF} =~ m/^http/i ? $hash->{DEF} : "http://$hash->{DEF}");
  112. HttpUtils_NonblockingGet({
  113. url=>"$host/webservices/homeautoswitch.lua?sid=$sid".
  114. "&switchcmd=getdevicelistinfos",
  115. loglevel => AttrVal($name, "verbose", 4),
  116. timeout => AttrVal($name, "fbTimeout", 4),
  117. callback => sub {
  118. if($_[1]) {
  119. Log3 $name, 3, "$name: $_[1]";
  120. delete $hash->{".SID"};
  121. return;
  122. }
  123. Log 5, $_[2] if(AttrVal($name, "verbose", 1) >= 5);
  124. if($_[2] !~ m,^<devicelist.*</devicelist>$,s) {
  125. Log3 $name, 3, "$name: unexpected reply from device: $_[2]";
  126. delete $hash->{".SID"};
  127. return;
  128. }
  129. $_[2] =~ s+<(device|group) (.*?)</\g1>+
  130. Dispatch($hash, "<$1 $2</$1>", undef);""+gse; # Quick&Hack
  131. }
  132. });
  133. my $polltime = AttrVal($name, "polltime", 300);
  134. RemoveInternalTimer($hash);
  135. InternalTimer(gettimeofday()+$polltime, "FBAHAHTTP_Poll", $hash);
  136. return;
  137. }
  138. #####################################
  139. sub
  140. FBAHAHTTP_Ready($)
  141. {
  142. my ($hash) = @_;
  143. return if($hash->{NEXT_OPEN} && time() < $hash->{NEXT_OPEN});
  144. FBAHAHTTP_Poll($hash);
  145. }
  146. #####################################
  147. sub
  148. FBAHAHTTP_Attr($@)
  149. {
  150. my ($type, $devName, $attrName, @param) = @_;
  151. my $hash = $defs{$devName};
  152. if($attrName eq "fritzbox-user") {
  153. return "Cannot delete fritzbox-user" if($type eq "del");
  154. if($init_done) {
  155. delete($hash->{".SID"});
  156. InternalTimer(1, sub { FBAHAHTTP_Poll($hash); }, 0);
  157. }
  158. }
  159. return undef;
  160. }
  161. #####################################
  162. sub
  163. FBAHAHTTP_Set($@)
  164. {
  165. my ($hash, @a) = @_;
  166. my $name = shift @a;
  167. my %sets = (password=>2, refreshstate=>1);
  168. return "set $name needs at least one parameter" if(@a < 1);
  169. my $type = shift @a;
  170. return "Unknown argument $type, choose one of refreshstate:noArg password"
  171. if(!defined($sets{$type}));
  172. return "Missing argument for $type" if(int(@a) < $sets{$type}-1);
  173. if($type eq "password") {
  174. setKeyValue("FBAHAHTTP_PASSWORD_$name", $a[0]);
  175. delete($hash->{".SID"});
  176. FBAHAHTTP_Poll($hash);
  177. return;
  178. }
  179. if($type eq "refreshstate") {
  180. FBAHAHTTP_Poll($hash);
  181. return;
  182. }
  183. return undef;
  184. }
  185. sub
  186. FBAHAHTTP_ProcessStack($)
  187. {
  188. my ($hash) = @_;
  189. my $name = $hash->{NAME};
  190. my $msg = $hash->{CmdStack}->[0];
  191. my $host = ($hash->{DEF} =~ m/^http/i ? $hash->{DEF} : "http://$hash->{DEF}");
  192. HttpUtils_NonblockingGet({
  193. url=>"$host/webservices/homeautoswitch.lua?$msg",
  194. loglevel => AttrVal($name, "verbose", 4),
  195. timeout => AttrVal($name, "fbTimeout", 4),
  196. callback => sub {
  197. if($_[1]) {
  198. Log3 $name, 3, "$name: $_[1]";
  199. delete $hash->{".SID"};
  200. $hash->{CmdStack} = ();
  201. return;
  202. }
  203. Log3 $name, 5, "FBAHAHTTP_Write reply for $name: $_[2]";
  204. if(!defined($_[2]) || $_[2] eq "") {
  205. if($hash->{RetriedCmd}) {
  206. Log3 $name, 1, "No sensible respone after reconnect, giving up";
  207. return;
  208. }
  209. return if(FBAHAHTTP_connect($hash));
  210. $hash->{RetriedCmd} = $msg;
  211. FBAHAHTTP_ProcessStack($hash);
  212. return;
  213. }
  214. delete($hash->{RetriedCmd});
  215. shift @{$hash->{CmdStack}};
  216. if(@{$hash->{CmdStack}} > 0) {
  217. my $ad = AttrVal($name, "async_delay", 0);
  218. InternalTimer(gettimeofday()+$ad, sub(){
  219. FBAHAHTTP_ProcessStack($hash);
  220. }, $hash);
  221. }
  222. }
  223. });
  224. }
  225. #####################################
  226. sub
  227. FBAHAHTTP_Write($$$)
  228. {
  229. my ($hash,$fn,$msg) = @_;
  230. my $name = $hash->{NAME};
  231. return if(IsDisabled($name));
  232. my $sid = $hash->{".SID"};
  233. if(!$sid) {
  234. my $ret = FBAHAHTTP_connect($hash); # try to reconnect
  235. return $ret if($ret);
  236. $sid = $hash->{".SID"};
  237. }
  238. push(@{$hash->{CmdStack}}, "sid=$sid&ain=$fn&switchcmd=$msg");
  239. FBAHAHTTP_ProcessStack($hash) if(@{$hash->{CmdStack}} == 1);
  240. }
  241. 1;
  242. =pod
  243. =item summary connection to the Fritz!OS AHA Server via HTTP
  244. =item summary_DE Anbindung des Fritz!OS AHA Servers &uuml;ber HTTP
  245. =begin html
  246. <a name="FBAHAHTTP"></a>
  247. <h3>FBAHAHTTP</h3>
  248. <ul>
  249. This module connects to the AHA server (AVM Home Automation) on a FRITZ!Box
  250. via HTTP, it is a successor/drop-in replacement for the FBAHA module. It is
  251. necessary, as the FBAHA interface is deprecated by AVM. Since the AHA HTTP
  252. interface do not offer any notification mechanism, the module is regularly
  253. polling the FRITZ!Box.<br>
  254. Important: For an existing installation with an FBAHA device, defining a
  255. new FBAHAHTTP device will change the IODev of all FBDECT devices from the
  256. old FBAHA to this FBAHAHTTP device, and it will delete the FBAHA device.<br>
  257. This module serves as the "physical" counterpart to the <a
  258. href="#FBDECT">FBDECT</a> devices. Note: you have to enable the access to
  259. Smart Home in the FRITZ!Box frontend for the fritzbox-user, and take care
  260. to configure the login in the home network with username AND password.
  261. <br><br>
  262. <a name="FBAHAHTTPdefine"></a>
  263. <b>Define</b>
  264. <ul>
  265. <code>define &lt;name&gt; FBAHAHTTP &lt;hostname&gt;</code><br>
  266. <br>
  267. &lt;hostnamedevice&gt; is most probably fritz.box.
  268. Example:
  269. <ul>
  270. <code>define fb1 FBAHAHTTP fritz.box</code><br>
  271. </ul>
  272. Note: to specify HTTPS for the connection use https://fritz.box as
  273. hostname. To explicitly specify the port, postfix the hostname with :port,
  274. as in https://fritz.box:443
  275. </ul>
  276. <br>
  277. <a name="FBAHAHTTPset"></a>
  278. <b>Set</b>
  279. <ul>
  280. <li>password &lt;password&gt;<br>
  281. This is the only way to set the password
  282. </li>
  283. <li>refreshstate<br>
  284. The state of all devices is polled every &lt;polltime&gt; seconds (default
  285. is 300). This command forces a state-refresh.
  286. </li>
  287. </ul>
  288. <br>
  289. <a name="FBAHAHTTPget"></a>
  290. <b>Get</b>
  291. <ul>N/A</ul>
  292. <br>
  293. <a name="FBAHAHTTPattr"></a>
  294. <b>Attributes</b>
  295. <ul>
  296. <li><a href="#async_delay">async_delay</a><br>
  297. additional delay inserted, when switching more than one device, default
  298. is 0.2 seconds. Note: even with async_delay 0 there will be a delay, as
  299. FHEM avoids sending commands in parallel, to avoid malfunctioning of the
  300. Fritz!BOX AHA server).
  301. </li>
  302. <li><a href="#disable">disable</a></li>
  303. <li><a href="#disabledForIntervals">disabledForIntervals</a></li>
  304. <li><a href="#dummy">dummy</a></li>
  305. <li><a href="#fritzbox-user">fritzbox-user</a></li>
  306. <li><a name="polltime">polltime</a><br>
  307. measured in seconds, default is 300 i.e. 5 minutes
  308. </li>
  309. <li><a name="fbTimeout">fbTimeout</a><br>
  310. timeout for getting answer from the Fritz!BOX. Default is 4 (seconds).
  311. </li>
  312. </ul>
  313. <br>
  314. </ul>
  315. =end html
  316. =cut