Skip to content

Commit

Permalink
Merge pull request #14 from michaelfromyeg/backend
Browse files Browse the repository at this point in the history
Fix: Got physical emulator to connect to backend
  • Loading branch information
oltimaloku authored Apr 1, 2024
2 parents f296d75 + 8e219bf commit d37822e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
3 changes: 2 additions & 1 deletion client/lib/constants/env_variables.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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://${dotenv.env['IP_ADDRESS']}:8000';
}
29 changes: 21 additions & 8 deletions client/lib/features/geo_sphere/services/geo_sphere_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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<void> getAllGeoSpheres() async {
static Future<List<GeoSphere>> getAllGeoSpheres() async {
try {
var response = await http.get(Uri.parse('${EnvVariables.uri}/geofences'));

if (response.statusCode == 200) {
List<dynamic> geofencesData = json.decode(response.body);

List<GeoSphere> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ class GeoSphereViewModel extends ChangeNotifier {
final LocationViewModel _locationViewModel;
final List<GeoSphere> _geoSpheres = [];

GeoSphereViewModel(this._locationViewModel);
GeoSphereViewModel(this._locationViewModel) {
fetchGeoSpheres();
}

List<GeoSphere> get geoSpheres => _geoSpheres;

Future<void> fetchGeoSpheres() async {
List<GeoSphere> geoSpheres = await GeoSphereService.getAllGeoSpheres();
_geoSpheres.addAll(geoSpheres);
notifyListeners();
}

/*
Future<void> setAndCreateGeoSphere(double radius, String name) async {
LocationData? locationData = _locationViewModel.currentLocation;
Expand Down Expand Up @@ -61,6 +70,23 @@ class GeoSphereViewModel extends ChangeNotifier {
}
}
}
*/
Future<void> 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
Expand Down
4 changes: 3 additions & 1 deletion client/lib/global_variables.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions client/lib/models/geo_sphere_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@ class GeoSphere {
}) : geoSphereId = const Uuid().v4();

factory GeoSphere.fromMap(Map<String, dynamic> map) {
var geojson = map['geojson'] ?? {};
var geometry = geojson['geometry'] ?? {};
var properties = geojson['properties'] ?? {};
var geojson = map['geojson'] as Map<String, dynamic>? ?? {};
var geometry = geojson['geometry'] as Map<String, dynamic>? ?? {};
var coordinates = geometry['coordinates'] as List<dynamic>? ?? [0.0, 0.0];
var properties = geojson['properties'] as Map<String, dynamic>? ?? {};

// 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
);
}

Expand Down
11 changes: 2 additions & 9 deletions georeal/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit d37822e

Please sign in to comment.