Connected RemoteDeviceLocationService to the new BE API, instead of directly fetching the data from OpenWeather Api's.

This commit is contained in:
Faris Armoush
2025-06-04 09:18:28 +03:00
parent bcb6e49a01
commit 8d999f118c

View File

@ -1,97 +1,32 @@
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';
import 'package:syncrow_web/services/api/http_service.dart';
class RemoteDeviceLocationService implements DeviceLocationService {
const RemoteDeviceLocationService(this._dio);
const RemoteDeviceLocationService(this._httpService);
final Dio _dio;
static final _openWeatherApiKey = dotenv.env['OPEN_WEATHER_API_KEY']!;
final HTTPService _httpService;
static const _defaultErrorMessage = 'Failed to load device location';
@override
Future<DeviceLocationInfo> 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,
final response = await _httpService.get(
path: '/weather',
queryParameters: param.toJson(),
expectedResponseModel: (data) => DeviceLocationInfo.fromJson(data),
);
return response;
} on DioException catch (e) {
final message = e.response?.data as Map<String, dynamic>?;
final error = message?['error'] as Map<String, dynamic>?;
final errorMessage = error?['error'] as String? ?? '';
throw Exception(errorMessage);
} catch (e) {
throw Exception('Failed to fetch location data: $e');
}
}
Future<double?> _getAirQualityData(GetDeviceLocationDataParam param) async {
final response = await _dio.get<Map<String, dynamic>>(
'/air_pollution/history',
options: Options(
method: 'GET',
responseType: ResponseType.json,
),
queryParameters: {
'lat': param.latitude,
'lon': param.longitude,
'appid': _openWeatherApiKey,
},
);
final data = response.data ?? {};
final list = data['list'] as List<dynamic>;
if (list.isEmpty) return null;
final main = list[0]['main'] as Map<String, dynamic>;
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<Map<String, dynamic>>(
'/weather',
options: Options(
method: 'GET',
responseType: ResponseType.json,
),
queryParameters: {
'lat': param.latitude,
'lon': param.longitude,
'start': start.millisecondsSinceEpoch,
'end': end.millisecondsSinceEpoch,
'appid': _openWeatherApiKey,
},
);
final data = response.data as Map<String, dynamic>;
final main = data['main'] as Map<String, dynamic>;
return _WeatherData(
temperature: (main['temp'] as num).toDouble(),
humidity: (main['humidity'] as num).toDouble(),
);
} catch (e) {
return null;
throw Exception('$_defaultErrorMessage: $e');
}
}
}
class _WeatherData {
const _WeatherData({
required this.temperature,
required this.humidity,
});
final double temperature;
final double humidity;
}