Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Canada (French): Fix Watch and Severe Thunderstorm events detection #222

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/integrations/env_canada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class EnvironmentCanada implements MeteoalarmIntegration {
},
{
en: 'Severe Thunderstorm',
fr: 'Orage Violent',
fr: 'Orages Violents',
type: MeteoalarmEventType.Thunderstorms,
},
{
Expand Down Expand Up @@ -187,29 +187,29 @@ export default class EnvironmentCanada implements MeteoalarmIntegration {
];
}

private get entityTypeTranslation(): { en: string; fr: string; type: EnvCanadaEntityType }[] {
private get entityTypeTranslation(): { en: string[]; fr: string[]; type: EnvCanadaEntityType }[] {
// English from: https://www.canada.ca/en/environment-climate-change/services/types-weather-forecasts-use/public/criteria-alerts.html
// French from : https://www.canada.ca/fr/environnement-changement-climatique/services/types-previsions-meteorologiques-utilisation/publiques/criteres-alertes-meteo.html
return [
{
type: EnvCanadaEntityType.Warning,
en: 'Warning',
fr: 'Avertissement De',
en: ['Warning'],
fr: ['Avertissement De'],
},
{
type: EnvCanadaEntityType.Watch,
en: 'Watch',
fr: 'Veille De',
en: ['Watch'],
fr: ['Veille De', "Veille D'"],
},
{
type: EnvCanadaEntityType.Statement,
en: 'Statement',
fr: 'Bulletin',
en: ['Statement'],
fr: ['Bulletin'],
},
{
type: EnvCanadaEntityType.Advisory,
en: 'Advisory',
fr: 'Avis De',
en: ['Advisory'],
fr: ['Avis De'],
},
];
}
Expand All @@ -222,13 +222,17 @@ export default class EnvironmentCanada implements MeteoalarmIntegration {
*/
private praseAlertName(alertName: string, type: EnvCanadaEntityType, isFrench: boolean) {
const prefixTranslation = this.entityTypeTranslation.find((t) => t.type == type)!;
const prefix = isFrench ? prefixTranslation.fr : prefixTranslation.en;
const prefixes = isFrench ? prefixTranslation.fr : prefixTranslation.en;

if (!alertName.includes(prefix)) {
const prefix = prefixes.find((p) => alertName.includes(p));
if (prefix == undefined) {
throw new Error(
`Translated event prefix was not found in alert name '${prefix}' (isFrench=${isFrench})`,
`Failed to match one of the known prefixes to alert name! ` +
`Was looking for [ "${prefixes.join('", "')}" ] and failed ` +
`(isFrench=${isFrench})`,
);
}

alertName = alertName.replace(prefix, '').trim();

return this.eventTypes.find((e) => {
Expand All @@ -253,7 +257,9 @@ export default class EnvironmentCanada implements MeteoalarmIntegration {
event: alert.type,
});
} else {
throw new Error(`Unknown Env canada alert type: ${alertName} (isFrench=${isFrench})`);
throw new Error(
`Unknown Environnement Canada alert encountered! Name: ${alertName} (isFrench=${isFrench})`,
);
}
}

Expand Down
Loading