Home AMX User Forum AMX Technical Discussion

RSS Feeds

Dear experts can any one post an example source code for using the RSS feeds and retrieving data from this feeds (for example Rss feed from the AMX web site for their latest firm ware updates)

Comments

  • ColzieColzie Posts: 470
    Here is something I was playing around with..displaying weather from Yahoo's RSS feed on a DMS keypad. This is the include file, and is still pretty messy.



    PROGRAM_NAME='Weather'
    (***********************************************************)
    (* FILE CREATED ON: 06/12/2006 AT: 09:45:39 *)
    (***********************************************************)
    (* FILE_LAST_MODIFIED_ON: 06/16/2006 AT: 14:01:21 *)
    (***********************************************************)
    (***********************************************************)
    (* DEVICE NUMBER DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_DEVICE

    dvRSS = 0:3:0

    vdv_KP = vdv_KP2 //from master


    (***********************************************************)
    (* CONSTANT DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_CONSTANT

    LINE1 = 301
    LINE2 = 302
    LINE3 = 303
    LINE4 = 304
    LINE5 = 305
    LINE6 = 306


    (***********************************************************)
    (* VARIABLE DEFINITIONS GO BELOW *)
    (***********************************************************)
    define_variable

    char strPage[255]
    char buff_RSS[42048]

    char curr_buff[42048]
    char curr_item[42048]
    char curr_item_temp[10][42048]


    (***********************************************************)
    (* STARTUP CODE GOES BELOW *)
    (***********************************************************)
    define_start

    create_buffer dvRSS, buff_RSS


    (***********************************************************)
    (* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW *)
    (***********************************************************)


    (****************************)
    (* NAME: OnIP_Error *)
    (***********************************************************)
    (***********************************************************)
    define_call 'OnIP_Error' (SLONG nErrorCode, char strSite[])
    {
    switch(nErrorCode)
    {
    case 1 : send_string 0, "'IP Connect Error (',strSite,'): invalid server address'"
    case 2 : send_string 0, "'IP Connect Error (',strSite,'): invalid server port'"
    case 3 : send_string 0, "'IP Connect Error (',strSite,'): invalid value for Protocol'"
    case 4 : send_string 0, "'IP Connect Error (',strSite,'): unable to open communication port with server'"
    case 6 : send_string 0, "'IP Connect Error (',strSite,'): Connection refused'"
    case 7 : send_string 0, "'IP Connect Error (',strSite,'): Connection timed out'"
    case 8 : send_string 0, "'IP Connect Error (',strSite,'): Unknown connection error'"
    case 9 : send_string 0, "'IP Connect Error (',strSite,'): Already closed'"
    case 10 : send_string 0, "'IP Connect Error (',strSite,'): Binding error'"
    case 11 : send_string 0, "'IP Connect Error (',strSite,'): Listening error'"
    case 14 : send_string 0, "'IP Connect Error (',strSite,'): Local port already used'"
    case 15 : send_string 0, "'IP Connect Error (',strSite,'): UDP socket already listening'"
    case 16 : send_string 0, "'IP Connect Error (',strSite,'): Too many open sockets'"
    }
    }


    (****************************)
    (* NAME: get_ip *)
    (***********************************************************)
    (***********************************************************)
    define_function get_ip(integer intPort, char strSite[], char strBuffer[])
    {
    local_var slong nError

    strBuffer = ''
    ip_client_close(intPort)

    nError = ip_client_open(intPort, strSite, 80, 1)
    if(nError) call 'OnIP_Error' (nError, strSite)
    }


    (****************************)
    (* NAME: parse_xml_for *)
    (***********************************************************)
    (***********************************************************)
    define_function char[42048] parse_xml_for (char xml_string[42048], char xml_value[42048])
    {
    local_var char strReturn[42048]
    local_var integer pos_start
    local_var integer pos_end

    pos_start = find_string(xml_string,"'<',xml_value,'>'",1)
    pos_start = pos_start + length_string(xml_value) + 2
    pos_end = find_string(xml_string,"'</',xml_value,'>'",1)
    pos_end = pos_end - pos_start

    strReturn = mid_string(xml_string, pos_start, pos_end)
    send_string 0, "'xml_value(',xml_value,')=',strReturn"

    return strReturn
    }


    (*************************************)
    (* NAME: parse_xml_for_unclosed *)
    (***********************************************************)
    (***********************************************************)
    define_function char[42048] parse_xml_for_unclosed (char xml_string[42048], char xml_value[42048])
    {
    local_var char strReturn[42048]
    local_var integer pos_start
    local_var integer pos_end

    pos_start = find_string(xml_string, "'<', xml_value", 1)
    pos_start = pos_start + length_string(xml_value) + 2
    pos_end = find_string(xml_string, "'/>'", pos_start)

    strReturn = mid_string(xml_string, pos_start, (pos_end - pos_start))
    send_string 0, "'xml_value(', xml_value, ')=', strReturn"

    return strReturn
    }


    (*************************************)
    (* NAME: ParseStringFor *)
    (***********************************************************)
    (***********************************************************)
    define_function char[42048] ParseStringFor (char str_string[42048], char str_value[42048])
    {
    local_var char strReturn[42048]
    local_var integer pos_start
    local_var integer pos_end

    pos_start = find_string(str_string, str_value, 1)
    pos_start = pos_start + length_string(str_value) + 2
    pos_end = find_string(str_string, "'"'", pos_start)

    strReturn = mid_string(str_string, pos_start, (pos_end - pos_start))
    send_string 0, "'str_value(', str_value, ')=', strReturn"

    return strReturn
    }


    (*************************************)
    (* NAME: FixDayOfWeek *)
    (***********************************************************)
    (***********************************************************)
    define_function char[25] FixDayOfWeek (char strValue[25])
    {
    local_var char strReturn[42048]

    switch(strValue)
    {
    case 'Sun': strReturn = 'Sunday'
    case 'Mon': strReturn = 'Monday'
    case 'Tue': strReturn = 'Tuesday'
    case 'Wed': strReturn = 'Wednesday'
    case 'Thu': strReturn = 'Thursday'
    case 'Fri': strReturn = 'Friday'
    case 'Sat': strReturn = 'Saturday'
    }

    send_string 0, "'FixDayOfWeek(', strValue, ') = ', strReturn"

    return strReturn
    }


    (****************************)
    (* NAME: display_rss *)
    (***********************************************************)
    (***********************************************************)
    define_function display_rss(integer nItems)
    {
    local_var integer nItemCount
    local_var integer pos
    local_var char strTimeDate[50]
    local_var char strData[255]
    local_var char strDay[50]
    local_var char strDesc[50]
    local_var char strTemps[50]

    curr_buff = buff_RSS

    strData = parse_xml_for_unclosed(curr_buff, 'yweather:condition')
    remove_string(curr_buff, strData, 1)

    if(time_to_hour(time) >= 13)
    {
    strTimeDate = itoa(time_to_hour(time) - 12)
    }
    else
    {
    strTimeDate = itoa(time_to_hour(time))
    }

    if(time_to_hour(time) >= 12)
    {
    strTimeDate = "strTimeDate, ':', itoa(time_to_minute(time)), 'pm'"
    }
    else
    {
    strTimeDate = "strTimeDate, ':', itoa(time_to_minute(time)), 'am'"
    }

    strTimeDate = "itoa(date_to_month(date)), '/', itoa(date_to_day(date)), ' - ', strTimeDate"
    send_command vdv_KP, "'TEXT', itoa(LINE1), '-%SF%JC', 'Last update:| ', strTimeDate"

    //

    strData = parse_xml_for_unclosed(curr_buff, 'yweather:forecast')
    strDay = FixDayOfWeek(ParseStringFor(strData, 'day'))
    strDesc = ParseStringFor(strData, 'text')
    strTemps = "'low ', ParseStringFor(strData, 'low'), ' high ', ParseStringFor(strData, 'high')"

    send_command vdv_KP, "'TEXT', itoa(LINE2), '-%LF%JC', strDay"
    send_command vdv_KP, "'TEXT', itoa(LINE3), '-%SF%JC', strDesc, '|', strTemps"

    remove_string(curr_buff, strData, 1)

    //

    strData = parse_xml_for_unclosed(curr_buff, 'yweather:forecast')
    strDay = FixDayOfWeek(ParseStringFor(strData, 'day'))
    strDesc = ParseStringFor(strData, 'text')
    strTemps = "'low ', ParseStringFor(strData, 'low'), ' high ', ParseStringFor(strData, 'high')"

    send_command vdv_KP, "'TEXT', itoa(LINE4), '-%LF%JC', strDay"
    send_command vdv_KP, "'TEXT', itoa(LINE5), '-%SF%JC', strDesc, '|', strTemps"

    remove_string(curr_buff, strData, 1)

    //

    send_command vdv_KP, "'TEXT', itoa(LINE6), '-'"
    send_command vdv_KP, "'PAGE-(CURRENT)'" //to refresh new text

    }


    (****************************)
    (* NAME: string_replace *)
    (***********************************************************)
    (***********************************************************)
    define_function char[42048] string_replace(char strBuff[42048],char strFind[],char strReplace[])
    {
    local_var integer pos
    local_var char strLeft[50000]
    local_var char strRight[50000]

    pos = find_string(strBuff, strFind, 1)
    while(pos <> 0)
    {
    strLeft = left_string(strBuff, (pos - 1))
    send_string 0, "'strLeft=', strLeft"
    strRight = right_string(strBuff, (length_string(strBuff) - (pos + length_string(strFind) - 1)))
    send_string 0, "'strRight=', strRight"

    strBuff = "strLeft, strReplace, strRight" //build the string back up

    pos = find_string(strBuff, strFind, 1)
    }

    return strBuff
    }


    (****************************)
    (* NAME: clean_up_output *)
    (***********************************************************)
    (***********************************************************)
    define_function char[42048] clean_up_output(char strSTR[42048])
    {
    strSTR = string_replace(strSTR, ''', '''')
    strSTR = string_replace(strSTR, ''', '''')
    strSTR = string_replace(strSTR, '"', '''')
    strSTR = string_replace(strSTR, '&', '&')
    strSTR = string_replace(strSTR, '?', '-')
    //add new ones here as necessary
    //
    //
    return strSTR
    }


    (***********************************************************)
    (* EVENTS GO BELOW *)
    (***********************************************************)
    define_event

    data_event [dvRSS]
    {
    online:
    {
    send_string 0, 'dvRSS is ONLINE'
    send_string dvRSS, "'GET ', strPage, ' HTTP/1.0', 13, 10, 'Connection: Close', 13, 10, 13, 10"
    }

    offline:
    {
    send_string 0, 'dvRSS is OFFLINE'
    display_rss(4)
    }

    string:
    {
    (* do not remove me! *)
    //send_string 0, 'dvRSS is STRING'
    //send_string 0, data.text
    }
    }


    button_event[vdv_KP, KP_WEATHER]
    {
    push:
    {
    send_string 0, 'push'

    get_ip(dvRSS.PORT, "'xml.weather.yahoo.com'", buff_RSS)
    strPage = 'http://xml.weather.yahoo.com/forecastrss?p=46236'
    }
    }
  • viningvining Posts: 4,368
    RSSweather.axi

    Here's one I created last week. Reasonbly complete and ready for mutliple cities. I didn't get to any TP buttons or feedback but this will get you all the values and you can figure out the rest from there. I haven't tested this fully because of other problems that have distracted me from continuing it. It's neat and clean and ready to go and will get you 99% of the way there.

    Enjoy! Hopefully it will saves you from some of the misery that I've been through.
  • viningvining Posts: 4,368
    RSSweather.axi

    OK, here's the neat version without some constants commented out.
  • wcravenelwcravenel Posts: 114
    Thanks Chad. I changed your variables to volatile - locked up my NI-3000 before that.

    VAV - your posts do not have attachments - am I missing something?

    Bill
Sign In or Register to comment.