diff --git a/.env.development b/.env.development index e77609dc..8b8c7587 100644 --- a/.env.development +++ b/.env.development @@ -1,2 +1,3 @@ ENV_NAME=development -BASE_URL=https://syncrow-dev.azurewebsites.net \ No newline at end of file +BASE_URL=https://syncrow-dev.azurewebsites.net +OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02 diff --git a/.env.production b/.env.production index 4e9dcb81..73a13524 100644 --- a/.env.production +++ b/.env.production @@ -1,2 +1,3 @@ ENV_NAME=production -BASE_URL=https://syncrow-staging.azurewebsites.net \ No newline at end of file +BASE_URL=https://syncrow-staging.azurewebsites.net +OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02 \ No newline at end of file diff --git a/.env.staging b/.env.staging index 9565b426..8ab31d93 100644 --- a/.env.staging +++ b/.env.staging @@ -1,2 +1,3 @@ ENV_NAME=staging -BASE_URL=https://syncrow-staging.azurewebsites.net \ No newline at end of file +BASE_URL=https://syncrow-staging.azurewebsites.net +OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02 \ No newline at end of file diff --git a/lib/pages/analytics/services/device_location/remote_device_location_service.dart b/lib/pages/analytics/services/device_location/remote_device_location_service.dart new file mode 100644 index 00000000..b78be6cc --- /dev/null +++ b/lib/pages/analytics/services/device_location/remote_device_location_service.dart @@ -0,0 +1,88 @@ +import 'package:dio/dio.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:syncrow_web/pages/analytics/models/device_location_info.dart'; +import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.dart'; +import 'package:syncrow_web/pages/analytics/services/device_location/device_location_service.dart'; + +class RemoteDeviceLocationService implements DeviceLocationService { + const RemoteDeviceLocationService(this._dio); + + final Dio _dio; + static final _openWeatherApiKey = dotenv.env['OPEN_WEATHER_API_KEY']!; + @override + Future get(GetDeviceLocationDataParam param) async { + try { + final results = await Future.wait([ + _getAirQualityData(param), + _getWeatherData(param), + ]); + + final airQuality = results[0] as double?; + final weatherData = results[1] as _WeatherData?; + + return DeviceLocationInfo( + airQuality: airQuality, + temperature: weatherData?.temperature, + humidity: weatherData?.humidity, + ); + } catch (e) { + throw Exception('Failed to fetch location data: $e'); + } + } + + Future _getAirQualityData(GetDeviceLocationDataParam param) async { + final response = await _dio.get( + 'https://api.openweathermap.org/data/2.5/air_pollution', + queryParameters: { + 'lat': param.latitude, + 'lon': param.longitude, + 'appid': _openWeatherApiKey, + }, + ); + + final data = response.data as Map; + final list = data['list'] as List; + if (list.isEmpty) return null; + + final main = list[0]['main'] as Map; + return (main['aqi'] as num).toDouble(); + } + + Future<_WeatherData?> _getWeatherData(GetDeviceLocationDataParam param) async { + final now = DateTime.now(); + final start = DateTime(now.year, now.month, now.day); + final end = DateTime(now.year, now.month, now.day, 23, 59, 59); + try { + final response = await _dio.get( + 'https://api.openweathermap.org/data/2.5/weather', + queryParameters: { + 'lat': param.latitude, + 'lon': param.longitude, + 'start': start.millisecondsSinceEpoch, + 'end': end.millisecondsSinceEpoch, + 'appid': _openWeatherApiKey, + }, + ); + + final data = response.data as Map; + final main = data['main'] as Map; + + return _WeatherData( + temperature: (main['temp'] as num).toDouble(), + humidity: (main['humidity'] as num).toDouble(), + ); + } catch (e) { + return null; + } + } +} + +class _WeatherData { + const _WeatherData({ + required this.temperature, + required this.humidity, + }); + + final double temperature; + final double humidity; +}