Help with getting XML into Variables

Posted on
Fri Sep 09, 2011 12:00 pm
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Help with getting XML into Variables

Hello i just bought an Aquarium controller (apex) that outputs XML i would like to get this xml into indigo variables.

i am not very good at programming so i thought i could hack the NOAA script. The script gets the xML feed but i am confused at the code to tell it to get the XML name and value and put it into variables. been trying various ways for the last 2 hours but need some asistance

here is my altered script.

Code: Select all
do shell script "curl -L " & "http://10.0.1.201:1001/cgi-bin/status.xml"
return true
end

on ParseXML()
   try
      tell application "System Events"
         
         set windchill_f to value of XML elements of elem1 whose name is "windchill_f"
         set windchill_c to value of XML elements of elem1 whose name is "windchill_c"
         set visibility_mi to value of XML elements of elem1 whose name is "visibility_mi"
      end tell
      return true
   on error errString number errNum
      my LogNOAA("ParseXML", errString, errNum)
      return false
   end try
end ParseXML

on SendToIndigo()
   set arr to {¬
      {"Weather_Windchill_F", GetWindchillF, windchill_f}, ¬
      {"Weather_Windchill_C", GetWindchillC, windchill_c}, ¬
      {"Weather_Visibility_mi", GetVisibility, visibility_mi} ¬
         }
   
   repeat with thisArr in arr
      try
         tell application "IndigoServer"
            if (item 2 of thisArr) is true then
               if (length of (item 3 of thisArr) = 0) then
                  set logString to "Variable \"" & (item 1 of thisArr) & "\" is not present in the NOAA data file."
                  my LogNOAA(logString, "", 0)
               else
                  if not (variable (item 1 of thisArr) exists) then
                     make new variable with properties {name:(item 1 of thisArr), value:(item 3 of thisArr)}
                  else
                     set value of variable (item 1 of thisArr) to (item 3 of thisArr)
                  end if
               end if
            else
               if (variable (item 1 of thisArr) exists) then delete variable (item 1 of thisArr)
            end if
         end tell
      on error errString number errNum
         my LogNOAA("SendToIndigo", errString, errNum)
      end try
   end repeat
end SendToIndigo


and here is the XML output when i run it in AS editor.

Code: Select all
tell current application
   do shell script "curl -L http://10.0.1.201:1001/cgi-bin/status.xml"
      --> "<?xml version=\"1.0\"?>
<status software=\"4.10L_6D11\" hardware=\"1.0\">
<hostname>apex</hostname>
<serial>AC4:04376</serial>
<timezone>-8</timezone>
<date>09/09/2011 10:50:56</date>
<power><failed>none</failed>
<restored>none</restored></power>
<probes>
<probe>
<name>Temp</name> <value>80.0</value>
 <type>Temp</type></probe>
<probe>
<name>pH</name> <value>7.91</value>
 <type>pH</type></probe>
<probe>
 <name>Amp_3</name> <value>0.0  </value>
 <type>Amps</type></probe></probes>
<outlets>
<outlet>
 <name>SndAlm_I6</name>
 <state>AOF</state>
 <deviceID>base_Alarm</deviceID>
</outlet>
<outlet>
 <name>SndWrn_I7</name>
 <state>AOF</state>
 <deviceID>base_Warn</deviceID>
</outlet>
<outlet>
 <name>EmailAlm_I5</name>
 <state>AOF</state>
 <deviceID>base_email</deviceID>
</outlet>
<outlet>
 <name>BlueLed</name>
 <state>AON</state>
 <deviceID>3_1</deviceID>
</outlet>
<outlet>
 <name>Skimmer</name>
 <state>AON</state>
 <deviceID>3_2</deviceID>
</outlet>
<outlet>
 <name>Rs_Pump</name>
 <state>AON</state>
 <deviceID>3_3</deviceID>
</outlet>
<outlet>
 <name>Heater</name>
 <state>AOF</state>
 <deviceID>3_4</deviceID>
</outlet>
<outlet>
 <name>Vortech</name>
 <state>AOF</state>
 <deviceID>3_5</deviceID>
</outlet>
<outlet>
 <name>Fan</name>
 <state>AOF</state>
 <deviceID>3_6</deviceID>
</outlet>
<outlet>
 <name>spare</name>
 <state>OFF</state>
 <deviceID>3_7</deviceID>
</outlet>
<outlet>
 <name>PCLights</name>
 <state>AOF</state>
 <deviceID>3_8</deviceID>
</outlet>
</outlets></status>"
end tell
Result:
true


thanks if you can help :D

Posted on
Tue May 08, 2012 12:33 pm
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Help with getting XML into Variables

i am still trying with this but get an error. and would really like to get this going

Thanks

if you want the IP to do a live test let me know.

Result:
error "System Events got an error: Can’t get XML element \"Probes\" of contents of XML file \"Macintosh HD:Usersritesktop:ApexData.xml\"." number -1728 from XML element "Probes" of contents of XML file "Macintosh HD:Users:rtesktop:ApexData.xml"


here is my latest code.

set gatewayAddress to "http://7x.1xx.9x.xxx"
set thePort to "1001"
set output_path to "~/Desktop/ApexData.xml"


set fileURL to gatewayAddress & ":" & thePort & "/cgi-bin/status.xml"
do shell script "curl " & fileURL & " -o " & output_path

set XMLfile to "~/Desktop/ApexData.xml"

tell application "System Events"
set xml_data to contents of XML file XMLfile
tell xml_data to set probes to XML element "probes"
tell xml_data to set probe to XML element "probe" of probes
tell xml_data to set temperature to XML element "value" of probe
set tanktemp to value of temperature
tell application "IndigoServer"
set value of variable "Temp" to tanktemp as integer
end tell
end tell


here is my XML file.

Code: Select all
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<status software="4.12L_2A12" hardware="1.0">
<hostname>apex</hostname>
<serial>AC4:04376</serial>
<timezone>-8</timezone>
<date>05/08/2012 11:29:58</date>
<power>
<failed>04/26/2012 08:55:46</failed>
<restored>04/26/2012 08:56:20</restored>
</power>
<probes>
<probe>
<name>Temp</name>
<value>78.4</value>
<type>Temp</type>
</probe>
<probe>
<name>pH</name>
<value>8.23</value>
<type>pH</type>
</probe>
<probe>
<name>Amp_3</name>
<value>0.0</value>
<type>Amps</type>
</probe>
</probes>
<outlets>
<outlet>
<name>zSndAlm_I6</name>
<outputID>0</outputID>
<state>AOF</state>
<deviceID>base_Alarm</deviceID>
</outlet>
<outlet>
<name>zSndWrn_I7</name>
<outputID>1</outputID>
<state>AOF</state>
<deviceID>base_Warn</deviceID>
</outlet>
<outlet>
<name>zEmailAlm_I5</name>
<outputID>2</outputID>
<state>AOF</state>
<deviceID>base_email</deviceID>
</outlet>
<outlet>
<name>HighPump</name>
<outputID>3</outputID>
<state>ON</state>
<deviceID>3_1</deviceID>
</outlet>
<outlet>
<name>Skimmer</name>
<outputID>4</outputID>
<state>ON</state>
<deviceID>3_2</deviceID>
</outlet>
<outlet>
<name>LowPump</name>
<outputID>5</outputID>
<state>ON</state>
<deviceID>3_3</deviceID>
</outlet>
<outlet>
<name>Heater</name>
<outputID>6</outputID>
<state>AOF</state>
<deviceID>3_4</deviceID>
</outlet>
<outlet>
<name>Vortech</name>
<outputID>7</outputID>
<state>AON</state>
<deviceID>3_5</deviceID>
</outlet>
<outlet>
<name>Fan</name>
<outputID>8</outputID>
<state>AOF</state>
<deviceID>3_6</deviceID>
</outlet>
<outlet>
<name>Reactor</name>
<outputID>9</outputID>
<state>OFF</state>
<deviceID>3_7</deviceID>
</outlet>
<outlet>
<name>Radion</name>
<outputID>10</outputID>
<state>AOF</state>
<deviceID>3_8</deviceID>
</outlet>
<outlet>
<name>Feed</name>
<outputID>11</outputID>
<state>OFF</state>
<deviceID>Cntl:A1</deviceID>
</outlet>
<outlet>
<name>Maintain</name>
<outputID>12</outputID>
<state>OFF</state>
<deviceID>Cntl:A2</deviceID>
</outlet>
<outlet>
<name>z----</name>
<outputID>13</outputID>
<state>OFF</state>
<deviceID>Cntl:A3</deviceID>
</outlet>
<outlet>
<name>z----------</name>
<outputID>14</outputID>
<state>OFF</state>
<deviceID>Cntl:A4</deviceID>
</outlet>
</outlets>
</status>

Posted on
Thu May 10, 2012 1:25 pm
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Help with getting XML into Variables

i got it working. :P

EDIT FIXED decimals:
my temp is 78.4 but it only gets the variable value 78 how do i get the true reading 78.4

i guess i have to add to this code but what do i add.

tell application "IndigoServer"
set value of variable "ApexTemp" to theValue as string
end tell

Code: Select all
set apex to " /usr/bin/curl 'http://myip.com:myport/cgi-bin/status.xml' > /tmp/apex.xml"
do shell script apex

tell application "System Events"
   set file1 to XML file "/tmp/apex.xml"
   tell file1
      tell XML element "status"
         tell XML element "probes"
            -- there are multiple probe elements. First is default, but you can specify whichever you want
            tell (first XML element whose name is "probe")
               set theValue to value of XML element "value"
            end tell
         end tell
      end tell
   end tell
end tell

tell application "IndigoServer"
   set value of variable "ApexTemp" to theValue as integer
end tell

Posted on
Thu May 10, 2012 8:19 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Help with getting XML into Variables

Late reply here. So, did you get it working the way you want?

The "as integer" part will chop off any decimals you may have in a numeric value. If you want to keep the decimal value, take out the "as integer" part in the "set value of variable" command. It may also be helpful to note that Indigo variables are ALWAYS strings, even if you send numbers or boolean values, they're converted to strings upon insertion. When getting the value of a variable in which you wish to perform mathematic operations (i.e. something like set myAppleScriptVariable to value of variable "SomeIndigoVariable"), be sure to force that value into a numeric value by using the "as number" directive at the end (e.g. set myAppleScriptVariable to (value of variable "SomeIndigoVariable") as number). You can then perform mathematical functions on the AppleScript variable without getting odd AppleScript errors.

Posted on
Thu Aug 16, 2012 10:34 pm
CRBatchelor offline
Posts: 4
Joined: Aug 16, 2012

Re: Help with getting XML into Variables

Ok so I am getting back into this with my house and have been getting everything to talk together. I also have an Apex and would like to integrate. Would love to also have some two-way communications too if possible.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 22 guests