Add live Dunnellon weather and clean up Roku guide titles
This commit is contained in:
@@ -43,6 +43,8 @@ sub runRequest()
|
||||
registry.Write("id", request.server.id)
|
||||
registry.Flush()
|
||||
m.top.result = {data: readJson(request.server.baseUrl + "/api/v1/channels")}
|
||||
else if request.kind = "weather"
|
||||
m.top.result = {data: readJson(request.url, 12000)}
|
||||
else
|
||||
m.top.result = {data: readJson(request.url)}
|
||||
end if
|
||||
@@ -55,14 +57,14 @@ function readServer(baseUrl as string) as dynamic
|
||||
return {id: data.id, name: data.name, baseUrl: baseUrl}
|
||||
end function
|
||||
|
||||
function readJson(url as string) as dynamic
|
||||
function readJson(url as string, timeoutMs = 3500 as integer) as dynamic
|
||||
port = CreateObject("roMessagePort")
|
||||
transfer = CreateObject("roUrlTransfer")
|
||||
transfer.SetMessagePort(port)
|
||||
transfer.SetUrl(url)
|
||||
transfer.AddHeader("Accept", "application/json")
|
||||
if not transfer.AsyncGetToString() then return invalid
|
||||
event = Wait(3500, port)
|
||||
event = Wait(timeoutMs, port)
|
||||
if Type(event) = "roUrlEvent" and event.GetResponseCode() = 200
|
||||
data = ParseJson(event.GetString())
|
||||
if GetInterface(data, "ifAssociativeArray") <> invalid then return data
|
||||
|
||||
@@ -12,6 +12,7 @@ sub init()
|
||||
|
||||
m.menu.ObserveField("itemSelected", "onMenuSelect")
|
||||
m.video.ObserveField("state", "onVideoState")
|
||||
initWeather()
|
||||
m.bannerTimer.ObserveField("fire", "hideBanner")
|
||||
m.clockTimer.ObserveField("fire", "refreshClock")
|
||||
m.retryTimer.ObserveField("fire", "retryPlayback")
|
||||
@@ -23,6 +24,7 @@ sub findServer(baseUrl as string, useSaved as boolean)
|
||||
if m.serverRequest <> invalid then return
|
||||
m.banner.visible = false
|
||||
m.weather.visible = false
|
||||
stopWeather()
|
||||
m.video.control = "stop"
|
||||
m.video.visible = false
|
||||
m.playbackStatus.visible = false
|
||||
@@ -156,7 +158,11 @@ sub startPlayback()
|
||||
m.weather.visible = channel.type = "weather"
|
||||
m.video.visible = not m.weather.visible
|
||||
m.playbackStatus.visible = false
|
||||
if m.weather.visible then return
|
||||
if m.weather.visible
|
||||
startWeather()
|
||||
return
|
||||
end if
|
||||
stopWeather()
|
||||
|
||||
streamUrl = channel.streamUrl
|
||||
if GetInterface(streamUrl, "ifString") = invalid or streamUrl = ""
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<component name="RetroScene" extends="Scene">
|
||||
<script type="text/brightscript" uri="pkg:/components/RetroScene.brs" />
|
||||
<script type="text/brightscript" uri="pkg:/components/Guide.brs" />
|
||||
<script type="text/brightscript" uri="pkg:/components/Weather.brs" />
|
||||
<children>
|
||||
<Video id="video" width="1280" height="720" enableUI="false" />
|
||||
<Group id="weather" visible="false">
|
||||
@@ -9,8 +10,16 @@
|
||||
<Rectangle translation="[70,120]" width="1140" height="5" color="0x78E5EBFF" />
|
||||
<Label translation="[70,55]" width="900" height="55" text="retroLabs Weather"
|
||||
font="font:LargeBoldSystemFont" color="0xF8F7EFFF" />
|
||||
<Label translation="[70,195]" width="1100" height="50" text="Live local weather is coming soon."
|
||||
font="font:MediumSystemFont" color="0xF8F7EFFF" />
|
||||
<Label id="weatherLocation" translation="[70,135]" width="1140" height="50"
|
||||
font="font:MediumBoldSystemFont" color="0x78E5EBFF" />
|
||||
<Label id="weatherHeading" translation="[70,198]" width="1140" height="42"
|
||||
font="font:MediumBoldSystemFont" />
|
||||
<Label id="weatherBody" translation="[70,252]" width="1140" height="285"
|
||||
font="font:MediumSystemFont" wrap="true" numLines="7" />
|
||||
<Label id="weatherAlerts" translation="[70,553]" width="1140" height="74"
|
||||
font="font:SmallBoldSystemFont" color="0xFFD57AFF" wrap="true" numLines="2" />
|
||||
<Label id="weatherSource" translation="[70,646]" width="1140" height="32"
|
||||
font="font:SmallSystemFont" color="0xB7D4E9FF" />
|
||||
</Group>
|
||||
<Group id="banner" translation="[50,525]" visible="false">
|
||||
<Rectangle width="1180" height="145" color="0x101A32EF" />
|
||||
@@ -41,6 +50,8 @@
|
||||
</Group>
|
||||
<Timer id="bannerTimer" duration="5" />
|
||||
<Timer id="clockTimer" duration="1" repeat="true" />
|
||||
<Timer id="weatherRotation" duration="9" repeat="true" />
|
||||
<Timer id="weatherRefresh" duration="60" repeat="true" />
|
||||
<Timer id="retryTimer" duration="3" />
|
||||
</children>
|
||||
</component>
|
||||
|
||||
111
roku/components/Weather.brs
Normal file
111
roku/components/Weather.brs
Normal file
@@ -0,0 +1,111 @@
|
||||
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
|
||||
Reference in New Issue
Block a user