diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java b/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java index a8617a3..285b89d 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java @@ -83,6 +83,8 @@ WeatherCode.TORNADO.getCode() }; + public MyWeather() {} + public MyWeather(DayForecast dayForecast) { this(dayForecast.weather, dayForecast.timestamp); } @@ -210,4 +212,68 @@ public float getWindSpeed() { return windSpeed; } + + public void setClouds(int clouds) { + this.clouds = clouds; + } + + public void setCity(String city) { + this.city = city; + } + + public void setHumidity(float humidity) { + this.humidity = humidity; + } + + public void setLocation(Location location) { + this.location = location; + } + + public void setMaxTemp(float maxTemp) { + this.maxTemp = maxTemp; + } + + public void setMinTemp(float minTemp) { + this.minTemp = minTemp; + } + + public void setPressure(float pressure) { + this.pressure = pressure; + } + + public void setSnow(float snow) { + this.snow = snow; + } + + public void setRain(float rain) { + this.rain = rain; + } + + public void setSunrise(long sunrise) { + this.sunrise = sunrise; + } + + public void setSunset(long sunset) { + this.sunset = sunset; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public void setWeatherCode(int weatherCode) { + this.weatherCode = weatherCode; + } + + public void setWeatherId(int weatherId) { + this.weatherId = weatherId; + } + + public void setWindDir(float windDir) { + this.windDir = windDir; + } + + public void setWindSpeed(float windSpeed) { + this.windSpeed = windSpeed; + } } diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/WeatherDatabase.java b/app/src/main/java/de/apps4ics/mountainnavigation/WeatherDatabase.java index d639914..c65f442 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/WeatherDatabase.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/WeatherDatabase.java @@ -1,8 +1,11 @@ package de.apps4ics.mountainnavigation; +import android.content.ContentValues; import android.content.Context; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.location.Location; /** * This file is part of MountainNavigation. @@ -51,13 +54,14 @@ private static final String KEY_LAT = "_lat"; private static final String KEY_LON = "_lon"; - private static final String KEY_ALT = "_alt"; private static final String KEY_NAME = "_name"; private static final String KEY_COUNTRY = "_country"; private static final String KEY_SPEED = "_speed"; private static final String KEY_DIR = "_dir"; + private static final float MAX_WEATHER_DISTANCE = 5.0f; // max distance in km + public WeatherDatabase(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @@ -87,7 +91,6 @@ + KEY_ID + " INTEGER PRIMARY KEY," + KEY_LAT + " REAL," + KEY_LON + " REAL," - + KEY_ALT + " REAL," + KEY_NAME + " TEXT," + KEY_COUNTRY + " TEXT" + ")"; db.execSQL(CREATE_LOCATION_TABLE); @@ -103,7 +106,138 @@ //TODO? } - public int insertWeather(MyWeather weather) { - return 0; + private MyWeather addLocationToWeather(Cursor cursor, MyWeather weather) { + Location l = new Location(""); + l.setLatitude(cursor.getDouble(cursor.getColumnIndex(KEY_LAT))); + l.setLongitude(cursor.getDouble(cursor.getColumnIndex(KEY_LON))); + weather.setCity(cursor.getString(cursor.getColumnIndex(KEY_NAME))); + weather.setLocation(l); + return weather; + } + + private MyWeather addWindToWeather(Cursor cursor, MyWeather weather) { + weather.setWindSpeed(cursor.getFloat(cursor.getColumnIndex(KEY_SPEED))); + weather.setWindDir(cursor.getFloat(cursor.getColumnIndex(KEY_DIR))); + return weather; + } + + private MyWeather addWeatherToWeather(Cursor cursor, MyWeather weather) { + weather.setTimestamp(cursor.getLong(cursor.getColumnIndex(KEY_TIMESTAMP))); + weather.setMinTemp(cursor.getFloat(cursor.getColumnIndex(KEY_TEMP_MIN))); + weather.setMaxTemp(cursor.getFloat(cursor.getColumnIndex(KEY_TEMP_MAX))); + weather.setSunrise(cursor.getLong(cursor.getColumnIndex(KEY_SUNRISE))); + weather.setSunset(cursor.getLong(cursor.getColumnIndex(KEY_SUNSET))); + weather.setWeatherId(cursor.getInt(cursor.getColumnIndex(KEY_WEATHER_ID))); + weather.setWeatherCode(cursor.getInt(cursor.getColumnIndex(KEY_WEATHER_CODE))); + weather.setPressure(cursor.getFloat(cursor.getColumnIndex(KEY_PRESSURE))); + weather.setHumidity(cursor.getFloat(cursor.getColumnIndex(KEY_HUMIDITY))); + //cursor.getInt(cursor.getColumnIndex(KEY_MAIN)); + //cursor.getInt(cursor.getColumnIndex(KEY_DESC)); + weather.setRain(cursor.getFloat(cursor.getColumnIndex(KEY_RAIN))); + weather.setSnow(cursor.getFloat(cursor.getColumnIndex(KEY_SNOW))); + weather.setClouds(cursor.getInt(cursor.getColumnIndex(KEY_CLOUDS))); + return weather; + } + + private long getId(Cursor cursor) { + return cursor.getLong(cursor.getColumnIndex(KEY_ID)); + } + + private long getWindId(Cursor cursor) { + return cursor.getLong(cursor.getColumnIndex(KEY_WIND_ID)); + } + + public long insertWeather(MyWeather weather) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues values = getLocationValues(weather); + long locId = db.insert(TABLE_LOCATION, null, values); + values = getWindValues(weather); + long windId = db.insert(TABLE_WIND, null, values); + values = getWeatherValues(weather); + values.put(KEY_LOCATION_ID, locId); + values.put(KEY_WIND_ID, windId); + long rowId = db.insert(TABLE_WEATHER, null, values); + db.close(); + return rowId; + } + + public MyWeather getWeather(double lat, double lon, int offset) { + SQLiteDatabase db = this.getReadableDatabase(); + Cursor locCursor = db.query(TABLE_LOCATION, + new String[]{"*", "(6371 * acos(cos(radians(" + lat + ")) * cos(radians(" + KEY_LAT + ")) * cos(radians(" + KEY_LON + ") - radians(" + lon + ")) + sin(radians(" + lat + ")) * sin(radians(" + KEY_LAT + ")))) AS distance"}, + null, + null, + null, + "distance < " + MAX_WEATHER_DISTANCE, //having + "distance", //orderby + "1"); //only get closest entry + MyWeather weather = new MyWeather(); + if(locCursor == null) { + return null; + } + locCursor.moveToFirst(); + long locId = getId(locCursor); + weather = addLocationToWeather(locCursor, weather); + locCursor.close(); + + Cursor weatherCursor = db.query(TABLE_WEATHER, + null, + "WHERE " + KEY_LOCATION_ID + " =?", + new String[]{String.valueOf(locId)}, null, null, null, "1"); + if(weatherCursor == null) { + return null; + } + weatherCursor.moveToFirst(); + weather = addWeatherToWeather(weatherCursor, weather); + long windId = getWindId(weatherCursor); + weatherCursor.close(); + + Cursor windCursor = db.query(TABLE_WIND, + null, + "WHERE " + KEY_ID + " =?", + new String[]{String.valueOf(windId)}, null, null, null, "1"); + if(windCursor == null) { + return null; + } + windCursor.moveToFirst(); + weather = addWindToWeather(windCursor, weather); + windCursor.close(); + db.close(); + + return weather; + } + + private ContentValues getLocationValues(MyWeather weather){ + ContentValues values = new ContentValues(); + values.put(KEY_LAT, weather.getLocation().getLatitude()); + values.put(KEY_LON, weather.getLocation().getLongitude()); + values.put(KEY_NAME, weather.getCity()); + return values; + } + + private ContentValues getWindValues(MyWeather weather){ + ContentValues values = new ContentValues(); + values.put(KEY_SPEED, weather.getWindSpeed()); + values.put(KEY_DIR, weather.getWindDir()); + return values; + } + + private ContentValues getWeatherValues(MyWeather weather){ + ContentValues values = new ContentValues(); + values.put(KEY_TIMESTAMP, weather.getTimestamp()); + values.put(KEY_TEMP_MIN, weather.getMinTemp()); + values.put(KEY_TEMP_MAX, weather.getMaxTemp()); + values.put(KEY_SUNRISE, weather.getSunrise()); + values.put(KEY_SUNSET, weather.getSunset()); + values.put(KEY_WEATHER_ID, weather.getWeatherId()); + values.put(KEY_WEATHER_CODE, weather.getWeatherCode()); + values.put(KEY_PRESSURE, weather.getPressure()); + values.put(KEY_HUMIDITY, weather.getHumidity()); + values.put(KEY_MAIN, ""); //TODO what is main? + values.put(KEY_DESC, ""); //TODO what is desc? + values.put(KEY_RAIN, weather.getRain()); + values.put(KEY_SNOW, weather.getSnow()); + values.put(KEY_CLOUDS, weather.getClouds()); + return values; } } diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java index fdbf177..1bfeff6 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java @@ -3,7 +3,6 @@ import android.app.Activity; import android.app.FragmentManager; import android.content.Context; -import android.content.SharedPreferences; import android.content.res.Resources; import android.location.Location; import android.util.Log; @@ -133,7 +132,6 @@ //TODO private void getCurrentWeatherOnline(double lat, double lon, final OnSingleWeatherRetrieved callback) { - //return new MyWeather(); weatherClient.getCurrentCondition(new WeatherRequest(lon, lat), new WeatherClient.WeatherEventListener() { @Override public void onWeatherRetrieved(CurrentWeather weather) { @@ -151,9 +149,8 @@ } //TODO - private MyWeather getCurrentWeatherCached(double lat, double lon, int offset, OnSingleWeatherRetrieved callback) { - //return new MyWeather(); - return null; + private void getCurrentWeatherCached(double lat, double lon, int offset, OnSingleWeatherRetrieved callback) { + getFromDatabase(lat, lon, offset, callback); } public void getForecast(int days, OnWeatherRetrieved callback) { @@ -289,7 +286,7 @@ } private void insertIntoDatabase(Weather weather) { - insertIntoDatabase(weather, 0); + insertIntoDatabase(weather, System.currentTimeMillis()); } private void insertIntoDatabase(Weather weather, long timestamp) { @@ -300,6 +297,11 @@ weatherDb.insertWeather(weather); } + private void getFromDatabase(double lat, double lon, int offset, OnSingleWeatherRetrieved callback) { + MyWeather weather = weatherDb.getWeather(lat, lon, offset); + callback.postRetrieve(weather); + } + public void displayHints(MyWeather weather) { //TODO add more hints if (MainActivity.showWeatherHints()) {