User Tools

Site Tools


script_owm

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
script_owm [2015/05/17 20:35]
jappie created
script_owm [2015/05/21 11:27] (current)
jappie [OWM-load] improved timer
Line 1: Line 1:
-tbd+===== About the script ====== 
 +  *  Purpose : Get OpenWeather-data for given city. (current & next 10 days) 
 +  *  - binds weather-data as json to .self and binding-variables. 
 +  *  - refreshes every: data_refresh_interval_minutes 
 +  *  Author ​ : Jappie Toutenhoofd (https://​plus.google.com/​+JappieToutenhoofd) 
 +  *  Link    : https://​plus.google.com/​+JappieToutenhoofd/​posts/​
  
 +====== Script code ======
 +======= OWM-load =======
 <​code>​ <​code>​
 +var data_refresh_interval_minutes = 60;
  
 +if ( self.APIcomm == null)
 + {var api_script = LL.getScriptByName("​APIcomm"​);​
 +  if (api_script == null)
 +  { alert( "Also import script '​APIcomm'​. \n This script depends on it."); return;}
 +  LL.runScript('​APIcomm',​ null);
 +}
 +if (self.owm == null)
 + ​{self.owm ​      = {};
 +  self.owm.req ​  = {};
 +  self.owm.last ​ = 0;
 +  self.owm.timer = 0;
 +}
 +//​api.openweathermap.org/​data/​2.5
 +
 +var Req1  = "​http://​api.openweathermap.org/​data/​2.5/";​
 +var Req2  = "?​q=hoofddorp,​nl"; ​   // <-- your city,land here !!
 +    Req2 += "&​units=metric"; ​    // or imperial
 +    Req2 += "&​lang=nl"; ​        // or other country code for language
 +    Req2 += "&​APPID=59a20bd1ce89c6599758a2a0aeae9423";​
 +
 +self.owm.req.current = Req1 + "​weather" ​ + Req2;
 +self.owm.req.forcast = Req1 + "​forecast/​daily"​ + Req2 + "&​cnt=10";​
 +
 +self.owm.days = ["​Zo","​Ma","​Di","​Wo","​Do","​Vr","​Za"​];​
 +
 +
 +function refreshOWM() ​
 +{ // prevent event flouding
 +  var  nu = new Date();
 +  if ((nu - self.owm.last) > (data_refresh_interval_minutes *60000))
 +  { 
 +    var repl = self.APIcomm(self.owm.req.current);​
 +    if (repl == null){return;​};​
 +    self.owm.current = JSON.parse(repl);​
 +    LL.setVariableString("​owm",​ repl);
 +
 +    var repl = self.APIcomm(self.owm.req.forcast);​
 +    if (repl == null){return;​};​
 +    self.owm.forcast = JSON.parse(repl);​
 +  }
 +    self.owm.timer = setTimeout(refreshOWM,​ 600000);
 +}
 +
 +clearTimeout(self.owm.timer);​
 +
 +refreshOWM();​
 </​code> ​ </​code> ​
 +======= OWM-example =======
 +<​code>​
 +if (self.owm == null)
 +{ var owm_script = LL.getScriptByName("​OWM-load"​);​
 +  if (owm_script == null)
 +  { alert( "Also import script '​OWM-load'​. \n This script depends on it.");
 +    return;
 +  };
 +  LL.runScript('​OWM-load',​ null);
 +};
 +if (self.owm.current == null)
 +{ LL.runScript('​OWM-load',​ null);
 +  alert("​Weather data requested, \n Tap again to update."​);​
 +  return;
 +};
 +
 +var i = LL.getEvent().getItem();​
 +var t = self.owm.current.main.temp.toFixed(1);​
 +var c = self.owm.current.weather[0].id;​
 +var ofset = 60000;
 +if (self.owm.current.dt < self.owm.current.sys.sunrise)
 +{ofset += 1000 }
 +if (self.owm.current.dt > self.owm.current.sys.sunset)
 +{ofset += 1000 }
 +var s=String.fromCharCode(ofset + c);
 +var d = self.owm.current.weather[0].description;​
 +var w = parseInt(self.owm.current.wind.deg);​
 +i.setLabel(w + " " + t + "C "+ s +" " + d,true);
 +
 +alert("​last updated: " + new Date(1000 * self.owm.current.dt));​
 +alert(JSON.stringify( self.owm.current));​
 +alert(JSON.stringify( self.owm.forcast));​
 +
 +var test = new Date(1000 * self.owm.forcast.list[9].dt);​
 +alert( "in 9 days it is: " + self.owm.days[test.getDay()] + " " + test.getDate() + " and " + self.owm.forcast.list[9].weather[0].main );
 +
 +</​code>​
script_owm.1431894922.txt.gz · Last modified: 2015/05/17 20:35 by jappie