sub initWeather() for each id in ["weatherLocation", "weatherHeading", "weatherBody", "weatherAlerts", "weatherSource", "weatherRotation", "weatherRefresh"] 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.weatherLocation.text = "" m.weatherHeading.text = "Loading local weather" m.weatherBody.text = "" m.weatherAlerts.text = "" m.weatherSource.text = "" refreshWeather() m.weatherRefresh.control = "start" end sub sub stopWeather() 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 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() 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 periods = data.forecast m.weatherHeading.text = "EXTENDED 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