60_allergy.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. ##############################################
  2. # $Id: 60_allergy.pm 12020 2016-08-20 21:13:52Z markus-m $$$
  3. #
  4. # 60_allergy.pm
  5. #
  6. # 2016 Markus Moises < vorname at nachname . de >
  7. #
  8. # This module provides allergy forecast data
  9. #
  10. # http://forum.fhem.de/index.php/topic,37194.0.html
  11. #
  12. #
  13. ##############################################################################
  14. #
  15. # define <name> allergy <zipcode>
  16. #
  17. ##############################################################################
  18. package main;
  19. use strict;
  20. use warnings;
  21. use Time::Local;
  22. use Encode;
  23. use XML::Simple;
  24. use LWP::UserAgent;
  25. use HTTP::Request;
  26. use utf8;
  27. ##############################################################################
  28. sub allergy_Initialize($) {
  29. my ($hash) = @_;
  30. my $name = $hash->{NAME};
  31. $hash->{DefFn} = "allergy_Define";
  32. $hash->{UndefFn} = "allergy_Undefine";
  33. $hash->{GetFn} = "allergy_Get";
  34. $hash->{AttrList} = "disable:0,1 ".
  35. "ignoreList ".
  36. "updateIgnored:1 ".
  37. "updateEmpty:1 ".
  38. "levelsFormat ".
  39. "weekdaysFormat ".
  40. $readingFnAttributes;
  41. }
  42. sub allergy_Define($$$) {
  43. my ($hash, $def) = @_;
  44. my @a = split("[ \t][ \t]*", $def);
  45. my ($found, $dummy);
  46. return "syntax: define <name> allergy <zipcode>" if(int(@a) != 3 );
  47. my $name = $hash->{NAME};
  48. $hash->{helper}{ZIPCODE} = $a[2];
  49. $hash->{helper}{INTERVAL} = 10800;
  50. my $req = eval
  51. {
  52. require XML::Simple;
  53. XML::Simple->import();
  54. 1;
  55. };
  56. if($req)
  57. {
  58. InternalTimer( gettimeofday() + 60, "allergy_GetUpdate", $hash, 0);
  59. if (!defined($attr{$name}{stateFormat}))
  60. {
  61. $attr{$name}{stateFormat} = 'fc1_maximum';
  62. }
  63. }
  64. else
  65. {
  66. $hash->{STATE} = "XML::Simple is required!";
  67. $attr{$name}{disable} = "1";
  68. return undef;
  69. }
  70. $hash->{STATE} = "Initialized";
  71. return undef;
  72. }
  73. sub allergy_Undefine($$) {
  74. my ($hash, $arg) = @_;
  75. my $name = $hash->{NAME};
  76. RemoveInternalTimer($hash);
  77. fhem("deletereading $name fc.*", 1);
  78. return undef;
  79. }
  80. sub allergy_Get($@) {
  81. my ($hash, @a) = @_;
  82. my $command = $a[1];
  83. my $parameter = $a[2] if(defined($a[2]));
  84. my $name = $hash->{NAME};
  85. my $usage = "Unknown argument $command, choose one of data:noArg ";
  86. return $usage if $command eq '?';
  87. RemoveInternalTimer($hash);
  88. if(AttrVal($name, "disable", 0) eq 1) {
  89. $hash->{STATE} = "disabled";
  90. return "allergy $name is disabled. Aborting...";
  91. }
  92. allergy_GetUpdate($hash);
  93. return undef;
  94. }
  95. sub allergy_GetUpdate($) {
  96. my ($hash) = @_;
  97. my $name = $hash->{NAME};
  98. if(AttrVal($name, "disable", 0) eq 1) {
  99. $hash->{STATE} = "disabled";
  100. Log3 ($name, 2, "allergy $name is disabled, data update cancelled.");
  101. return undef;
  102. }
  103. my $url="http://www.allergie.hexal.de/pollenflug/xml-interface-neu/pollen_de_7tage.php?plz=".$hash->{helper}{ZIPCODE};
  104. Log3 ($name, 4, "Getting URL $url");
  105. HttpUtils_NonblockingGet({
  106. url => $url,
  107. noshutdown => 1,
  108. hash => $hash,
  109. type => 'allergydata',
  110. callback => \&allergy_Parse,
  111. });
  112. return undef;
  113. }
  114. sub allergy_Parse($$$)
  115. {
  116. my ($param, $err, $data) = @_;
  117. my $hash = $param->{hash};
  118. my $name = $hash->{NAME};
  119. if( $err )
  120. {
  121. Log3 $name, 1, "$name: URL error: ".$err;
  122. $hash->{STATE} = "error";
  123. return undef;
  124. }
  125. Log3 $name, 5, "Received XML data ".$data;
  126. my $xml = new XML::Simple();
  127. my $xmldata = $xml->XMLin($data,forcearray => [qw( pollenbelastungen pollen )],keyattr => {pollen => 'name'});
  128. my @wdays = split(',',AttrVal($hash->{NAME}, "weekdaysFormat", "Sun,Mon,Tue,Wed,Thu,Fri,Sat" ));
  129. my @levels = split(',',AttrVal($hash->{NAME}, "levelsFormat", "-,low,moderate,high,extreme" ));
  130. readingsBeginUpdate($hash); # Start update readings
  131. my $city = $xmldata->{'pollendaten'}->{'ort'};
  132. readingsBulkUpdate($hash, "city", allergy_utf8clean($city));
  133. Log3 $name, 4, "Received data for postcode ".$xmldata->{'pollendaten'}->{'plz'};
  134. foreach my $day (@{$xmldata->{'pollendaten'}{'pollenbelastungen'}})
  135. {
  136. my $daycode = $day->{'tag'}+1;
  137. my @daydata = $day->{'pollen'};
  138. my $daymax = 0;
  139. my $pollenkey='';
  140. my $pollenvalue='';
  141. my $pollendata=0;
  142. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time+($day->{'tag'}*86400));
  143. readingsBulkUpdate($hash, "fc".$daycode."_day_of_week", $wdays[$wday]);
  144. foreach my $pollenhash (@daydata)
  145. {
  146. while(($pollenkey, $pollenvalue) = each(%$pollenhash))
  147. {
  148. $pollenkey = allergy_utf8clean($pollenkey);
  149. $pollendata = $pollenvalue->{'belastung'};
  150. if (( AttrVal($hash->{NAME}, "updateEmpty", 0 ) gt 0 or $pollendata gt 0) and ( AttrVal($hash->{NAME}, "updateIgnored", 0 ) gt 0 or ( index(AttrVal($hash->{NAME}, "ignoreList", ""), $pollenkey ) == -1 )))
  151. {
  152. readingsBulkUpdate($hash, "fc".$daycode."_".$pollenkey, $levels[$pollendata]);
  153. $daymax = $pollendata if($pollendata gt $daymax);
  154. Log3 $name, 4, "Received pollen level for ".$pollenkey.": day".$daycode." level ".$pollendata;
  155. }
  156. else
  157. {
  158. fhem( "deletereading $name fc".$daycode."_".$pollenkey, 1 );
  159. Log3 $name, 5, "Received pollen level for ".$pollenkey.": day".$daycode." level ".$pollendata." (ignored)";
  160. }
  161. }
  162. }
  163. readingsBulkUpdate($hash, "fc".$daycode."_maximum", $levels[$daymax]);
  164. }
  165. readingsEndUpdate($hash, 1);
  166. $hash->{UPDATED} = FmtDateTime(time());
  167. my $nextupdate = gettimeofday()+$hash->{helper}{INTERVAL};
  168. InternalTimer($nextupdate, "allergy_GetUpdate", $hash, 1);
  169. return undef;
  170. }
  171. sub allergy_utf8clean($) {
  172. my ($string) = @_;
  173. my $log = "";
  174. if($string !~ m/^[A-Za-z\d_\.-]+$/)
  175. {
  176. $log .= $string."(standard) ";
  177. $string =~ s/Ä/Ae/g;
  178. $string =~ s/Ö/Oe/g;
  179. $string =~ s/Ü/Ue/g;
  180. $string =~ s/ä/ae/g;
  181. $string =~ s/ö/oe/g;
  182. $string =~ s/ü/ue/g;
  183. $string =~ s/ß/ss/g;
  184. }
  185. if($string !~ m/^[A-Za-z\d_\.-]+$/)
  186. {
  187. $log .= $string."(single) ";
  188. $string =~ s/Ä/Ae/g;
  189. $string =~ s/Ö/Oe/g;
  190. $string =~ s/Ü/Ue/g;
  191. $string =~ s/ä/ae/g;
  192. $string =~ s/ö/oe/g;
  193. $string =~ s/ü/ue/g;
  194. $string =~ s/ß/ss/g;
  195. }
  196. if($string !~ m/^[A-Za-z\d_\.-]+$/)
  197. {
  198. $log .= $string."(double) ";
  199. $string =~ s/Ä/Ae/g;
  200. $string =~ s/Ö/Oe/g;
  201. $string =~ s/Ü/Ue/g;
  202. $string =~ s/ä/ae/g;
  203. $string =~ s/ö/oe/g;
  204. $string =~ s/ü/ue/g;
  205. $string =~ s/ß/ss/g;
  206. }
  207. if($string !~ m/^[A-Za-z\d_\.-]+$/)
  208. {
  209. $log .= $string."(unknown)";
  210. #$string =~ s/[^!-~\s]//g;
  211. $string =~ s/[^A-Za-z\d_\.-]//g;
  212. }
  213. Log3 "utf8clean", 5, "Cleaned $string // $log" if($log ne "");
  214. return $string;
  215. }
  216. ##########################
  217. 1;
  218. =pod
  219. =item device
  220. =item summary Allergy forecast data for Germany
  221. =begin html
  222. <a name="allergy"></a>
  223. <h3>allergy</h3>
  224. <ul>
  225. This modul provides allergy forecast data for Germany.<br/>
  226. It requires the Perl module XML::Simple to be installed
  227. <br/><br/>
  228. <b>Define</b>
  229. <ul>
  230. <code>define &lt;name&gt; allergy &lt;zipcode&gt;</code>
  231. <br>
  232. Example: <code>define allergydata allergy 12345</code>
  233. <br>&nbsp;
  234. <li><code>zipcode</code>
  235. <br>
  236. German zipcode
  237. </li><br>
  238. </ul>
  239. <br>
  240. <b>Get</b>
  241. <ul>
  242. <li><code>data</code>
  243. <br>
  244. Manually trigger data update
  245. </li><br>
  246. </ul>
  247. <br>
  248. <b>Readings</b>
  249. <ul>
  250. <li><code>city</code>
  251. <br>
  252. Name of the city the forecast is read for
  253. </li><br>
  254. <li><code>fc<i>n</i>_total</code>
  255. <br>
  256. Daily maximum levels for all allergens that are not being ignored due to <i>ignoreList</i><br/>
  257. </li><br>
  258. <li><code>fc<i>n</i>_day_of_week</code>
  259. <br>
  260. Weekday, can be localized through <i>weekdaysFormat</i><br/>
  261. </li><br>
  262. <li><code>fc<i>n</i>_<i>allergen</i></code>
  263. <br>
  264. Daily levels for all allergens that are not being ignored due to <i>ignoreList</i>
  265. </li><br>
  266. </ul>
  267. <br>
  268. <b>Attributes</b>
  269. <ul>
  270. <li><code>ignoreList</code>
  271. <br>
  272. Comma-separated list of allergen names that are to be ignored during updates and for cumulated day levels calculation
  273. </li><br>
  274. <li><code>updateEmpty</code>
  275. <br>
  276. Also update (and keep) level readings for inactive allergens that are otherwise removed
  277. </li><br>
  278. <li><code>updateIgnored</code>
  279. <br>
  280. Also update (and keep) level readings for ignored allergens that are otherwise removed
  281. </li><br>
  282. <li><code>levelsFormat</code>
  283. <br>
  284. Localize levels by adding them comma separated (default: -,low,moderate,high,extreme)
  285. </li><br>
  286. <li><code>weekdaysFormat</code>
  287. <br>
  288. Localize Weekdays by adding them comma separated (default: Sun,Mon,Tue,Wed,Thu,Fr,Sat)
  289. </li><br>
  290. </ul>
  291. </ul>
  292. =end html
  293. =begin html_DE
  294. <a name="allergy"></a>
  295. <h3>allergy</h3>
  296. <ul>
  297. <br>Dieses Modul prognostiziert Allergie Daten für Deutschland.</br>
  298. Es erfordert dass das Perlmodul XML:: Simple installiert ist.
  299. <br/><br/>
  300. <b>Define</b>
  301. <ul>
  302. <code>define &lt;name&gt; allergy &lt;Postleitzahl&gt;</code>
  303. <br>
  304. Beispiel: <code>define allergydata allergy 12345</code>
  305. <br><br>
  306. <li><code>Postleitzahl</code>
  307. <br>
  308. Deutsche Postleitzahl
  309. </li><br>
  310. </ul>
  311. <br>
  312. <b>Get</b>
  313. <ul>
  314. <li><code>data</code>
  315. <br>
  316. Manuelles Datenupdate
  317. </li><br>
  318. </ul>
  319. <br>
  320. <b>Readings</b>
  321. <ul>
  322. <li><code>city</code>
  323. <br>
  324. Name der Stadt, für die Prognosen gelesen werden.
  325. </li><br>
  326. <li><code>fc<i>n</i>_total</code>
  327. <br>
  328. Täglicher Höchstwerte für alle Allergene, die nicht aufgrund der Ignoreliste <i>(attr ignoreList)</i> ignoriert werden<br/>
  329. </li><br>
  330. <li><code>fc<i>n</i>_day_of_week</code>
  331. <br>
  332. Wochentag, kann durch <i>weekdaysFormat</i> lokalisiert werden.<br/>
  333. </li><br>
  334. <li><code>fc<i>n</i>_<i>allergen</i></code>
  335. <br>
  336. Tägliche Werte für alle Allergene, die nicht aufgrund der Ignoreliste <i>(attr ignoreList)</i> ignoriert werden.
  337. </li><br>
  338. </ul>
  339. <br>
  340. <b>Attribute</b>
  341. <ul>
  342. <li><code>ignoreList</code>
  343. <br>
  344. Kommagetrennte Liste von Allergen-Namen, die bei der Aktualisierung ignoriert werden sollen.
  345. <br>
  346. </li><br>
  347. <li><code>updateEmpty (Standard: 0|1)</code>
  348. <br>
  349. Aktualisierung von Allergenen.
  350. <code> <br>
  351. 0 = nur Allergene mit Belastung.
  352. <br>
  353. 1 = auch Allergene die keine Belastung haben.
  354. </code>
  355. </li><br>
  356. <li><code>updateIgnored (1)</code>
  357. <br>
  358. Aktualisierung von Allergenen, die sonst durch die ignoreList entfernt werden.
  359. </li><br>
  360. <li><code>levelsFormat (Standard: -,low,moderate,high,extreme)</code>
  361. <br>
  362. Lokalisierte Levels, durch Kommas getrennt.
  363. </li><br>
  364. <li><code>weekdaysFormat (Standard: Sun,Mon,Tue,Wed,Thu,Fri,Sat)</code>
  365. <br>
  366. Lokalisierte Wochentage, durch Kommas getrennt.
  367. </li><br>
  368. </ul>
  369. </ul>
  370. =end html_DE
  371. =cut