شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » WeatherController

Public general use code classes and xml files that we've compiled and used over the years:

OpenWeatherMap API Controller class.

   1:  using System;
   2:  using System.Web.Http;
   3:  using System.Xml.Linq;
   4:  using System.Configuration;
   5:  using System.Collections.Generic;
   6:  using System.Runtime.Serialization;
   7:   
   8:  namespace Ia.Ngn.Cl.Model.Api.Controller
   9:  {
  10:      ////////////////////////////////////////////////////////////////////////////
  11:   
  12:      /// <summary publish="true">
  13:      /// OpenWeatherMap API Controller class.
  14:      /// </summary>
  15:      /// 
  16:      /// <remarks> 
  17:      /// Copyright © 2001-2016 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  18:      ///
  19:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  20:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  21:      ///
  22:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  23:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  24:      /// 
  25:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  26:      /// 
  27:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  28:      /// </remarks> 
  29:      public class WeatherController : ApiController
  30:      {
  31:          private static string weatherTick;
  32:          private static List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast> weather;
  33:   
  34:          ////////////////////////////////////////////////////////////////////////////
  35:   
  36:          /// <summary>
  37:          /// Structure is take from OpenWeatherMap response
  38:          /// </summary>
  39:          [DataContract]
  40:          public class Forecast
  41:          {
  42:              /// <summary/>
  43:              [DataMember]
  44:              public DateTime TimeFrom { get; set; }
  45:   
  46:              /// <summary/>
  47:              [DataMember]
  48:              public DateTime TimeTo { get; set; }
  49:   
  50:              /// <summary/>
  51:              [DataMember]
  52:              public int SymbolNumber { get; set; }
  53:   
  54:              /// <summary/>
  55:              [DataMember]
  56:              public string SymbolName { get; set; }
  57:   
  58:              /// <summary/>
  59:              [DataMember]
  60:              public string SymbolVariable { get; set; }
  61:   
  62:              /// <summary/>
  63:              [DataMember]
  64:              public string Precipitation { get; set; }
  65:   
  66:              /// <summary/>
  67:              [DataMember]
  68:              public float WindDirectionDegree { get; set; }
  69:   
  70:              /// <summary/>
  71:              [DataMember]
  72:              public string WindDirectionCode { get; set; }
  73:   
  74:              /// <summary/>
  75:              [DataMember]
  76:              public string WindDirectionName { get; set; }
  77:   
  78:              /// <summary/>
  79:              [DataMember]
  80:              public float WindSpeedMps { get; set; }
  81:   
  82:              /// <summary/>
  83:              [DataMember]
  84:              public string WindSpeedName { get; set; }
  85:   
  86:              /// <summary/>
  87:              [DataMember]
  88:              public string TemperatureUnit { get; set; }
  89:   
  90:              /// <summary/>
  91:              [DataMember]
  92:              public float TemperatureValue { get; set; }
  93:   
  94:              /// <summary/>
  95:              [DataMember]
  96:              public float TemperatureMin { get; set; }
  97:   
  98:              /// <summary/>
  99:              [DataMember]
 100:              public float TemperatureMax { get; set; }
 101:   
 102:              /// <summary/>
 103:              [DataMember]
 104:              public string PressureUnit { get; set; }
 105:   
 106:              /// <summary/>
 107:              [DataMember]
 108:              public float PressureValue { get; set; }
 109:   
 110:              /// <summary/>
 111:              [DataMember]
 112:              public int HumidityValue { get; set; }
 113:   
 114:              /// <summary/>
 115:              [DataMember]
 116:              public string HumidityUnit { get; set; }
 117:   
 118:              /// <summary/>
 119:              [DataMember]
 120:              public string CloudsValue { get; set; }
 121:   
 122:              /// <summary/>
 123:              [DataMember]
 124:              public int CloudsAll { get; set; }
 125:   
 126:              /// <summary/>
 127:              [DataMember]
 128:              public string CloudsUnit { get; set; }
 129:          }
 130:   
 131:          ////////////////////////////////////////////////////////////////////////////
 132:   
 133:          /// <summary>
 134:          ///
 135:          /// </summary>
 136:          public WeatherController()
 137:          {
 138:          }
 139:   
 140:          ////////////////////////////////////////////////////////////////////////////
 141:   
 142:          /// <summary>
 143:          /// Reads OpenWeatherMap.org API Service for weather information in Kuwait.
 144:          /// </summary>
 145:          [HttpGet]
 146:          public List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast> ReadKuwait()
 147:          {
 148:              long ticks;
 149:              DateTime lastTime, now;
 150:              List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast> forecastList, readingForcastList;
 151:   
 152:              now = DateTime.UtcNow.AddHours(3);
 153:   
 154:              // debug
 155:              // forecastList = Retrieve();
 156:   
 157:              if (weather != null && weatherTick != null)
 158:              {
 159:                  readingForcastList = weather;
 160:                  ticks = long.Parse(weatherTick);
 161:                  lastTime = new DateTime(ticks);
 162:   
 163:                  // check that 60 minutes had passed:
 164:                  if (now.Ticks > lastTime.AddMinutes(60).Ticks)
 165:                  {
 166:                      forecastList = Retrieve();
 167:                      weather = forecastList;
 168:                      weatherTick = now.Ticks.ToString();
 169:                  }
 170:                  else
 171:                  {
 172:                      // if an hour did not pass, return the saved value;
 173:                      forecastList = readingForcastList;
 174:                  }
 175:              }
 176:              else
 177:              {
 178:                  forecastList = Retrieve();
 179:                  weather = forecastList;
 180:                  weatherTick = now.Ticks.ToString();
 181:              }
 182:   
 183:              return forecastList;
 184:          }
 185:   
 186:          ////////////////////////////////////////////////////////////////////////////
 187:   
 188:          /// <summary>
 189:          ///
 190:          /// </summary>
 191:          private List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast> Retrieve()
 192:          {
 193:              /*
 194:                <appSettings>
 195:                  <add key="openWeatherMapApiKey" value="{API Key}" />
 196:                </appSettings>
 197:              */
 198:   
 199:              string apiKey, line;
 200:              XDocument xdd;
 201:              Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast forecast;
 202:              List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast> forecastList;
 203:   
 204:              xdd = new XDocument();
 205:   
 206:              apiKey = ConfigurationManager.AppSettings["openWeatherMapApiKey"].ToString();
 207:   
 208:              // Kuwait city id: 387990
 209:              line = Ia.Cl.Model.Http.Request("http://api.openweathermap.org/data/2.5/forecast?id=387990&mode=xml&APPID=" + apiKey);
 210:   
 211:              try
 212:              {
 213:                  forecastList = null;
 214:   
 215:                  xdd = XDocument.Parse(line);
 216:   
 217:                  if(xdd != null)
 218:                  {
 219:                      forecastList = new List<Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast>();
 220:   
 221:                      foreach (XElement xe in xdd.Element("weatherdata").Element("forecast").Elements("time"))
 222:                      {
 223:                          forecast = new Ia.Ngn.Cl.Model.Api.Controller.WeatherController.Forecast();
 224:   
 225:                          // <time from="2016-06-13T03:00:00" to="2016-06-13T06:00:00">
 226:                          forecast.TimeFrom = DateTime.Parse(xe.Attribute("from").Value);
 227:                          forecast.TimeTo = DateTime.Parse(xe.Attribute("to").Value);
 228:   
 229:                          // <symbol number="800" name="clear sky" var="01d"/>
 230:                          forecast.SymbolNumber = int.Parse(xe.Element("symbol").Attribute("number").Value);
 231:                          forecast.SymbolName = xe.Element("symbol").Attribute("name").Value;
 232:                          forecast.SymbolVariable = xe.Element("symbol").Attribute("var").Value;
 233:   
 234:                          // <precipitation/>
 235:                          forecast.Precipitation = (xe.Element("precipitation").Attribute("variable") != null) ? xe.Element("precipitation").Attribute("variable").Value : null;
 236:   
 237:                          // <windDirection deg="322.501" code="NW" name="Northwest"/>
 238:                          forecast.WindDirectionDegree = float.Parse(xe.Element("windDirection").Attribute("deg").Value);
 239:                          forecast.WindDirectionCode = xe.Element("windDirection").Attribute("code").Value;
 240:                          forecast.WindDirectionName = xe.Element("windDirection").Attribute("name").Value;
 241:   
 242:                          // <windSpeed mps="6.05" name="Moderate breeze"/>
 243:                          forecast.WindSpeedMps = float.Parse(xe.Element("windSpeed").Attribute("mps").Value);
 244:                          forecast.WindSpeedName = xe.Element("windSpeed").Attribute("name").Value;
 245:   
 246:                          // <temperature unit="celsius" value="37.14" min="33.12" max="37.14"/>
 247:                          forecast.TemperatureUnit = xe.Element("temperature").Attribute("unit").Value;
 248:                          forecast.TemperatureValue = float.Parse(xe.Element("temperature").Attribute("value").Value);
 249:                          forecast.TemperatureMin = float.Parse(xe.Element("temperature").Attribute("min").Value);
 250:                          forecast.TemperatureMax = float.Parse(xe.Element("temperature").Attribute("max").Value);
 251:   
 252:                          // <pressure unit="hPa" value="1017.48"/>
 253:                          forecast.PressureUnit =xe.Element("pressure").Attribute("unit").Value;
 254:                          forecast.PressureValue = float.Parse(xe.Element("pressure").Attribute("value").Value);
 255:   
 256:                          // <humidity value="46" unit="%"/>
 257:                          forecast.HumidityValue = int.Parse(xe.Element("humidity").Attribute("value").Value);
 258:                          forecast.HumidityUnit = xe.Element("humidity").Attribute("unit").Value;
 259:   
 260:                          // <clouds value="clear sky" all="0" unit="%"/>                        
 261:                          forecast.CloudsValue = xe.Element("clouds").Attribute("value").Value;
 262:                          forecast.CloudsAll = int.Parse(xe.Element("clouds").Attribute("all").Value);
 263:                          forecast.CloudsUnit = xe.Element("clouds").Attribute("unit").Value;
 264:   
 265:                          forecastList.Add(forecast);
 266:                      }
 267:                  }
 268:              }
 269:              catch (Exception)
 270:              {
 271:                  forecastList = null;
 272:              }
 273:   
 274:              return forecastList;
 275:          }
 276:   
 277:          ////////////////////////////////////////////////////////////////////////////
 278:          ////////////////////////////////////////////////////////////////////////////
 279:      }
 280:   
 281:      ////////////////////////////////////////////////////////////////////////////
 282:      ////////////////////////////////////////////////////////////////////////////
 283:  }