46_Aqicn.pm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. ###############################################################################
  2. #
  3. # Developed with Kate
  4. #
  5. # (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
  6. # All rights reserved
  7. #
  8. # Special thanks goes to comitters:
  9. # - maddhin(FHEM Forum) Thanks for Readings and Commandref
  10. #
  11. #
  12. # This script is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # any later version.
  16. #
  17. # The GNU General Public License can be found at
  18. # http://www.gnu.org/copyleft/gpl.html.
  19. # A copy is found in the textfile GPL.txt and important notices to the license
  20. # from the author is found in LICENSE.txt distributed with these scripts.
  21. #
  22. # This script is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. #
  28. # $Id: 46_Aqicn.pm 15326 2017-10-27 04:21:08Z CoolTux $
  29. #
  30. ###############################################################################
  31. ##
  32. ##
  33. ## Das JSON Modul immer in einem eval aufrufen
  34. # $data = eval{decode_json($data)};
  35. #
  36. # if($@){
  37. # Log3($SELF, 2, "$TYPE ($SELF) - error while request: $@");
  38. #
  39. # readingsSingleUpdate($hash, "state", "error", 1);
  40. #
  41. # return;
  42. # }
  43. #
  44. #######
  45. #######
  46. #
  47. ##
  48. ##
  49. package main;
  50. my $missingModul = "";
  51. use strict;
  52. use warnings;
  53. use HttpUtils;
  54. eval "use Encode qw(encode encode_utf8 decode_utf8);1" or $missingModul .= "Encode ";
  55. eval "use JSON;1" or $missingModul .= "JSON ";
  56. my $version = "0.2.1";
  57. ### Air Quality Index scale
  58. my %AQIS = (
  59. 1 => { 'i18nde' => 'Gut' ,'i18nen' => 'Good' ,'bgcolor' => '#009966' ,'font color' => '#FFFFFF'},
  60. 2 => { 'i18nde' => 'Moderat' ,'i18nen' => 'Moderate' ,'bgcolor' => '#ffde33' ,'font color' => '#000000'},
  61. 3 => { 'i18nde' => 'Ungesund für empfindliche Personengruppen' ,'i18nen' => 'Unhealthy for Sensitive Groups' ,'bgcolor' => '#ff9933' ,'font color' => '#000000'},
  62. 4 => { 'i18nde' => 'Ungesund' ,'i18nen' => 'Unhealthy' ,'bgcolor' => '#cc0033' ,'font color' => '#FFFFFF'},
  63. 5 => { 'i18nde' => 'Sehr ungesund' ,'i18nen' => 'Very Unhealthy' ,'bgcolor' => '#660099' ,'font color' => '#FFFFFF'},
  64. 6 => { 'i18nde' => 'Gefährlich' ,'i18nen' => 'Hazardous' ,'bgcolor' => '#7e0023' ,'font color' => '#FFFFFF'},
  65. );
  66. # Declare functions
  67. sub Aqicn_Attr(@);
  68. sub Aqicn_Define($$);
  69. sub Aqicn_Initialize($);
  70. sub Aqicn_Get($$@);
  71. sub Aqicn_Notify($$);
  72. sub Aqicn_GetData($;$);
  73. sub Aqicn_Undef($$);
  74. sub Aqicn_ResponseProcessing($$$);
  75. sub Aqicn_ReadingsProcessing_SearchStationResponse($$);
  76. sub Aqicn_ReadingsProcessing_AqiResponse($);
  77. sub Aqicn_ErrorHandling($$$);
  78. sub Aqicn_WriteReadings($$);
  79. sub Aqicn_Timer_GetData($);
  80. sub Aqicn_AirPollutionLevel($);
  81. sub Aqicn_HtmlStyle($);
  82. sub Aqicn_i18n_de($);
  83. sub Aqicn_i18n_en($);
  84. sub Aqicn_HealthImplications($$);
  85. my %paths = ( 'statussoe' => 'system_status/soe',
  86. 'aggregates' => 'meters/aggregates',
  87. 'siteinfo' => 'site_info',
  88. 'sitemaster' => 'sitemaster',
  89. 'powerwalls' => 'powerwalls',
  90. 'registration' => 'customer/registration',
  91. 'status' => 'status'
  92. );
  93. sub Aqicn_Initialize($) {
  94. my ($hash) = @_;
  95. # Consumer
  96. $hash->{GetFn} = "Aqicn_Get";
  97. $hash->{DefFn} = "Aqicn_Define";
  98. $hash->{UndefFn} = "Aqicn_Undef";
  99. $hash->{NotifyFn} = "Aqicn_Notify";
  100. $hash->{AttrFn} = "Aqicn_Attr";
  101. $hash->{AttrList} = "interval ".
  102. "disable:1 ".
  103. "language:de,en ".
  104. $readingFnAttributes;
  105. foreach my $d(sort keys %{$modules{Aqicn}{defptr}}) {
  106. my $hash = $modules{Aqicn}{defptr}{$d};
  107. $hash->{VERSION} = $version;
  108. }
  109. }
  110. sub Aqicn_Define($$) {
  111. my ( $hash, $def ) = @_;
  112. my @a = split( "[ \t][ \t]*", $def );
  113. if( $a[2] =~ /^token=/ ) {
  114. $a[2] =~ m/token=([^\s]*)/;
  115. $hash->{TOKEN} = $1;
  116. } else {
  117. $hash->{UID} = $a[2];
  118. }
  119. return "Cannot define a Aqicn device. Perl modul $missingModul is missing." if ( $missingModul );
  120. return "too few parameters: define <name> Aqicn <OPTION-PARAMETER>" if( @a != 3 );
  121. return "too few parameters: define <name> Aqicn token=<TOKEN-KEY>" if( not defined($hash->{TOKEN}) and not defined($modules{Aqicn}{defptr}{TOKEN}) );
  122. return "too few parameters: define <name> Aqicn <STATION-UID>" if( not defined($hash->{UID}) and defined($modules{Aqicn}{defptr}{TOKEN}) );
  123. my $name = $a[0];
  124. $hash->{VERSION} = $version;
  125. $hash->{NOTIFYDEV} = "global";
  126. if( defined($hash->{TOKEN}) ) {
  127. return "there is already a Aqicn Head Device, did you want to define a Aqicn station use: define <name> Aqicn <STATION-UID>" if( $modules{Aqicn}{defptr}{TOKEN} );
  128. $hash->{HOST} = 'api.waqi.info';
  129. $attr{$name}{room} = "AQICN" if( !defined( $attr{$name}{room} ) );
  130. readingsSingleUpdate ( $hash, "state", "ready for search", 1 );
  131. Log3 $name, 3, "Aqicn ($name) - defined Aqicn Head Device with API-Key $hash->{TOKEN}";
  132. $modules{Aqicn}{defptr}{TOKEN} = $hash;
  133. } elsif( defined($hash->{UID}) ) {
  134. $attr{$name}{room} = "AQICN" if( !defined( $attr{$name}{room} ) );
  135. $hash->{INTERVAL} = 3600;
  136. $hash->{HEADDEVICE} = $modules{Aqicn}{defptr}{TOKEN}->{NAME};
  137. readingsSingleUpdate ( $hash, "state", "initialized", 1 );
  138. Log3 $name, 3, "Aqicn ($name) - defined Aqicn Station Device with Station UID $hash->{UID}";
  139. $modules{Aqicn}{defptr}{UID} = $hash;
  140. }
  141. return undef;
  142. }
  143. sub Aqicn_Undef($$) {
  144. my ( $hash, $arg ) = @_;
  145. my $name = $hash->{NAME};
  146. if( defined($modules{Aqicn}{defptr}{TOKEN}) and $hash->{TOKEN} ) {
  147. return "there is a Aqicn Station Device present, please delete all Station Device first"
  148. unless( not defined($modules{Aqicn}{defptr}{UID}) );
  149. delete $modules{Aqicn}{defptr}{TOKEN};
  150. } elsif( defined($modules{Aqicn}{defptr}{UID}) and $hash->{UID} ) {
  151. delete $modules{Aqicn}{defptr}{UID};
  152. }
  153. RemoveInternalTimer( $hash );
  154. Log3 $name, 3, "Aqicn ($name) - Device $name deleted";
  155. return undef;
  156. }
  157. sub Aqicn_Attr(@) {
  158. my ( $cmd, $name, $attrName, $attrVal ) = @_;
  159. my $hash = $defs{$name};
  160. if( $attrName eq "disable" ) {
  161. if( $cmd eq "set" and $attrVal eq "1" ) {
  162. RemoveInternalTimer($hash);
  163. readingsSingleUpdate ( $hash, "state", "disabled", 1 );
  164. Log3 $name, 3, "Aqicn ($name) - disabled";
  165. } elsif( $cmd eq "del" ) {
  166. Log3 $name, 3, "Aqicn ($name) - enabled";
  167. }
  168. }
  169. if( $attrName eq "disabledForIntervals" ) {
  170. if( $cmd eq "set" ) {
  171. return "check disabledForIntervals Syntax HH:MM-HH:MM or 'HH:MM-HH:MM HH:MM-HH:MM ...'"
  172. unless($attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/);
  173. Log3 $name, 3, "Aqicn ($name) - disabledForIntervals";
  174. readingsSingleUpdate ( $hash, "state", "disabled", 1 );
  175. } elsif( $cmd eq "del" ) {
  176. Log3 $name, 3, "Aqicn ($name) - enabled";
  177. readingsSingleUpdate( $hash, "state", "active", 1 );
  178. }
  179. }
  180. if( $attrName eq "interval" ) {
  181. if( $cmd eq "set" ) {
  182. if( $attrVal < 30 ) {
  183. Log3 $name, 3, "Aqicn ($name) - interval too small, please use something >= 30 (sec), default is 300 (sec)";
  184. return "interval too small, please use something >= 30 (sec), default is 300 (sec)";
  185. } else {
  186. RemoveInternalTimer($hash);
  187. $hash->{INTERVAL} = $attrVal;
  188. Log3 $name, 3, "Aqicn ($name) - set interval to $attrVal";
  189. Aqicn_Timer_GetData($hash);
  190. }
  191. } elsif( $cmd eq "del" ) {
  192. RemoveInternalTimer($hash);
  193. $hash->{INTERVAL} = 300;
  194. Log3 $name, 3, "Aqicn ($name) - set interval to default";
  195. Aqicn_Timer_GetData($hash);
  196. }
  197. }
  198. return undef;
  199. }
  200. sub Aqicn_Notify($$) {
  201. my ($hash,$dev) = @_;
  202. my $name = $hash->{NAME};
  203. return if (IsDisabled($name));
  204. my $devname = $dev->{NAME};
  205. my $devtype = $dev->{TYPE};
  206. my $events = deviceEvents($dev,1);
  207. return if (!$events);
  208. Aqicn_Timer_GetData($hash) if( (grep /^INITIALIZED$/,@{$events}
  209. or grep /^DELETEATTR.$name.disable$/,@{$events}
  210. or (grep /^DEFINED.$name$/,@{$events} and $init_done))
  211. and defined($hash->{UID}) );
  212. return;
  213. }
  214. sub Aqicn_Get($$@) {
  215. my ($hash, $name, @aa) = @_;
  216. my ($cmd, @args) = @aa;
  217. if( $cmd eq 'update' ) {
  218. Aqicn_GetData($hash);
  219. return undef;
  220. } elsif( $cmd eq 'stationSearchByCity' ) {
  221. return "usage: $cmd" if( @args == 0 );
  222. my $city = join( " ", @args );
  223. my $ret;
  224. $ret = Aqicn_GetData($hash,$city);
  225. return $ret;
  226. } else {
  227. my $list = '';
  228. $list .= 'update:noArg' if( defined($hash->{UID}) );
  229. $list .= 'stationSearchByCity' if( defined($hash->{TOKEN}) );
  230. return "Unknown argument $cmd, choose one of $list";
  231. }
  232. }
  233. sub Aqicn_Timer_GetData($) {
  234. my $hash = shift;
  235. my $name = $hash->{NAME};
  236. if( not IsDisabled($name) ) {
  237. Aqicn_GetData($hash);
  238. } else {
  239. readingsSingleUpdate($hash,'state','disabled',1);
  240. }
  241. InternalTimer( gettimeofday()+$hash->{INTERVAL}, 'Aqicn_Timer_GetData', $hash );
  242. Log3 $name, 4, "Aqicn ($name) - Call InternalTimer Aqicn_Timer_GetData";
  243. }
  244. sub Aqicn_GetData($;$) {
  245. my ($hash,$cityName) = @_;
  246. my $name = $hash->{NAME};
  247. my $host = $modules{Aqicn}{defptr}{TOKEN}->{HOST};
  248. my $token = $modules{Aqicn}{defptr}{TOKEN}->{TOKEN};
  249. my $uri;
  250. if( $hash->{UID} ) {
  251. my $uid = $hash->{UID};
  252. $uri = $host . '/feed/@' . $hash->{UID} . '/?token=' . $token;
  253. readingsSingleUpdate($hash,'state','fetch data',1);
  254. } else {
  255. $uri = $host . '/search/?token=' . $token . '&keyword=' . urlEncode($cityName);
  256. }
  257. my $param = {
  258. url => "https://".$uri,
  259. timeout => 5,
  260. method => 'GET',
  261. hash => $hash,
  262. doTrigger => 1,
  263. callback => \&Aqicn_ErrorHandling,
  264. };
  265. $param->{cl} = $hash->{CL} if( $hash->{TOKEN} and ref($hash->{CL}) eq 'HASH' );
  266. HttpUtils_NonblockingGet($param);
  267. Log3 $name, 4, "Aqicn ($name) - Send with URI: https://$uri";
  268. }
  269. sub Aqicn_ErrorHandling($$$) {
  270. my ($param,$err,$data) = @_;
  271. my $hash = $param->{hash};
  272. my $name = $hash->{NAME};
  273. Log3 $name, 4, "Aqicn ($name) - Recieve JSON data: $data";
  274. #Log3 $name, 3, "Aqicn ($name) - Recieve HTTP Code: $param->{code}";
  275. #Log3 $name, 3, "Aqicn ($name) - Recieve Error: $err";
  276. ### Begin Error Handling
  277. if( defined( $err ) ) {
  278. if( $err ne "" ) {
  279. if( $param->{cl} && $param->{cl}{canAsyncOutput} ) {
  280. asyncOutput( $param->{cl}, "Request Error: $err\r\n" );
  281. }
  282. readingsBeginUpdate( $hash );
  283. readingsBulkUpdate( $hash, 'state', $err, 1);
  284. readingsBulkUpdate( $hash, 'lastRequestError', $err, 1 );
  285. readingsEndUpdate( $hash, 1 );
  286. Log3 $name, 3, "Aqicn ($name) - RequestERROR: $err";
  287. return;
  288. }
  289. }
  290. if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
  291. #if( $param->{cl} && $param->{cl}{canAsyncOutput} ) {
  292. # asyncOutput( $param->{cl}, "Request Error: $param->{code}\r\n" );
  293. #}
  294. readingsBeginUpdate( $hash );
  295. readingsBulkUpdate( $hash, 'state', $param->{code}, 1 );
  296. readingsBulkUpdate( $hash, 'lastRequestError', $param->{code}, 1 );
  297. Log3 $name, 3, "Aqicn ($name) - RequestERROR: ".$param->{code};
  298. readingsEndUpdate( $hash, 1 );
  299. Log3 $name, 5, "Aqicn ($name) - RequestERROR: received http code ".$param->{code}." without any data after requesting";
  300. return;
  301. }
  302. if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
  303. #if( $param->{cl} && $param->{cl}{canAsyncOutput} ) {
  304. # asyncOutput( $param->{cl}, "Request Error: $param->{code}\r\n" );
  305. #}
  306. readingsBeginUpdate( $hash );
  307. readingsBulkUpdate( $hash, 'state', $param->{code}, 1 );
  308. readingsBulkUpdate( $hash, "lastRequestError", $param->{code}, 1 );
  309. readingsEndUpdate( $hash, 1 );
  310. Log3 $name, 3, "Aqicn ($name) - statusRequestERROR: http error ".$param->{code};
  311. return;
  312. ### End Error Handling
  313. }
  314. Log3 $name, 4, "Aqicn ($name) - Recieve JSON data: $data";
  315. Aqicn_ResponseProcessing($hash,$data,$param);
  316. }
  317. sub Aqicn_ResponseProcessing($$$) {
  318. my ($hash,$json,$param) = @_;
  319. my $name = $hash->{NAME};
  320. my $decode_json;
  321. my $readings;
  322. $decode_json = eval{decode_json($json)};
  323. if($@){
  324. Log3 $name, 4, "Aqicn ($name) - error while request: $@";
  325. readingsBeginUpdate($hash);
  326. readingsBulkUpdate($hash, 'JSON_Error', $@);
  327. readingsBulkUpdate($hash, 'httpCode', $param->{code});
  328. readingsBulkUpdate($hash, 'state', 'JSON error');
  329. readingsEndUpdate($hash,1);
  330. return;
  331. }
  332. #### Verarbeitung der Readings zum passenden
  333. if( $hash->{TOKEN} ) {
  334. Aqicn_ReadingsProcessing_SearchStationResponse($decode_json,$param);
  335. readingsSingleUpdate($hash,'state','search finished',1);
  336. return;
  337. } elsif( $hash->{UID} ) {
  338. $readings = Aqicn_ReadingsProcessing_AqiResponse($decode_json);
  339. }
  340. Aqicn_WriteReadings($hash,$readings);
  341. }
  342. sub Aqicn_WriteReadings($$) {
  343. my ($hash,$readings) = @_;
  344. my $name = $hash->{NAME};
  345. Log3 $name, 4, "Aqicn ($name) - Write Readings";
  346. readingsBeginUpdate($hash);
  347. while( my ($r,$v) = each %{$readings} ) {
  348. readingsBulkUpdate($hash,$r,$v);
  349. }
  350. if( defined($readings->{'PM2.5-AQI'}) ) {
  351. readingsBulkUpdateIfChanged($hash,'htmlStyle','<div style="background-color: '.$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'bgcolor'}.';"><font color="'.$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'font color'}.'">'.( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nde'}: $readings->{'PM2.5-AQI'} " : " $AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nen'}: $readings->{'PM2.5-AQI'}").'</div>');
  352. readingsBulkUpdateIfChanged($hash,'state',( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nde'}: $readings->{'PM2.5-AQI'}" : "$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nen'}: $readings->{'PM2.5-AQI'}") );
  353. readingsBulkUpdateIfChanged($hash,'APL',( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nde'}" : "$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'i18nen'}") );
  354. readingsBulkUpdateIfChanged($hash,'healthImplications',Aqicn_HealthImplications($hash,Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})) );
  355. } else {
  356. readingsBulkUpdateIfChanged($hash,'htmlStyle','<div style="background-color: '.$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'bgcolor'}.';"><font color="'.$AQIS{Aqicn_AirPollutionLevel($readings->{'PM2.5-AQI'})}{'font color'}.'">'.( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nde'}: $readings->{'AQI'} " : " $AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nen'}: $readings->{'AQI'}").'</div>');
  357. readingsBulkUpdateIfChanged($hash,'state',( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nde'}: $readings->{'AQI'}" : "$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nen'}: $readings->{'AQI'}") );
  358. readingsBulkUpdateIfChanged($hash,'APL',( ((AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en') ? "$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nde'}" : "$AQIS{Aqicn_AirPollutionLevel($readings->{'AQI'})}{'i18nen'}") );
  359. readingsBulkUpdateIfChanged($hash,'healthImplications',Aqicn_HealthImplications($hash,Aqicn_AirPollutionLevel($readings->{'AQI'})) );
  360. }
  361. readingsEndUpdate($hash,1);
  362. }
  363. #####
  364. #####
  365. ## my little Helper
  366. sub Aqicn_ReadingsProcessing_SearchStationResponse($$) {
  367. my ($decode_json,$param) = @_;
  368. if( $param->{cl} and $param->{cl}->{TYPE} eq 'FHEMWEB' ) {
  369. my $ret = '<html><table><tr><td>';
  370. $ret .= '<table class="block wide">';
  371. $ret .= '<tr class="even">';
  372. $ret .= "<td><b>City</b></td>";
  373. $ret .= "<td><b>Last Update Time</b></td>";
  374. $ret .= "<td><b>Latitude</b></td>";
  375. $ret .= "<td><b>Longitude</b></td>";
  376. $ret .= "<td></td>";
  377. $ret .= '</tr>';
  378. if( ref($decode_json->{data}) eq "ARRAY" and scalar(@{$decode_json->{data}}) > 0 ) {
  379. my $linecount=1;
  380. foreach my $dataset (@{$decode_json->{data}}) {
  381. if ( $linecount % 2 == 0 ) {
  382. $ret .= '<tr class="even">';
  383. } else {
  384. $ret .= '<tr class="odd">';
  385. }
  386. $dataset->{station}{name} =~ s/'//g;
  387. $ret .= "<td>".encode_utf8($dataset->{station}{name})."</td>";
  388. $ret .= "<td>$dataset->{'time'}{stime}</td>";
  389. $ret .= "<td>$dataset->{station}{geo}[0]</td>";
  390. $ret .= "<td>$dataset->{station}{geo}[1]</td>";
  391. ###### create Links
  392. my $aHref;
  393. # create Google Map Link
  394. $aHref="<a target=\"_blank\" href=\"https://www.google.de/maps/search/".$dataset->{station}{geo}[0]."+".$dataset->{station}{geo}[1]."\">Station on Google Maps</a>";
  395. $ret .= "<td>".$aHref."</td>";
  396. # create define Link
  397. my @headerHost = grep /Origin/, @FW_httpheader;
  398. $headerHost[0] = 'Origin: no Hostname at FHEMWEB Header available'
  399. unless( defined($headerHost[0]) );
  400. $headerHost[0] =~ m/Origin:.([^\s]*)/;
  401. $headerHost[0] = $1;
  402. $aHref="<a href=\"".$headerHost[0]."/fhem?cmd=define+".makeDeviceName($dataset->{station}{name})."+Aqicn+".$dataset->{uid}.$FW_CSRF."\">Create Station Device</a>";
  403. $ret .= "<td>".$aHref."</td>";
  404. $ret .= '</tr>';
  405. $linecount++;
  406. }
  407. }
  408. $ret .= '</table></td></tr>';
  409. $ret .= '</table></html>';
  410. asyncOutput( $param->{cl}, $ret ) if( $param->{cl} and $param->{cl}{canAsyncOutput} );
  411. return;
  412. } elsif( $param->{cl} and $param->{cl}->{TYPE} eq 'telnet' ) {
  413. my $ret = '';
  414. foreach my $dataset (@{$decode_json->{data}}) {
  415. $ret .= encode_utf8($dataset->{station}{name}) . "| $dataset->{'time'}{stime} | $dataset->{station}{geo}[0] | $dataset->{station}{geo}[1] | define " . makeDeviceName($dataset->{station}{name}) . " Aqicn $dataset->{uid}\r\n";
  416. }
  417. asyncOutput( $param->{cl}, $ret ) if( $param->{cl} && $param->{cl}{canAsyncOutput} );
  418. return;
  419. }
  420. }
  421. sub Aqicn_ReadingsProcessing_AqiResponse($) {
  422. my ($decode_json) = @_;
  423. my %readings;
  424. $readings{'CO-AQI'} = $decode_json->{data}{iaqi}{co}{v};
  425. $readings{'NO2-AQI'} = $decode_json->{data}{iaqi}{no2}{v};
  426. $readings{'PM10-AQI'} = $decode_json->{data}{iaqi}{pm10}{v};
  427. $readings{'PM2.5-AQI'} = $decode_json->{data}{iaqi}{pm25}{v};
  428. $readings{'AQI'} = $decode_json->{data}{aqi};
  429. $readings{'O3-AQI'} = $decode_json->{data}{iaqi}{o3}{v};
  430. $readings{'SO2-AQI'} = $decode_json->{data}{iaqi}{so2}{v};
  431. $readings{'temperature'} = $decode_json->{data}{iaqi}{t}{v};
  432. $readings{'pressure'} = $decode_json->{data}{iaqi}{p}{v};
  433. $readings{'humidity'} = $decode_json->{data}{iaqi}{h}{v};
  434. $readings{'status'} = $decode_json->{status};
  435. $readings{'pubDate'} = $decode_json->{data}{time}{s};
  436. $readings{'pubUnixTime'} = $decode_json->{data}{time}{v};
  437. $readings{'pubTimezone'} = $decode_json->{data}{time}{tz};
  438. $readings{'windSpeed'} = $decode_json->{data}{iaqi}{w}{v};
  439. $readings{'windDirection'} = $decode_json->{data}{iaqi}{wd}{v};
  440. $readings{'dewpoint'} = $decode_json->{data}{iaqi}{d}{v};
  441. $readings{'dominatPoll'} = $decode_json->{data}{dominentpol};
  442. return \%readings;
  443. }
  444. sub Aqicn_AirPollutionLevel($) {
  445. my $aqi = shift;
  446. my $apl;
  447. if($aqi < 51) { $apl = 1}
  448. elsif($aqi < 101) { $apl = 2}
  449. elsif($aqi < 151) { $apl = 3}
  450. elsif($aqi < 201) { $apl = 4}
  451. elsif($aqi < 301) { $apl = 5}
  452. else { $apl = 6}
  453. return $apl;
  454. }
  455. sub Aqicn_HealthImplications($$) {
  456. my ($hash,$apl) = @_;
  457. my $name = $hash->{NAME};
  458. my %HIen = (
  459. 1 => 'Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution.',
  460. 2 => 'Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution.',
  461. 3 => 'Members of sensitive groups may experience health effects. The general public is not likely to be affected.',
  462. 4 => 'Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects',
  463. 5 => 'Health warnings of emergency conditions. The entire population is more likely to be affected.',
  464. 6 => 'Health alert: everyone may experience more serious health effects'
  465. );
  466. my %HIde = (
  467. 1 => 'Die Qualität der Luft gilt als zufriedenstellend und die Luftverschmutzung stellt ein geringes oder kein Risiko dar',
  468. 2 => 'Die Luftqualität ist insgesamt akzeptabel. Bei manchen Schadstoffe besteht jedoch eventuell eine geringe Gesundheitsgefahr für einen sehr kleinen Personenkreis, der sehr empfindlich auf Luftverschmutzung ist.',
  469. 3 => 'Bei Mitgliedern von empfindlichen Personengruppen können gesundheitliche Auswirkungen auftreten. Die allgemeine Öffentlichkeit ist wahrscheinlich nicht betroffen.',
  470. 4 => 'Erste gesundheitliche Auswirkungen können sich bei allen Personen einstellen. Bei empfindlichen Personengruppen können ernstere gesundheitliche Auswirkungen auftreten.',
  471. 5 => 'Gesundheitswarnung aufgrund einer Notfallsituation. Die gesamte Bevölkerung ist voraussichtlich betroffen.',
  472. 6 => 'Gesundheitsalarm: Jeder muss mit dem Auftreten ernsterer Gesundheitsschäden rechnen'
  473. );
  474. return ( (AttrVal('global','language','none') eq 'DE' or AttrVal($name,'language','none') eq 'de') and AttrVal($name,'language','none') ne 'en' ? $HIde{$apl} : $HIen{$apl} );
  475. }
  476. 1;
  477. =pod
  478. =item device
  479. =item summary Air Quality Index proving a transparent Air Quality information
  480. =item summary_DE Air Quality Index Nachweis einer transparenten Luftqualitätsinformation
  481. =begin html
  482. <a name="Aqicn"></a>
  483. <h3>Air Quality Index</h3>
  484. <ul>
  485. This modul fetch Air Quality data from http://aqicn.org.
  486. <br><br>
  487. <a name="Aqicndefine"></a>
  488. <b>Define</b>
  489. <ul><br>
  490. <code>define &lt;name&gt; Aqicn</code>
  491. <br><br>
  492. Example:
  493. <ul><br>
  494. <code>define aqicnMaster Aqicn</code><br>
  495. </ul>
  496. <br>
  497. This statement creates the Aqicn Master Device.<br>
  498. After the device has been created, you can search Aqicn Station by city name and create automatically the station device.
  499. </ul>
  500. <br><br>
  501. <a name="Aqicnreadings"></a>
  502. <b>Readings</b>
  503. <ul>
  504. <li>APL - Air Pollution Level</li>
  505. <li>AQI - Air Quality Index (AQI) of the dominant pollutant in city. Values are converted from µg/m³ to AQI level using US EPA standards. For more detailed information: https://en.wikipedia.org/wiki/Air_quality_index and https://www.airnow.gov/index.cfm?action=aqi_brochure.index. </li>
  506. <li>CO-AQI - AQI of CO (carbon monoxide). An AQI of 100 for carbon monoxide corresponds to a level of 9 parts per million (averaged over 8 hours).</li>
  507. <li>NO2-AQI - AQI of NO2 (nitrogen dioxide). See also https://www.airnow.gov/index.cfm?action=pubs.aqiguidenox</li>
  508. <li>PM10-AQI - AQI of PM10 (respirable particulate matter). For particles up to 10 micrometers in diameter: An AQI of 100 corresponds to 150 micrograms per cubic meter (averaged over 24 hours).</li>
  509. <li>PM2.5-AQI - AQI of PM2.5 (fine particulate matter). For particles up to 2.5 micrometers in diameter: An AQI of 100 corresponds to 35 micrograms per cubic meter (averaged over 24 hours).</li>
  510. <li>O3-AQI - AQI of O3 (ozone). An AQI of 100 for ozone corresponds to an ozone level of 0.075 parts per million (averaged over 8 hours). See also https://www.airnow.gov/index.cfm?action=pubs.aqiguideozone</li>
  511. <li>SO2-AQI - AQI of SO2 (sulfur dioxide). An AQI of 100 for sulfur dioxide corresponds to a level of 75 parts per billion (averaged over one hour).</li>
  512. <li>temperature - Temperature in degrees Celsius</li>
  513. <li>pressure - Atmospheric pressure in hectopascals (hPa)</li>
  514. <li>humidity - Relative humidity in percent</li>
  515. <li>state- Current AQI and air pollution level</li>
  516. <li>status - condition of the data</li>
  517. <li>pubDate- Local time of publishing the data</li>
  518. <li>pubUnixTime - Unix time stamp of local time but converted wrongly, if local time is e.g. 1300 GMT+1, the time stamp shows 1300 UTC.</li>
  519. <li>pubTimezone - Time zone of the city (UTC)</li>
  520. <li>windspeed - Wind speed in kilometer per hour</li>
  521. <li>windDirection - Wind direction</li>
  522. <li>dominatPoll - Dominant pollutant in city</li>
  523. <li>dewpoint - Dew in degrees Celsius</li>
  524. <li>healthImplications - Information about Health Implications</li>
  525. <li>htmlStyle - can be used to format the STATE and FHEMWEB (Example: stateFormate htmlStyle</li>
  526. </ul>
  527. <br>
  528. <a name="Aqicnget"></a>
  529. <b>get</b>
  530. <ul>
  531. <li>stationSearchByCity - search station by city name and open the result in seperate popup window</li>
  532. <li>update - fetch new data every x times</li>
  533. </ul>
  534. <br>
  535. <a name="Aqicnattribute"></a>
  536. <b>Attribute</b>
  537. <ul>
  538. <li>interval - interval in seconds for automatically fetch data (default 3600)</li>
  539. </ul>
  540. </ul>
  541. =end html
  542. =begin html_DE
  543. <a name="Aqicn"></a>
  544. <h3>Air Quality Index</h3>
  545. =end html_DE
  546. =cut