package uk.me.steev.java.heating.io.api;

import java.io.IOException;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import uk.me.steev.java.heating.controller.HeatingConfiguration;
import uk.me.steev.java.heating.controller.HeatingException;
import uk.me.steev.java.heating.utils.JSONUtils;

public class WeatherAdapter {
  static final Logger logger = LogManager.getLogger(WeatherAdapter.class.getName());
  protected JSONObject latestReading;
  protected HeatingConfiguration config;
  protected TemperatureUpdater updater;
  
  public WeatherAdapter() throws HeatingException {
    this.updater = new TemperatureUpdater();
  }
  
  protected void update() throws HeatingException {
    try {
      this.latestReading = JSONUtils.readJsonFromUrl("https://api.darksky.net/forecast/" +
                                                     HeatingConfiguration.getStringSetting("darksky", "api_key") + 
                                                     "/" + 
                                                     HeatingConfiguration.getStringSetting("darksky", "latlong") +
                                                     "?exclude=[minutely,hourly,daily]&units=si");
      logger.trace("Got response from DarkSky API: " + this.latestReading.toString());
    } catch (IOException ioe) {
      logger.catching(Level.WARN, ioe);
    }
  }
  
  public double getLatestTemperature() throws CallFailedException {
    if (null != this.latestReading) {
      try {
        return this.latestReading.getJSONObject("currently").getDouble("temperature");
      } catch (JSONException jsone) {
        throw new CallFailedException("Unable to find temperature", jsone);
      }
    }
    throw new CallFailedException("Stored JSON is null");
  }

  public double getApparentTemperature() throws CallFailedException {
    if (null != this.latestReading) {
      try {
        return this.latestReading.getJSONObject("currently").getDouble("apparentTemperature");
      } catch (JSONException jsone) {
        throw new CallFailedException("Unable to find apparentTemperature", jsone);
      }
    }
    throw new CallFailedException("Stored JSON is null");
  }
  
  public JSONObject getLatestReading() {
    return latestReading;
  }

  public void setLatestReading(JSONObject latestReading) {
    this.latestReading = latestReading;
  }

  public HeatingConfiguration getConfig() {
    return config;
  }

  public void setConfig(HeatingConfiguration config) {
    this.config = config;
  }

  public TemperatureUpdater getUpdater() {
    return updater;
  }

  public void setUpdater(TemperatureUpdater updater) {
    this.updater = updater;
  }

  public class TemperatureUpdater implements Runnable {
    public void run() {
      try {
        update();
      } catch (HeatingException he) {
        logger.catching(Level.ERROR, he);
      }
    }
  }
}

@@ -657,19 +657,17 @@
 pdater;%0A
-  
 %0A
+
   public
@@ -759,19 +759,17 @@
 ();%0A  %7D%0A
-  
 %0A
+
   protec
@@ -892,29 +892,50 @@
 api.
-darksky.net/forecast/
+openweathermap.org/data/2.5/weather?appid=
 %22 +%0A
@@ -1022,31 +1022,38 @@
 ingSetting(%22
-darksky
+openweathermap
 %22, %22api_key%22
@@ -1055,17 +1055,16 @@
 _key%22) +
- 
 %0A       
@@ -1114,13 +1114,16 @@
    %22
-/
+&lat=
 %22 +
- 
 %0A   
@@ -1215,127 +1215,213 @@
 ng(%22
-darksky%22, %22latlong%22) +%0A                                                     %22?exclude=%5Bminutely,hourly,daily%5D&units=si%22
+openweathermap%22, %22lat%22) +%0A                                                     %22&lon=%22 +%0A                                                     HeatingConfiguration.getStringSetting(%22openweathermap%22, %22long%22)
 );%0A 
@@ -1461,15 +1461,22 @@
 rom 
-DarkSky
+OpenWeatherMap
  API
@@ -1591,27 +1591,25 @@
 ;%0A    %7D%0A  %7D%0A
-  
 %0A
+
   public dou
@@ -1759,33 +1759,28 @@
 JSONObject(%22
-currently
+main
 %22).getDouble
@@ -1785,25 +1785,27 @@
 le(%22temp
-erature%22)
+%22) - 273.15
 ;%0A      
@@ -2162,17 +2162,12 @@
 ct(%22
-currently
+main
 %22).g
@@ -2180,29 +2180,29 @@
 le(%22
-apparentTemperature%22)
+feels_like%22) - 273.15
 ;%0A  
@@ -2397,18 +2397,16 @@
 %22);%0A  %7D%0A
-  
 %0A  publi

Switch to OpenWeatherMap API