Community Tip - You can change your system assigned username to something more personal in your community settings. X
Hello Everyone
I have tried to alter the Weather forecast API tutorial to get current weather data and write it to some thing properties instead of an infotable. I'm doing this so that I can log it against some other data. Currently when i run my script i get back NaN in the properties. My code is below
var stringLocation=me.Location+" ";
var arrayLocations=stringLocation.split(",");
var lat=arrayLocations[0];
var long=arrayLocations[1];
var params = {
url: "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7" /* STRING */,
timeout: 160 /* NUMBER */
};
// result:
var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);
me.WindDirection = xmlPage.current.wind.direction.@name;
me.WindSpeed=xmlPage.current.wind.speed.@value;
me.WeatherTemp=parseInt(xmlPage.current.temperature.@value);
me.WeatherHumidity=parseFloat(xmlPage.current.humidity.@value);
me.WeatherTime = xmlPage.current.lastupdate.@value;
me.Title=xmlPage.current.city.@name+" "+xmlPage.city.country;
me.Precipitation=(xmlPage.current.precipitation.@mode);
I'm not sure if it is correct and am new to javascript and API calls
Any help will be greatly appreciated.
Thanks Matt
Solved! Go to Solution.
Hi Matt,
Use this code:
if(location!=null){
var test=location+"";
var arrayLocations=test.split(",");
var lat=arrayLocations[0];
var long=arrayLocations[1];
var params = { url: "http://api.openweathermap.org/data/2.5/weather?lat=" +lat+"&lon="+long+"&appid=4abc8a3c9f101ccf9c7cfe7cbf3dfed7&mode=xml&units=metric"/* STRING */,
timeout: 60 /* NUMBER */ };
var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);
var params = {
infoTableName : "InfoTable", dataShapeName : "WeatherDataShape"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(YahooWeatherFeed)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
}
var row = new Object()
row.City = xmlPage.city.@name;
row.WeatherTemperature = parseFloat(xmlPage.temperature.@value);
row.WeatherHumidity = parseFloat(xmlPage.humidity.@value);
row.WindSpeed = parseFloat(xmlPage.wind.speed.@value.toString());
row.WindDirection = xmlPage.wind.direction.@name;
row.Sunrise = xmlPage.city.sun.@rise;
row.Sunset = xmlPage.city.sun.@set;
row.WeatherCondition = "http://openweathermap.org/img/w/" + xmlPage.weather.@icon + ".png";
result.AddRow(row);
me.City = row.City;
me.WindSpeed = row.WindSpeed;
me.WindDirection = row.WindDirection;
me.Temperature = row.WeatherTemperature;
me.Sunrise = row.Sunrise;
me.Humidity = row.WeatherHumidity;
me.Sunset = row.Sunset;
In this way you will keep your InfoTable and also write the values inside your properties. You might have to change some spelling if you don't have the same "naming". For example, I think you have defined the property WeatherTemp while i have Temperature setup for the same property.
Thanks,
Adrian
Hello, are you sure that your REST request returns information when run in the browser? I have seen issues with NaN just because the syntax wasn't quite right in the request. Also, are you sure you need those "@" signs in there? I have never seen this before when working with XML files.
Hello Tori
The rest request returns this in the browser. I copied the @ signs across from the weather tutorial and i'm not sure of their function. I assumed there were some kind of location marker for the value.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<country>FR</country>
<sun rise="2017-01-03T07:56:03" set="2017-01-03T16:12:01"/>
</city>
<temperature value="-0.58" min="-2" max="1" unit="metric"/>
<humidity value="100" unit="%"/>
<pressure value="1030" unit="hPa"/>
<gusts/>
<direction value="190" code="S" name="South"/>
</wind>
<clouds value="20" name="few clouds"/>
<visibility value="3800"/>
<precipitation mode="no"/>
<weather number="701" value="mist" icon="50d"/>
<lastupdate value="2017-01-03T08:00:00"/>
</current>
Sorry copying the text seemed to mess up the formatting
Hey Matt, are you attempting to bind the values at runtime with "@" because you sure can't annotate in JS to make calls like so in Java, not without using some special libraries, keep in mind they are still in early stage.
Hi Sushant
I copied the @ across from the weather tutorial. i have removed them now and tested the code. I still get NAN do you know what i would need for binding the xml values to a property? Does anyone have an example? Thanks for the help
Hi Matthew, i checked "@" used in your original code appears to be for accessing the child attributes so this should be fine (apologies i misunderstood it to be something like invoking service/method in Java ), besides is there any issue in using the values from the Infotable, since you can also have property of datatype Infotable so you can store all the fetched values at once.
Found some useful hints here XML to Infotable detailed example not sure if you have already checked this. Could you please share the link to the tutorial you are using for weather app?
Hi Sushant this is a link to the tutorial i was using Predictive Weather Application : ThingWorx
Hi Matt,
Use this code:
if(location!=null){
var test=location+"";
var arrayLocations=test.split(",");
var lat=arrayLocations[0];
var long=arrayLocations[1];
var params = { url: "http://api.openweathermap.org/data/2.5/weather?lat=" +lat+"&lon="+long+"&appid=4abc8a3c9f101ccf9c7cfe7cbf3dfed7&mode=xml&units=metric"/* STRING */,
timeout: 60 /* NUMBER */ };
var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);
var params = {
infoTableName : "InfoTable", dataShapeName : "WeatherDataShape"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(YahooWeatherFeed)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
}
var row = new Object()
row.City = xmlPage.city.@name;
row.WeatherTemperature = parseFloat(xmlPage.temperature.@value);
row.WeatherHumidity = parseFloat(xmlPage.humidity.@value);
row.WindSpeed = parseFloat(xmlPage.wind.speed.@value.toString());
row.WindDirection = xmlPage.wind.direction.@name;
row.Sunrise = xmlPage.city.sun.@rise;
row.Sunset = xmlPage.city.sun.@set;
row.WeatherCondition = "http://openweathermap.org/img/w/" + xmlPage.weather.@icon + ".png";
result.AddRow(row);
me.City = row.City;
me.WindSpeed = row.WindSpeed;
me.WindDirection = row.WindDirection;
me.Temperature = row.WeatherTemperature;
me.Sunrise = row.Sunrise;
me.Humidity = row.WeatherHumidity;
me.Sunset = row.Sunset;
In this way you will keep your InfoTable and also write the values inside your properties. You might have to change some spelling if you don't have the same "naming". For example, I think you have defined the property WeatherTemp while i have Temperature setup for the same property.
Thanks,
Adrian
Thank you Adrian that has worked perfectly and furthered my understanding of how to access the xml data and use info tables. Cheers Matt