$pageLoaded) { $string .= ' => check script name: '.$pageName; } $SITE['wsModules'][$pageLoaded] = $string; ws_message (''); # ------------------------------------- SETTINGS $cacheDir = './cache/'; # replaced with $SITE["cacheDir"] $cacheTime = 200; ##### seconds when using luftdaten.info $cacheTimeExtra = 10; # when no data retrieved, use 10 times older cache (2000 seconds => 3 hours) # $language = 'en'; # replaced with $SITE["lang"] $timezone = 'Europe/Brussels'; # replaced with $SITE["tz"] #$timezone = 'America/New_York'; $local_date_ft = 'd-m-Y H:i'; # replaced with $SITE["timeFormat"] $local_time_ft = 'H:i'; # replaced with $SITE["timeOnlyFormat"] # $debugOn = true; #$debugOn = false; # $yourCharset = 'UTF-8'; $local_title = 'aq_local_title'; $luftdt_html = true; # enclosing html generated also # $fullAQpage = '?p=mnu_aqhi_luft'; # link to full page for the small output (component) # '?p=test'; #$fullAQpage = false; # we do not need a link to the full page # $arr_all = array('P1','P2'); # all sensors used $arr_show_large = array('P1','P2'); # for the large html (full page) $arr_show_small = array('P1','P2'); # for the small html # $is_current = 'P2'; # for the single number on a startpage / header a.s.o. $current_style = 'font-size: 16pt; -webkit-border-radius: 5px; border-radius: 5px;'; # #----------------------------------------------- # END OF USER SETTINGS # ---------------------------------- TESTFILES # $useTestFile = $useOwnDir.'/last_luft_data.json'; $useTestFile = false; #----------------------------------------------- $ws_curl_follow = 1; # ---------------------------- check environment if (isset ($SITE["region"]) ) { $luftdt_html = false;} # if (isset ($SITE['cacheDir']) ) { $cacheDir = $SITE['cacheDir'];} elseif (isset ($SITE['cacheFileDir']) ) { $cacheDir = $SITE['cacheFileDir'];} # if (!isset ($lang) ) { if (isset ($SITE['lang']) ) { $lang = $SITE['lang'];} else { $lang = $language;}} if (isset($_REQUEST['lang']) ) { $lang = $_REQUEST['lang'];} # if (!isset ($SITE["tz"])) { if (!function_exists('date_default_timezone_set')) { putenv("TZ=" . $timezone);} else { date_default_timezone_set($timezone);} } # if (isset ($SITE["timeFormat"]) ) { $local_date_ft = $SITE["timeFormat"];} #----------------------------------------------- # check legends / threshold values #----------------------------------------------- if (!isset ($aqhi_type)) {$aqhi_type = 'US';} # ws_load_aq_langs (); # echo langtransstr($local_title); exit; aqhi_load_arrays_luftdt() ; # if (isset($_REQUEST['debug']) ) { $wsDebug = true;} else { $wsDebug = $debugOn;} # # ------------------------------- set file names $cacheFile = $cacheDir.basename(__FILE__,'.php').'_'.$apiLuftID; $cacheFake = $cacheDir.basename(__FILE__,'.php').'_xxxx'; $cacheTimeSaved = $cacheTime; $ws_msg_string = false; # $luftdtURL = 'http://data.sensor.community/airrohr/v1/sensor/'.$apiLuftID.'/'; $fakeURL = 'http://data.sensor.community/airrohr/v1/sensor/xxxx/'; ws_message (ws_debug_times('module '.basename(__FILE__).' ('.__LINE__.'):'),true); # # ----------------------- do we skip cached data if (isset($_REQUEST['force']) && $_REQUEST['force'] == 'luftdt') { ws_message('',true ); $cacheTime = -1; unset ($_REQUEST['force']); } # # --------------- do we have to use cached data if (isset($_REQUEST['cache']) && $_REQUEST['cache'] == 'luftdt') { ws_message('',true ); $cacheTime = 9999999999; unset ($_REQUEST['cache']); } # if ($useTestFile <> false || $useOwnFile <> false) { ws_message('',true ); $cacheTime = -1;} # ------------------ check available cached data $aq_warn_color = 'black'; $cache_used = false; if ($cacheTime > 0) { $cache_used = ws_load_cache($cacheFile, $cacheTime,$cacheFake); if ($cache_used <> false) { $aq_warn_color = $cache_used['wanrcolor'];}} ws_message (ws_debug_times('module '.basename(__FILE__).' ('.__LINE__.'):'),true); # # ------------------ do we need to load new data $new_data = false; $correct_data = true; # if ($cache_used == false) { $dataLoaded = 'luftdaten'; if ($useTestFile === false && !$useOwnFile === false) { $dataLoaded = 'own'; $useTestFile = $useOwnData;} $new_data = ws_makeRequest($luftdtURL, $useTestFile, $fakeURL); if ($new_data == false) { ws_message('',true ); $cacheTime = $cacheTimeSaved * $cacheTimeExtra; $cache_used = ws_load_cache($cacheFile, $cacheTime,$cacheFake); if ($cache_used == false) { ws_message('',true ); $aq_fullpage_html = $aq_condensed_html = ''; $current_AQ = '?'; return false;} $aq_warn_color = 'red'; $cache_used['wanrcolor']= $aq_warn_color;} ws_message (ws_debug_times('module '.basename(__FILE__).' ('.__LINE__.'):'),true); } if ($new_data <> false) { $json = ws_json_check($new_data); #echo '
'.print_r($json,true); 
        $aq             = array();
        if ($dataLoaded == 'own')
# we use 1 sensor value as uploaded to our server
             {  $arr    = $json['sensordatavalues'];
                foreach ($arr as $mes)
                     {  $type   = $mes['value_type'];
                        if (isset ($useOwn[$type]) )
                             {  $sensor = $useOwn[$type];
                                $aq[$sensor]['mes'] = $mes['value'];}
                        else {  continue;}} #  echo '
'.print_r($aq,true); exit;
                $userSensor     = $json['esp8266id'];       
                $aq['location'] = 'sensor '.$userSensor;
                $aq['timestamp']= filemtime($useOwnData);
                $difference     = time() - $aq['timestamp'];
                if     ($difference > 5 * 60) 
                     {  $aq_warn_color  = 'red';} 
                elseif ($difference > 3 * 60)
                     {  $aq_warn_color  = 'orange';}}  
# or we use either luftdaten data url or testfile
        else {  $use_mes        = count($json) - 1;
                if ($use_mes == 0) {$aq_warn_color  = 'orange';}
                $data           = $json[$use_mes]['sensordatavalues'];          #echo '
'.print_r($data,true); exit;
                foreach ($data as $arr)
                     {  $mes            = $arr['value'];
                        $key            = $arr['value_type'];
                        $aq[$key]['mes']= $mes;}                #echo '
'.print_r($aq,true); exit;
                $loc            = $json[$use_mes]['location'];
                $aq['location'] = $loc['country'].'-'.$loc['id'].'-'.$loc['latitude'].'-'.$loc['longitude'];
                $aq['timestamp']= strtotime($json[$use_mes]['timestamp'].' UTC'); }  
        $aq['wanrcolor']= $aq_warn_color;    #echo '
halt'.print_r($aq,true); exit;          
# calculate AQHI for the measurments                                        
        foreach ($aq as $key => $arr)
             {  if (!in_array ($key,$arr_all) ) {continue;}
                $arr2   = calc_aq($arr['mes'],$key);   # echo '
'.__LINE__.print_r($arr2,true).'
'; # exit; $arr['aqnr'] = $arr2[0]; $arr['nr'] = $arr2[1]; $aq[$key] = $arr; } # echo '
'.print_r($aq,true); exit;
        #
        if ($useOwn24hours == true && $useOwnFile <> false) 
             {  $today  = date('Ymd');
                $unix   = time() - 24*3600;  
                $Yday   = date('Ymd',$unix); #echo $Yday; exit;
                $oldest = date('c',$unix);
                $dataYd = $useOwnDir.'/data-esp8266-'.$userSensor.'-'.$Yday.'.csv';
                $fakeURL= $useOwnDir.'/data-mysensor-'.$Yday.'.csv';
                $fileYd = ws_makeRequest($dataYd, true, $fakeURL);
                if ($fileYd == false) {$fileYd   = '';}
                $dataTd = $useOwnDir.'/data-esp8266-'.$userSensor.'-'.$today.'.csv';
                $fakeURL= $useOwnDir.'/data-mysensor-'.$today.'.csv';
                $fileTd = ws_makeRequest($dataTd, true, $fakeURL);
                if ($fileTd == false) {$fileTd   = '';}
                $lines  = (explode ("\n",$fileYd.$fileTd) );
                if (!is_array($lines) ) {$lines = array();}
                $count  = count($lines);
                $locations      = array();
                $values         = array();
                $nr_mes         = 0;
                if ($count > 0) 
                     {  $arr    = explode (';',$lines[0]); } #echo '
'.print_r($arr,true); exit;
                else {  $arr    = array('n/a'); }
                if ($arr[0] == 'time') 
                     {  for ($n = 0; $n < count($arr); $n++)    # check every name if we need it
                             {  $string = $arr[$n];
                                foreach ($useOwn as $key => $sensor)     
                                     {  if ($key == $string)            # is a needed sensor
                                             {  $locations[$sensor]     = $n;
                                                $values[$sensor]        = 0.0;
                                                }
                                        } // eo search sensor
                                } // eo for every name
                        for ($n = 1; $n < $count; $n++)
                             {  $arr    = explode (';',$lines[$n]);  
                                if ($arr[0] == 'time') {continue;}
                                if ($arr[0] < $oldest) {continue;} #echo   $lines[$n]; exit; 
                                foreach ($locations as $sensor => $location )
                                     {  $value  = (float) $arr[$location];
                                        $values[$sensor] += $value;}  # echo '
'.print_r($values,true); exit;
                                $nr_mes++;} // eo for loop lines
# -------------------------- calculate averages
                        $string = ' nr of mes: '.$nr_mes;
                        foreach ($values as $sensor => $value)
                             {  $avg    =  round(($value / $nr_mes),2);
                                $string .= ' - '.$sensor.' total = '.$value.' - avg = '.$avg;
                                $aq[$sensor]['24avg']   =$avg; }
                        ws_message('');
                        } // eo found names
                        foreach ($aq as $key => $arr)
                             {  if (!in_array ($key,$arr_all) ) {continue;}
                                if (!isset ($arr['24avg']) )    {continue;}
                                $arr2   = calc_aq($arr['24avg'],$key);
                                $arr['aqnr24']    = $arr2[0];     #echo '
'.print_r($arr2,true); exit;
                                $arr['nr24']      = $arr2[1];
                                $aq[$key] = $arr;  }              #echo '
'.print_r($aq,true).'
'; #exit; } // eo 24 hours ws_save_cache($cacheFile, $aq); ws_message (ws_debug_times('module '.basename(__FILE__).' ('.__LINE__.'):'),true); } elseif ($cache_used == true) { $aq = $cache_used; ws_message(''); ws_message (ws_debug_times('module '.basename(__FILE__).' ('.__LINE__.'):'),true); } # #echo '
'.print_r($aq,true); exit;
#
$arrow_black  = ' 
'.PHP_EOL; # $aq_hide_show = ''.PHP_EOL; #------------------------------- component html $aq_link_page = ''; # $fullAQpage = '?p=mnu_aqhi_luft'; if (strpos( ' '.$fullAQpage, '?') > 1) { $fullAQpage .= '&lang='.$lang;} else { $fullAQpage .= '?lang='.$lang;} if ($fullAQpage <> false ) { $aq_link_page = ' ('.langtransstr('aq_full_page').')';} $strH3 = '

'.langtransstr($local_title); $strH3 .= '  ('.langtransstr('aq_more').')'.$aq_link_page.'

'.PHP_EOL.$aq_hide_show; $display_max ='block'; $display_small ='none'; $nr_data_coloms = 5; // nr of values in the table $str_max = '
'.PHP_EOL; $str_small = '
'.PHP_EOL; $str_hr = ''.PHP_EOL; $string = ''.PHP_EOL; $string .= str_repeat ('',$nr_blocks-1); # echo __LINE__.' $nr_blocks = '.$nr_blocks; exit; $string .= ''.PHP_EOL; $str_max .= $string; $str_small .= $string; $current_AQ = ''; $string_24h = ''; foreach ($arr_all as $value) { $done = false; $particle= langtransstr('aq_'.$value); $mesured= $aq[$value]['mes'].' ug/m3'; $ahqi = $aq[$value]['aqnr']; $risk = langtransstr($short_txt.$aq[$value]['nr']); $period = langtransstr('aq_last'); $string = ''.PHP_EOL; $nr = $aq[$value]['nr']; for ($p = 1; $p <= $nr_blocks - 1; $p++) { if ($nr == $p) { $string .= $arrow_black;} else { $string .= ''; } } $string .= ''.PHP_EOL; if ( in_array ($value, $arr_show_large) ) { $str_max .= $string;} if ( in_array ($value, $arr_show_small) ) { $str_small .= $string;} if ($useOwn24hours == true && isset ($aq[$value]['24avg']) ) { $risk = langtransstr($short_txt.$aq[$value]['nr24']); $mesured= $aq[$value]['24avg'].' ug/m3'; $ahqi = $ahqi = $aq[$value]['aqnr24']; $period = langtransstr('aq_24hrs'); $string_24h .= ''.PHP_EOL; $nr24 = $aq[$value]['nr24']; for ($p = 1; $p <= $nr_blocks - 1; $p++) { if ($nr24 == $p) { $string_24h .= $arrow_black;} else { $string_24h .= ''; } } $string_24h .= ''.PHP_EOL; } if ($value == $is_current) { $current_AQ = ''.$ahqi.'';} } // eoforeach value if ($useOwn24hours == true) { $str_max .= $str_hr.$string_24h;} # $str_colors = ''.str_repeat('',$nr_data_coloms); $width_color = 1+floor(50/$nr_blocks); for ($n = 1; $n <= $nr_blocks-1; $n++) { $str_colors .= '';} $issued = date($local_date_ft,$aq['timestamp']); # # if (isset ($aq['temp']['mes'])) { $extra = '    ('.round($aq['temp']['mes'],1).'°/'.round($aq['humi']['mes'],0).'%)';} else {$extra = '';} $str_colors .= ''.str_repeat('',$nr_data_coloms).' '.PHP_EOL; $str_small .= $str_colors.'

'. langtransstr('aq_classification') .' '. langtransstr('aq_period') .' '. langtransstr('aq_concentration') .' '.langtransstr('aq_aq') .' '. langtransstr('aq_particle') .' 
'. $risk .' '. $period .' '. $mesured .' '.$ahqi .' '. $particle .' 
'. $risk .' '. $period .' '. $mesured .' '.$ahqi .' '. $particle .' 
 '.$n.'     
 '.langtransstr('aq_from').': '.$issued.''.$extra.'
'.PHP_EOL; $str_max .= $str_colors.''.PHP_EOL; #----------------------------------------------- #--------------------------- EXPLANATION BLOCK #----------------------------------------------- $coloms = array ('band','index','P10','P25','risk','general'); $coloms = array ('band','index','P10','P25','risk'); $nr_cols = count($coloms); $explain = ''.PHP_EOL; if (isset ($range_aq) && $range_aq <> false) { $extra = 'style="width: 80px;"';} else { $extra = '';} if (in_array ('band',$coloms) ) { $explain .= ''.PHP_EOL;} if (in_array ('index',$coloms) ) { $explain .= ''.PHP_EOL;} if (in_array ('P10',$coloms) ) { $explain .= ''.PHP_EOL;} if (in_array ('P25',$coloms) ) { $explain .= ''.PHP_EOL;} if (in_array ('risk',$coloms) ) { $explain .= ''.PHP_EOL;} $explain .= ''.PHP_EOL; # for ($p = 1; $p <= $nr_blocks - 1; $p++) { $explain .= ' '.PHP_EOL; if ($xband_aq <> false ) if (isset ($xband_aq[$p])) { $rowspan= $xband_aq[$p]['cnt']; $colornr= $xband_aq[$p]['colors']; $bgcnr = $xband_aq[$p]['colors']; $textnr = $xband_aq[$p]['text']; $bandprint=true;} else { $bandprint=false;} else { $rowspan= 1; $colornr= $p; $bgcnr = $p; $textnr = $p; $bandprint=true;} if (in_array ('band',$coloms) && $bandprint == true ) { $explain .= ''.PHP_EOL;} if (in_array ('index',$coloms) ) { if ($xband_aq <> false ) { $extra = 'color: '. $aqhi_color_luftdt[$p]['color'].'; '. 'background-color:'. $aqhi_color_luftdt[$p]['bgc'].'; ';} else { $extra = '';} $explain .= ''.PHP_EOL;} $extra = ''; if (in_array ('P10',$coloms) ) { if ($p < $nr_blocks - 1) { $tekst = '< '.$thrshld_pm10[$p].' μg/m3';} else { $tekst = '> '.$thrshld_pm10[$p-1].' μg/m3';} $explain .= ''.PHP_EOL;} if (in_array ('P25',$coloms) ) { if ($p < $nr_blocks - 1) { $tekst = '< '.$thrshld_pm25[$p].' μg/m3';} else { $tekst = '> '.$thrshld_pm25[$p-1].' μg/m3';} $explain .= ''.PHP_EOL;} if (in_array ('risk',$coloms) && $bandprint == true ) { $explain .= ''.PHP_EOL;}} #' $explain .= '
'.langtransstr('aq_classification').''.langtransstr('aq_aq').''.langtransstr('aq_p1').''.langtransstr('aq_p2').''.langtransstr('aq_disclaimer1'); $string = langtransstr('aq_disclaimer2'); if ($string <> '') { $explain .= '
'.$string;} $explain .= '
 '. langtransstr($short_txt.$textnr).' '. $aqhi_range_luftdt[$p].''.$tekst.''.$tekst.''.langtransstr($long_txt.$textnr).'
'. langtransstr('aq_visit'). ' '.langtransstr('aq_link_regional').' & '. ' Luftdaten.info '. langtransstr('aq_information').'
'.PHP_EOL; #----------------------------------------------- #------------------------------ ENCLOSING HTML #----------------------------------------------- if (isset ($luftdt_html) && $luftdt_html == true) { $pageHTML_0 = ' Air-quality '.PHP_EOL; $pageHTML_9 = ' '.PHP_EOL;} else { $pageHTML_0 = $pageHTML_9 = 'xx';} # $aq_fullpage_html = $str_max. '
'. $explain; $aq_condensed_html = $strH3. $str_small. ''. PHP_EOL. $str_max. '
'. $explain. ''. PHP_EOL. ''; if (isset ($luftdt_html) && $luftdt_html == true) { echo $pageHTML_0.$aq_fullpage_html.$pageHTML_9; } #----------------------------------------------- # FUNCTIONS # ---------------------------------------------- # calc_aq # calculate AQ inde based on concentration # ---------------------------------------------- function calc_aq($conc,$pm = 'P1') { global $nr_blocks, $thrshld_aq, $thrshld_pm10, $thrshld_pm25; $ws_script_environment = '',true ); return $result; } // eof calc_aq # ---------------------------------------------- # InvLinear # calculation subfunction for calc_aq # ---------------------------------------------- function InvLinear($AQIhigh, $AQIlow, $Conchigh, $Conclow, $mes) { $ws_script_environment = '',true); return $return;} # ---------------------------------------------- # arrays # ---------------------------------------------- function aqhi_load_arrays_luftdt() { global $aqhi_type, $aqhi_range_luftdt, $aqhi_color_luftdt, $local_link, $range_aq, $nr_blocks, $thrshld_aq, $thrshld_pm10, $thrshld_pm25, $long_txt , $short_txt, $xband_aq; if ($aqhi_type == 'BE') { $local_link = 'http://www.irceline.be/nl/luchtkwaliteit/metingen/belaqi-luchtkwaliteitsindex/informatie'; $thrshld_aq = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 99999); $thrshld_pm10 = array (0, 10, 20, 30, 40, 50, 60, 70, 80, 100, 500, 99999); $thrshld_pm25 = array (0, 5, 10, 15, 25, 35, 40, 50, 60, 70, 500, 99999); $nr_blocks = count($thrshld_aq) - 1; $long_txt = 'aq_txt_lng_be'; $short_txt = 'aq_txt_sh_be'; $xband_aq = array (); $xband_aq[1] = array ('text' => 1, 'cnt' => 2, 'colors' => 1 ); $xband_aq[3] = array ('text' => 3, 'cnt' => 2, 'colors' => 3 ); $xband_aq[5] = array ('text' => 6, 'cnt' => 3, 'colors' => 6 ); $xband_aq[8] = array ('text' => 9, 'cnt' => 3, 'colors' => 9); $aqhi_color_luftdt[0] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#000'); $aqhi_color_luftdt[1] = array ('bgc' => 'rgb(3,46,235)' , 'color' => '#FFF'); $aqhi_color_luftdt[2] = array ('bgc' => 'rgb(90,153,2407)', 'color' => '#FFF'); $aqhi_color_luftdt[3] = array ('bgc' => 'rgb(90,146,59)' , 'color' => '#000'); #160,205,98 $aqhi_color_luftdt[4] = array ('bgc' => 'rgb(155,246,104)', 'color' => '#000'); $aqhi_color_luftdt[5] = array ('bgc' => 'rgb(255,252,114)', 'color' => '#000'); $aqhi_color_luftdt[6] = array ('bgc' => 'rgb(236,189,89)' , 'color' => '#000'); $aqhi_color_luftdt[7] = array ('bgc' => 'rgb(220,118,64)' , 'color' => '#000'); $aqhi_color_luftdt[8] = array ('bgc' => 'rgb(215,69,51)' , 'color' => '#FFF'); $aqhi_color_luftdt[9] = array ('bgc' => 'rgb(128,37,25)' , 'color' => '#FFF'); $aqhi_color_luftdt[10] = array ('bgc' => 'rgb(84,22,13)' , 'color' => '#FFF'); $aqhi_color_luftdt[11] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#FFF'); } elseif ($aqhi_type == 'IE') { $local_link = 'http://www.epa.ie/air/quality/'; $thrshld_aq = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 99999); $thrshld_pm10 = array (0, 16, 33, 50, 58, 66, 75, 83, 91, 100, 500, 99999); $thrshld_pm25 = array (0, 11, 23, 35, 41, 47, 53, 58, 64, 70, 500, 99999); $nr_blocks = count($thrshld_aq) - 1; $long_txt = 'aq_txt_lng_ie'; $short_txt = 'aq_txt_sh_ie'; $xband_aq = array (); $xband_aq[1] = array ('text' => 2, 'cnt' => 3, 'colors' => 2 ); $xband_aq[4] = array ('text' => 5, 'cnt' => 3, 'colors' => 5 ); $xband_aq[7] = array ('text' => 8, 'cnt' => 3, 'colors' => 8 ); $xband_aq[10] = array ('text' => 10,'cnt' => 1, 'colors' => 10); $aqhi_color_luftdt[0] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#000'); $aqhi_color_luftdt[1] = array ('bgc' => 'rgb(196,213,82)' , 'color' => '#000'); $aqhi_color_luftdt[2] = array ('bgc' => 'rgb(121,176,84)' , 'color' => '#000'); $aqhi_color_luftdt[3] = array ('bgc' => 'rgb(75,129,61)' , 'color' => '#000');; $aqhi_color_luftdt[4] = array ('bgc' => 'rgb(233,191,70)' , 'color' => '#000'); $aqhi_color_luftdt[5] = array ('bgc' => 'rgb(232,151,63)' , 'color' => '#000'); $aqhi_color_luftdt[6] = array ('bgc' => 'rgb(224,109,56)' , 'color' => '#000'); $aqhi_color_luftdt[7] = array ('bgc' => 'rgb(224,109,56)' , 'color' => '#000'); $aqhi_color_luftdt[8] = array ('bgc' => 'rgb(216,56,50)' , 'color' => '#000'); $aqhi_color_luftdt[9] = array ('bgc' => 'rgb(161,39,34)' , 'color' => '#000'); $aqhi_color_luftdt[10] = array ('bgc' => 'rgb(108,57,31)' , 'color' => '#000'); $aqhi_color_luftdt[11] = array ('bgc' => 'rgb(165,73,147)' , 'color' => '#000'); } elseif ($aqhi_type == 'NL') { $local_link = 'https://www.luchtmeetnet.nl/stations/alle-provincies/alle-gemeentes/PM25'; $thrshld_aq = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 99999); $thrshld_pm10 = array (0, 10, 20, 30, 45, 60, 75, 100, 125, 150, 200, 99999); $thrshld_pm25 = array (0, 10, 15, 20, 30, 40, 50, 70, 90, 100, 140, 99999); $nr_blocks = count($thrshld_aq) - 1; $long_txt = 'aq_txt_lng_nl'; $short_txt = 'aq_txt_sh_nl'; $xband_aq = array (); $xband_aq[1] = array ('text' => 2, 'cnt' => 3, 'colors' => 2 ); $xband_aq[4] = array ('text' => 5, 'cnt' => 3, 'colors' => 5 ); $xband_aq[7] = array ('text' => 7, 'cnt' => 2, 'colors' => 7 ); $xband_aq[9] = array ('text' => 9, 'cnt' => 2, 'colors' => 9 ); $xband_aq[11] = array ('text' => 11,'cnt' => 1, 'colors' => 11 ); $aqhi_color_luftdt[0] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#000'); $aqhi_color_luftdt[1] = array ('bgc' => 'rgb(0,29,245)' , 'color' => '#FFF'); $aqhi_color_luftdt[2] = array ('bgc' => 'rgb(68,152,247)' , 'color' => '#FFF'); $aqhi_color_luftdt[3] = array ('bgc' => 'rgb(154,186,240)', 'color' => '#000');; $aqhi_color_luftdt[4] = array ('bgc' => 'rgb(244,212,119)' , 'color' => '#000'); $aqhi_color_luftdt[5] = array ('bgc' => 'rgb(255,254,84)' , 'color' => '#000'); $aqhi_color_luftdt[6] = array ('bgc' => 'rgb(255,252,172)' , 'color' => '#000'); $aqhi_color_luftdt[7] = array ('bgc' => 'rgb(238,136,80)' , 'color' => '#000'); $aqhi_color_luftdt[8] = array ('bgc' => 'rgb(244,189,65)' , 'color' => '#FFF'); $aqhi_color_luftdt[9] = array ('bgc' => 'rgb(236,11,46)' , 'color' => '#FFF'); $aqhi_color_luftdt[10] = array ('bgc' => 'rgb(233,51,36)' , 'color' => '#FFF'); $aqhi_color_luftdt[11] = array ('bgc' => 'rgb(102,55,153)' , 'color' => '#FFF'); $aqhi_color_luftdt[12] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#FFF'); } else { $local_link = 'https://www.airnow.gov/index.cfm?action=aqibasics.aqi'; $thrshld_aq = array (0, 50, 100, 150, 200, 300, 500, 99999); $thrshld_pm10 = array (0, 54, 154, 254, 354, 424, 604, 99999); $thrshld_pm25 = array (0.0, 12.0, 35.4 ,55.4, 150.4, 250.4, 500.4, 99999); $nr_blocks = count($thrshld_aq) - 1; $long_txt = 'aq_txt_lng_us'; $short_txt = 'aq_txt_sh_us'; $xband_aq = false; $range_aq = true; $aqhi_color_luftdt[] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#000'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(0,228,0)' , 'color' => '#000'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(255,255,0)' , 'color' => '#000'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(255,126,0)' , 'color' => '#000');; $aqhi_color_luftdt[] = array ('bgc' => 'rgb(255,0,0)' , 'color' => '#FFF'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(153,0,76)' , 'color' => '#FFF'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(76,0,38)' , 'color' => '#FFF'); $aqhi_color_luftdt[] = array ('bgc' => 'rgb(0,0,0)' , 'color' => '#FFF'); } $previous = ''; $aqhi_range_luftdt = array(0); for ($n = 0; $n <= $nr_blocks; $n++) { if ($n == $nr_blocks) // mostly invalid to large value { $aqhi_range_luftdt[$n] = '>'.$thrshld_aq[$n];} elseif ($n == 0) // mostly invalid < 0 { $aqhi_range_luftdt[$n] = '<'. $thrshld_aq[$n];} else { if (($thrshld_aq[$n - 1] + 1) == $thrshld_aq[$n]) {$previous = '';} $aqhi_range_luftdt[$n] = $previous. $thrshld_aq[$n];} $previous = ($thrshld_aq[$n]+1).'-';} }// eof aqhi_load_arrays_luftdt #----------------------------------------------- # load all "missing" template functions # ---------------------------------------------- function load_missing() { if (!function_exists ('ws_message') ){ function ws_message ($message,$always=false,&$string=false) { global $wsDebug, $SITE; $echo = $always; if ( $echo == false && isset ($wsDebug) && $wsDebug == true ) {$echo = true;} if ( $echo == false && isset ($SIE['wsDebug']) && $SIE['wsDebug'] == true ) {$echo = true;} if ( $echo == true && $string === false) {echo $message.PHP_EOL;} if ( $echo == true && $string <> false) {$string .= $message.PHP_EOL;}} } // check ws_message if (!function_exists ('ws_load_cache') ){ function ws_load_cache($cacheFile, $cacheTime,$cacheFake='n/a'){ global $ws_msg_string, $ws_cron_all; $ws_script_environment = '',true ); return false; } if ($cacheFake == 'n/a') {$cacheFake = $cacheFile;} $file_time = filemtime($cacheFile); $now = time(); $diff = ($now - $file_time); ws_message('" ); if (isset ($ws_cron_all) ) // runnig a cron job { $cacheTime = $cacheTime - 600; ws_message($ws_script_environment .' ('.__LINE__.'): max cache lowered with 600 seconds as cron job is running -->'); } if ($diff <= abs($cacheTime) ) { ws_message($ws_script_environment.' ('.__LINE__.'): '.$cacheFake.' loaded from cache -->'); $return = unserialize(file_get_contents($cacheFile)); return $return;} ws_message($ws_script_environment.' ('.__LINE__.'): data to old to use -->',true ); return false;} // eof ws_load_cache } // check ws_load_cache if (!function_exists ('ws_makeRequest') ){ function ws_makeRequest($file, $get_contents = false, $fake= '') { $ws_script_environment = '',true ); return ws_curl($file, $fake);} if ($get_contents === true) // use file_get_contents to load http(s) data { $file_to_load = $file; } else { $file_to_load = $get_contents;} // use file_get_contents to load local (or test) file ws_message($ws_script_environment.' ('.__LINE__.'): data will be loaded from file '.$file_to_load.' using file get contents -->',true ); return file_get_contents($file_to_load);} // eof ws_makeRequest } // eo check ws_makeRequest if (!function_exists ('ws_curl') ){ function ws_curl ($url = '',$fake = '') { global $ws_msg_string, $ws_curl_follow, $ws_timeout, $ws_user_agent ; $ws_script_environment = '',false , $ws_msg_string ); $ch = curl_init(); // initialize a cURL session curl_setopt ($ch, CURLOPT_URL, $url); // connect to provided URL curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); // verify peer off,removes a lot of errors with older hosts curl_setopt ($ch, CURLOPT_USERAGENT, $ws_user_agent); // most host checks this nowadays curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $ws_timeout); // connection timeout curl_setopt ($ch, CURLOPT_TIMEOUT, $ws_timeout); // data timeout curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); // return the data transfer curl_setopt ($ch, CURLOPT_NOBODY, false); // do the download request without getting the body curl_setopt ($ch, CURLOPT_HEADER, false); // include header information if (! isset ($ws_curl_follow) ) {$ws_curl_follow = 1;} curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $ws_curl_follow); // follow Location: redirect allowed curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // but only one time $rawData= curl_exec ($ch); ws_message(ws_debug_times('module '.basename(__FILE__).' => '.__FUNCTION__.' ('.__LINE__.'):'),true); $info = curl_getinfo($ch); $errors = curl_error($ch); curl_close ($ch); unset ($ch); ws_message($ws_script_environment.' ('.__LINE__.'): Return codes:'.print_r ($info,true).' -->'); ws_message($ws_script_environment.' ('.__LINE__.'): Error codes:'.print_r ($errors,true).' -->'); if (strlen($rawData) > 2) { return $rawData;} # echo '

No data was retrieved for '.$url.'

'.PHP_EOL; return false;} // eo curl function } // eo check ws_curl if (!function_exists ('ws_debug_times') ){ function ws_debug_times($start = '') { global $ws_start_time, $ws_passed_time; if (!isset ($ws_passed_time)) { $ws_start_time = $ws_passed_time = microtime(true); return '';} # $now = microtime(true); $since_last = $now - $ws_passed_time; $ws_passed_time = $now; if ($since_last < 0.0001) {$string1 = '< 0.0001';} else {$string1 = round($since_last,4);} $until_last = $now - $ws_start_time; if ($until_last < 0.0001) {$string2 = '< 0.0001';} else {$string2 = round($until_last,4);} return ''; } // eof ws_debug_times } // eo exists ws_debug_times? if (!function_exists ('ws_debug_info') ){ function ws_debug_info($start = '') { global $wsLANGMISSING, $ws_start_time, $ws_passed_time, $ws_lang_dir, $lang; if (isset ($wsLANGMISSING) && count($wsLANGMISSING) > 0) { $string = '# missing entries added at '.date('c').PHP_EOL; foreach ($wsLANGMISSING as $value => $script) # 2017-12-03 add script name for missing translation { $string .= 'langlookup|'.$value.'|'.$value.'|'.$script.PHP_EOL;} $langfile = $ws_lang_dir. 'fctlanguage-'.$lang.'.txt'; $return = file_put_contents($langfile,$string,FILE_APPEND); $return = PHP_EOL.''.PHP_EOL; } // eo lang missing else { $return = '';} # $return .= PHP_EOL.ws_debug_times($start); # $size = memory_get_peak_usage(true); $unit = array('b','kb','mb','gb','tb','pb'); $used = round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; $seconds= microtime(true) - $ws_start_time; return $return.PHP_EOL.''.PHP_EOL; } } // eo exists ws_debug_info if (!function_exists ('ws_json_check') ){ #-------------------------------------------------------------------------------------------------- # ws_noaa_json_check check if there are errors in the json, thanks to Ken True for this code snippet #-------------------------------------------------------------------------------------------------- function ws_json_check ($rawData = '',$check_contents = false) { if (strlen($rawData) < 10 ) {return false;} $array = json_decode($rawData,true); // parse the JSON into an associative array if (!function_exists('json_last_error')){return $array;} $error = true; switch (json_last_error()) { case JSON_ERROR_NONE: $JSONerror = '- No errors'; $error = false; break; case JSON_ERROR_DEPTH: $JSONerror = '- Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $JSONerror = '- Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $JSONerror = '- Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $JSONerror = '- Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $JSONerror = '- Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $JSONerror = '- Unknown error'; break; } if ($error) { ws_message ('',true); ws_message ('',true); return false;} return $array;} // eof ws_noaa_json_check } // eo exists ws_debug_info if (!function_exists ('ws_save_cache') ){ function ws_save_cache($cacheFile, $data, $proceed = 'n/a',$cacheFake = 'n/a'){ global $ws_msg_string ; if ($proceed == 'n/a' || $proceed == false || $proceed == '') { $proceed = false;} else {$proceed = true;} if ($cacheFake == 'n/a' || $cacheFake == '') {$cacheFake = $cacheFile;} $ws_script_environment = '',true , $ws_msg_string ); return true;} if ($proceed == true) { ws_message($ws_script_environment.' ('.__LINE__.'): ERROR Could not save data to cache '.$cacheFake.' -->',true , $ws_msg_string ); return true;} exit ('

'.__FUNCTION__. ": ERROR Could not save data to cache ($cacheFile).
Please make sure your cache directory exists and is writable.
Program ends

"); } // eof ws_save_cache } // eo exists ws_save_cache } // eof load_missing #----------------------------------------------- # load language translations and optional function # ---------------------------------------------- function ws_load_aq_langs () { global $lang, $LANGLOOKUP, $aqhi_type; $ws_script_environment = '',true); include $lang_file;} else { ws_message($ws_script_environment.' ('.__LINE__.'): '.$lang_file.' not found -->',true); $lang_file = __DIR__.'/ws_luft_langEN.php'; if (is_file ($lang_file)) { ws_message($ws_script_environment.' ('.__LINE__.'): loading '.$lang_file.' -->',true); include $lang_file;} ws_message($ws_script_environment.' ('.__LINE__.'): '.$lang_file.' not found -->',true);} } // eof ws_load_aq_langs