From a7577b803f1cafd66a27db7d5b8f8460029f766f Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Wed, 6 Mar 2024 12:45:44 +0100 Subject: [PATCH] add: write most recenet get_relation time to file --- .../pages/marker_handler/get_relation.dart | 15 +++++++++-- app/lib/pages/widgets/cloropleth_map.dart | 2 +- app/pubspec.lock | 2 +- app/pubspec.yaml | 3 ++- server/main.py | 24 ++++-------------- .../__pycache__/get_markers.cpython-311.pyc | Bin 3544 -> 3986 bytes .../__pycache__/get_relation.cpython-311.pyc | Bin 3310 -> 3310 bytes server/map/get_markers.py | 12 ++++++--- 8 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app/lib/pages/marker_handler/get_relation.dart b/app/lib/pages/marker_handler/get_relation.dart index 9b306a54..c965faaf 100644 --- a/app/lib/pages/marker_handler/get_relation.dart +++ b/app/lib/pages/marker_handler/get_relation.dart @@ -3,7 +3,9 @@ import 'dart:convert'; import 'dart:io'; import '../consts.dart'; import 'dart:typed_data'; - +import 'dart:io'; +import 'package:path/path.dart'; +import 'package:path_provider/path_provider.dart'; /// Fetch relation data from server Future<Uint8List> fetchRelation() async { @@ -17,11 +19,20 @@ Future<Uint8List> fetchRelation() async { var request = await client.getUrl(Uri.parse('${serverURI}get_relation')); var response = await request.close(); // Close response body at end of function - // Parse body to JSON if request is ok + // Try to parse body to JSON if request is ok if (response.statusCode == 200) { var responseBody = await response.transform(utf8.decoder).join(); if (responseBody.isNotEmpty) { + Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); + String filePath = '${appDocumentsDirectory.path}/last_update.txt'; + + try { // Write most recent time of update to file + await File(filePath).writeAsString('${DateTime.now()}', mode: FileMode.write); + print('Update time written to file'); + } catch (error) { print('Error in writing to file: $error');} + + // Return relation data from the response body return Uint8List.fromList(utf8.encode(responseBody)); } else { throw Exception('Response body is empty'); diff --git a/app/lib/pages/widgets/cloropleth_map.dart b/app/lib/pages/widgets/cloropleth_map.dart index 3e4fb6bc..e776663a 100644 --- a/app/lib/pages/widgets/cloropleth_map.dart +++ b/app/lib/pages/widgets/cloropleth_map.dart @@ -48,7 +48,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> { MapShapeLayer( source: MapShapeSource.memory( widget.relation, - shapeDataField: 'name', + shapeDataField: 'SubDivID', ), color: Colors.orange, zoomPanBehavior: _zoomPanBehavior, diff --git a/app/pubspec.lock b/app/pubspec.lock index 52aec0aa..d2c8c50a 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -252,7 +252,7 @@ packages: source: hosted version: "0.2.1" path_provider: - dependency: transitive + dependency: "direct main" description: name: path_provider sha256: b27217933eeeba8ff24845c34003b003b2b22151de3c908d0e679e8fe1aa078b diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 339a8292..c23cc5b2 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: fl_chart: ^0.20.0-nullsafety1 google_fonts: any syncfusion_flutter_maps: ^20.4.41 + path_provider: ^2.0.8 dev_dependencies: flutter_test: @@ -25,4 +26,4 @@ dev_dependencies: flutter: uses-material-design: true assets: - - assets/icons/ + - assets/ diff --git a/server/main.py b/server/main.py index 647d223d..eb93636f 100644 --- a/server/main.py +++ b/server/main.py @@ -11,6 +11,7 @@ import sqlite3 app = Flask(__name__) terminate_server = 0 + class IceHTTPServer(HTTPServer): def __init__(self, server_address, handler_class, cursor): super().__init__(server_address, handler_class) @@ -34,31 +35,21 @@ class IceHTTP(BaseHTTPRequestHandler): self.send_header("Content-type", "text/plain") self.end_headers() - self.wfile.write(b"Root path hit!") + self.wfile.write(b"The root path provides no functionality. Please use a valid endpoint") elif self.path == '/update_map': # NB: should be POST? - get_all_markers(self.cursor, False, 'Mjosa') # Get all markers + get_all_markers(self, self.cursor, False, 'Mjosa') # Get all markers # NB: temporary hardcoded waterBodyName elif self.path == '/get_valid_markers': # NB: should be POST? - get_all_markers(self.cursor, True, 'Mjosa') # Get only valid markers + get_all_markers(self, self.cursor, True, 'Mjosa') # Get only valid markers # NB: temporary hardcoded waterBodyName elif self.path == '/get_relation': - get_relation(self, 'Mjosa') # NB temp hardcoded value + get_relation(self, 'Mjosa') # NB temp hardcoded value def do_POST(self): if self.path == '/get_weather_data': get_weather(self) -# Terminate server on key press q -def on_key_press(server, event, cursor, conn): - if event.name == 'q': - print('Terminating server...') - server.server_close() - cursor.close() - conn.close() - keyboard.unhook_all() - quit() - # Start a server on port 8443 using self defined HTTP class if __name__ == "__main__": @@ -77,11 +68,6 @@ if __name__ == "__main__": print("Server running on port ", PORT) - # Register key press event handler - keyboard.on_press(lambda event: on_key_press(server, event, cursor, conn)) - - print("Server running on port ", PORT) - # Run server indefinitely server.serve_forever() diff --git a/server/map/__pycache__/get_markers.cpython-311.pyc b/server/map/__pycache__/get_markers.cpython-311.pyc index e0b4752477d0eafd895b1d8937cd74c85045d82a..9c6562798a9b9aed213deae40f3d106e1a2f5a3d 100644 GIT binary patch delta 1186 zcmaKs&u`N(6vyqx`JJ={b=^h>D?i5=gKa`$zy=!7@*4zH0)zlnwM#p7NtI+2rW6q3 z$bsSm2RQTuli;?0fD4FYI#mqiFM!+5oN!h~2|`%$d;a+Oee-NN`I>q$7=5ej3SdpW z_>DZ?*oZbDYBUoAyAS{`febLLBR694F2a!o9RNnK;q}rK7ZV_GVG}l52^xp7M|>I7 z1(!bwZnf&7D`KuckX>tai81OjBR)hzh{O;{A(BI+bSZ(=*J53EC0DoytO-|k1y`aT zOiK5lwe=T_UXtHH0N%0QX%IYfSc-`Ct`ehYh+-j%|4H<Gq#ug37#X}K--V96t_#5b zRNh2b4Xk$>PP`U_`lc8@TXxj-d8Q#Wh2VHsBb#H26Z<2;(*bTLROoT;5LBtl^&e4i z?0s)oxUL!_+kunp=IsF1mb&I_=+59iSEC-+JE-0O7}T|gnjv+34c5SY?gpqq7c@0i zMiTe`eRwRQvq&G_gj3;nv%3MB(azn7#sixDU4Jvq=8>y5H!!~o4}?|21X)#A!SLoK z2`zE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SL-|5`?U4H-0YA3oNzxlIA WmJq|<>^|i6A9_Px4CdAU1o#22iH7_D diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 52d93ce095e8b69937d65db4bbf026a67c5c6a81..bec3ee45489a6064fd5d5de765d0a1ff2f53b1e7 100644 GIT binary patch delta 20 acmaDS`A(91IWI340}y0+zu3tAm<IqsBL*!1 delta 20 acmaDS`A(91IWI340}z}Rd%ltTF%JMjhXz;x diff --git a/server/map/get_markers.py b/server/map/get_markers.py index 6b777a4f..5ffd40c7 100644 --- a/server/map/get_markers.py +++ b/server/map/get_markers.py @@ -4,7 +4,7 @@ from math import pi, cos # get_markers requests all marker data or valid markers, converts the data to json, and writes # the data to the response object -def get_all_markers(cursor, valid: bool, waterBodyName): +def get_all_markers(self, cursor, valid: bool, waterBodyName): try: sql_query = ''' SELECT m.MeasurementID, m.SensorID, m.TimeMeasured, m.CenterLat, m.CenterLon, @@ -72,7 +72,7 @@ def get_all_markers(cursor, valid: bool, waterBodyName): data = list(measurement_data.values()) if len(rows) == 0 or len(data) == 0: # Return 500 and empty list if no data is found - print(f"Error in querying database") + print(f"No data which meets the condition found") marker_data = '[]' else: # Convert list of dictionaries to JSON @@ -82,7 +82,13 @@ def get_all_markers(cursor, valid: bool, waterBodyName): print(f"Error in querying database: {e}") marker_data = '[]' - return marker_data + # Set headers + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + + # Write marker data to response object + self.wfile.write(marker_data.encode('utf-8')) EARTH = 6378.137 # Radius of the earth in kilometer -- GitLab