00_FBAHAHTTP.pm 8.9 KB

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