Search code examples
rokubrightscript

Call function of main.brs in roku


How can we call the function of main.brs into other .brs file? Can you guys help me out? Since i am the new in Roku. I have already gave the path of main.brs file in customscreen.xml but by this method it is not working I have my one API URL which give state as 0,1 and another random code.i have made a function in main.brs and have call them in main itself to show different scene in state of 0 and 1 and in custom.brs i have made a screen display method so now i want that random code in that so that i can display that random code in screen Here is the code of main.brs and custom brs file

main.brs ' ********** Copyright 2016 Roku Corp. All Rights Reserved. **********

 sub RunUserInterface()
    screen = CreateObject("roSGScreen")
     port = CreateObject("roMessagePort")
    screen.SetMessagePort(port)

    res=getauth()
    response = parseJSON(res)
'    print "what we got:"response
'    
'    print response.valid
'    print response.atuh
'    chck=status.valid
'    print chck
status=response.valid

    if status = "1" then
    scene = screen.CreateScene("HomeScene")
      screen.Show()

    else 
    scene = screen.CreateScene("MessageScreen") 
      screen.Show()
  while (true)
   msg = wait(10, m.port)
    msgType = type(msg)


  endwhile

    endif



    oneRow = GetApiArray()
    list = [
        {
            Title:"First row"
            ContentList : oneRow
        }
        {
            Title:"Second row"
            ContentList : oneRow
        }
    ]
    scene.gridContent = ParseXMLContent(list)

    while true
        msg = wait(0, port)
        print "------------------"
        print "msg = "; msg
    end while

    if screen <> invalid then
        screen.Close()
        screen = invalid
    end if
end sub


Function ParseXMLContent(list As Object)
    RowItems = createObject("RoSGNode","ContentNode")

    for each rowAA in list
        row = createObject("RoSGNode","ContentNode")
        row.Title = rowAA.Title

        for each itemAA in rowAA.ContentList
            item = createObject("RoSGNode","ContentNode")
            ' We don't use item.setFields(itemAA) as doesn't cast streamFormat to proper value
            for each key in itemAA
                item[key] = itemAA[key]
            end for
            row.appendChild(item)
        end for
        RowItems.appendChild(row)
    end for

    return RowItems
End Function


Function GetApiArray()
    url = CreateObject("roUrlTransfer")
    url.SetUrl("http://api.delvenetworks.com/rest/organizations/59021fabe3b645968e382ac726cd6c7b/channels/1cfd09ab38e54f48be8498e0249f5c83/media.rss")
    rsp = url.GetToString()

    responseXML = ParseXML(rsp)
    responseXML = responseXML.GetChildElements()
    responseArray = responseXML.GetChildElements()

    result = []

    for each xmlItem in responseArray
        if xmlItem.getName() = "item"
            itemAA = xmlItem.GetChildElements()
            if itemAA <> invalid
                item = {}
                for each xmlItem in itemAA
                    item[xmlItem.getName()] = xmlItem.getText()
                    if xmlItem.getName() = "media:content"
                        item.stream = {url : xmlItem.url}
                        item.url = xmlItem.getAttributes().url
                        item.streamFormat = "mp4"

                        mediaContent = xmlItem.GetChildElements()
                        for each mediaContentItem in mediaContent
                            if mediaContentItem.getName() = "media:thumbnail"
                                item.HDPosterUrl = mediaContentItem.getattributes().url
                                item.hdBackgroundImageUrl = mediaContentItem.getattributes().url
                            end if
                        end for
                    end if
                end for
                result.push(item)
            end if
        end if
    end for

    return result
End Function


Function ParseXML(str As String) As dynamic
    if str = invalid return invalid
    xml = CreateObject("roXMLElement")
    if not xml.Parse(str) return invalid
    return xml
End Function


function getauth() As string

    url = CreateObject("roUrlTransfer") 
    url.SetUrl("http://demo8592579.mockable.io/rokuTest/auth")
    rsp = url.GetToString()


    return rsp



end function


function GetCode() As string

    url = CreateObject("roUrlTransfer") 
    url.SetUrl("http://demo8592579.mockable.io/rokuTest/auth")
    rsp = url.GetToString()
     response=parseJSON(rsp)

    return response



end function

MessageScreen.brs

function init()
m.top.setFocus(true)
m.myLabel = m.top.findNode("myLabel")
 m.myLabel.text = "HOW"

m.myLabel.font.size=92
'
'    'Set the color to light blue
m.myLabel.color="0x72D7EEFF"

end function

Now i want to display random code in Messagescreen.brs can you guys help me out?


Solution

  • If you want to get response from your GetCode() function to the scene, put GetCode() function inside a TASK, then run the TASK in Scene init() function for example and you will get the code once TASK get your response from the API. It looks like you were not aware of this option so please read this before starting: https://sdkdocs.roku.com/display/sdkdoc/Task.

    Also check my repo if you need a real world example on how to use tasks: https://github.com/umitic/Roku-Custom-Audio-Player