mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 01:56:24 +00:00
Connected RemoteDeviceLocationService
to the new BE API, instead of directly fetching the data from OpenWeather Api's.
This commit is contained in:
@ -1,97 +1,32 @@
|
|||||||
import 'package:dio/dio.dart';
|
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/models/device_location_info.dart';
|
||||||
import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.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/pages/analytics/services/device_location/device_location_service.dart';
|
||||||
|
import 'package:syncrow_web/services/api/http_service.dart';
|
||||||
|
|
||||||
class RemoteDeviceLocationService implements DeviceLocationService {
|
class RemoteDeviceLocationService implements DeviceLocationService {
|
||||||
const RemoteDeviceLocationService(this._dio);
|
const RemoteDeviceLocationService(this._httpService);
|
||||||
|
|
||||||
final Dio _dio;
|
final HTTPService _httpService;
|
||||||
static final _openWeatherApiKey = dotenv.env['OPEN_WEATHER_API_KEY']!;
|
|
||||||
|
static const _defaultErrorMessage = 'Failed to load device location';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<DeviceLocationInfo> get(GetDeviceLocationDataParam param) async {
|
Future<DeviceLocationInfo> get(GetDeviceLocationDataParam param) async {
|
||||||
try {
|
try {
|
||||||
final results = await Future.wait([
|
final response = await _httpService.get(
|
||||||
_getAirQualityData(param),
|
path: '/weather',
|
||||||
_getWeatherData(param),
|
queryParameters: param.toJson(),
|
||||||
]);
|
expectedResponseModel: (data) => DeviceLocationInfo.fromJson(data),
|
||||||
|
|
||||||
final airQuality = results[0] as double?;
|
|
||||||
final weatherData = results[1] as _WeatherData?;
|
|
||||||
|
|
||||||
return DeviceLocationInfo(
|
|
||||||
airQuality: airQuality,
|
|
||||||
temperature: weatherData?.temperature,
|
|
||||||
humidity: weatherData?.humidity,
|
|
||||||
);
|
);
|
||||||
|
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) {
|
} catch (e) {
|
||||||
throw Exception('Failed to fetch location data: $e');
|
throw Exception('$_defaultErrorMessage: $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WeatherData {
|
|
||||||
const _WeatherData({
|
|
||||||
required this.temperature,
|
|
||||||
required this.humidity,
|
|
||||||
});
|
|
||||||
|
|
||||||
final double temperature;
|
|
||||||
final double humidity;
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user