Skip to content

Commit

Permalink
New api wethear alert (#4)
Browse files Browse the repository at this point in the history
* New weather alert API

* Update readme
  • Loading branch information
WoCha-FR committed Jun 21, 2023
1 parent c9f9876 commit c06bd3d
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 173 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

Publish values from french open api to MQTT and other usefull data.

## 1.1.0 UPDATE

Weather alert is now unavailable without an API Subscription.

Manual to create one : [API Manager Meteo France](https://portail-api.meteofrance.fr/authenticationendpoint/aide.do)

mqtt4frenchtools use Acces by [API key protocol](https://portail-api.meteofrance.fr/authenticationendpoint/aide.do#logic-schema)

### Data published

- Year information (number of days in year, day number in year, week number, ...)
Expand All @@ -20,14 +28,13 @@ And for each city provided as parameter :
- Moon Information (moonset, moonrise, ....)
- Statutory holiday (with Alsace-Moselle)
- School holidays (of the city School Zone)
- Wethear Alert

### API Used

- [geo.api.gouv.fr](https://geo.api.gouv.fr/)
- [Risques Météorologiques](https://public.opendatasoft.com/explore/dataset/risques-meteorologiques-copy/api/)
- [Le calendrier scolaire](https://data.education.gouv.fr/explore/dataset/fr-en-calendrier-scolaire/information/)
- [Jours fériés en France](https://calendrier.api.gouv.fr/jours-feries/)
- [DonneesPubliquesVigilance](https://portail-api.meteofrance.fr/devportal/apis/5e99a87c-d50d-465b-a33f-1f12cf675161/overview)

## Installing

Expand All @@ -44,6 +51,7 @@ Usage: mqtt4frenchtools [options]
Options:
-a, --ville ville [array]
-m, --apikey apikey for weather alert
-u, --mqttUrl mqtt broker url [default: "mqtt://127.0.0.1"]
-t, --mqttTopic mqtt topic prefix [default: "frenchtools"]
-v, --logVerbosity log verbosity
Expand Down Expand Up @@ -169,10 +177,7 @@ frenchtools/Lyon69/vigilance
"vigiCanicule": "Vert",
"vigiFroid": "Vert",
"vigiAvalanche": "Vert",
"vigiVague": "Vert",
"vigiCrue": "",
"vigiConseil": "",
"vigiComment": "Vents forts sur la Corse du Sud associés à la tempête Denise."
"vigiVague": "Vert"
}
```

Expand Down
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ function updateData () {
for (const key in mesVilles) {
infoVi.getSunData(mesVilles[key].lat, mesVilles[key].lon, key)
infoVi.getMoonData(mesVilles[key].lat, mesVilles[key].lon, key)
infoVi.getMeteoVigilance(mesVilles[key].lat, mesVilles[key].lon, key)
if (!_.isUndefined(config.apikey)) {
infoVi.getMeteoVigilance(mesVilles[key].dtp, key, config.apikey)
}
}
lastMinute = 1
} else {
Expand Down Expand Up @@ -69,14 +71,15 @@ async function main () {
logger.warn('Pas de ville à traiter.')
} else {
for (let i = 0, len = config.ville.length; i < len; i++) {
const tempVille = await infoVi.getVille(config.ville[i])
const tempVille = await infoVi.getVille(config.ville[i], false)
if (!_.isObject(tempVille)) {
continue
}
// on ajoute au tableau
mesVilles[tempVille.top] = tempVille
logger.info(`Nouvelle ville: ${tempVille.nom} - Departement: ${tempVille.dpt} - Zone: ${tempVille.vac}`)
logger.info(`Les données auront comme topic principal: '${config.mqttTopic}/${tempVille.top}'`)
await infoVi.getVille(config.ville[i], true)
// Feries
await infoVi.getJourFerie(tempVille.dpt, tempVille.top)
// Vacances
Expand All @@ -86,7 +89,11 @@ async function main () {
// Lune
await infoVi.getMoonData(tempVille.lat, tempVille.lon, tempVille.top)
// Vigilance
await infoVi.getMeteoVigilance(tempVille.lat, tempVille.lon, tempVille.top)
if (_.isUndefined(config.apikey)) {
logger.warn('Pas de vigilance meteo, apikey manquant.')
} else {
await infoVi.getMeteoVigilance(tempVille.dpt, tempVille.top, config.apikey)
}
}
}
// Interval
Expand Down
2 changes: 2 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const yargs = require('yargs')
const config = yargs
.usage('Usage: $0 [options]')
.describe('a', 'ville')
.describe('m', 'apikey for weather alert')
.describe('u', 'mqtt broker url')
.describe('t', 'mqtt topic prefix')
.describe('v', 'log verbosity')
.describe('s', 'allow ssl connections with invalid certs')
.describe('z', 'log with no color')
.alias({
a: 'ville',
m: 'apikey',
u: 'mqttUrl',
t: 'mqttTopic',
v: 'logVerbosity',
Expand Down
95 changes: 51 additions & 44 deletions lib/infoville.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class InfosVille {
* @param {string} villeStr Ville recherchée
* @return {object} Données utiles de la ville
*/
async getVille (villeStr) {
async getVille (villeStr, publishData = false) {
// Parametres de base
const params = {
zone: 'metro',
Expand Down Expand Up @@ -82,7 +82,9 @@ class InfosVille {
}
// Publish Data
const topic = res.nom.replace(/[^a-zA-Z]/g, '') + res.departement.code
eventEmitter.emit('frame', `${topic}/infos`, result)
if (publishData) {
eventEmitter.emit('frame', `${topic}/infos`, result)
}
// Renvoie
const ret = {
nom: res.nom,
Expand Down Expand Up @@ -259,52 +261,57 @@ class InfosVille {
/**
* Recupère les vigilances meteos
*
* @param {number} lat Latitude
* @param {number} lon Longitude
* @param {number} dpt departement
* @param {string} topic Main topic to publish to
* @param {string} api APIKey form meteofrance portal
*/
async getMeteoVigilance (lat, lon, topic) {
async getMeteoVigilance (dpt, topic, api) {
// Variables
const viginom = { 1: 'vigiVent', 2: 'vigiPluie', 3: 'vigiOrage', 4: 'vigiInondation', 5: 'vigiNeige', 6: 'vigiCanicule', 7: 'vigiFroid', 8: 'vigiAvalanche', 9: 'vigiVague' }
const couleur = { 1: 'Vert', 2: 'Jaune', 3: 'Orange', 4: 'Rouge' }
const result = {}
// Requete
const params = {
dataset: 'risques-meteorologiques-copy@public',
lang: 'fr',
rows: 1,
timezone: 'Europe/Paris',
'geofilter.distance': lat + ',' + lon
apikey: api
}
const response = await this.request('https://data.opendatasoft.com/api/records/1.0/search/', params)
if (_.isUndefined(response.records[0])) {
logger.warn('Pas de données pour cette position')
const response = await this.request('https://public-api.meteofrance.fr/public/DPVigilance/v1/cartevigilance/encours', params)
if (_.isUndefined(response.product) || _.isUndefined(response.product.periods)) {
logger.warn('Pas de données de vigilance')
return
}
const res = response.records[0].fields
const result = {
vigiVent: res.etat_vent,
vigiOrage: res.etat_orage,
vigiPluie: res.etat_pluie_inondation,
vigiInondation: res.etat_inondation,
vigiNeige: res.etat_neige,
vigiCanicule: res.etat_canicule,
vigiFroid: res.etat_grand_froid,
vigiAvalanche: res.etat_avalanches,
vigiVague: res.etat_vague_submersion,
vigiCrue: '',
vigiConseil: '',
vigiComment: ''
}
// Crue
if (Object.prototype.hasOwnProperty.call(res, 'crue_valeur')) {
result.vigiCrue = res.crue_valeur
}
// Conseil
if (Object.prototype.hasOwnProperty.call(res, 'vigilanceconseil_texte')) {
result.vigiConseil = res.vigilanceconseil_texte
}
// Commentaire
if (Object.prototype.hasOwnProperty.call(res, 'vigilancecommentaire_texte')) {
result.vigiComment = res.vigilancecommentaire_texte
// Parcours des periods
for (const key in response.product.periods) {
const period = response.product.periods[key]
if (period.echeance !== 'J') {
break
}
// Parcours des timelaps.domain_ids
for (const key2 in period.timelaps.domain_ids) {
const domain = period.timelaps.domain_ids[key2]
// Données du departement domain 1 à 7
if (domain.domain_id === dpt) {
// Parcours des phenomenon_items
for (const key3 in domain.phenomenon_items) {
const phenomenon = domain.phenomenon_items[key3]
result[viginom[phenomenon.phenomenon_id]] = couleur[phenomenon.phenomenon_max_color_id]
}
}
// Données du departement domain 8 et 9
if (domain.domain_id === dpt + '10') {
// Parcours des phenomenon_items
for (const key3 in domain.phenomenon_items) {
const phenomenon = domain.phenomenon_items[key3]
result[viginom[phenomenon.phenomenon_id]] = couleur[phenomenon.phenomenon_max_color_id]
}
}
}
}
// Publish Data
eventEmitter.emit('frame', `${topic}/vigilance`, result)
if (_.isEmpty(result)) {
logger.warn('Pas de données de vigilance')
} else {
eventEmitter.emit('frame', `${topic}/vigilance`, result)
}
}

/**
Expand Down Expand Up @@ -441,10 +448,10 @@ class InfosVille {
const result = await axios.request(config)
return result.data
} catch (e) {
// OpenDatasoft
if (e.response && e.response.data && e.response.data.error) {
logger.warn(`HTTP request ${path} failed: ${e.response.data.error} (${e.response.data.errorcode})`)
return
// MeteoFrance API
if (e.response && e.response.status && e.response.data && e.response.data.includes('Invalid Credentials')) {
logger.warn(`HTTP request ${path} failed: Invalid Credentials. Make sure you have provided the correct security credentials`)
return {}
}
// Axios error
logger.warn(`HTTP request ${path} failed: ${e.message}`)
Expand Down

0 comments on commit c06bd3d

Please sign in to comment.