Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 3757 → Rev 3761

/trunk/scripts/modules/pull_plantnet/PullPlantnet.php
54,6 → 54,7
protected $parametres_autorises = array(
'-startDate' => array(false, '1', 'Date de début (parsée par strtotime). Ex: "YYYY:MM:DD HH:II:SS"'),
'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
);
 
72,7 → 73,7
$this->updateLatest($this->getParametre('startDate'));
break;
case 'updateOnePN':
$this->updateOne($this->getParametre('pnObsId'));
$this->updateOnePN((int)$this->getParametre('pnObsId'), $this->getParametre('project'));
break;
case 'updateOneTB':
$this->updateOneTB($this->getParametre('tbObsId'));
84,7 → 85,7
$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
. ', utilisez plutôt :' . "\n"
. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS"' . "\n"
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
;
149,7 → 150,7
$this->hasMore = $responseJson['hasMore'];
if ($this->hasMore) {
$this->count += count($observations_PN);
$this->currentPage = Config::get('urlPlantnetBase').$responseJson['next'].$this->count;
$this->currentPage = Config::get('urlPlantnetBase').$responseJson['next'];
} else {
$this->currentPage = '';
$this->count = 0;
220,7 → 221,7
if (!$infos_utilisateur) {
continue; // c'est pas un telabotaniste
}
echo "Synchro d'une obs d'un telabotaniste : $email\n";
//echo "Synchro d'une obs d'un telabotaniste : $email";
// echo json_encode($obs);
// die(var_dump($obs));
 
234,10 → 235,10
// la date de l'obs est un microtime, donc on coupe les millièmes
// die(var_dump((int)($obs['dateUpdated']/1000), strtotime($res[0]['date_maj'])));
if ((int)($obs['dateUpdated']/1000) > strtotime($res[0]['date_maj'])) {
echo "Obs déjà en base, mise à jour (TB {$res[0]['id_observation']})\n";
echo "Obs déjà en base, mise à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
$this->updateFromPN($obs, $res[0]['id_observation']);
} else {
echo "Obs déjà en base : ID PN {$obs['id']}, ID TB {$res[0]['id_observation']}\n";
echo "Obs déjà en base, déjà à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
}
continue;
}
259,7 → 260,7
fwrite($handle, file_get_contents($image['o']));
fclose($handle);
 
echo "cool on a fait un fichier de " . filesize($tempfile) . " octets là : $tempfile\n";
echo "Image de " . number_format(filesize($tempfile), 0, ',', ' ') . " octets créé : $tempfile\n";
 
$params = [
'name' => 'image' . $i,
297,18 → 298,22
// var_dump($images, $tags_images);
// die();
 
$geo = $this->getGeoInfo($obs['geo']);
 
// on insère l'obs via le service CelWidgetSaisie
$infos_obs = [
'obsId1[date]' => date('d/m/Y', intdiv($obs['dateObs'], 1000)),
'obsId1[latitude]' => $geo['lat'] ?? null,
'obsId1[longitude]' => $geo['lon'] ?? null,
'obsId1[pays]' => $geo['countryCode'] ?? null,
'obsId1[code_postal]' => $geo['postcode'] ?? null,
'obsId1[commune_nom]' => $geo['city'] ?? null,
// 'obsId1[commune_code_insee]' => '',
'obsId1[commune_nom]' => $obs['geo']['place'] ?? '',
'obsId1[date]' => date('d/m/Y', intdiv($obs['dateObs'], 1000)),
// 'obsId1[famille]' => '',
'obsId1[latitude]' => (string)$obs['geo']['lat'] ?? '',
// 'obsId1[lieudit]' => '',
'obsId1[longitude]' => (string)$obs['geo']['lon'] ?? '',
// 'obsId1[milieu]' => '',
'obsId1[nom_sel]' => $obs['currentName'],
// 'obsId1[nom_ret]' => '',
'obsId1[nom_sel]' => $obs['currentName'],
// 'obsId1[famille]' => '',
'obsId1[certitude]' => 'douteux',
// 'obsId1[notes]' => '',
// 'obsId1[num_nom_ret]' => '',
330,7 → 335,7
foreach ($images as $i => $image) {
$infos_obs["obsId1[image_nom][$i]"] = $image;
}
// die(var_dump($infos_obs));
// var_dump($infos_obs);
 
// curl post $infos_obs
$ch = curl_init($url_cel_widget_saisie);
346,14 → 351,16
}
 
// var_dump($reponse);
$id_obs_tb = $this->bdd->proteger(json_decode($reponse, true)['id']);
$id_obs_pn = $this->bdd->proteger($obs['id']);
$id_obs_tb = json_decode($reponse, true)['id'];
$id_obs_pn = $obs['id'];
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
$sql = 'INSERT INTO cel_plantnet (id_observation, id_plantnet, date_maj)'
.' VALUES (%s, %s, NOW())'
. ' -- ' . __FILE__ . ':' . __LINE__;
$sql = sprintf($sql, $id_obs_tb, $id_obs_pn);
$sql = sprintf($sql, $this->bdd->proteger($id_obs_tb), $this->bdd->proteger($id_obs_pn));
$this->bdd->requeter($sql);
 
echo "Obs insérée en base : ID PN $id_obs_pn ; ID TB $id_obs_tb ; utilisateur_tb $email\n";
}
}
}
451,13 → 458,61
return $infosUtilisateur;
}
 
private function getGeoInfo($obs) {
$geo = [];
if (!isset($obs['lat']) && !isset($obs['lon'])) {
return $geo;
}
 
// $data = [
// 'hitsPerPage' => 1,
// 'aroundLatLng' => "{$obs['lat']},{$obs['lon']}"
// ];
$headers = [
'X-Algolia-Application-Id' => Config::get('algoliaApplicationId'),
'X-Algolia-API-Key' => Config::get('algoliaAPIKey'),
];
 
$lat = number_format($obs['lat'], 6, '.', '');
$lon = number_format($obs['lon'], 6, '.', '');
 
$ch = curl_init(Config::get('urlReverseGeocodingLatLng')."$lat,$lon");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reponse = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
 
if ($reponse) {
$reponse = json_decode($reponse, true);
// die(var_dump($reponse));
$infos = $reponse['hits'][0];
 
$geo = [
'lat' => (string) $obs['lat'],
'lon' => (string) $obs['lon'],
'country' => $infos['country']['fr'] ?? $infos['country']['default'],
'city' => $infos['city']['default'][0] ?? null,
'postcode' => $infos['postcode'][0] ?? null,
'countryCode' => strtoupper($infos['country_code']),
];
}
 
return $geo;
}
 
private function updateFromPN(array $pnObs, string $tbObsId) {
if (!is_array($pnObs) || !is_string($tbObsId)) {
// die(var_dump($pnObs, $tbObsId));
throw new Exception("\nBad params types\n");
throw new Exception("\nBad params types, give me an array and an integer plz\n");
}
// die(var_dump($pnObs));
 
// log update date to cel_plantnet
$sql = "UPDATE cel_plantnet SET date_maj = NOW() WHERE id_observation = $tbObsId"
. ' -- ' . __FILE__ . ':' . __LINE__;
$this->bdd->requeter($sql);
 
// check for deleted
if (isset($pnObs['deleted']) && (true === $pnObs['deleted'])) {
// est-ce une obs issue de PN ?
465,14 → 520,16
$sql = "SELECT input_source FROM occurrence WHERE id = '$tbObsId'"
. ' -- ' . __FILE__ . ':' . __LINE__;
$res = $this->bdd->recupererTous($sql);
die(var_dump($res));
 
if (isset($res[0]) && ('PlantNet' === $res[0]['input_source'])) {
// oui ? alors supprimer obs !
echo("obs à supprimer\n");
$sql = "DELETE FROM occurrence WHERE id = '$tbObsId'"
echo "Obs supprimée coté PN, suppression : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']}\n";
 
$sql = "UPDATE photos SET occurrence_id = NULL WHERE occurrence_id = '$tbObsId'"
. ' -- ' . __FILE__ . ':' . __LINE__;
$this->bdd->requeter($sql);
$sql = "UPDATE photos SET occurrence_id = NULL WHERE occurrence_id = '$tbObsId'"
 
$sql = "DELETE FROM occurrence WHERE id = '$tbObsId'"
. ' -- ' . __FILE__ . ':' . __LINE__;
$this->bdd->requeter($sql);
}
509,34 → 566,58
// // insert_new_votes($pn_sorted_votes, $existing_votes);
}
 
private function updateOnePN($id) {
// vérification que l'obs est bien dans la BdD de Tela
$sql = "SELECT date_maj, id_observation FROM cel_plantnet WHERE id_plantnet = '$id'"
. ' -- ' . __FILE__ . ':' . __LINE__;
$res = $this->bdd->recupererTous($sql);
// die(var_dump($res));
if (!$res) {
throw new Exception("\nPlantnet occurrence id ($id) doesn't exist in tela db. \n");
private function updateOnePN($pnObsId, $pnProjectId) {
if (!is_int($pnObsId) || !is_string($pnProjectId)) {
die(var_dump($pnObsId, $pnProjectId));
throw new Exception("\nBad params types, give me an integer and a string plz\n");
}
// get PN project list
$list = [];
$pnProjects = $this->getProjects(); // refresh projects list
foreach ($pnProjects as $project) {
$list[$project['id']] = $project['name'];
}
 
// l'obs existe déjà à Tela, on vérifie si il faut màj
// if project not in list display list
if (!array_key_exists($pnProjectId, $list)) {
echo "Available projects:\n";
foreach ($list as $projectId => $projectName) {
echo " - $projectId ($projectName)\n";
}
throw new Exception("Project $pnProjectId does not exist\n");
} else {
$this->currentProject = [
'id' => $pnProjectId,
'name' => $list[$pnProjectId],
];
}
 
// get PN obs
$urlInfosObs = str_replace(
['{token}', '{ids}'],
[Config::get('tokenPlantnet'), $id],
['{token}', '{project}', '{id}'],
[Config::get('tokenPlantnet'), $pnProjectId, $pnObsId],
Config::get('urlPlantnetBase').Config::get('urlPlantnetObsById')
);
$ch = curl_init($urlInfosObs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reponse = curl_exec($ch);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
 
if (!$reponse || 200 != $code) {
throw new Exception("\nPlantnet api ($urlInfosObs) replied with code $code : $reponse\n");
if (!$response || 200 != $code) {
throw new Exception("\nPlantnet api ($urlInfosObs) replied with code $code : $response\n");
}
 
echo "La suite n'a pas été implémentée.\n";
die(var_dump($reponse));
// change last modification date to force update
$obs = json_decode($response, true);
$date = new DateTime();
$date->setTimestamp(intdiv($obs['dateUpdated'], 1000) - 1);
$date = $this->bdd->proteger($date->format('Y-m-d H:i:s'));
$sql = "UPDATE cel_plantnet SET date_maj = $date WHERE id_plantnet = '$pnObsId'"
. ' -- ' . __FILE__ . ':' . __LINE__;
$this->bdd->requeter($sql);
 
$this->updateObs([$obs]);
}
 
private function updateOneTB($id) {
589,6 → 670,7
. ' -- ' . __FILE__ . ':' . __LINE__;
$values = '';
 
echo "Updating matching partners ids table\n";
foreach ($data as $id => $corres) {
$id_obs_tb = $this->bdd->proteger($corres['tb']);
$id_obs_pn = $this->bdd->proteger($corres['pn']);
609,6 → 691,8
// var_dump($sql);
$this->bdd->requeter($sql);
 
$count = count($data);
echo "Success: #$count updated\n";
}
 
private function updateMatchingPartnersIds() {
/trunk/scripts/modules/pull_plantnet/config.defaut.ini
6,7 → 6,8
urlPlantnetMatchingPartner = '/v1/edi/partners/observations/partnerids?token={token}'
; Url de base du service de suivi des obs plantnet
urlPlantnetLatestChanges = '/v1/edi/partners/projects/{project}/observations/modified/latest?token={token}&startDate={startDate}';
urlPlantnetObsById = '/v1/edi/observations/modified/latest?token={token}&ids=[{ids}]&startDate=1';
; Url pour obtenir une obs PN
urlPlantnetObsById = '/v1/edi/partners/projects/{project}/observations/{id}?token={token}'
; token (prod ou test)
tokenPlantnet = 'celuiquilitcaestuntron';
; Url d'appel du service CelWidgetSaisie
16,4 → 17,6
; Url pour envoyer les images temporaires
urlCelUploadImageTemp = 'https://api-test.tela-botanica.org/service:cel:CelWidgetUploadImageTemp'
; Fichier de cache pour les correspondances d'id des obs PN et TB
correspondingIdsFilename = '/home/test/www/scripts/cel/modules/pull_plantnet/idstbpn.json'
correspondingIdsFilename = '/home/test/www/scripts/cel/modules/pull_plantnet/idstbpn.json'
; API de reverse geocoding Algolia (remplacer 'places' au début par le Application ID)
urlReverseGeocodingLatLng = 'https://places-dsn.algolia.net/1/places/reverse?hitsPerPage=1&aroundLatLng='