-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from farmassistX/master_0.1
Master 0.1
- Loading branch information
Showing
46 changed files
with
2,761 additions
and
103 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'dart:convert'; | ||
import 'package:farmassist/data/farm/services/weather_api.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:farmassist/data/farm/models/Forecast.dart'; | ||
import 'dart:developer'; | ||
|
||
import 'package:farmassist/data/farm/models/Location.dart'; | ||
|
||
class OpenWeatherMapWeatherApi extends WeatherApi { | ||
static const endPointUrl = 'https://api.openweathermap.org/data/2.5'; | ||
static const apiKey = "92ef38427d2c28239bb706af37d77737"; | ||
http.Client httpClient; | ||
|
||
OpenWeatherMapWeatherApi() { | ||
this.httpClient = new http.Client(); | ||
} | ||
|
||
Future<Location> getLocation(String city) async { | ||
final requestUrl = '$endPointUrl/weather?q=$city&APPID=$apiKey'; | ||
final response = await this.httpClient.get(Uri.encodeFull(requestUrl)); | ||
|
||
if (response.statusCode != 200) { | ||
throw Exception( | ||
'error retrieving location for city $city: ${response.statusCode}'); | ||
} | ||
|
||
return Location.fromJson(jsonDecode(response.body)); | ||
} | ||
|
||
@override | ||
Future<Forecast> getWeather(Location location) async { | ||
final requestUrl = | ||
'$endPointUrl/onecall?lat=${location.latitude}&lon=${location.longitude}&exclude=hourly,minutely&APPID=$apiKey'; | ||
final response = await this.httpClient.get(Uri.encodeFull(requestUrl)); | ||
|
||
if (response.statusCode != 200) { | ||
throw Exception('error retrieving weather: ${response.statusCode}'); | ||
} | ||
|
||
return Forecast.fromJson(jsonDecode(response.body)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:farmassist/data/farm/models/Weather.dart'; | ||
|
||
class Forecast { | ||
final DateTime lastUpdated; | ||
final double longitude; | ||
final double latitude; | ||
final List<Weather> daily; | ||
final Weather current; | ||
final bool isDayTime; | ||
String city; | ||
|
||
Forecast( | ||
{this.lastUpdated, | ||
this.longitude, | ||
this.latitude, | ||
this.daily: const [], | ||
this.current, | ||
this.city, | ||
this.isDayTime}); | ||
|
||
static Forecast fromJson(dynamic json) { | ||
var weather = json['current']['weather'][0]; | ||
var date = DateTime.fromMillisecondsSinceEpoch(json['current']['dt'] * 1000, | ||
isUtc: true); | ||
|
||
var sunrise = DateTime.fromMillisecondsSinceEpoch( | ||
json['current']['sunrise'] * 1000, | ||
isUtc: true); | ||
|
||
var sunset = DateTime.fromMillisecondsSinceEpoch( | ||
json['current']['sunset'] * 1000, | ||
isUtc: true); | ||
|
||
bool isDay = date.isAfter(sunrise) && date.isBefore(sunset); | ||
|
||
// get the forecast for the next 3 days, excluding the current day | ||
bool hasDaily = json['daily'] != null; | ||
var tempDaily = []; | ||
if (hasDaily) { | ||
List items = json['daily']; | ||
tempDaily = items | ||
.map((item) => Weather.fromDailyJson(item)) | ||
.toList() | ||
.skip(1) | ||
.take(5) | ||
.toList(); | ||
} | ||
|
||
var currentForcast = Weather( | ||
cloudiness: int.parse(json['current']['clouds'].toString()), | ||
temp: json['current']['temp'].toDouble(), | ||
iconCode: weather['icon'], | ||
condition: Weather.mapStringToWeatherCondition( | ||
weather['main'], int.parse(json['current']['clouds'].toString())), | ||
description: weather['description'], | ||
feelLikeTemp: json['current']['feels_like'], | ||
date: date); | ||
|
||
return Forecast( | ||
lastUpdated: DateTime.now(), | ||
current: currentForcast, | ||
latitude: json['lat'].toDouble(), | ||
longitude: json['lon'].toDouble(), | ||
daily: tempDaily, | ||
isDayTime: isDay); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Location { | ||
final double longitude; | ||
final double latitude; | ||
|
||
Location({ | ||
this.longitude, | ||
this.latitude, | ||
}); | ||
|
||
static Location fromJson(dynamic json) { | ||
return Location( | ||
longitude: json['coord']['lon'].toDouble(), | ||
latitude: json['coord']['lat'].toDouble()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import 'package:farmassist/data/farm/utils/WeatherIconMapper.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
enum WeatherCondition { | ||
thunderstorm, | ||
drizzle, | ||
rain, | ||
snow, | ||
atmosphere, // dust, ash, fog, sand etc. | ||
mist, | ||
fog, | ||
lightCloud, | ||
heavyCloud, | ||
clear, | ||
unknown | ||
} | ||
|
||
class Weather { | ||
final WeatherCondition condition; | ||
final String description; | ||
final String iconCode; | ||
final double temp; | ||
final double feelLikeTemp; | ||
final int cloudiness; | ||
final DateTime date; | ||
|
||
Weather( | ||
{this.condition, | ||
this.description, | ||
this.iconCode, | ||
this.temp, | ||
this.feelLikeTemp, | ||
this.cloudiness, | ||
this.date}); | ||
|
||
static Weather fromDailyJson(dynamic daily) { | ||
var cloudiness = daily['clouds']; | ||
var weather = daily['weather'][0]; | ||
|
||
return Weather( | ||
condition: mapStringToWeatherCondition(weather['main'], cloudiness), | ||
description: weather['description'], | ||
cloudiness: cloudiness, | ||
iconCode: weather['icon'], | ||
temp: daily['temp']['day'].toDouble(), | ||
date: DateTime.fromMillisecondsSinceEpoch(daily['dt'] * 1000, | ||
isUtc: true), | ||
feelLikeTemp: daily['feels_like']['day'].toDouble() | ||
); | ||
} | ||
|
||
static WeatherCondition mapStringToWeatherCondition( | ||
String input, int cloudiness) { | ||
WeatherCondition condition; | ||
switch (input) { | ||
case 'Thunderstorm': | ||
condition = WeatherCondition.thunderstorm; | ||
break; | ||
case 'Drizzle': | ||
condition = WeatherCondition.drizzle; | ||
break; | ||
case 'Rain': | ||
condition = WeatherCondition.rain; | ||
break; | ||
case 'Snow': | ||
condition = WeatherCondition.snow; | ||
break; | ||
case 'Clear': | ||
condition = WeatherCondition.clear; | ||
break; | ||
case 'Clouds': | ||
condition = (cloudiness >= 85) | ||
? WeatherCondition.heavyCloud | ||
: WeatherCondition.lightCloud; | ||
break; | ||
case 'Mist': | ||
condition = WeatherCondition.mist; | ||
break; | ||
case 'fog': | ||
condition = WeatherCondition.fog; | ||
break; | ||
case 'Smoke': | ||
case 'Haze': | ||
case 'Dust': | ||
case 'Sand': | ||
case 'Ash': | ||
case 'Squall': | ||
case 'Tornado': | ||
condition = WeatherCondition.atmosphere; | ||
break; | ||
default: | ||
condition = WeatherCondition.unknown; | ||
} | ||
|
||
return condition; | ||
} | ||
|
||
IconData getIconData(String iconCode){ | ||
switch(iconCode){ | ||
case '01d': return WeatherIcons.clear_day; | ||
case '01n': return WeatherIcons.clear_night; | ||
case '02d': return WeatherIcons.few_clouds_day; | ||
case '02n': return WeatherIcons.few_clouds_day; | ||
case '03d': | ||
case '04d': | ||
return WeatherIcons.clouds_day; | ||
case '03n': | ||
case '04n': | ||
return WeatherIcons.clear_night; | ||
case '09d': return WeatherIcons.shower_rain_day; | ||
case '09n': return WeatherIcons.shower_rain_night; | ||
case '10d': return WeatherIcons.rain_day; | ||
case '10n': return WeatherIcons.rain_night; | ||
case '11d': return WeatherIcons.thunder_storm_day; | ||
case '11n': return WeatherIcons.thunder_storm_night; | ||
case '13d': return WeatherIcons.snow_day; | ||
case '13n': return WeatherIcons.snow_night; | ||
case '50d': return WeatherIcons.mist_day; | ||
case '50n': return WeatherIcons.mist_night; | ||
default: return WeatherIcons.clear_day; | ||
} | ||
} | ||
} |
Oops, something went wrong.