Haiku Using weatherForLocation in HaikuHelper

DaveHg

New Member
I'm just beginning to play around with HaikuHelper scripting and can't seem to get access to the fields in the object returned by helper.weatherForLocation().  I've got plenty of experience in Java, but this is my first use of JavaScript.  Any explanation of what I'm doing wrong would be much appreciated.
 
Here's the script I'm using:
 

function onSyncStart(forced, timestamp) {
        runTest1();
}

function runTest1(){
    alert('test2');    
    if(controller.preferences.weatherLocation != null) {
        var weather = helper.weatherForLocation(controller.preferences.weatherLocation);
        if (weather != null){
            alert(weather);
            alert(weather.current);
        } else {
            alert('weather is null');
        }
    }
}
 
 
This generates three alert pop-ups.  The first says 'test2'.  The second has a dump of the weather object in it.  Like this:

{
current = {
code = 116;
conditions = "Partly Cloudy ";
humidity = 79;
location = 78704;
temperature = 79;
};
forecast = (
{
code = 293;
conditions = "Patchy light rain";
day = Wed;
high = 81;
low = 71;
precip = "11.7";
},
{
code = 113;
conditions = Sunny;
day = Thu;
high = 78;
low = 56;
precip = "30.3";
},
{
code = 113;
conditions = Sunny;
day = Fri;
high = 80;
low = 55;
precip = "0.0";
},
{
code = 113;
conditions = Sunny;
day = Sat;
high = 71;
low = 47;
precip = "0.0";
},
{
code = 116;
conditions = "Partly Cloudy ";
day = Sun;
high = 64;
low = 55;
precip = "0.7";
}
);
}

The third just says 'undefined'.  I expect that I should be able to access the fields in weather using syntax like weather.current.location, but this causes the script to throw an exception since weather.current is apparently undefined even though I can see it in that second alert.
 
How have I screwed this up?  And what's the preferred method for accesing those members?
 
Thanks,
 - dave
 
 
 
Yes, there is actually a bug causing this. You can work around in the
mean time by retrieving it off of the web interface, which needs to be
enabled by assigning a password in HaikuHelper's Web tab. Here's the
code to get it off the web interface:
 
loadWeather("73078");

function loadWeather(location) {
var req = new XMLHttpRequest();
req.onload = weatherLoaded;
req.open("GET",

"http://localhost:9399/api/helper.weatherForLocation('" + location +
"')", true, "username", "password");
req.send();
}

function weatherLoaded() {
var weather = JSON.parse(this.responseText);
alert("current temperature = " + weather.current.temperature);
}


You will need to set the username/password to match your controller
name/password. Any special characters and spaces will need to be HTML
escaped/encoded. Ie. %20 replaces any spaces in the username/password,
etc. If you already have the password saved on this machine in Safari
you may not need to specify the username/password.

We will be correcting this in the future and then you can access it
directly, but the result weather object will be identical so it will be
easy for you to switch back once its working.
 
lupinglade said:
Yes, there is actually a bug causing this. You can work around in the
mean time by retrieving it off of the web interface, which needs to be
enabled by assigning a password in HaikuHelper's Web tab. Here's the
code to get it off the web interface:
 

loadWeather("73078");

function loadWeather(location) {
var req = new XMLHttpRequest();
req.onload = weatherLoaded;
req.open("GET",
"http://localhost:9399/api/helper.weatherForLocation('" + location +
"')", true, "username", "password");
>>
req.send();
}

function weatherLoaded() {
var weather = JSON.parse(this.responseText);
alert("current temperature = " + weather.current.temperature);
}


You will need to set the username/password to match your controller
name/password. Any special characters and spaces will need to be HTML
escaped/encoded. Ie. %20 replaces any spaces in the username/password,
etc. If you already have the password saved on this machine in Safari
you may not need to specify the username/password.

We will be correcting this in the future and then you can access it
directly, but the result weather object will be identical so it will be
easy for you to switch back once its working.

luinglad - When will the issue with the fields in the object returned by helper.weatherForLocation( ) be fixed? I have not been able to get the information off the web interface.
 
Back
Top