00_FBAHAHTTP.pm 9.8 KB

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