Forum Discussion

haamro's avatar
haamro
Visitor
8 years ago

[SOLVED] Returning value from function with Task

Hello, I am trying to get a content from the web using Task. The Sub works great, except I am having difficulty returning the value from the function. Can somebody help me.

<?xml version="1.0" encoding="utf-8" ?>
 
<component name = "postergridCR" extends = "Task" >
 
  <interface>
    <field id = "postergriduri" type = "string" />
    <field id = "postergridcontent" type = "string"/>
  </interface>
 
  <script type = "text/brightscript" >
    <![CDATA[
 
    sub init()
      m.top.functionName = "getContent"
    end sub
 
    sub getContent()
      readInternet = createObject("roUrlTransfer")
      readInternet.setUrl(m.top.postergriduri)
      postergridcontent = readInternet.GetToString()
      m.top.postergridcontent = postergridcontent
   end sub
 
    ]]>
  </script>
 
</component>

I am trying to return the content from the above function
function getwebcontent(webpage as string)

   m.myObj = createObject("RoSGNode", "postergridCR")
   m.myObj.postergriduri=webpage
   m.myObj.functionName="getcontent"
   m.myObj.control = "RUN"
   return m.myObj.postergridcontent  <<======== How do I return the content ??

end function

Edit: Post marked as solved

16 Replies

  • I think you're almost there,  But you can't expect m.top.content to be set immediately after you call the task. You need to wait for a callback and process it there.
    html=m.top.content    <-- isn't m.top.content now set ??


    >> no, result will be in "m.haamro.HTTPcontent" in "gotContent" sub, not here, and not in "m.top.content".  If you don't want to / can't re-use gotContent sub, then you can set a new different observer on m.haamro to handle the response differently than gotContent:

    m.haamro.ObserveField("HTTPcontent","newDifferentFunction")

    ...

    sub newDifferentFunction()
       m.top.content = m.haamro.HTTPcontent
       ' or '
       html = m.haamro.HTTPcontent
       print html
    end sub
  • Here is the more complete solution with your code:

    function getHTMLContent(webpage as string)
       m.haamro = createObject("RoSGNode", "haamroContent")
       m.haamro.HTTPurl=webpage
       m.haamro.functionName="getdata"
       m.haamro.ObserveField("HTTPcontent","gotContent")
       m.haamro.control = "RUN"
    end function

    sub gotContent()
        m.top.content = m.haamro.HTTPcontent
       print m.top.content '<--- this is working and prints the output'
    end sub

    function GrabWOD(webpage as string)
       m.haamro.HTTPurl=webpage
       m.haamro.ObserveField("HTTPcontent","newDifferentFunction")
       m.haamro.control = "RUN"
      ' This function can never see the response from the task, it has to use the observeField callback to get it'
    end function

    sub newDifferentFunction()
        m.top.content = m.haamro.HTTPcontent
        ' or '
        html = m.haamro.HTTPcontent
        print html
    end sub
  • Getting HTTPcontent works great ! Thank you very much for making it possible. But how do I return the HTTPContent  value from the function itself so that I can use it elsewhere

    function show_web_content (webpage)
      ......
    return webcontent
    end function


    function getHTMLContent(webpage as string)   <=== HTTPcontent is not set yet so I cannot return its value from this function
       m.haamro = createObject("RoSGNode", "haamroContent")
       m.haamro.HTTPurl=webpage
       m.haamro.functionName="getdata"
       m.haamro.ObserveField("HTTPcontent","gotContent")
       m.haamro.control = "RUN"
    end function

    function gotContent() <=== this function is called from above, so not sure how do I return the value from this function to the main function that is making HTTP Request.
        m.top.content = m.haamro.HTTPcontent
       print m.top.content
    end function
  • Are you calling this code from Main or from a node?
    Assuming this is inside a node, then after you assign m.top.content to the current node's (lets call it NODE) content field, that will be visible externally via NODE.content
    Would need to see more code / what you're trying to do to give you exact steps.

    Maybe this will help, from inside a node the fields are referenced by "m.top.FIELDNAME" and from outside the same node, the fields can be retrieved via NODE.FIELDNAME where NODE is the node object, usually preceded by "m." so it can be referenced throughout the current scope.