00_FBAHAHTTP.pm 9.2 KB

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