mirror of
https://github.com/bastienwirtz/homer.git
synced 2025-06-20 17:57:53 +02:00
Weather service refactoring
This commit is contained in:
parent
fd18715085
commit
f11b1c9dcf
@ -23,14 +23,13 @@ The following configuration is available for the PiHole service.
|
|||||||
## OpenWeatherMap
|
## OpenWeatherMap
|
||||||
|
|
||||||
Using the OpenWeatherMap service you can display weather information about a given location.
|
Using the OpenWeatherMap service you can display weather information about a given location.
|
||||||
|
The following configuration is available for the OpenWeatherMap service:
|
||||||
The following configuration is available for the OpenWeatherMap service
|
|
||||||
|
|
||||||
```
|
```
|
||||||
items:
|
items:
|
||||||
- name: "Weather"
|
- name: "Weather"
|
||||||
location: "Amsterdam" # your location.
|
location: "Amsterdam" # your location.
|
||||||
locationId: "2759794" # OpenWeatherMap city ID.
|
locationId: "2759794" # Optional: Specify OpenWeatherMap city ID for better accuracy
|
||||||
apiKey: "<---insert-api-key-here--->" # insert your own API key here. Request one from https://openweathermap.org/api.
|
apiKey: "<---insert-api-key-here--->" # insert your own API key here. Request one from https://openweathermap.org/api.
|
||||||
units: "metric" # units to display temperature. Can be one of: metric, imperial, kelvin. Defaults to kelvin.
|
units: "metric" # units to display temperature. Can be one of: metric, imperial, kelvin. Defaults to kelvin.
|
||||||
background: "square" # choose which type of background you want behind the image. Can be one of: square, cicle, none. Defaults to none.
|
background: "square" # choose which type of background you want behind the image. Can be one of: square, cicle, none. Defaults to none.
|
||||||
|
12622
package-lock.json
generated
12622
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,43 +1,32 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<div class="card" :class="item.class">
|
<div class="card" :class="item.class">
|
||||||
<a :href="`https://openweathermap.org/city/` + locationId" :target="item.target" rel="noreferrer">
|
<a
|
||||||
|
:href="`https://openweathermap.org/city/${id}`"
|
||||||
|
:target="item.target"
|
||||||
|
rel="noreferrer"
|
||||||
|
>
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
<div class="media">
|
<div class="media">
|
||||||
<div v-if="image" class="media-left" :class="item.background">
|
<div v-if="icon" class="media-left" :class="item.background">
|
||||||
<figure class="image is-48x48">
|
<figure class="image is-48x48">
|
||||||
<img
|
<img
|
||||||
:src="`http://openweathermap.org/img/wn/` + image + `@2x.png`"
|
:src="`https://openweathermap.org/img/wn/${icon}@2x.png`"
|
||||||
:alt="conditions"
|
:alt="conditions"
|
||||||
:title="conditions"
|
:title="conditions"
|
||||||
/>
|
/>
|
||||||
</figure>
|
</figure>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="item.icon" class="media-left">
|
|
||||||
<figure class="image is-48x48">
|
|
||||||
<i style="font-size: 35px" :class="['fa-fw', item.icon]"></i>
|
|
||||||
</figure>
|
|
||||||
</div>
|
|
||||||
<div class="media-content">
|
<div class="media-content">
|
||||||
<p class="title is-4">{{ locationName }}</p>
|
<p v-if="error" class="error">Data could not be retrieved</p>
|
||||||
|
<div v-else>
|
||||||
|
<p class="title is-4">{{ name }}</p>
|
||||||
<p class="subtitle is-6">
|
<p class="subtitle is-6">
|
||||||
<template v-if="item.subtitle">
|
{{ temp | tempSuffix(this.item.units) }}
|
||||||
{{ item.subtitle }}
|
|
||||||
</template>
|
|
||||||
<template v-else-if="api">
|
|
||||||
<template v-if="temp !== ''">
|
|
||||||
{{ temp }}
|
|
||||||
<span v-if="this.item.units === 'metric'">°C</span>
|
|
||||||
<span v-else-if="this.item.units === 'imperial'">°F</span>
|
|
||||||
<span v-else>K</span>
|
|
||||||
</template>
|
|
||||||
<template v-else>
|
|
||||||
<span class="error">Data could not be retrieved</span>
|
|
||||||
</template>
|
|
||||||
</template>
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<div class="tag" :class="item.tagstyle" v-if="item.tag">
|
<div class="tag" :class="item.tagstyle" v-if="item.tag">
|
||||||
<strong class="tag-text">#{{ item.tag }}</strong>
|
<strong class="tag-text">#{{ item.tag }}</strong>
|
||||||
</div>
|
</div>
|
||||||
@ -54,63 +43,21 @@ export default {
|
|||||||
item: Object,
|
item: Object,
|
||||||
},
|
},
|
||||||
data: () => ({
|
data: () => ({
|
||||||
|
id: null,
|
||||||
|
icon: null,
|
||||||
|
name: null,
|
||||||
|
temp: null,
|
||||||
|
conditions: null,
|
||||||
error: false,
|
error: false,
|
||||||
api: {
|
|
||||||
id: "",
|
|
||||||
name: "",
|
|
||||||
weather: [
|
|
||||||
{
|
|
||||||
main: "",
|
|
||||||
description: "",
|
|
||||||
icon: "",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
main: {
|
|
||||||
temp: "",
|
|
||||||
humidity: "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
computed: {
|
|
||||||
locationId: function () {
|
|
||||||
if (this.api) {
|
|
||||||
return this.api.id;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
locationName: function () {
|
|
||||||
if (this.api) {
|
|
||||||
return this.api.name;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
temp: function () {
|
|
||||||
if (this.api && this.api.main.temp !== "") {
|
|
||||||
return parseInt(this.api.main.temp).toFixed(1);
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
image: function () {
|
|
||||||
if (this.api) {
|
|
||||||
return this.api.weather[0].icon;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
conditions: function () {
|
|
||||||
if (this.api) {
|
|
||||||
return this.api.weather[0].description;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
},
|
|
||||||
created() {
|
created() {
|
||||||
this.fetchStatus();
|
this.fetchWeather();
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
fetchStatus: async function () {
|
fetchWeather: async function () {
|
||||||
let locationQuery;
|
let locationQuery;
|
||||||
|
|
||||||
// If a specific location ID was specified, use it. Otherwise retrieve value from location (name).
|
// Use location ID if specified, otherwise retrieve value from location (name).
|
||||||
if (this.item.locationId) {
|
if (this.item.locationId) {
|
||||||
locationQuery = `id=${this.item.locationId}`;
|
locationQuery = `id=${this.item.locationId}`;
|
||||||
} else {
|
} else {
|
||||||
@ -118,19 +65,43 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = `https://api.openweathermap.org/data/2.5/weather?${locationQuery}&appid=${this.item.apiKey}&units=${this.item.units}`;
|
const url = `https://api.openweathermap.org/data/2.5/weather?${locationQuery}&appid=${this.item.apiKey}&units=${this.item.units}`;
|
||||||
this.api = await fetch(url)
|
fetch(url)
|
||||||
.then((response) => response.json())
|
.then((response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw Error(response.statusText);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then((weather) => {
|
||||||
|
this.id = weather.id;
|
||||||
|
this.name = weather.name;
|
||||||
|
this.temp = parseInt(weather.main.temp).toFixed(1);
|
||||||
|
this.icon = weather.weather[0].icon;
|
||||||
|
this.conditions = weather.weather[0].description;
|
||||||
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
|
console.log(e);
|
||||||
this.error = true;
|
this.error = true;
|
||||||
console.log(e)
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
filters: {
|
||||||
|
tempSuffix: function (value, type) {
|
||||||
|
if (!value) return "";
|
||||||
|
|
||||||
|
let unit = "K";
|
||||||
|
if (type === "metric") {
|
||||||
|
unit = "°C";
|
||||||
|
} else if (type === "imperial") {
|
||||||
|
unit = "°F";
|
||||||
|
}
|
||||||
|
return `${value} ${unit}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
|
|
||||||
// Add a border around the weather image.
|
// Add a border around the weather image.
|
||||||
// Otherwise the image is not always distinguishable.
|
// Otherwise the image is not always distinguishable.
|
||||||
.media-left {
|
.media-left {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user