sub initWeather() for each id in ["weatherLocation", "weatherHeading", "weatherBody", "weatherAlerts", "weatherSource", "weatherRotation", "weatherRefresh", "weatherAudio", "weatherPreviewFrame", "weatherPreviewLabel", "forecastCards"] m[id] = m.top.FindNode(id) end for m.weatherRotation.ObserveField("fire", "rotateWeather") m.weatherRefresh.ObserveField("fire", "refreshWeather") end sub sub startWeather() m.weatherPage = 0 m.weatherData = invalid m.forecastCards.visible = false m.weatherBody.visible = true m.weatherLocation.text = "" m.weatherHeading.text = "Loading local weather" m.weatherBody.text = "" m.weatherAlerts.text = "" m.weatherSource.text = "" startWeatherPreview() refreshWeather() m.weatherRefresh.control = "start" end sub sub startWeatherPreview() preview = invalid if m.previousIndex >= 0 and m.previousIndex < m.channels.Count() candidate = m.channels[m.previousIndex] if candidate.type = "linear" then preview = candidate end if if preview = invalid for each candidate in m.channels if candidate.type = "linear" preview = candidate exit for end if end for end if if preview = invalid hideWeatherPreview() return end if m.video.translation = [804, 212] m.video.width = 408 m.video.height = 230 m.video.mute = true m.video.visible = true m.weatherPreviewFrame.visible = true m.weatherPreviewLabel.text = "LIVE " + channelLabel(preview) if m.videoChannelId <> preview.id or m.video.state <> "playing" then playLinear(preview) end sub sub hideWeatherPreview() m.video.visible = false m.video.control = "stop" m.videoChannelId = invalid m.weatherPreviewFrame.visible = false m.weatherPreviewLabel.text = "" end sub sub stopWeather() m.weatherAudio.control = "stop" m.weatherMusicUrl = invalid m.weatherRotation.control = "stop" m.weatherRefresh.control = "stop" if m.weatherWorker <> invalid m.weatherWorker.UnobserveFieldScoped("result") m.weatherWorker = invalid end if end sub sub refreshWeather() if not m.weather.visible or m.weatherWorker <> invalid then return m.weatherWorker = startRequest({kind: "weather", url: m.server.baseUrl + "/api/v1/weather"}, "onWeather") end sub sub onWeather(event as object) if m.weatherWorker = invalid or not event.GetRoSGNode().IsSameNode(m.weatherWorker) then return m.weatherWorker = invalid if not m.weather.visible then return firstResult = m.weatherData = invalid m.weatherData = event.GetData().data if m.weatherData <> invalid then updateWeatherMusic(m.weatherData.musicUrl) renderWeather() if firstResult then m.weatherRotation.control = "start" end sub sub rotateWeather() if not m.weather.visible then return m.weatherPage = (m.weatherPage + 1) mod 4 renderWeather() end sub sub renderWeather() m.forecastCards.visible = false m.weatherBody.visible = true data = m.weatherData m.weatherAlerts.text = "" m.weatherSource.text = "" if data = invalid m.weatherHeading.text = "Local weather unavailable" m.weatherBody.text = "" return end if m.weatherLocation.text = data.location if data.status <> "ok" m.weatherHeading.text = "Local weather unavailable" if data.status = "unconfigured" then m.weatherHeading.text = "Set a weather location on the server" m.weatherBody.text = "" return end if newline = Chr(10) m.weatherSource.text = data.source if data.stale then m.weatherSource.text += " | Some data may be delayed" if data.alerts.Count() > 0 alert = data.alerts[0] m.weatherAlerts.text = "ACTIVE ALERT | " + alert.event if data.alerts.Count() > 1 then m.weatherAlerts.text += " (+" + StrI(data.alerts.Count() - 1).Trim() + " more)" else if not data.alertsAvailable m.weatherAlerts.text = "Alert feed temporarily unavailable" end if if m.weatherPage = 0 m.weatherHeading.text = "CURRENT CONDITIONS" current = data.current if current = invalid m.weatherBody.text = "Station observation unavailable" return end if m.weatherBody.text = tempLabel(current.temperature) + " | " + current.conditions if current.humidity <> invalid then m.weatherBody.text += newline + "Humidity " + StrI(current.humidity).Trim() + "%" if current.windMph <> invalid then m.weatherBody.text += newline + "Wind " + StrI(current.windMph).Trim() + " mph" else if m.weatherPage = 3 m.weatherHeading.text = "TODAY'S ALMANAC" m.weatherBody.text = "Sunrise " + data.almanac.sunrise + newline + "Sunset " + data.almanac.sunset else periods = data.hourly m.weatherHeading.text = "NEXT SIX HOURS" if m.weatherPage = 2 m.weatherHeading.text = "EXTENDED FORECAST" if showForecastCards(data.forecast) then return periods = data.forecast end if lines = [] for each period in periods lines.Push(period.name + " | " + tempLabel(period.temperature) + " | " + period.conditions) end for m.weatherBody.text = lines.Join(newline) if lines.Count() = 0 then m.weatherBody.text = "Forecast temporarily unavailable" end if end sub function tempLabel(value as dynamic) as string if value = invalid then return "—" return StrI(value).Trim() + "°F" end function sub updateWeatherMusic(path as dynamic) if path = m.weatherMusicUrl then return m.weatherAudio.control = "stop" m.weatherMusicUrl = path if GetInterface(path, "ifString") = invalid or path = "" then return track = CreateObject("roSGNode", "ContentNode") track.url = m.server.baseUrl + path track.streamFormat = "mp3" m.weatherAudio.content = track m.weatherAudio.control = "play" end sub function showForecastCards(periods as object) as boolean days = [] for i = 0 to periods.Count() - 2 day = periods[i] night = periods[i + 1] if day.daytime = true and night.daytime = false then days.Push({day: day, night: night}) if days.Count() = 3 then exit for end for if days.Count() < 3 then return false for i = 0 to 2 suffix = StrI(i).Trim() forecast = days[i] m.top.FindNode("forecastDay" + suffix).text = UCase(forecast.day.name) m.top.FindNode("forecastCondition" + suffix).text = forecast.day.conditions m.top.FindNode("forecastTemps" + suffix).text = "Lo " + tempLabel(forecast.night.temperature) + " Hi " + tempLabel(forecast.day.temperature) end for m.weatherBody.visible = false m.forecastCards.visible = true return true end function