Skip to content
Snippets Groups Projects
Commit 2b068946 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

add: error handling for get_divided_map

parent eebde52d
No related branches found
No related tags found
1 merge request!12Clhp map
Showing
with 49 additions and 41 deletions
import 'dart:async';
import 'package:app/server_requests/fetch_relation.dart';
import 'package:app/server_requests/init_state.dart';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:app/server_requests/init_state.dart';
import 'package:liquid_pull_to_refresh/liquid_pull_to_refresh.dart';
import '../consts.dart';
import '../data_classes.dart';
import '../widgets/main_layout.dart';
import '../utils/custom_search_delegate.dart';
import '../server_requests/fetch_relation.dart';
import '../server_requests/fetch_markers.dart';
class DefaultPage extends StatefulWidget {
const DefaultPage({Key? key}) : super(key: key);
......@@ -31,7 +28,7 @@ class _DefaultPageState extends State<DefaultPage> {
}
Future<void> _handleRefresh() async {
return await initialiseState();
return await initialiseState(false);
}
@override
......@@ -64,10 +61,13 @@ class _DefaultPageState extends State<DefaultPage> {
showSearch( // Fetch new relation and measurements on search
context: context,
delegate: CustomSearchDelegate((String result) {
setState(() {
selectedLake = result;
});
initialiseState();
// Make request only if the selected lake is different from the current selected lake
if (result != selectedLake) {
setState(() {
selectedLake = result;
});
initialiseState(false);
}
}),
);
},
......
......@@ -25,7 +25,7 @@ class _LoadingPageState extends State<LoadingPage>
}
Future<void> _navigateToDefaultPage() async {
await initialiseState();
await initialiseState(true);
// Navigate to the default page once state is initialised
Navigator.of(context).pushReplacement(MaterialPageRoute(
......
......@@ -13,7 +13,7 @@ import '../server_requests/fetch_relation.dart';
/// initialiseState makes three requests to the server, one requesting
/// measurements for the selected relation, the other requesting the relation,
/// and the last requesting the list of all system lakes
Future<void> initialiseState() async {
Future<void> initialiseState(bool fetchSearchOptions) async {
bool serverConnection = true;
late Future<List<Measurement>> markerListFuture;
......@@ -46,7 +46,9 @@ Future<void> initialiseState() async {
relationFuture = loadSavedRelation();
}
initSearchOptions();
if (fetchSearchOptions) {
initSearchOptions();
}
//selectedRelation = await relationFuture;
selectedRelation = await relationFuture; // NB update once fixed
......
import 'dart:typed_data';
import 'package:flutter_map/flutter_map.dart';
import 'package:fuzzy/fuzzy.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
......
......@@ -8,7 +8,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler
from map_handler.add_lake import cut_map
from server.consts import LAKE_RELATIONS_PATH
from map_handler.get_lake import get_divided_map
from map_handler.get_lake_relation import get_divided_map
from map_handler.get_measurements import get_all_markers
from map_handler.input_new_data import input_new_Lidar_data
......
No preview for this file type
File deleted
File added
File added
No preview for this file type
from server.consts import LAKE_RELATIONS_PATH
# Writes contents of a lake json file to the response
def get_divided_map(self, file_name):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Extract contents from JSON file
with open(LAKE_RELATIONS_PATH + file_name + "_div.json", "r") as file:
data = file.read()
# Write contents of the JSON file to response
self.wfile.write(data.encode('utf-8'))
from server.consts import LAKE_RELATIONS_PATH
# Writes contents of a lake json file to the response
def get_divided_map(self, file_name):
try:
# Extract contents from JSON file
with open(LAKE_RELATIONS_PATH + file_name + "_div.json", "r") as file:
data = file.read()
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write contents of the JSON file to response
self.wfile.write(data.encode('utf-8'))
except FileNotFoundError:
self.send_response(404)
self.send_header("Content-type", "application/json")
self.end_headers()
except Exception as e:
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
......@@ -133,9 +133,8 @@ def get_all_markers(self, cursor, lake_name):
# Convert dictionary values to list of measurements
data = list(measurement_data.values()) + test_measurements
if len(rows) == 0 or len(data) == 0: # Return 500 and empty list if no data is found
print(f"No data which meets the condition found")
marker_data = '[]'
if len(data) == 0:
marker_data = json.dumps(['no measurements'])
else:
# Convert list of dictionaries to JSON
marker_data = json.dumps(data, indent=4)
......@@ -144,6 +143,11 @@ def get_all_markers(self, cursor, lake_name):
print(f"Error in querying database: {e}")
marker_data = '[]'
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
......
[
"Mj\u00c3\u00b8sa",
"Bogstadsvannet",
"Einavatnet",
"Femsj\u00c3\u00b8en",
"Femunden",
"Fjellsj\u00c3\u00b8en",
"Gjende",
"Gjersj\u00c3\u00b8en",
"Skumsj\u00c3\u00b8en"
]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment