Skip to content

Commit

Permalink
Edits: for the weather Edge App
Browse files Browse the repository at this point in the history
  • Loading branch information
rusko124 committed Jul 15, 2024
1 parent d3c0e38 commit b089b7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion edge-apps/weather/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h2>Loading...</h2>
<template x-if="fetchError">
<div class="error">
<div class="group">
<h2>Waiting for Internet connection...</h2>
<h2 x-text="errorMessage"></h2>
<div class="loader"></div>
</div>
</div>
Expand Down
54 changes: 24 additions & 30 deletions edge-apps/weather/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async function getWeatherApiData (context) {
})

context.fetchError = !isComplete
context.errorMessage = error.message
}

return result
Expand All @@ -88,16 +89,9 @@ function formatTime (today) {
}

function refreshDateTime (context) {
clearTimeout(context.clockTimer)

const now = moment().utcOffset(context.tzOffset)
context.currentTime = formatTime(now)
context.currentDate = now.format('dddd, MMM DD')

context.clockTimer = setTimeout(
() => refreshDateTime(context),
context.clockTimerInterval
)
}

function findCurrentWeatherItem (list) {
Expand Down Expand Up @@ -222,8 +216,6 @@ const getTemp = (context, temp) => {
}

async function refreshWeather (context) {
clearTimeout(context.weatherTimer)

const data = await getWeatherApiData(context)

if (data.list !== undefined) {
Expand All @@ -234,7 +226,13 @@ async function refreshWeather (context) {
context.city = `${name}, ${country}`
context.tzOffset = parseInt(tzOffset / 60) // in minutes
context.tempScale = countriesUsingFahrenheit.includes(country) ? 'F' : 'C'

refreshDateTime(context)
setInterval(
() => {
refreshDateTime(context)
}, 1000 // 1 second
)

context.firstFetchComplete = true
context.isLoading = false
Expand Down Expand Up @@ -280,36 +278,22 @@ async function refreshWeather (context) {
}
})
}

context.weatherTimer = setTimeout(
() => refreshWeather(context),
context.weatherTimerInterval
)
}

function getWeatherData () {
return {
bgClass: '',
city: '',
currentDate: '',
currentFormattedTempScale: '',
currentTemp: null,
currentTime: '',
city: '',
lat: 0,
lng: 0,
currentWeatherId: 0,
clockTimer: null,
clockTimerInterval: 1000,
weatherTimer: null,
weatherTimerInterval: 1000 * 60 * 15, // 15 minutes
tzOffset: 0,
bgClass: '',
tempScale: 'C',
currentWeatherIcon: '',
currentWeatherId: 0,
currentWeatherStatus: '',
currentTemp: null,
currentFormattedTempScale: '',
forecastedItems: [],
fetchError: false,
firstFetchComplete: false,
isLoading: true,
forecastedItems: [],
init: async function () {
if (screenly.settings.override_coordinates) {
[this.lat, this.lng] = screenly.settings.override_coordinates.split(',')
Expand All @@ -322,8 +306,18 @@ function getWeatherData () {
this.apiKey = screenly.settings.openweathermap_api_key

await refreshWeather(this)
setInterval(
() => {
refreshWeather(this)
}, 1000 * 60 * 15 // 15 minutes
)
},
settings: {}
isLoading: true,
lat: 0,
lng: 0,
settings: {},
tempScale: 'C',
tzOffset: 0
}
}

Expand Down

0 comments on commit b089b7d

Please sign in to comment.