Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #302 from rilling/group_1_case_2
Browse files Browse the repository at this point in the history
Feature: Weather info
  • Loading branch information
hardik221 authored Nov 27, 2023
2 parents 8b247a3 + c3c0c42 commit f6ee7bb
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/main/java/de/dennisguse/opentracks/data/models/WeatherInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package de.dennisguse.opentracks.data.models;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public final class WeatherInfo implements Parcelable {

private Track.Id id;
private double temperature;
private double windSpeed;

private double humidity;
private String windDirection;

public WeatherInfo(double temperature, double windSpeed, double humidity, String windDirection) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.humidity = humidity;
this.windDirection = windDirection;
}

protected WeatherInfo(Parcel in) {
temperature = in.readDouble();
windSpeed = in.readDouble();
humidity = in.readDouble();
windDirection = in.readString();
}

public static final Creator<WeatherInfo> CREATOR = new Creator<WeatherInfo>() {
@Override
public WeatherInfo createFromParcel(Parcel in) {
return new WeatherInfo(in);
}

@Override
public WeatherInfo[] newArray(int size) {
return new WeatherInfo[size];
}
};

@Nullable
public Track.Id getId() {
return id;
}

public void setId(Track.Id id) {
this.id = id;
}

public double getTemperature() {
return temperature;
}

public void setTemperature(double temperature) {
this.temperature = temperature;
}

public double getWindSpeed() {
return windSpeed;
}

public void setWindSpeed(double windSpeed) {
this.windSpeed = windSpeed;
}

public double getHumidity() {
return humidity;
}

public void setHumidity(double humidity) {
this.humidity = humidity;
}

public String getWindDirection() {
return windDirection;
}

public void setWindDirection(String windDirection) {
this.windDirection = windDirection;
}

@NonNull
@Override
public String toString() {
return "WeatherInfo{" +
"temperature=" + temperature +
", windSpeed=" + windSpeed +
", windDirection=" + windDirection +
'}';
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeDouble(temperature);
parcel.writeDouble(windSpeed);
parcel.writeString(windDirection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package de.dennisguse.opentracks.services;

import androidx.annotation.Nullable;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import de.dennisguse.opentracks.data.models.WeatherInfo;

public class WeatherFetchService {
public static final String API_KEY = "fa97a8d025bc2ed677edfd981a7491b7";
public static final String API_URL = "http://api.weatherstack.com/current";

@Nullable
public static WeatherInfo fetchWeatherData(double latitudeDouble, double longitudeDouble) {
try {

String latitude = String.valueOf(latitudeDouble);
String longitude = String.valueOf(longitudeDouble);

URL url = getURL(latitude, longitude);

HttpURLConnection connection = getHttpURLConnection(url);
StringBuilder result = getWeatherData(connection);
JSONObject current = getJsonConverter(result);

// Extract weather information
double temperature = getTemperature(current);
double windSpeed = getWindSpeed(current);
double humidity = getHumidity(current);
String windDirection = getWindDirection(current);

return new WeatherInfo(temperature, windSpeed, humidity, windDirection);

} catch (IOException | JSONException e) {
e.printStackTrace();
}

return null;
}

private static JSONObject getJsonConverter(StringBuilder result) throws JSONException {
JSONObject json = new JSONObject(result.toString());

return json.getJSONObject("current");
}

private static double getTemperature(JSONObject current) throws JSONException {
return current.getDouble("temperature");
}

private static double getHumidity(JSONObject current) throws JSONException {
return current.getDouble("humidity");
}

private static String getWindDirection(JSONObject current) throws JSONException {
return current.getString("wind_dir");
}

private static double getWindSpeed(JSONObject current) throws JSONException {
return current.getDouble("wind_speed");
}

private static StringBuilder getWeatherData(HttpURLConnection connection) throws IOException {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
result.append(line);
}

return result;
}

private static HttpURLConnection getHttpURLConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

return connection;
}

private static URL getURL(String latitude, String longitude) throws MalformedURLException {
return new URL(API_URL + "?access_key=" + API_KEY +
"&query=" + latitude + "," + longitude);
}
}

0 comments on commit f6ee7bb

Please sign in to comment.