mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-11-26 18:04:55 +00:00
Created a remote implementation for DeviceLocationService.
This commit is contained in:
@ -1,2 +1,3 @@
|
|||||||
ENV_NAME=development
|
ENV_NAME=development
|
||||||
BASE_URL=https://syncrow-dev.azurewebsites.net
|
BASE_URL=https://syncrow-dev.azurewebsites.net
|
||||||
|
OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
ENV_NAME=production
|
ENV_NAME=production
|
||||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||||
|
OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02
|
||||||
@ -1,2 +1,3 @@
|
|||||||
ENV_NAME=staging
|
ENV_NAME=staging
|
||||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||||
|
OPEN_WEATHER_API_KEY=5253339f3f994603cd406b0817823d02
|
||||||
@ -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<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,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Failed to fetch location data: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<double?> _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<String, dynamic>;
|
||||||
|
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(
|
||||||
|
'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<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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _WeatherData {
|
||||||
|
const _WeatherData({
|
||||||
|
required this.temperature,
|
||||||
|
required this.humidity,
|
||||||
|
});
|
||||||
|
|
||||||
|
final double temperature;
|
||||||
|
final double humidity;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user