#https://easydomoticz.com/forum/viewtopic.php?f=17&t=3366
<?php
date_default_timezone_set('Europe/Paris');

$class = new prevair();

# Rennes
# Station: 19002
# Insee: 35238
$class->_isStation = true;
$class->_inseeCode = "35238";
$class->_stationCode = "19002";
$class->_long = "-2.1333";
$class->_lat = "47.7333";

#echo "\n Station: ".$class->getStationCode("Rennes");
#echo "\n Insee: ".$class->getInseeCode("Rennes")."\n";


echo "\n Station: ".$class->getCoStation();
echo "\n CO: ".$class->getCo();
echo "\n Max: ".$class->getCoMax();
echo "\n Station: ".$class->getNo2Station();
echo "\n NO2: ".$class->getNo2();
echo "\n Max: ".$class->getNo2Max();
echo "\n Station: ".$class->getSo2Station();
echo "\n SO2: ".$class->getSo2();
echo "\n Max: ".$class->getSo2Max();
echo "\n Station: ".$class->getO3Station();
echo "\n O3: ".$class->getO3();
echo "\n Max: ".$class->getO3Max();
echo "\n Station: ".$class->getPm10Station();
echo "\n PM10: ".$class->getPm10();
echo "\n Max: ".$class->getPm10Max();
echo "\n Station: ".$class->getPm25Station();
echo "\n PM2.5: ".$class->getPm25();
echo "\n Max: ".$class->getPm25Max();
echo "\n Station: ".$class->getAtmoIndiceStation();
echo "\n Atmo: ".$class->getAtmoIndice();
echo "\n  -NO2: ".$class->getAtmoNo2Indice();
echo "\n  -SO2: ".$class->getAtmoSo2Indice();
echo "\n  -O3: ".$class->getAtmoO3Indice();
echo "\n  -PM10: ".$class->getAtmoPm10Indice();
echo "\n  -Comment: ".$class->getAtmoIndiceComment();
echo "\n  -in 1 day: ".$class->getAtmoTomorrowIndice();
echo "\n  -in 2 days: ".$class->getAtmo2DayIndice();

echo "\n Station: ".$class->getStationCode("yourCity");
echo "\n Insee: ".$class->getInseeCode("yourCity")."\n";

#Ozone
file_get_contents("http://localhost:81/json.htm?type=command&param=udevice&idx=90&nvalue=".$class->getAtmoO3Indice());
file_get_contents("http://localhost:81/json.htm?type=command&param=udevice&idx=93&nvalue=".$class->getAtmoPm10Indice());
file_get_contents("http://localhost:81/json.htm?type=command&param=udevice&idx=92&nvalue=".$class->getAtmoSo2Indice());
file_get_contents("http://localhost:81/json.htm?type=command&param=udevice&idx=91&nvalue=".$class->getAtmoNo2Indice());

class prevair
{
   private $_base_url = "http://www2.prevair.org/ineris-web-services.php?url=mesureJourna&date=";
   private $_url_station = "http://www2.prevair.org/ineris-web-services.php?url=stations&date=";
   private $_atmo_url = "http://www2.prevair.org/ineris-web-services.php?url=atmo&date=";
   private $_atmo_data;
   private $_date;

   public $_isStation = false;
   public $_inseeCode;
   public $_stationCode;
   public $_long;
   public $_lat;


   public function __construct()
   {
      $this->_date = date("Y-m-d");
      $this->_base_url = $this->_base_url.$this->_date."&code_polluant=";
      $this->_atmo_data = json_decode(file_get_contents($this->_atmo_url.$this->_date),true);
   }

   private function getDistance($lat1, $lng1, $lat2, $lng2) {
      $earth_radius = 6378137;
      $rlo1 = deg2rad($lng1);
      $rla1 = deg2rad($lat1);
      $rlo2 = deg2rad($lng2);
      $rla2 = deg2rad($lat2);
      $dlo = ($rlo2 - $rlo1) / 2;
      $dla = ($rla2 - $rla1) / 2;
      $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo));
      $d = 2 * atan2(sqrt($a), sqrt(1 - $a));
      return round(($earth_radius * $d)/1000);
   }

   private function findDataId($data,$isAtmo = false)
   {
      $i = 1;
      $distMin = 2000000;
      $minId = 0;
      $arrLen = count($data);

      if($isAtmo == true)
      {
         $longIndice = 2;
         $latIndice = 3;
      }
      else
      {
         $longIndice = 2;
         $latIndice = 1;
      }

      if($this->_isStation != false)
      {
         while($i != $arrLen)
         {
            if($isAtmo != false)
            {
               if($data[$i][1] == $this->_inseeCode)
               {
                  $minId = $i;
                  break;
               }
            }
            else
            {
               if($data[$i][0] == $this->_stationCode)
               {
                  $minId = $i;
                  break;
               }
            }
            $i++;
         }
      }
      else
      {
         while($i != $arrLen)
         {
            $distance = $this->getDistance($this->_lat, $this->_long, $data[$i][$latIndice], $data[$i][$longIndice]);
            if($distMin > $distance)
            {
               $distMin = $distance;
               $minId = $i;
            }
         $i++;
         }
      }
      return $minId;
   }

   public function getStationName($long,$lat)
   {
      $data = file_get_contents($this->_url_station.$this->_date);
      $data = json_decode($data,true);
      $len = count($data);
      $i = 0;
      $name = false;
      while($i != $len)
      {
         if($data[$i][5] == $lat && $data[$i][6] == $long)
         {
            $name = $data[$i][1];
            break;
         }
         $i++;
      }
      return $data[$i][2]." - ".$data[$i][4];
   }

   public function getStationCode($city)
   {
      $code = "";
      $data = file_get_contents($this->_url_station.$this->_date);
      $data = json_decode($data,true);
      $len = count($data);
      $i = 0;
      $long = false;
      $lat = false;
      while($i != $len)
      {
         if(strcasecmp($data[$i][4],$city) == 0)
         {
            $code = $data[$i][0];
            break;
         }
         $i++;
      }
      return $code;
   }

   public function getInseeCode($city)
   {
      $code = "";
      $data = file_get_contents($this->_url_station.$this->_date);
      $data = json_decode($data,true);
      $len = count($data);
      $i = 0;
      $long = false;
      $lat = false;
      while($i != $len)
      {
         if(strcasecmp($data[$i][4],$city) == 0)
         {
            $code = $data[$i][3];
            break;
         }
         $i++;
      }
      return $code;
   }

   public function getNo2()
   {
      $data = json_decode(file_get_contents($this->_base_url."03"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getSo2()
   {
      $data = json_decode(file_get_contents($this->_base_url."01"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getO3()
   {
      $data = json_decode(file_get_contents($this->_base_url."08"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getPm10()
   {
      $data = json_decode(file_get_contents($this->_base_url."24"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getPm25()
   {
      $data = json_decode(file_get_contents($this->_base_url."39"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getCo()
   {
      $data = json_decode(file_get_contents($this->_base_url."04"),true);
      $id = $this->findDataId($data);
      return $data[$id][6];
   }

   public function getNo2Max()
   {
      $data = json_decode(file_get_contents($this->_base_url."03"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getSo2Max()
   {
      $data = json_decode(file_get_contents($this->_base_url."01"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getO3Max()
   {
      $data = json_decode(file_get_contents($this->_base_url."08"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getPm10Max()
   {
      $data = json_decode(file_get_contents($this->_base_url."24"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getPm25Max()
   {
      $data = json_decode(file_get_contents($this->_base_url."39"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getCoMax()
   {
      $data = json_decode(file_get_contents($this->_base_url."04"),true);
      $id = $this->findDataId($data);
      return $data[$id][5];
   }

   public function getNo2Station()
   {
      $data = json_decode(file_get_contents($this->_base_url."03"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getSo2Station()
   {
      $data = json_decode(file_get_contents($this->_base_url."01"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getO3Station()
   {
      $data = json_decode(file_get_contents($this->_base_url."08"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getPm10Station()
   {
      $data = json_decode(file_get_contents($this->_base_url."24"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getPm25Station()
   {
      $data = json_decode(file_get_contents($this->_base_url."39"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getCoStation()
   {
      $data = json_decode(file_get_contents($this->_base_url."04"),true);
      $id = $this->findDataId($data);
      return $this->getStationName($data[$id][2],$data[$id][1]);
   }

   public function getAtmoIndice()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][7];
   }

   public function getAtmoSo2Indice()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][8];
   }

   public function getAtmoNo2Indice()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][9];
   }

   public function getAtmoO3Indice()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][10];
   }

   public function getAtmoPm10Indice()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][11];
   }

   public function getAtmoIndiceStation()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][4]." - ".$this->_atmo_data[$id][5]." - ".$this->_atmo_data[$id][6]." - ".$this->_atmo_data[$id][13];
   }

   public function getAtmoIndiceComment()
   {
      $id = $this->findDataId($this->_atmo_data,true);
      return $this->_atmo_data[$id][12];
   }

   public function getAtmoTomorrowIndice()
   {
      $date = date("Y-m-d", strtotime("+1 day"));
      $data = json_decode(file_get_contents($this->_atmo_url.$date),true);
      $id = $this->findDataId($data,true);
      return $data[$id][7];
   }

   public function getAtmo2DayIndice()
   {
      $date = date("Y-m-d", strtotime("+2 day"));
      $data = json_decode(file_get_contents($this->_atmo_url.$date),true);
      $id = $this->findDataId($data,true);
      return $data[$id][7];
   }
}

?>
