[PHP] Script Qualité de l'air (remplacement lcsqa)
Publié : 26 janv. 2017, 23:47
Bonjour,
Suite a l'indisponibilité du service lcsqa qu'utilisait le script perl,j'ai codé un nouveau script a partir des données du site prev'air.
Voici le script php qui se presente sous la forme d'une classe:
C'est assez long mais on recupere pas mal de données.
Pour executer le script un sipmle "php fichier.php" suffit.
Le script est parametrable de 2 facons:
-une premiere ou vous indiquez votre longitude et latitude (en haut du script) et cela calcule la station la plus proche
-une seconde ou vous pouvez specifier un code insee (pour l'indice atmo) et un code station (pour les autres données)
(vous pouvez obtenir ces codes en remplacant yourCity par votre ville)
Pour le lien avec domoticz,il faut mettre des lignes comme celle ci avec xxxxx l'ip de domoticz,yyyy l'idx du device et zzzzz une fonction
exemple:
Voila !
PS: si vous obtenez des resultats comme ceux-ci,c'est que ces valeurs ne sont pas disponibles pour votre station.
Suite a l'indisponibilité du service lcsqa qu'utilisait le script perl,j'ai codé un nouveau script a partir des données du site prev'air.
Voici le script php qui se presente sous la forme d'une classe:
Code : Tout sélectionner
<?php
$class = new prevair();
$class->_isStation = true;
$class->_inseeCode = "yourInseeCode";
$class->_stationCode = "yourStationCode";
$class->_long = "yourLongitude";
$class->_lat = "yourLatitude";
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";
//file_get_contents("http://192.168.1.xxx:8080/json.htm?type=command¶m=udevice&idx=yyyyyyyy&nvalue=".zzzzz);
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)
{
$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)
{
$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];
}
}
?>Pour executer le script un sipmle "php fichier.php" suffit.
Le script est parametrable de 2 facons:
-une premiere ou vous indiquez votre longitude et latitude (en haut du script) et cela calcule la station la plus proche
Code : Tout sélectionner
$class->_isStation = false;
$class->_long = "yourLongitude";
$class->_lat = "yourLatitude";Code : Tout sélectionner
$class->_isStation = true;
$class->_inseeCode = "yourInseeCode";
$class->_stationCode = "yourStationCode";Code : Tout sélectionner
echo "\n Station: ".$class->getStationCode("yourCity");
echo "\n Insee: ".$class->getInseeCode("yourCity")."\n";Code : Tout sélectionner
file_get_contents("http://192.168.1.xxxxx:8080/json.htm?type=command¶m=udevice&idx=yyyyyyyy&nvalue=".zzzzz);Code : Tout sélectionner
file_get_contents("http://192.168.1.147:8080/json.htm?type=command¶m=udevice&idx=125&nvalue=".$class->getCo());Code : Tout sélectionner
file_get_contents("http://192.168.1.147:8080/json.htm?type=command¶m=udevice&idx=126&nvalue=".$class->getAtmoNo2Indice());PS: si vous obtenez des resultats comme ceux-ci,c'est que ces valeurs ne sont pas disponibles pour votre station.
Code : Tout sélectionner
Station: AASQA - Commune
SO2: Moyenne journaliere
Max: Max moyenne horaire