From 5b22bba07367a3319be1ca30fe760c7815b405f7 Mon Sep 17 00:00:00 2001 From: oltimaloku Date: Thu, 28 Mar 2024 15:58:47 -0700 Subject: [PATCH 1/2] Changes to work around location limatations due to physical emulator not working with local host. GeoSphere has a static lat and lot. Although this shouldn't be a problem when we have the server up and running. --- client/lib/constants/env_variables.dart | 3 +- .../services/geo_sphere_service.dart | 29 ++++++++++++++----- .../view_model/geo_sphere_view_model.dart | 28 +++++++++++++++++- client/lib/global_variables.dart | 4 ++- client/lib/models/geo_sphere_model.dart | 25 ++++++++++------ georeal/server.py | 11 ++----- 6 files changed, 71 insertions(+), 29 deletions(-) diff --git a/client/lib/constants/env_variables.dart b/client/lib/constants/env_variables.dart index 70d577b..00188c7 100644 --- a/client/lib/constants/env_variables.dart +++ b/client/lib/constants/env_variables.dart @@ -2,5 +2,6 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; class EnvVariables { static String googleApiKey = dotenv.env['GOOGLE_API_KEY']!; - static String uri = dotenv.env['SERVER_URI']!; + // static String uri = dotenv.env['SERVER_URI']!; + static String uri = 'http://localhost:5000'; } diff --git a/client/lib/features/geo_sphere/services/geo_sphere_service.dart b/client/lib/features/geo_sphere/services/geo_sphere_service.dart index a28f009..0ef66fc 100644 --- a/client/lib/features/geo_sphere/services/geo_sphere_service.dart +++ b/client/lib/features/geo_sphere/services/geo_sphere_service.dart @@ -13,8 +13,10 @@ class GeoSphereService { required GeoSphere geoSphere, }) async { try { - log(geoSphere.geoSphereId); - log(geoSphere.toGeoJsonString()); + log('Making request to ${EnvVariables.uri}/geofences with body: ${json.encode({ + 'name': geoSphere.name, + 'geojson': geoSphere.toGeoJson() + })}'); http.Response res = await http.post( Uri.parse('${EnvVariables.uri}/geofences'), @@ -26,28 +28,39 @@ class GeoSphereService { ); if (res.statusCode == 201) { // Handle the successful response here, e.g., updating UI or local data - print('Geosphere created successfully'); + log('Geosphere created successfully'); } else { // Handle the failure case - print('Failed to create geosphere. Status code: ${res.statusCode}'); + log('Making request to ${EnvVariables.uri}/geofences with body: ${json.encode({ + 'name': geoSphere.name, + 'geojson': geoSphere.toGeoJson() + })}'); + log('Failed to create geosphere. Status code: ${res.statusCode} ${res.body}'); } } catch (e) { throw Exception("Error occured: $e"); } } - static Future getAllGeoSpheres() async { + static Future> getAllGeoSpheres() async { try { var response = await http.get(Uri.parse('${EnvVariables.uri}/geofences')); if (response.statusCode == 200) { List geofencesData = json.decode(response.body); - + List geoSpheres = []; for (var geofenceData in geofencesData) { - // TODO: handle geosjson data + log(geofencesData.toString()); + geoSpheres.add(GeoSphere.fromMap(geofenceData)); + } + + for (var geoSphere in geoSpheres) { + log(geoSphere.toGeoJsonString()); } + return geoSpheres; } else { - print('Request failed with status: ${response.statusCode}.'); + log('Request failed with status: ${response.statusCode}.'); + return []; } } catch (e) { throw Exception("Error occurred: $e"); diff --git a/client/lib/features/geo_sphere/view_model/geo_sphere_view_model.dart b/client/lib/features/geo_sphere/view_model/geo_sphere_view_model.dart index 0e80700..75f1c9b 100644 --- a/client/lib/features/geo_sphere/view_model/geo_sphere_view_model.dart +++ b/client/lib/features/geo_sphere/view_model/geo_sphere_view_model.dart @@ -20,10 +20,19 @@ class GeoSphereViewModel extends ChangeNotifier { final LocationViewModel _locationViewModel; final List _geoSpheres = []; - GeoSphereViewModel(this._locationViewModel); + GeoSphereViewModel(this._locationViewModel) { + fetchGeoSpheres(); + } List get geoSpheres => _geoSpheres; + Future fetchGeoSpheres() async { + List geoSpheres = await GeoSphereService.getAllGeoSpheres(); + _geoSpheres.addAll(geoSpheres); + notifyListeners(); + } + +/* Future setAndCreateGeoSphere(double radius, String name) async { LocationData? locationData = _locationViewModel.currentLocation; @@ -61,6 +70,23 @@ class GeoSphereViewModel extends ChangeNotifier { } } } +*/ + Future setAndCreateGeoSphere(double radius, String name) async { + // Static latitude and longitude values + double dummyLatitude = 49.257471832085294; + double dummyLongitude = -123.15328134664118; + + GeoSphere newGeoSphere = GeoSphere( + latitude: dummyLatitude, + longitude: dummyLongitude, + radiusInMeters: radius, + name: name, + ); + + _geoSpheres.add(newGeoSphere); + GeoSphereService.createGeoSphere(geoSphere: newGeoSphere); + notifyListeners(); + } void deleteGeoSphere(GeoSphere geoSphereToDelete) { geoSpheres diff --git a/client/lib/global_variables.dart b/client/lib/global_variables.dart index 0b2edec..17f8784 100644 --- a/client/lib/global_variables.dart +++ b/client/lib/global_variables.dart @@ -2,7 +2,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; class GlobalVariables { - static String get URI => dotenv.env['SERVER_URI'] ?? 'http://localhost:3000'; + //static String get URI => dotenv.env['SERVER_URI'] ?? 'http://localhost:5000'; + static String get URI => 'http://localhost:5000'; + static String get GOOGLE_API_KEY => dotenv.env['GOOGLE_API_KEY'] ?? ''; // COLORS diff --git a/client/lib/models/geo_sphere_model.dart b/client/lib/models/geo_sphere_model.dart index 296e1cf..0e721b5 100644 --- a/client/lib/models/geo_sphere_model.dart +++ b/client/lib/models/geo_sphere_model.dart @@ -19,17 +19,24 @@ class GeoSphere { }) : geoSphereId = const Uuid().v4(); factory GeoSphere.fromMap(Map map) { - var geojson = map['geojson'] ?? {}; - var geometry = geojson['geometry'] ?? {}; - var properties = geojson['properties'] ?? {}; + var geojson = map['geojson'] as Map? ?? {}; + var geometry = geojson['geometry'] as Map? ?? {}; + var coordinates = geometry['coordinates'] as List? ?? [0.0, 0.0]; + var properties = geojson['properties'] as Map? ?? {}; + + // Extracting name from the top level of the input map + String name = map['name']?.toString() ?? + properties['name']?.toString() ?? + 'Unknown Name'; + double latitude = coordinates.isNotEmpty ? coordinates[1].toDouble() : 0.0; + double longitude = coordinates.isNotEmpty ? coordinates[0].toDouble() : 0.0; + double radiusInMeters = properties['radius']?.toDouble() ?? 0.0; return GeoSphere( - latitude: - geometry['coordinates'] != null ? geometry['coordinates'][1] : 0.0, - longitude: - geometry['coordinates'] != null ? geometry['coordinates'][0] : 0.0, - radiusInMeters: properties['radius'] ?? 0.0, - name: properties['name'] ?? '', + latitude: latitude, + longitude: longitude, + radiusInMeters: radiusInMeters, + name: name, // Use the correctly extracted name ); } diff --git a/georeal/server.py b/georeal/server.py index aeaf8b3..fd3f38f 100644 --- a/georeal/server.py +++ b/georeal/server.py @@ -17,15 +17,8 @@ from werkzeug.utils import secure_filename # noqa: E402 from .logger import logger # noqa: E402 -from .utils import ( # noqa: E402 - DATA_PATH, - FLASK_ENV, - GIT_COMMIT_HASH, - HOST, - PORT, - UPLOAD_PATH, - allowed_file, -) +from .utils import (DATA_PATH, FLASK_ENV, GIT_COMMIT_HASH, HOST, # noqa: E402 + PORT, UPLOAD_PATH, allowed_file) DATABASE_URI = "sqlite:///geofences.db" From 8e219bfabcf09c9fef632c5af339d03737e393d6 Mon Sep 17 00:00:00 2001 From: oltimaloku Date: Sun, 31 Mar 2024 19:21:04 -0700 Subject: [PATCH 2/2] changed address to accept all devices on local network to allow external emulator to connect to backend --- client/lib/constants/env_variables.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lib/constants/env_variables.dart b/client/lib/constants/env_variables.dart index 00188c7..0d88129 100644 --- a/client/lib/constants/env_variables.dart +++ b/client/lib/constants/env_variables.dart @@ -3,5 +3,5 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; class EnvVariables { static String googleApiKey = dotenv.env['GOOGLE_API_KEY']!; // static String uri = dotenv.env['SERVER_URI']!; - static String uri = 'http://localhost:5000'; + static String uri = 'http://${dotenv.env['IP_ADDRESS']}:8000'; }