9 Replies Latest reply on Jan 15, 2018 11:16 PM by msutton RSS
    msutton Newbie

    Updating Weather Data Properties

    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.WeatherTime = xmlPage.current.lastupdate.@value;

    me.Title=xmlPage.current.city.@name+" "+xmlPage.city.country;




    I'm not sure if it is correct and am new to javascript and API calls


    Any help will be greatly appreciated.


    Thanks Matt

      • Re: Updating Weather Data Properties
        ttielebein Collaborator

        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.

          • Re: Updating Weather Data Properties
            msutton Newbie

            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.





            <city id="3019355" name="Etretat">



            <coord lon="0.21" lat="49.71"/>



            <sun rise="2017-01-03T07:56:03" set="2017-01-03T16:12:01"/>


            <temperature value="-0.58" min="-2" max="1" unit="metric"/>

            <humidity value="100" unit="%"/>

            <pressure value="1030" unit="hPa"/>





            <speed value="3.1" name="Light breeze"/>



            <direction value="190" code="S" name="South"/>


            <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"/>


          • Re: Updating Weather Data Properties
            supandey Ninja

            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.

            • Re: Updating Weather Data Properties
              adrianpet Creator

              Hi Matt,


                  Use this code:



              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";




              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.