Starting to build display
This commit is contained in:
+98
-92
@@ -15,123 +15,136 @@ export default class Weather {
|
||||
this.options = new WeatherOptions(latitude ?? 47.61002138071677, longitude ?? -122.17906310779568)
|
||||
|
||||
const me = this
|
||||
me.getWeather().then(() => {
|
||||
})
|
||||
setInterval(() => me.getWeather().then(() => me.setDisplay()), 60 * 1000)
|
||||
me.getWeather().then(() => me.setDisplay())
|
||||
}
|
||||
|
||||
async getWeather(): Promise<void> {
|
||||
const me = this
|
||||
const parameters: any = { ...me.options }
|
||||
let getData = false
|
||||
|
||||
if (!me.lastFetched) {
|
||||
parameters["daily"] = me.dailyVariables
|
||||
parameters["hourly"] = me.hourlyVariables
|
||||
parameters["current"] = me.currentVariables
|
||||
me.lastFetched = new LastFetched()
|
||||
getData = true
|
||||
} else {
|
||||
// Reasonable-ish timers?
|
||||
if (me.lastFetched.minutesSinceCurrent() > 15) {
|
||||
parameters["current"] = me.currentVariables
|
||||
me.lastFetched.current = new Date()
|
||||
getData = true
|
||||
}
|
||||
|
||||
if (me.lastFetched.hoursSinceHourly() > 3) {
|
||||
parameters["hourly"] = me.hourlyVariables
|
||||
me.lastFetched.hourly = new Date()
|
||||
getData = true
|
||||
}
|
||||
|
||||
if (me.lastFetched.hoursSinceDaily() > 8) {
|
||||
parameters["daily"] = me.dailyVariables
|
||||
me.lastFetched.daily = new Date()
|
||||
getData = true
|
||||
}
|
||||
}
|
||||
|
||||
if (!getData) return
|
||||
|
||||
const responses = await fetchWeatherApi(me.apiUrl, parameters)
|
||||
|
||||
for (const response of responses) {
|
||||
// Attributes for timezone and location
|
||||
const utcOffsetSeconds = response.utcOffsetSeconds()
|
||||
const timezone = response.timezone()
|
||||
const timezoneAbbreviation = response.timezoneAbbreviation()
|
||||
const latitude = response.latitude()
|
||||
const longitude = response.longitude()
|
||||
const current = response.current()
|
||||
const hourly = response.hourly()
|
||||
const daily = response.daily()
|
||||
|
||||
const current = response.current()!
|
||||
const hourly = response.hourly()!
|
||||
const daily = response.daily()!
|
||||
|
||||
const sunrise = daily.variables(2)!
|
||||
const sunset = daily.variables(3)!
|
||||
|
||||
// Note: The order of weather variables in the URL query and the indices below need to match!
|
||||
const weatherData = {
|
||||
current: {
|
||||
if (current != null) {
|
||||
me.current = {
|
||||
time: new Date((Number(current.time()) + utcOffsetSeconds) * 1000),
|
||||
temperature2m: current.variables(0)!.value(),
|
||||
relativeHumidity2m: current.variables(1)!.value(),
|
||||
apparentTemperature: current.variables(2)!.value(),
|
||||
temperature: current.variables(0)!.value(),
|
||||
humidity: current.variables(1)!.value(),
|
||||
feelsLike: current.variables(2)!.value(),
|
||||
precipitation: current.variables(3)!.value(),
|
||||
windSpeed10m: current.variables(4)!.value(),
|
||||
windGusts10m: current.variables(5)!.value(),
|
||||
windDirection10m: current.variables(6)!.value(),
|
||||
},
|
||||
hourly: {
|
||||
time: [...Array((Number(hourly.timeEnd()) - Number(hourly.time())) / hourly.interval())].map(
|
||||
(_, i) => new Date((Number(hourly.time()) + i * hourly.interval() + utcOffsetSeconds) * 1000)
|
||||
),
|
||||
temperature2m: hourly.variables(0)!.valuesArray()!,
|
||||
precipitation: hourly.variables(1)!.valuesArray()!,
|
||||
precipitationProbability: hourly.variables(2)!.valuesArray()!,
|
||||
},
|
||||
daily: {
|
||||
time: [...Array((Number(daily.timeEnd()) - Number(daily.time())) / daily.interval())].map(
|
||||
(_, i) => new Date((Number(daily.time()) + i * daily.interval() + utcOffsetSeconds) * 1000)
|
||||
),
|
||||
temperature2mMax: daily.variables(0)!.valuesArray()!,
|
||||
temperature2mMin: daily.variables(1)!.valuesArray()!,
|
||||
sunrise: [...Array(sunrise.valuesInt64Length())].map(
|
||||
(_, i) => new Date((Number(sunrise.valuesInt64(i)) + utcOffsetSeconds) * 1000)
|
||||
),
|
||||
sunset: [...Array(sunset.valuesInt64Length())].map(
|
||||
(_, i) => new Date((Number(sunset.valuesInt64(i)) + utcOffsetSeconds) * 1000)
|
||||
),
|
||||
uvIndexMax: daily.variables(4)!.valuesArray()!,
|
||||
precipitationSum: daily.variables(5)!.valuesArray()!,
|
||||
precipitationHours: daily.variables(6)!.valuesArray()!,
|
||||
windSpeed10mMax: daily.variables(7)!.valuesArray()!,
|
||||
windGusts10mMax: daily.variables(8)!.valuesArray()!,
|
||||
windDirection10mDominant: daily.variables(9)!.valuesArray()!,
|
||||
},
|
||||
windSpeed: current.variables(4)!.value(),
|
||||
windGusts: current.variables(5)!.value(),
|
||||
windDirection: current.variables(6)!.value()
|
||||
}
|
||||
}
|
||||
|
||||
console.log(weatherData)
|
||||
if (hourly != null) {
|
||||
const length = Number(hourly.timeEnd() - hourly.time()) / hourly.interval()
|
||||
const temperatures = hourly.variables(0)!.valuesArray()
|
||||
const precipitationTotals = hourly.variables(1)!.valuesArray()
|
||||
const precipitationProbabilities = hourly.variables(2)!.valuesArray()
|
||||
me.hourly.length = 0
|
||||
|
||||
// `weatherData` now contains a simple structure with arrays for datetime and weather data
|
||||
for (let i = 0; i < weatherData.hourly.time.length; i++) {
|
||||
console.log(
|
||||
weatherData.hourly.time[i].toISOString(),
|
||||
weatherData.hourly.temperature2m[i],
|
||||
weatherData.hourly.precipitation[i],
|
||||
weatherData.hourly.precipitationProbability[i]
|
||||
)
|
||||
for (let i = 0; i < length; i++) {
|
||||
me.hourly.push({
|
||||
time: new Date((Number(hourly.time()) + i * hourly.interval() + utcOffsetSeconds) * 1000),
|
||||
temperature: temperatures[i],
|
||||
precipitation: precipitationTotals[i],
|
||||
precipitationProbability: precipitationProbabilities[i]
|
||||
})
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < weatherData.daily.time.length; i++) {
|
||||
console.log(
|
||||
weatherData.daily.time[i].toISOString(),
|
||||
weatherData.daily.temperature2mMax[i],
|
||||
weatherData.daily.temperature2mMin[i],
|
||||
weatherData.daily.sunrise[i].toISOString(),
|
||||
weatherData.daily.sunset[i].toISOString(),
|
||||
weatherData.daily.uvIndexMax[i],
|
||||
weatherData.daily.precipitationSum[i],
|
||||
weatherData.daily.precipitationHours[i],
|
||||
weatherData.daily.windSpeed10mMax[i],
|
||||
weatherData.daily.windGusts10mMax[i],
|
||||
weatherData.daily.windDirection10mDominant[i]
|
||||
)
|
||||
|
||||
if (daily != null) {
|
||||
const length = Number(daily.timeEnd() - daily.time()) / daily.interval()
|
||||
const maxTemperatures = daily.variables(0)!.valuesArray()
|
||||
const minTemperatures = daily.variables(1)!.valuesArray()
|
||||
const sunrise = daily.variables(2)
|
||||
const sunset = daily.variables(3)
|
||||
const maxUVs = daily.variables(4)!.valuesArray()
|
||||
const precipitationTotals = daily.variables(5)!.valuesArray()
|
||||
const precipitationHours = daily.variables(6)!.valuesArray()
|
||||
const maxWindSpeeds = daily.variables(7)!.valuesArray()
|
||||
const maxWindGusts = daily.variables(8)!.valuesArray()
|
||||
const prominentWindDirections = daily.variables(9)!.valuesArray()
|
||||
|
||||
me.daily.length = 0
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
me.daily.push({
|
||||
time: new Date((Number(daily.time()) + i * daily.interval() + utcOffsetSeconds) * 1000),
|
||||
maxTemperature: maxTemperatures[i],
|
||||
minTemperature: minTemperatures[i],
|
||||
sunrise: new Date((Number(sunrise.valuesInt64(i)) + utcOffsetSeconds) * 1000),
|
||||
sunset: new Date((Number(sunset.valuesInt64(i)) + utcOffsetSeconds) * 1000),
|
||||
maxUV: maxUVs[i],
|
||||
totalPrecipitation: precipitationTotals[i],
|
||||
hoursOfPrecipitation: precipitationHours[i],
|
||||
maxWindSpeed: maxWindSpeeds[i],
|
||||
maxWindGusts: maxWindGusts[i],
|
||||
prominentWindDirection: prominentWindDirections[i]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
console.log(me)
|
||||
}
|
||||
}
|
||||
|
||||
private setDisplay() {
|
||||
let panel = document.getElementById("weather-panel")
|
||||
if (!panel) {
|
||||
panel = document.createElement("main")
|
||||
panel.id = "weather-panel"
|
||||
document.body.appendChild(panel)
|
||||
}
|
||||
|
||||
while (panel.firstChild) panel.removeChild(panel.firstChild)
|
||||
|
||||
const currentSection = document.createElement("section")
|
||||
currentSection.id = "current-section"
|
||||
panel.appendChild(currentSection)
|
||||
this.setDisplayCurrent(currentSection)
|
||||
}
|
||||
|
||||
private setDisplayCurrent(section: HTMLElement) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class WeatherOptions {
|
||||
@@ -149,7 +162,7 @@ class WeatherOptions {
|
||||
}
|
||||
}
|
||||
|
||||
class CurrentWeather {
|
||||
interface CurrentWeather {
|
||||
time: Date
|
||||
temperature: number
|
||||
humidity: number
|
||||
@@ -160,7 +173,8 @@ class CurrentWeather {
|
||||
windDirection: number
|
||||
}
|
||||
|
||||
class DailyWeather {
|
||||
interface DailyWeather {
|
||||
time: Date
|
||||
hoursOfPrecipitation: number
|
||||
totalPrecipitation: number
|
||||
sunrise: Date
|
||||
@@ -171,10 +185,9 @@ class DailyWeather {
|
||||
prominentWindDirection: number
|
||||
maxWindGusts: number
|
||||
maxWindSpeed: number
|
||||
asOf: Date
|
||||
}
|
||||
|
||||
class HourlyWeather {
|
||||
interface HourlyWeather {
|
||||
time: Date
|
||||
precipitation: number
|
||||
precipitationProbability: number
|
||||
@@ -182,26 +195,19 @@ class HourlyWeather {
|
||||
}
|
||||
|
||||
class LastFetched {
|
||||
current: Date
|
||||
hourly: Date
|
||||
daily: Date
|
||||
|
||||
constructor() {
|
||||
const now = new Date()
|
||||
this.current = now
|
||||
this.hourly = now
|
||||
this.daily = now
|
||||
}
|
||||
current: Date = new Date()
|
||||
hourly: Date = new Date()
|
||||
daily: Date = new Date()
|
||||
|
||||
minutesSinceCurrent(): number {
|
||||
return 0
|
||||
return (new Date().getTime() - this.current.getTime()) / 60000
|
||||
}
|
||||
|
||||
hoursSinceHourly(): number {
|
||||
return 0
|
||||
return (new Date().getTime() - this.hourly.getTime()) / 3600000
|
||||
}
|
||||
|
||||
hoursSinceDaily(): number {
|
||||
return 0
|
||||
return (new Date().getTime() - this.daily.getTime()) / 3600000
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user