First of all, i just *had* to get rid of those scripts full of indexOf(), substrings etc. just to parse data coming from my HA app.
So i thought i'd give XML a try. This is so easy...
First i configured my HA app, which has a built-in HTTP-server, to supply the Pronto with XML. Prontoscript does the rest...
Here's a sample of XML i use (without the HTTP headers):
Code: Select all
<body>
<content>
<item>
<ID>K01</ID>
<Value>0</Value>
</item>
<item>
<ID>K02</ID>
<Value>0</Value>
</item>
<item>
<ID>K03</ID>
<Value>0</Value>
</item>
<item>
<ID>K04</ID>
<Value>0</Value>
</item>
</content>
</body>
Code: Select all
// get rid of HTTP headers.
result = result.substring(result.indexOf("<body>"));
// create an XML object.
var xx = new XML(result);
xx.content.item[0].ID.text() will result in 'K01'
xx.content.item[2].ID.text() will result in 'K03'
xx.content.item[0].Value.text() will result in '0'
In my case, i use this to retrieve the status of the lights in the livingroom. Depending on their status, different icons are shown on the Pronto.
It goes something like this:
Code: Select all
// get the ID.
var s = xx.content.item[i].ID.text();
// get the button.
var w = widget(s.toString());
// determine the status: ON or OFF.
status = xx.content.item[i].Value.indexOf("1") < 0 ?"OFF":"ON";
// set the button label
w.label = status;
// get the right image depending on the status.
var v = CF.widget("LED_"+status, "RESOURCES");
// if found, transfer image to the button.
if(v)
{
w.setImage( v.getImage() );
}