Skip to content

Commit

Permalink
Merge pull request #1462 from cewert/post-task-device-profile
Browse files Browse the repository at this point in the history
Create and use generic PostTask to post device profile
  • Loading branch information
1hitsong authored Nov 11, 2023
2 parents 24cb72b + e6c5886 commit ea16b6c
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 67 deletions.
13 changes: 5 additions & 8 deletions components/data/SceneManager.brs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ sub popScene()
if group <> invalid
registerOverhangData(group)

if group.subtype() = "Home"
currentTime = CreateObject("roDateTime").AsSeconds()
if group.timeLastRefresh = invalid or (currentTime - group.timeLastRefresh) > 20
group.timeLastRefresh = currentTime
group.callFunc("refresh")
end if
end if

group.visible = true

m.content.replaceChild(group, 0)
Expand Down Expand Up @@ -143,6 +135,11 @@ end function
' Clear all content from group stack
sub clearScenes()
if m.content <> invalid then m.content.removeChildrenIndex(m.content.getChildCount(), 0)
for each group in m.groups
if LCase(group.subtype()) = "jfscreen"
group.callFunc("OnScreenHidden")
end if
end for
m.groups = []
end sub

Expand Down
32 changes: 32 additions & 0 deletions components/home/Home.brs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import "pkg:/source/api/baserequest.brs"
import "pkg:/source/utils/config.brs"
import "pkg:/source/utils/misc.brs"
import "pkg:/source/utils/deviceCapabilities.brs"

sub init()
m.isFirstRun = true
m.top.overhangTitle = "Home"
m.top.optionsAvailable = true
m.postTask = createObject("roSGNode", "PostTask")

if m.global.session.user.settings["ui.home.splashBackground"] = true
m.backdrop = m.top.findNode("backdrop")
m.backdrop.uri = buildURL("/Branding/Splashscreen?format=jpg&foregroundLayer=0.15&fillWidth=1280&width=1280&fillHeight=720&height=720&tag=splash")
Expand All @@ -18,3 +22,31 @@ end sub
sub loadLibraries()
m.top.findNode("homeRows").callFunc("loadLibraries")
end sub

' JFScreen hook that gets ran as needed.
' Used to update the focus, the state of the data, and tells the server about the device profile
sub OnScreenShown()
if isValid(m.top.lastFocus)
m.top.lastFocus.setFocus(true)
else
m.top.setFocus(true)
end if

refresh()

' post the device profile the first time this screen is loaded
if m.isFirstRun
m.isFirstRun = false
m.postTask.arrayData = getDeviceCapabilities()
m.postTask.apiUrl = "/Sessions/Capabilities/Full"
m.postTask.control = "RUN"
m.postTask.observeField("responseCode", "postFinished")
end if
end sub

' Triggered by m.postTask after completing a post.
' Empty the task data when finished.
sub postFinished()
m.postTask.unobserveField("responseCode")
m.postTask.callFunc("empty")
end sub
3 changes: 1 addition & 2 deletions components/home/Home.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<component name="Home" extends="JFGroup">
<component name="Home" extends="JFScreen">
<children>
<Poster id="backdrop" loadDisplayMode="scaleToZoom" width="1920" height="1200" />
<HomeRows id="homeRows" />
Expand All @@ -8,7 +8,6 @@
<interface>
<field id="selectedItem" alias="homeRows.selectedItem" />
<field id="quickPlayNode" alias="homeRows.quickPlayNode" />
<field id="timeLastRefresh" type="integer" />
<function name="refresh" />
<function name="loadLibraries" />
</interface>
Expand Down
21 changes: 21 additions & 0 deletions components/settings/settings.brs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "pkg:/source/utils/config.brs"
import "pkg:/source/utils/misc.brs"
import "pkg:/source/roku_modules/log/LogMixin.brs"
' post device profile
import "pkg:/source/utils/deviceCapabilities.brs"

sub init()
m.log = log.Logger("Settings")
Expand All @@ -27,6 +29,8 @@ sub init()
m.boolSetting.observeField("checkedItem", "boolSettingChanged")
m.radioSetting.observeField("checkedItem", "radioSettingChanged")

m.postTask = createObject("roSGNode", "PostTask")

' Load Configuration Tree
m.configTree = GetConfigTree()
LoadMenu({ children: m.configTree })
Expand Down Expand Up @@ -201,6 +205,23 @@ sub radioSettingChanged()
set_user_setting(selectedSetting.settingName, m.radioSetting.content.getChild(m.radioSetting.checkedItem).id)
end sub

' JFScreen hook that gets ran as needed.
' Assumes settings were changed and they affect the device profile.
' Posts a new device profile to the server using the task thread
sub OnScreenHidden()
m.postTask.arrayData = getDeviceCapabilities()
m.postTask.apiUrl = "/Sessions/Capabilities/Full"
m.postTask.control = "RUN"
m.postTask.observeField("responseCode", "postFinished")
end sub

' Triggered by m.postTask after completing a post.
' Empty the task data when finished.
sub postFinished()
m.postTask.unobserveField("responseCode")
m.postTask.callFunc("empty")
end sub

' Returns true if any of the data entry forms are in focus
function isFormInFocus() as boolean
if isValid(m.settingDetail.focusedChild) or m.radioSetting.hasFocus() or m.boolSetting.hasFocus() or m.integerSetting.hasFocus()
Expand Down
2 changes: 1 addition & 1 deletion components/settings/settings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<component name="Settings" extends="JFGroup">
<component name="Settings" extends="JFScreen">
<children>

<Label id="path" translation="[95,175]" font="font:SmallestBoldSystemFont" />
Expand Down
79 changes: 79 additions & 0 deletions components/tasks/PostTask.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import "pkg:/source/api/baserequest.brs"
import "pkg:/source/utils/misc.brs"

sub init()
m.top.functionName = "postItems"
end sub

' Main function for PostTask.
' Posts either an array of data
' or a string of data to an API endpoint.
' Saves the response information
sub postItems()
if m.top.apiUrl = ""
print "ERROR in PostTask. Invalid API URL provided"
return
end if
if m.top.arrayData.count() > 0 and m.top.stringData = ""
print "PostTask Started - Posting array to " + m.top.apiUrl
req = APIRequest(m.top.apiUrl)
req.SetRequest("POST")
httpResponse = asyncPost(req, FormatJson(m.top.arrayData))
m.top.responseCode = httpResponse
print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr()
else if m.top.arrayData.count() = 0 and m.top.stringData <> ""
print "PostTask Started - Posting string(" + m.top.stringData + ") to " + m.top.apiUrl
req = APIRequest(m.top.apiUrl)
req.SetRequest("POST")
httpResponse = asyncPost(req, m.top.stringData)
m.top.responseCode = httpResponse
print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr()
else
print "ERROR processing data for PostTask"
end if
end sub

' Post data and wait for response code
function asyncPost(req, data = "" as string) as integer
' response code 0 means there was an error
respCode = 0

req.setMessagePort(CreateObject("roMessagePort"))
req.AddHeader("Content-Type", "application/json")
req.AsyncPostFromString(data)
' wait up to m.top.timeoutSeconds for a response
' NOTE: wait() uses milliseconds - multiply by 1000 to convert
resp = wait(m.top.timeoutSeconds * 1000, req.GetMessagePort())

respString = resp.GetString()
if isValidAndNotEmpty(respString)
m.top.responseBody = ParseJson(respString)
print "m.top.responseBody=", m.top.responseBody
end if

respCode = resp.GetResponseCode()
if respCode < 0
' there was an unexpected error
m.top.failureReason = resp.GetFailureReason()
else if respCode >= 200 and respCode < 300
' save response headers if they're available
m.top.responseHeaders = resp.GetResponseHeaders()
end if

return respCode
end function

' Revert PostTask to default state
sub empty()
' These should match the defaults set in PostTask.xml
m.top.apiUrl = ""
m.top.timeoutSeconds = 30

m.top.arrayData = {}
m.top.stringData = ""

m.top.responseCode = invalid
m.top.responseBody = {}
m.top.responseHeaders = {}
m.top.failureReason = ""
end sub
18 changes: 18 additions & 0 deletions components/tasks/PostTask.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>

<component name="PostTask" extends="Task">
<interface>
<field id="apiUrl" type="string" />
<field id="timeoutSeconds" type="integer" value="30" />

<field id="arrayData" type="assocarray" value="{}" />
<field id="stringData" type="string" value="" />

<field id="responseCode" type="integer" />
<field id="responseBody" type="assocarray" value="{}" />
<field id="responseHeaders" type="assocarray" value="{}" />
<field id="failureReason" type="string" value="" />

<function name="empty" />
</interface>
</component>
24 changes: 13 additions & 11 deletions source/Main.brs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ sub Main (args as dynamic) as void
app_start:
' First thing to do is validate the ability to use the API
if not LoginFlow() then return
' tell jellyfin server about device capabilities
PostDeviceProfile()
' remove previous scenes from the stack

' remove login scenes from the stack
sceneManager.callFunc("clearScenes")

stopLoadingSpinner()
Expand Down Expand Up @@ -634,14 +633,11 @@ sub Main (args as dynamic) as void
if event.exitedScreensaver = true
sceneManager.callFunc("resetTime")
group = sceneManager.callFunc("getActiveScene")
if isValid(group) and isValid(group.subtype())
if isValid(group)
' refresh the current view
if group.subtype() = "Home"
currentTime = CreateObject("roDateTime").AsSeconds()
group.timeLastRefresh = currentTime
group.callFunc("refresh")
if group.isSubType("JFScreen")
group.callFunc("OnScreenShown")
end if
' todo: add other screens to be refreshed - movie detail, tv series, episode list etc.
end if
else if isValid(event.audioGuideEnabled)
tmpGlobalDevice = m.global.device
Expand Down Expand Up @@ -673,12 +669,18 @@ sub Main (args as dynamic) as void
' The audio codec capability has changed if true.
print "event.audioCodecCapabilityChanged = ", event.audioCodecCapabilityChanged

PostDeviceProfile()
postTask = createObject("roSGNode", "PostTask")
postTask.arrayData = getDeviceCapabilities()
postTask.apiUrl = "/Sessions/Capabilities/Full"
postTask.control = "RUN"
else if isValid(event.videoCodecCapabilityChanged)
' The video codec capability has changed if true.
print "event.videoCodecCapabilityChanged = ", event.videoCodecCapabilityChanged

PostDeviceProfile()
postTask = createObject("roSGNode", "PostTask")
postTask.arrayData = getDeviceCapabilities()
postTask.apiUrl = "/Sessions/Capabilities/Full"
postTask.control = "RUN"
else if isValid(event.appFocus)
' It is set to False when the System Overlay (such as the confirm partner button HUD or the caption control overlay) takes focus and True when the channel regains focus
print "event.appFocus = ", event.appFocus
Expand Down
1 change: 0 additions & 1 deletion source/VideoPlayer.brs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ sub AddVideoContent(video as object, mediaSourceId as dynamic, audio_stream_idx
MarkItemWatched(video.id)
video.content.watched = not video.content.watched
group = m.scene.focusedChild
group.timeLastRefresh = CreateObject("roDateTime").AsSeconds()
group.callFunc("refresh")
video.content = invalid
return
Expand Down
11 changes: 8 additions & 3 deletions source/api/baserequest.brs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ sub setCertificateAuthority(request as object) as void
end sub

' Takes and returns a roUrlTransfer object after adding a Jellyfin "Authorization" header
function authRequest(request as object) as object
function authRequest(req as object) as object
req.AddHeader("Authorization", buildAuthHeader())
return req
end function

' Returns a string containing the "Authorization" header payload
function buildAuthHeader() as string
QUOTE = Chr(34)
auth = "MediaBrowser" + " Client=" + QUOTE + "Jellyfin Roku" + QUOTE
auth = auth + ", Device=" + QUOTE + m.global.device.name + " (" + m.global.device.model + ")" + QUOTE
Expand All @@ -211,6 +217,5 @@ function authRequest(request as object) as object
auth = auth + ", Token=" + QUOTE + m.global.session.user.authToken + QUOTE
end if

request.AddHeader("Authorization", auth)
return request
return auth
end function
Loading

0 comments on commit ea16b6c

Please sign in to comment.