55_weco.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # $Id: 55_weco.pm 10558 2016-01-17 14:17:58Z betateilchen $
  2. ####################################################################################################
  3. #
  4. # 55_weco.pm
  5. #
  6. # Copyright: betateilchen ®
  7. # e-mail: fhem.development@betateilchen.de
  8. #
  9. # Based on an idea from Frank S. in fhem forum.
  10. #
  11. # This file is part of fhem.
  12. #
  13. # Fhem is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation, either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # Fhem is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  25. #
  26. ####################################################################################################
  27. package main;
  28. use strict;
  29. use warnings;
  30. use Time::HiRes qw(gettimeofday);
  31. use HttpUtils;
  32. ####################################################################################################
  33. #
  34. # Main routines
  35. #
  36. ####################################################################################################
  37. sub weco_Initialize($) {
  38. my ($hash) = @_;
  39. $hash->{DefFn} = "weco_Define";
  40. $hash->{UndefFn} = "weco_Undef";
  41. $hash->{AttrList} = "disable:1,0 ".
  42. "wecoInterval:600,1800,3600 wecotest:true,false ".
  43. "wecohu wecote wecodp wecopr wecopcv wecopcf wecowd wecows wecowsbft wecowg wecopa ".
  44. "wecopai wecopaest wecouv wecosd wecosc wecovi wecoch wecocm wecocl wecodc wecoww ".
  45. $readingFnAttributes;
  46. }
  47. sub weco_Define($$$) {
  48. my ($hash, $def) = @_;
  49. my @a = split("[ \t][ \t]*", $def);
  50. return "syntax: define <name> weco <stationID> <password>" if(int(@a) != 4 );
  51. my $name = $hash->{NAME};
  52. $hash->{helper}{stationid} = $a[2];
  53. $hash->{helper}{password} = $a[3];
  54. $hash->{helper}{softwareid} = 'fhem';
  55. $hash->{helper}{url} = "http://interface.wetterarchiv.de/weather/";
  56. Log3($name, 4, "weco $name: created");
  57. readingsSingleUpdate($hash, "state", "defined",1);
  58. weco_send($hash);
  59. return undef;
  60. }
  61. sub weco_Undef($$) {
  62. my ($hash, $arg) = @_;
  63. RemoveInternalTimer($hash);
  64. return undef;
  65. }
  66. sub weco_send($) {
  67. my ($hash, $local) = @_;
  68. my $name = $hash->{NAME};
  69. return if IsDisabled($name);
  70. $local = 0 unless(defined($local));
  71. my $url = $hash->{helper}{url};
  72. $url .= "?id=". $hash->{helper}{stationid};
  73. $url .= "&pwd=". $hash->{helper}{password};
  74. $url .= "&sid=". $hash->{helper}{softwareid};
  75. $url .= "&dtutc=".strftime "%Y%m%d%H%M", gmtime;
  76. $url .= "&dt=". strftime "%Y%m%d%H%M", localtime;
  77. $attr{$name}{wecoInterval} = 600 if(AttrVal($name,"wecoInterval",0) < 600);
  78. RemoveInternalTimer($hash);
  79. my ($data, $d, $r, $o);
  80. my $a = $attr{$name};
  81. while ( my ($key, $value) = each($a) ) {
  82. next if substr($key,0,4) ne 'weco';
  83. next if substr($key,4,1) ~~ ["I"];
  84. $key = substr($key,4,length($key)-4);
  85. ($d, $r, $o) = split(":", $value);
  86. if(defined($r)) {
  87. $o = (defined($o)) ? $o : 0;
  88. $value = ReadingsVal($d, $r, 0) + $o;
  89. }
  90. $data .= "&$key=$value";
  91. }
  92. readingsBeginUpdate($hash);
  93. if(defined($data)) {
  94. readingsBulkUpdate($hash, "data", $data);
  95. Log3 ($name, 4, "weco $name data sent: $data");
  96. $url .= $data;
  97. my $response = GetFileFromURL($url);
  98. readingsBulkUpdate($hash, "response", $response);
  99. Log3 ($name, 4, "weco $name server response: $response");
  100. readingsBulkUpdate($hash, "state", "active");
  101. } else {
  102. CommandDeleteReading(undef, "$name data");
  103. CommandDeleteReading(undef, "$name response");
  104. Log3 ($name, 4, "weco $name no data");
  105. readingsBulkUpdate($hash, "state", "defined");
  106. $attr{$name}{wecoInterval} = 60;
  107. }
  108. readingsEndUpdate($hash, 1);
  109. InternalTimer(gettimeofday()+$attr{$name}{wecoInterval}, "weco_send", $hash, 0) unless($local == 1);
  110. return;
  111. }
  112. 1;
  113. ####################################################################################################
  114. #
  115. # Documentation
  116. #
  117. ####################################################################################################
  118. #
  119. # Changelog:
  120. #
  121. # 2014-04-12 initial release
  122. #
  123. ####################################################################################################
  124. =pod
  125. =item helper
  126. =begin html
  127. <a name="weco"></a>
  128. <h3>weco</h3>
  129. <ul>
  130. <a name="wecodefine"></a>
  131. <b>Define</b>
  132. <ul>
  133. <br/>
  134. <code>define &lt;name&gt; weco &lt;stationId&gt; &lt;password&gt;</code>
  135. <br/><br/>
  136. This module provides connection to <a href="http://www.wetter.com">www.wetter.com</a></br>
  137. to send data from your own weather station.<br/>
  138. </ul>
  139. <br/><br/>
  140. <a name="wecoset"></a>
  141. <b>Set-Commands</b><br/>
  142. <ul>
  143. <br/>
  144. - not implemented -<br/>
  145. </ul>
  146. <br/><br/>
  147. <a name="wecoget"></a>
  148. <b>Get-Commands</b><br/>
  149. <ul>
  150. <br/>
  151. - not implemented -<br/>
  152. </ul>
  153. <br/><br/>
  154. <a name="wecoattr"></a>
  155. <b>Attributes</b><br/><br/>
  156. <ul>
  157. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  158. <br/>
  159. <li><b>wecoInterval</b> - Interval (seconds) to send data to www.wetter.com
  160. Will be adjusted to 600 if set to a value lower than 600.</li>
  161. <li><b>wecotest</b> - If set to "true" data will not be stored on server. Used for development and testing.</li>
  162. <li><b>weco....</b> - Attribute name corresponding to <a href="http://support.wetter.com/attachments/token/titkme05m63xv8e/?name=2013-06-01+-+WeatherReport-API.de.pdf">parameter name from api.</a>
  163. Each of this attributes contains information about weather data to be sent in format
  164. <code>sensorName:readingName[:offset]</code><br/>
  165. Example: <code>attr weco wecote outside:temperature</code> will define the attribut wecote and <br/>
  166. reading "temperature" from device "outside" will be sent to network as paramater "te" (which indicates current temperature)<br/>
  167. Optional Parameter "offset" will be added to the read value
  168. (e.g. sometimes necessary to send dewpoint - use offset 273.15 if needed in Kelvin)
  169. </li>
  170. </ul>
  171. <br/><br/>
  172. <b>Generated Readings/Events:</b>
  173. <br/><br/>
  174. <ul>
  175. <li><b>data</b> - data string transmitted to www.wetter.com</li>
  176. <li><b>response</b> - response string received from server</li>
  177. </ul>
  178. <br/><br/>
  179. <b>Author's notes</b><br/><br/>
  180. <ul>
  181. <li>Find complete api description <a href="http://support.wetter.com/attachments/token/titkme05m63xv8e/?name=2013-06-01+-+WeatherReport-API.de.pdf">here</a></li>
  182. <li>Have fun!</li><br/>
  183. </ul>
  184. </ul>
  185. =end html
  186. =begin html_DE
  187. <a name="weco"></a>
  188. <h3>weco</h3>
  189. <ul>
  190. Sorry, keine deutsche Dokumentation vorhanden.<br/><br/>
  191. Die englische Doku gibt es hier: <a href='http://fhem.de/commandref.html#weco'>weco</a><br/>
  192. </ul>
  193. =end html_DE
  194. =cut